PDA

View Full Version : Qt, Databases & the DAO Pattern



Paul Drummond
22nd February 2006, 16:28
I need to make a decision whether to use inline SQL directly using the QtSql module or develop Database Access Objects using the DAO Pattern. How does QtSql handle the differences between databases? I realise there is a hasFeature() method but it doesn't include things like views, triggers, etc, why is that?

When I read about the DAO pattern I am very concerned about whether it actually works in the "real world"!

jacek
22nd February 2006, 17:13
DAO pattern comes from Java world and it's quite reasonable when you take into account all that code you need to write to read something from database using JDBC and the danger of resource leak when you forget to invoke dispose() (or whatever it's called) method on each of created objects.

Another thing is that DAO provides a separation between data and logic. If your database schema changes, you would have to change only apropriate DAO classes instead of looking through your whole code. On the other hand it might lead to performance loss, as you would have to use more general queries. Of course you could use something in between.

Anyway DAO (and QtSql module as well) won't save you from differences in SQL dialects. The best solution, IMO, is to use subset of SQL standard that is implemented by all DBMS that your application will use. I was thinking about other possibility too --- translating the SQL queries in the runtime depending on current database driver, but I'm not sure if it will be fast enough.