Switching Database Types at Compile Time

I want to be able to use MySQL when I compile for Mac, and MS SQL Server when compiling for Windows. Connection details are easy enough with the #If#Endif, but how do I handle the database type? I have an App property called DB which is my database, but it has be given a type in the IDE. I have thought of defining DB as a variant, but then I would have to cast it to the proper type every time I need to access the DB.

Am I missing something simple and obvious? I feel like I am…

You could define your connection code using conditional compilation (#if TargetWin32 for example). The Windows-specific code goes in the true branch and the Mac-specific code goes in the false branch (#else). The same would be feasible throughout the application, where different SQL syntax may be needed for your database accesses.

I am a bit puzzled with the choice of different databases based on target. Both are network-ready RDBMS. Another choice here would be PostgreSQL, which would be available for all targets.

The target-dependent DBs are a little weird, but it’s how things have evolved with this project.

I was hoping to avoid a ton of #If’s throughout my code when calling the DB.

Return a Database, which is the super for all the db types.

And then stick with Database methods and avoid the sub-class specific methods?

You’d have to do that anyway, right?

Where you might need the subclass-specific methods/properties, you’d wrap that in pragmas and cast it. You can also use something like my SQLBuilder_MTC project to insulate you from any SQL differences.

Indeed. Thanks for the input, gentlemen.

I made a myDatabase class, and inside a property for each database type I want to use
then I made the same methods for my class as those of the databaseclass, plus the ones that are specifis to certain databases types.
after that I use myDatabase the same way as xojo database, but I can switch to whatever i want without moving any line of code.
works fine for 6 years.

2 Likes

Same here. I made a wrapper class that contains the individual database types and presents a unified API to the rest of my code. The business code doesn’t need to know what kind of database (or flat file for that matter) that it’s talking to.