New connection callback

Use this forum to request new features or suggest modifications to existing features

New connection callback

Postby mdw » Thu Jun 01, 2017 12:43 pm

I need some callback each time a new DB connection is established. In my case I have to set the correct SQLite PRAGMAs (http://www.sqlite.org/pragma.html) before executing the first statement. But anyway this should be useful also for other DBMS and in other scenarios (logging, user access handling, etc.).

Please find my patch proposal:
Code: Select all
diff --git a/QxSqlDatabase.h.org b/QxSqlDatabase.h
index 58992e9..63dfca4 100644
--- a/QxSqlDatabase.h.org
+++ b/QxSqlDatabase.h
@@ -62,7 +62,8 @@ m_bTraceSqlBoundValues(false), m_bTraceSqlBoundValuesOnError(true), \
 m_ePlaceHolderStyle(ph_style_2_point_name), m_bSessionThrowable(false), \
 m_bSessionAutoTransaction(true), m_bValidatorThrowable(false), \
 m_bAutoReplaceSqlAliasIntoQuery(true), m_bVerifyOffsetRelation(false), \
-m_bAddAutoIncrementIdToUpdateQuery(true), m_bForceParentIdToAllChildren(false)
+m_bAddAutoIncrementIdToUpdateQuery(true), m_bForceParentIdToAllChildren(false), \
+m_fAddDatabaseCallback(0)
 
 namespace qx {
 
@@ -78,6 +79,7 @@ class QX_DLL_EXPORT QxSqlDatabase : public QxSingleton<QxSqlDatabase>
 public:
 
    enum ph_style { ph_style_question_mark, ph_style_2_point_name, ph_style_at_name };
+   typedef void (*db_callback)(QSqlDatabase&);
 
 private:
 
@@ -103,6 +105,7 @@ private:
    bool m_bVerifyOffsetRelation;                            //!< Only for debug purpose : assert if invalid offset detected fetching a relation
    bool m_bAddAutoIncrementIdToUpdateQuery;                 //!< For Microsoft SqlServer database compatibility : add or not auto-increment id to SQL update query
    bool m_bForceParentIdToAllChildren;                      //!< Force parent id to all children (for 1-n relationship for example)
+   db_callback m_fAddDatabaseCallback;                      //!< Called when a new DB connection is opened
 
 private:
 
@@ -130,6 +133,7 @@ public:
    bool getVerifyOffsetRelation() const            { return m_bVerifyOffsetRelation; }
    bool getAddAutoIncrementIdToUpdateQuery() const { return m_bAddAutoIncrementIdToUpdateQuery; }
    bool getForceParentIdToAllChildren() const      { return m_bForceParentIdToAllChildren; }
+   db_callback getAddDatabaseCallback() const      { return m_fAddDatabaseCallback; }
 
    void setDriverName(const QString & s)                          { m_sDriverName = s; getSqlGenerator(); }
    void setConnectOptions(const QString & s)                      { m_sConnectOptions = s; }
@@ -151,6 +155,7 @@ public:
    void setVerifyOffsetRelation(bool b)                           { m_bVerifyOffsetRelation = b; }
    void setAddAutoIncrementIdToUpdateQuery(bool b)                { m_bAddAutoIncrementIdToUpdateQuery = b; }
    void setForceParentIdToAllChildren(bool b)                     { m_bForceParentIdToAllChildren = b; }
+   void setAddDatabaseCallback(const db_callback f)               { m_fAddDatabaseCallback = f; }
 
    static QSqlDatabase getDatabase();
    static QSqlDatabase getDatabase(QSqlError & dbError);


Code: Select all
diff --git a/QxSqlDatabase.cpp.org b/QxSqlDatabase.cpp
index f14096d..acd75af 100644
--- a/QxSqlDatabase.cpp.org
+++ b/QxSqlDatabase.cpp
@@ -132,6 +132,8 @@ QSqlDatabase QxSqlDatabase::createDatabase(QSqlError & dbError)
    if (bError) { QSqlDatabase::removeDatabase(sDbKeyNew); return QSqlDatabase(); }
    m_lstDbByThread.insert(lCurrThreadId, sDbKeyNew);
    qDebug("[QxOrm] qx::QxSqlDatabase : create new database connection in thread '%s' with key '%s'", qPrintable(sCurrThreadId), qPrintable(sDbKeyNew));
+   if (m_fAddDatabaseCallback != 0)
+       m_fAddDatabaseCallback(QSqlDatabase::database(sDbKeyNew));
    return QSqlDatabase::database(sDbKeyNew);
 }

mdw
 
Posts: 34
Joined: Mon Feb 15, 2016 2:45 pm

Re: New connection callback

Postby qxorm » Fri Jun 02, 2017 2:04 pm

Hello,

Your patch has been included in QxOrm 1.4.4, here is a link to a BETA version : https://www.qxorm.com/version/QxOrm_1.4.4_BETA_60.zip
I used std::function to manage the callback function.
FYI, this new version should be officialy released next week.

Here is the changes log :
QxOrm library doesn't depend on boost framework anymore (the boost dependency has been fully removed, replaced by some C++11 features).
So QxOrm library is now a pure Qt library which depends only on QtCore and QtSql by default.
For backward compatibility, QxOrm library still supports some boost classes (boost smart-pointers, unordered containers, boost::optional, etc...) : you have to define _QX_ENABLE_BOOST compilation option to enable these features.

Main advantages are :
- QxOrm becomes a much lighter library
- easier to install (because you don't have to deal with boost anymore)
- reduce compilation times
- reduce output binary size

Thx also to Jimmy Taker for several improvments and new features in QxModelView module !

- QxOrm library now requires a C++11 compiler (please note that QxOrm doesn't require a full compliant C++11 compiler : for example, QxOrm can be built and used with MSVC 2012, GCC 4.5 or Clang 3.2)
- Implement PIMPL idiom for some QxOrm classes to reduce compilation times and output binary size
- New class named qx::any to replace boost::any (basic implementation of boost::any written by Kevlin Henney)
- qx_shared_ptr alias doesn't exist anymore : it is replaced everywhere by std::shared_ptr
- QxModelView module : all models based on qx::IxModel class can now be sorted (on all columns), please note that you can also use QSortFilterProxyModel Qt class to sort your model
- QxModelView module - qx::QxModel<T> : fix setData() with e_auto_update_on_field_change option when an error occurred saving data in database, now previous value is restored if an error occurred
- QxModelView module - qx::IxModel : fix setHeaderData() using it with default role (Qt::EditRole) changes the header in a header view (role Qt::DisplayRole)
- QxModelView module - qx::IxModel : if a description is registered in QxOrm context, then it is displayed in header for each property
- QxModelView module : new feature available to add automatically an empty row at the end of the table to insert quickly new items (setShowEmptyLine() method)
- QxModelView module : possibility to define an intermediate base class between qx::IxModel and qx::QxModel<T> to provide your own model features, for example imagine you develop a drag&drop feature in a class named IxModelDragDrop, you can now create a QxOrm model like this (see the second template parameter) : qx::IxModel * pModel = new qx::QxModel<MyPersistantClass, IxModelDragDrop>();
- QxOrm.pro : fix DESTDIR parameter on Windows
- QxOrm.pri and QxOrm.cmake : add a section to enable QT_USE_QSTRINGBUILDER to optimize QString operations
- QxOrm library is now tested with MSVC 2015 compiler (support all MSVC versions from 2012)
- Fix a bug in QxSqlError.h file with a qPrintable() call on a temporary object
- Provide more details in logs after executing a SQL query : time to execute query in database + time to fetch C++ instances
- Support std::optional<T> (if your compiler supports C++17 features) to manage NULL database value : new header available named <QxExtras/QxStdOptional.h> to include just after <QxOrm.h> header file (ideally in a precompiled header)
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am


Return to QxOrm - Feature request

Who is online

Users browsing this forum: No registered users and 3 guests

cron