Database codegen
What is the problem with the current Delphi database model? You might think I have gone off my trolley completely, but if you are an experienced Delphi programmer you will probably recognize what I’m talking about. Namely that when you have a huge application with many forms – all populated by query components, you quickly end up with spaghetti code. It depends of course on how you write your queries – but somehow big projects always end up with a fair amount of spaghetti. Especially when you have apps coded over several years, involving some 20+ programmers all with their own ideas (like myself) on how to solve it.
So how do we make database programming on a massive scale “human”? Well, leaving commercial solutions aside, I like my database code wrapped up neatly in classes. When working with database records it is much easier to work with normal objects rather than hundreds of lines of code with fieldByName(“somefield”). So I decided to create a little firebird database table-wrapper generator.
Consider the following task:
- select all items from a table
- loop through each item and sum up a total based on a value from each record
This task is now reduced to:
var mItems: TMytableItemList; mItem: TMyTableItem; mTotal: Int64; if TMyTableController.getItems(mItems) then Begin try for each mItem in mItems do inc(mTotal,mItem.value); finally mItems.free; end; end;
Thats pretty easy to debug and work with. And as you can imagine, the more tables and joins you have – the more complex a normal Delphi TQueryComponent based form gets. But with clear-cut classes neatly wrapped for both read and write — it’s actually fun to write large-scale database applications.
Sanity check
At present my little wrapper spits out some very basic code. Blobs are read verbatim on requesting an object or a list (which is not very friendly) and also you get choices like “should a list be read on demand? or all at once”. Im still busy tinkering with the API (the TDatabaseObj class and firebird database helper methods), but it will soon hit production. The bonus? It’s dead simple to add support for other databases – and the user doesn’t have to care at all about the underlying engine. It doesn’t matter if you use sqlserver, firebird, mysql, or a native delphi database like elevatedb. Just stick to the objects and you are home free.
Other alternatives
If you have the luck to start a project from scratch, then I would probably go for Remobjects Data Abstract product. I have been a fan of Remobjects since day one, and although we have had our differences over the years – i still love their products. They deliver polished, no hazzle and straight to the point solutions. At present my offering is little more than a “proof of concept”, so don’t expect to fork my codebase and re-wamp your databases just yet. But when im done – database programming on source level will be easier and more secure. I have spent the better part of 10 years writing rock solid database solutions for oil companies that demand 24/7 win32 service excellence so – even though some of my solutions might seem strange, rest assured that there are reasons (read: real life experience) for the way the code is generated. A simple exception in a win32 service that handles some 200.000 customers an hour will cost you an arm and a leg.. you dont want to mess about with code formating or excessive use of exception handling at that level.
But word to the wise – you need a good database solution? Remobjects will give you a great solution out of the box. It depends on how close to the code you want to work.
Leave a Reply Cancel reply
Recent
The vatican vault
- January 2022
- October 2021
- March 2021
- November 2020
- September 2020
- July 2020
- June 2020
- April 2020
- March 2020
- February 2020
- January 2020
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- February 2013
- August 2012
- June 2012
- May 2012
- April 2012
Martin Fowler’s “enterprise architecture patterns” is a good read on this subject..
There are alternatvies to DataAbstract, especially our Open Source mORMot!
BTW nice blog posts – I just discovered the web site!
Thx! Yes there are many alternatives out there. I just decided to roll my own.