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);
}