TL;DR -- Download a YesSQL database for Windows Phone 8 from here, then use it at your own risk.

Not too long ago, I finished up a "fun" project for myself that involved writing a chicken wire and duct tape dbms from scratch. It actually works fairly well, though I occasionally find additional stupid oversights (like splitting commands on "AND" without checking to see if it's part of a string literal) every so often while using it.

Actually, from my point of view, it works great. It's always insanely surprising when I try something fairly complicated in SQL and SqlDbSharp just works.  That's fun.

Anyhow, since my best laptop's still a Windows 8, I've been spending some time playing with Windows Phone 8.  I ebulliently detailed coming across some great tutorials a couple of weeks ago. And then, about a half-hour later, I completely 180-ed, whining that WP8 doesn't support pretty much any of the key stuff from System.Data, which bothered me enough to write more about the ORM mentality everything's taking on to the point that it arguably drove the choice of Microsoft's next CEO.

That's a topic for another post.

Windows Phone 8 Port

For today, I'll just say I've thrown away hours of free time, of and on and off again, porting my crazy database to Windows Phone 8. The biggest thing missing was, of course, System.Data, but I didn't really use enough of that namespace to make shimming up a DataTable too difficult.  Jon Skeet's DictionaryBackedSet example helped a ton, and made shimming up my DataColumn usage easy.

The only other strange thing was figuring out the origin of lots of PlatformNotSupportedException errors that were masked by TypeInitializationException errors for me. As it turns out, Windows Phone 8 doesn't support calls to GetFolderPath like...

System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal)

... that I was using to initialize directories, and the PlatformNotSupportedException painfully meant that though it compiled, WP8 wasn't going to honor the call.  Compiling code that'll later throw PlatformNotSupportedExceptions is, in my limited playing around, apparently pretty common.  Makes some sense, as that allows you to reuse a ton of C#'s tools very quickly, but it's a real pain in the rear at run time.

Since I had the codebase's "base" directory defined in a static readonly variable that used that GetFolderPath call, I got the TypeInitializationException whenever I tried to access any static variable on that file -- the file where I have all my psuedo-global, readonly settings.  So the errors popped up only at the time I tried to read a static variable or method on that class, even legitimate ones that didn't depend on GetFolderPath, as the code seemed to be sort of lazy-initialized, which red herringed the crud out of me.

Turns out, instead of the SpecialFolder setup, I needed to use some variation on...


... to make WP8 happy.  Which means I really needed to abstract how I set my base directory with one more level of abstraction that checks platform.

But the bottom line is that, yes, there's now an alternative to the now unsupported (?) C#-SQLite that lets you use SQL on Windows Phone 8 (link to dll in the readme displayed on that page).  Sorta.  ;^D  File bugs, and I'll start fixing them, though the code's simple enough I think any mid-level C# coder should be able to understand what's going on pretty quickly.

Labels: ,