All pastes #931008 Raw Edit

Stuff

public text v1 · immutable
#931008 ·published 2008-03-06 22:15 UTC
rendered paste body
--- src/sql/drivers/mysql/qsql_mysql.cpp	2007-09-26 13:53:41 -0000
+++ src/sql/drivers/mysql/qsql_mysql.cpp	2007-09-26 13:53:41 -0000

@@ -37,7 +37,6 @@
 #include "qsql_mysql.h"
 #include <private/qsqlextension_p.h>
 
-#include <qapplication.h>
 #include <qdatetime.h>
 #include <qvaluevector.h>
 #include <qsqlrecord.h>
@@ -52,6 +51,9 @@
 
 QPtrDict<QSqlOpenExtension> *qSqlOpenExtDict();
 
+static int qMySqlConnectionCount = 0;
+static bool qMySqlInitHandledByUser = FALSE;
+
 class QMYSQLOpenExtension : public QSqlOpenExtension
 {
 public:
@@ -354,9 +356,8 @@
 {
 #ifndef Q_NO_MYSQL_EMBEDDED
 # if MYSQL_VERSION_ID >= 40000
-    static bool init = FALSE;
-    if ( init )
-	return;
+    if ( qMySqlInitHandledByUser || qMySqlConnectionCount > 1 )
+        return;
 
     // this should only be called once
     // has no effect on client/server library
@@ -366,8 +367,6 @@
 	qWarning( "QMYSQLDriver::qServerInit: unable to start server." );
 #  endif
     }
-    qAddPostRoutine(qServerEnd);
-    init = TRUE;    
     
 # endif // MYSQL_VERSION_ID
 #endif // Q_NO_MYSQL_EMBEDDED
@@ -392,8 +391,10 @@
 	d->mysql = (MYSQL *) con;
 	setOpen( TRUE );
 	setOpenError( FALSE );
+        if (qMySqlConnectionCount == 1)
+            qMySqlInitHandledByUser = TRUE;
     } else {
-	qServerInit();
+        qServerInit();
     }
 }
 
@@ -402,10 +403,15 @@
     qSqlOpenExtDict()->insert( this, new QMYSQLOpenExtension(this) );
     d = new QMYSQLDriverPrivate();
     d->mysql = 0;
+    qMySqlConnectionCount++;
 }
 
 QMYSQLDriver::~QMYSQLDriver()
 {
+    qMySqlConnectionCount--;
+    if (qMySqlConnectionCount == 0 && !qMySqlInitHandledByUser)
+        qServerEnd();
+
     delete d;
     if ( !qSqlOpenExtDict()->isEmpty() ) {
 	QSqlOpenExtension *ext = qSqlOpenExtDict()->take( this );