Sql session qt cs

900 views

Published on

A small presentation on the challenged faced with the Qt 4 SQL module, and the changes that were needed.

Presentation for session at the Qt Contributors Summit.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
900
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sql session qt cs

  1. 1. April 20, 2011<br />Qt SQL module<br />SAINT adaptations & workarounds<br />André Somers | 1<br />
  2. 2. André Somers | 2 | SAINT database layer : Design and Rationale<br />Database use case<br />Situation:<br />All data stored in Relational Database, one database foreachstudy<br />OriginallyusingMS Access<br />Making database independent; first target MySql<br />Problem: <br />SQL dialects<br />Different names and limitationsfor data types<br />Solution: <br />No actual SQL code in code<br />Query textsgeneratedby (extended) SQL driverforQt<br />
  3. 3. André Somers | 3 | SAINT database layer : Design and Rationale<br />Qt & Databases<br />Qt drivers do not support DDL by default<br />No way to generate SQL to manipulate data structures<br />Solution: custom extension to Qt SQL driver model<br />Qt SQL drivers are plugins<br />New drivers subclassed from standard driver (so they can still be used where you’d otherwise use other drivers), but have an extended base to allow for more functionality.<br />
  4. 4. André Somers | 4 | SAINT database layer : Design and Rationale<br />SAINT extended Qt DB drivers<br />Standard Qt<br />SAINT extended drivers<br />QSqlDriver<br />SqlDriverExt<br />QODBCDriver<br />AccessDriver<br />QMySqlDriver<br />MySqlDriver<br />Other driver<br />Future other drivers<br />
  5. 5. André Somers | 5 | SAINT database layer : Design and Rationale<br />SAINT extended Qt DB drivers 2<br />//createtable (simplified)<br />QSqlRecordrecord;<br />foreach (QSqlField field, table->fieldList) {<br />record.append(field);<br />}<br />foreach(QStringrelationName, table->fkRelations) {<br />Relation* rel = m_relations.value(relationName);<br />QSqlFieldfkField(QString("%1_%2")<br /> .arg(tableName(rel->tableOne)) <br /> .arg(rel->fieldOne), QVariant::UInt);<br />fkField.setLength(30);<br />record.append(fkField);<br />}<br />sql= m_sqlDriver->sqlStatement(SqlDriverExt::CreateTableStatement, <br />table->name, record);<br />QSqlQueryquery(sql, m_db);<br />Construct object-based representation of the desired table structure<br />SqlDriverExt instance<br />One of the new sqlStatement types<br />Use representation to create SQL<br />
  6. 6. New DDL statement types<br />CreateTableStatement<br />AlterTableAddFieldStatement<br />AlterTableDropFieldStatement<br />AlterTableModifyFieldStatement<br />DropTableStatement<br />CreateViewStatement<br />DropViewStatement<br />CreateIndexStatement<br />DropDatabaseStatement<br />
  7. 7. Other added features<br />Create-if-not-exists<br />Use database templates<br />List databases on a server<br />RAII class for transactions<br />Return information on installed drivers:<br />Human readable name<br />Icon<br />Server or file based<br />Default settings for UI (like standard port number)<br />Workarounds for some smaller Qt SQL issues<br />
  8. 8. Unifying the UI<br />Goals:<br />Unified way to select a database independent of the type<br />Keep history of recently used databases for easy re-selecting<br />Re-usable components for selection for integration in all tools<br />Treat all the information on a database as a single unit of information<br />Avoid special-casing for different types of databases<br />
  9. 9. Resulting UI<br />André Somers | 9 | SAINT database layer: design and rationale<br />Drop-down box for easy re-selecting recently used databases<br />Access to more databases<br />List is provided by proxy on custom model<br />Icons representing database type<br />Unique alias for each used database<br />
  10. 10. Resulting UI (cont)<br />André Somers | 10 | SAINT database layer: design and rationale<br />Tabs to switch between already used databases and other databases<br />Sortable<br />Filter on alias <br />Human-readable dates and times of last use<br />
  11. 11. Resulting UI (cont)<br />André Somers | 11 | SAINT database layer: design and rationale<br />Drop down with discovered database drivers<br />File selector dialog<br />Recently used files<br />Database alias (auto suggested)<br />
  12. 12. Resulting UI (cont)<br />André Somers | 12 | SAINT database layer: design and rationale<br />Existing databases on selected server<br />
  13. 13. DatabaseDescription class<br />Properties<br />QString driverName<br />QString fileName<br />QString host<br />QString hostAlias<br />int port <br />QString user<br />QString password<br />QString databaseName<br />QString alias<br />QDateTime lastAccess<br />Methods<br />bool exists()<br />bool isValid()<br />State state()<br />QSqlDatabase sqlDatabase(const Qstring&)<br />SqlDriverExt::DriverInfo driverInfo()<br />bool dropDatabase()<br />Operators<br />QDataStream& operator<<(QDataStream&, const DatabaseDescription&)<br />QDataStream& operator>>(QDataStream&, DatabaseDescription&)<br />
  14. 14. Transactions<br />Perfect candidate for RAII<br />Creating an instance on a QSqlDatabase begins a transaction<br />Explicitly call commit() method to mark successful end of transaction<br />If goes out of scope before commit is called, it tries a roll-back.<br />André Somers | 14 | SAINT database layer: design and rationale<br />
  15. 15. Transaction RAII class<br />classSqltransaction {<br />public:<br />explicitSqlTransaction(QSqlDriver* driver);<br /> ~SqlTransaction();<br />QSqlDriver* driver() {return m_driver;}<br />inlineboolinTransaction() {return m_inTransaction;}<br />inlineboolactive() {return m_inTransaction;}<br />boolrollBack();<br />boolcommit();<br />boolcommitOrRollBack();<br />boolsupported();<br />private:<br /> //...<br />};<br />André Somers | 15 | SAINT database layer: design and rationale<br />
  16. 16. Succes?<br />Almost all database-dependent code has been removed from the code that is not part of the database layer presented.<br />Still some pieces of code like this:<br />Separate database template files are still needed<br />All in all: pretty good, and certainly workable<br />André Somers | 16 | SAINT database layer: design and rationale<br />
  17. 17. Remaining problems<br />Documentation in Qt is lacking on this<br />Approach is quite low-level. Higher level would be nice<br />Query construction primitives are very basic:<br />No sorting<br />Only = operator in WHERE statements<br />No limiting<br />Not async<br />André Somers | 17 | SAINT database layer: design and rationale<br />
  18. 18. Thanks<br />Thank you for your attention.<br />Questions & Discussion:<br />What issues remain for you?<br />André Somers | 18 | SAINT database layer: design and rationale<br />
  19. 19. André Somers | 19 | SAINT database layer: design and rationale<br />
  20. 20. André Somers | 20 | SAINT database layer: design and rationale<br />
  21. 21. Bio-energy, 2008-2009, 8414 publications, approx. 187400 links, in Gephi using Force Atlas. Image: E. Horlings.<br />

×