I’ve seen the future, and the future is … COBOL?
Ok, so in the process of migrating to my new laptop I’ve been forced to look at several of my old projects. I don’t remember where I found the time, but ten years ago I must have written a _lot_ of code. One of the more bizarre projects I did back then was a Y2K solution that involved cross-compiling COBOL programs to Java bytecode. I only implemented about 50% of the COBOL feature set back then, but it still turns out to be 600+ source files and who-knows-how-many thousands of LOCs.
So rather than let it sit on my hard drive gathering virtual dust, I’ve created a Sourceforge project for it. Check out the Universal COBOL Compiler project when you have a chance. There’s nothing out there but a one-paragraph synopsis and a bunch of code in the CVS repository, but at least it’s not hidden on my computer anymore!
Fun with SqlCommand.ExecuteXmlReader()
So I’m coding along this morning, minding my own business, when I run up against a really annoying problem with the XML support in the .Net SQL support classes. What I was trying to do was fetch some values from a support table as a simple XML document with an element for each row. The SQL FOR XML AUTO clause was just the ticket, so I wrote the following code:
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM "
+ "supportTable AS valueName FOR XML AUTO";
XmlReader res = cmd.ExecuteXmlReader();
XmlDocument doc = new XmlDocument();
doc.Load(res);
but when I tried to run the code I got the following error on the doc.Load() call:
This document already has a DocumentElement node.
It really had me irritated, because I’d already used the exact same code in another part of my application without a hitch. So after some research, it turns out that the problem (which is obvious in retrospect) is that if multiple rows are returned, the resulting XML is not a valid document, but a document fragment. Since there is no top-level element, the Load() method chokes when it encounters the second row. Not good.
I found several workarounds on the web, but none that I really cared for. So I resorted to some MS SQL funny business that I’ve used in the past that did the trick. The problem here is that there is no single top-level element to serve as a root element. One possible approach is to abandon the FOR XML AUTO in favor of FOR XML EXPLICIT, but the explicit support is so incredibly complex and unusable that I’d rather cut my own foot off with a rusty tin can lid. So, to save my foot, I resorted to some fun with joins.
The FOR XML AUTO clause will actually create nested elements in the case of joins between tables, so to create a single top-level element for my support table document I created a new table in my DB called dummy. It has a single column (dummy) and a single row with a single value (dummy). The single row part is important, because by changing the SQL statement above slightly, I end up with the nice, valid XML document I want:
SELECT * FROM dummy AS rootElementName,
supportTable AS valueName FOR XML AUTO
Voila! Now my code works and I can move on to more interesting pursuits, like the development of a Comet service for the masses. Good stuff!
Push it real good.
One of the big limitations of web-based programming is the pull nature of the HTTP protocol. Services like Google Talk, etc. get around this by using a concept that’s now called Comet, which allows the server to “push” content to the client, rather than forcing the client to continually ask for updates.
Since I tend to need this functionality more and more in my Ajax-style apps, I think there should be a web service to make this easier. I’ll keep you posted on my progress.
It’s almost time!
Apple is finally ready to tell us about the iPhone SDK. Put it on your calendars, March 6th at 10:00 AM PST. If the thing didn’t cost $400+ I would have JailBroken it already. Let’s keep our fingers crossed, and hope that I won’t have to donate a lung or anything to get my SDK license.
Anticipation… it’s making me wait.
Ok, so I’ve been patiently (at least as far as y’all know) waiting for the February iPhone SDK release that was promised last October. Well, here it is February 23rd and no SDK. According to the Businessweek blog it may be 1-3 weeks late. I’ve pretty much exhausted what can/should be done using DHTML and Javascript, and I’m itching to get my hands into the warm, steaming guts of my phone. So to speak.
