[mythtv] [patch] Initial support for other databases than MySQL

David Härdeman david at 2gen.com
Sun Aug 29 11:31:06 EDT 2004


On Fri, Aug 27, 2004 at 08:01:27PM -0400, Isaac Richards wrote:
>On Friday 27 August 2004 07:08 pm, David Härdeman wrote:
>> On Thu, Aug 26, 2004 at 01:25:59AM +0200, David Härdeman wrote:
>> >one of the top priorities on my MythTV wishlist is currently PostgreSQL
>> >support...so I've spent some time looking at the code trying to come up
>> >with a good, non-intrusive way of adding it.
>> >
>> >This patch is the first step in that direction.
>>
>> I just realized that this patch needs some more work, please disregard
>> it for now.
>>
>> Looking at libs/libmyth/oldsettings.cpp, its only used for getting the
>> settings out of the textfile necessary to connect to the database where
>> all other settings are.
>>
>> My current idea would be to revamp oldsettings and turn it into a
>> private class of a small db wrapper class, does anyone object to this?
>
>Yes.  Leave oldsettings as is, please.
>
>Isaac

Ok, said and done, I've produced a new version of the patch which leaves 
oldsettings alone and fixes some thinkos with the first patch. Compiled 
and tested with MySQL and PostgreSQL as backends and both work as 
expected (ie, perfectly and not at all respectively).

More patches will be available soon, I am currently working on the stuff 
in mythtv/libs/libmythtv/dbcheck.cpp in order to get at least the mythtv 
module working with PostgreSQL.

Please review and comment/commit.

Re,
David Härdeman
-------------- next part --------------
Index: libs/libmyth/libmyth.pro
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmyth/libmyth.pro,v
retrieving revision 1.50
diff -u -w -B -r1.50 libmyth.pro
Index: libs/libmyth/mythcontext.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmyth/mythcontext.cpp,v
retrieving revision 1.130
diff -u -w -B -r1.130 mythcontext.cpp
--- libs/libmyth/mythcontext.cpp	19 Aug 2004 02:04:36 -0000	1.130
+++ libs/libmyth/mythcontext.cpp	29 Aug 2004 12:32:36 -0000
@@ -64,6 +63,7 @@
 
     QSqlDatabase* m_db;
     QMutex dbLock;
+    QSqlDatabase *openDB(const QString &name = NULL);
 
     QMap<QString, QImage> imageCache;
 
@@ -96,6 +96,38 @@
     bool screensaverEnabled;
 };
 
+QSqlDatabase * MythContextPrivate::openDB(const QString &name)
+{
+    /* First see if this DB has already been added */
+    QSqlDatabase *db;
+    if(name)
+	db = QSqlDatabase::database(name);
+    else
+	db = QSqlDatabase::database();
+    if(db)
+	return db;
+
+    /* Nope, let's create it, first check which kind of DB is requested, default to MySQL */
+    db = QSqlDatabase::addDatabase(m_settings->GetSetting("DBType", "QMYSQL3"));
+    if(!db)
+    {
+	cerr << "Unable to add database driver, exiting\n";
+	exit(-1);
+    }
+
+    db->setDatabaseName(m_settings->GetSetting("DBName"));
+    db->setUserName(m_settings->GetSetting("DBUserName"));
+    db->setPassword(m_settings->GetSetting("DBPassword"));
+    db->setHostName(m_settings->GetSetting("DBHostName"));
+    if (!db->open()) 
+    {
+        cerr << "Unable to connect to database, exiting\n";
+	exit(-1);
+    }
+
+    return db;
+}
+
 MythContextPrivate::MythContextPrivate(MythContext *lparent)
 {
     pluginmanager = NULL;
@@ -109,21 +141,26 @@
         m_installprefix = PREFIX;
 
     m_settings = new Settings;
+    if (!m_settings->LoadSettingsFiles("mysql.txt", m_installprefix))
+        cerr << "Unable to read configuration file mysql.txt" << endl;
     m_qtThemeSettings = new Settings;
 
     language = "";
     m_themeloaded = false;
     m_backgroundimage = NULL;
 
-    m_db = QSqlDatabase::addDatabase("QMYSQL3", "MythContext");
+    /* Prepare the default connection and a private connection for MythContext
+     * The default is the connection returned by QSqlDatabase::database() so
+     * we don't need to keep a reference to it
+     */
+    openDB();
+    m_db = openDB("MythContext");
+
     screensaver = new ScreenSaverControl();
 }
 
 void MythContextPrivate::Init(bool gui, bool lcd)
 {
-    if (!parent->LoadSettingsFiles("mysql.txt"))
-        cerr << "Unable to read configuration file mysql.txt" << endl;
-
     m_localhostname = m_settings->GetSetting("LocalHostName", NULL);
     if (m_localhostname == NULL)
     {
@@ -750,6 +787,11 @@
     return d->m_themepathname;
 }
 
+/*
+ * This is for backwards compability only, as soon as
+ * all modules use the other version of OpenDatabase
+ * we can remove this.
+ */
 int MythContext::OpenDatabase(QSqlDatabase *db)
 {
     d->dbLock.lock();
@@ -774,6 +816,12 @@
     return res;
 }
 
+/* This should be used instead of the version above */
+QSqlDatabase *MythContext::OpenDatabase(const QString &name)
+{
+    return(d->openDB(name));
+}
+
 void MythContext::KickDatabase(QSqlDatabase *db)
 {
     // Some explanation is called for.  This exists because the mysql
Index: libs/libmyth/mythcontext.h
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmyth/mythcontext.h,v
retrieving revision 1.160
diff -u -w -B -r1.160 mythcontext.h
--- libs/libmyth/mythcontext.h	19 Aug 2004 02:04:36 -0000	1.160
+++ libs/libmyth/mythcontext.h	29 Aug 2004 12:32:36 -0000
@@ -142,8 +142,9 @@
     QString GetThemeDir(void);
 
     QString GetMenuThemeDir(void);
-
+    QSqlDatabase *OpenDatabase(const QString &name = NULL);
     int OpenDatabase(QSqlDatabase *db);
+
     static void KickDatabase(QSqlDatabase *db);
     static void DBError(const QString &where, const QSqlQuery& query);
     static QString DBErrorMessage(const QSqlError& err);
Index: libs/libmyth/mythdbcon.h
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmyth/mythdbcon.h,v
retrieving revision 1.3
diff -u -w -B -r1.3 mythdbcon.h
--- libs/libmyth/mythdbcon.h	9 Jun 2004 03:21:47 -0000	1.3
+++ libs/libmyth/mythdbcon.h	29 Aug 2004 12:32:36 -0000
@@ -17,22 +17,14 @@
     MythSqlDatabase(const QString &name)
     {
         m_name = name;
-        m_db = QSqlDatabase::addDatabase("QMYSQL3", name);
 
+	m_db = gContext->OpenDatabase(name);
         if (!m_db)
         {
             cerr << "Unable to init db connection: " << name << endl;
             return;
         }
 
-        if (!gContext->OpenDatabase(m_db))
-        {
-            cerr << "Unable to open db connect: " << name << endl;
-            QSqlDatabase::removeDatabase(name);
-            m_db = NULL;
-            return;
-        }
-
         m_dblock = new QMutex(true);
     }
 
Index: libs/libmythtv/tv_rec.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/tv_rec.cpp,v
retrieving revision 1.161
diff -u -w -B -r1.161 tv_rec.cpp
--- libs/libmythtv/tv_rec.cpp	19 Aug 2004 02:02:21 -0000	1.161
+++ libs/libmythtv/tv_rec.cpp	29 Aug 2004 12:32:37 -0000
@@ -1026,15 +1026,9 @@
 
     pthread_mutex_lock(&db_lock);
 
-    db_conn = QSqlDatabase::addDatabase("QMYSQL3", name);
+    db_conn = gContext->OpenDatabase(name);
     if (!db_conn)
     {
-        pthread_mutex_unlock(&db_lock);
-        printf("Couldn't initialize mysql connection\n");
-        return;
-    }
-    if (!gContext->OpenDatabase(db_conn))
-    {
         printf("Couldn't open database\n");
     }
 
Index: programs/mythbackend/main.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythbackend/main.cpp,v
retrieving revision 1.74
diff -u -w -B -r1.74 main.cpp
--- programs/mythbackend/main.cpp	19 Aug 2004 02:02:24 -0000	1.74
+++ programs/mythbackend/main.cpp	29 Aug 2004 12:32:38 -0000
@@ -417,63 +417,52 @@
 
     gContext = new MythContext(MYTH_BINARY_VERSION, false, false);
 
-    QSqlDatabase *db = QSqlDatabase::addDatabase("QMYSQL3");
+    QSqlDatabase *db = gContext->OpenDatabase();
     if (!db)
     {
-        cerr << "Couldn't connect to database\n";
+        cerr << "Couldn't connect to database (MythContext)\n";
         return -1;
     }
 
-    QSqlDatabase *subthread = QSqlDatabase::addDatabase("QMYSQL3", "SUBDB");
+    QSqlDatabase *subthread = gContext->OpenDatabase("SUBDB");
     if (!subthread)
     {
-        cerr << "Couldn't connect to database\n";
+        cerr << "Couldn't connect to database (SUBDB)\n";
         return -1;
     }
 
-    QSqlDatabase *expthread = QSqlDatabase::addDatabase("QMYSQL3", "EXPDB");
+    QSqlDatabase *expthread = gContext->OpenDatabase("EXPDB");
     if (!expthread)
     {
-        cerr << "Couldn't connect to database\n";
+        cerr << "Couldn't connect to database (EXPDB)\n";
         return -1;
     }
 
-    QSqlDatabase *hkthread = QSqlDatabase::addDatabase("QMYSQL3", "HKDB");
+    QSqlDatabase *hkthread = gContext->OpenDatabase("HKDB");
     if (!hkthread)
     {
-        cerr << "Couldn't connect to database\n";
+        cerr << "Couldn't connect to database (HKDB)\n";
         return -1;
     }
 
-    QSqlDatabase *transthread = QSqlDatabase::addDatabase("QMYSQL3", "TRANSDB");
+    QSqlDatabase *transthread = gContext->OpenDatabase("TRANSDB");
     if (!transthread)
     {
-        cerr << "Couldn't connect to database\n";
+        cerr << "Couldn't connect to database (TRANSDB)\n";
         return -1;
     }
 
-    QSqlDatabase *flagthread = QSqlDatabase::addDatabase("QMYSQL3", "FLAGDB");
+    QSqlDatabase *flagthread = gContext->OpenDatabase("FLAGDB");
     if (!flagthread)
     {
-        cerr << "Couldn't connect to database\n";
+        cerr << "Couldn't connect to database (FLAGDB)\n";
         return -1;
     }
 
-    QSqlDatabase *msdb = QSqlDatabase::addDatabase("QMYSQL3", "MSDB");
+    QSqlDatabase *msdb = gContext->OpenDatabase("MSDB");
     if (!msdb)
     {
-        cerr << "Couldn't connect to database\n";
-        return -1;
-    }
-
-    if (!gContext->OpenDatabase(db) || !gContext->OpenDatabase(subthread) ||
-        !gContext->OpenDatabase(expthread) ||
-        !gContext->OpenDatabase(hkthread) ||
-        !gContext->OpenDatabase(transthread) ||
-        !gContext->OpenDatabase(flagthread) ||
-        !gContext->OpenDatabase(msdb))
-    {
-        cerr << "Couldn't open database\n";
+        cerr << "Couldn't connect to database (MSDB)\n";
         return -1;
     }
 
Index: programs/mythcommflag/main.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythcommflag/main.cpp,v
retrieving revision 1.16
diff -u -w -B -r1.16 main.cpp
--- programs/mythcommflag/main.cpp	27 Aug 2004 04:26:24 -0000	1.16
+++ programs/mythcommflag/main.cpp	29 Aug 2004 12:32:39 -0000
@@ -489,8 +489,8 @@
 
     gContext = new MythContext(MYTH_BINARY_VERSION, false);
 
-    QSqlDatabase *db = QSqlDatabase::addDatabase("QMYSQL3");
-    if (!gContext->OpenDatabase(db))
+    QSqlDatabase *db = gContext->OpenDatabase();
+    if (!db)
     {
         printf("couldn't open db\n");
         exit(11);
Index: programs/mythepg/main.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythepg/main.cpp,v
retrieving revision 1.31
diff -u -w -B -r1.31 main.cpp
--- programs/mythepg/main.cpp	14 May 2004 02:41:37 -0000	1.31
+++ programs/mythepg/main.cpp	29 Aug 2004 12:32:39 -0000
@@ -14,8 +14,8 @@
 
     gContext = new MythContext(MYTH_BINARY_VERSION);
 
-    QSqlDatabase *db = QSqlDatabase::addDatabase("QMYSQL3");
-    if (!gContext->OpenDatabase(db))
+    QSqlDatabase *db = gContext->OpenDatabase();
+    if (!db)
     {
         printf("couldn't open db\n");
         return -1;
Index: programs/mythfilldatabase/filldata.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythfilldatabase/filldata.cpp,v
retrieving revision 1.131
diff -u -w -B -r1.131 filldata.cpp
--- programs/mythfilldatabase/filldata.cpp	19 Aug 2004 02:02:26 -0000	1.131
+++ programs/mythfilldatabase/filldata.cpp	29 Aug 2004 12:32:41 -0000
@@ -3113,8 +3113,8 @@
 
     gContext = new MythContext(MYTH_BINARY_VERSION, false);
 
-    QSqlDatabase *db = QSqlDatabase::addDatabase("QMYSQL3");
-    if (!gContext->OpenDatabase(db))
+    QSqlDatabase *db = gContext->OpenDatabase();
+    if (!db)
     {
         cerr << "couldn't open db\n";
         return -1;
Index: programs/mythfrontend/main.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythfrontend/main.cpp,v
retrieving revision 1.154
diff -u -w -B -r1.154 main.cpp
--- programs/mythfrontend/main.cpp	22 Aug 2004 18:14:16 -0000	1.154
+++ programs/mythfrontend/main.cpp	29 Aug 2004 12:32:42 -0000
@@ -810,16 +810,10 @@
 
     gContext = new MythContext(MYTH_BINARY_VERSION);
 
-    QSqlDatabase *db = QSqlDatabase::addDatabase("QMYSQL3");
+    QSqlDatabase *db = gContext->OpenDatabase();
     if (!db)
     {
-        printf("Couldn't connect to database\n");
-        return -1;
-    }
-
-    if (!gContext->OpenDatabase(db))
-    {
-        printf("couldn't open db\n");
+        printf("Couldn't open db\n");
         int pause;
         if ((pause = gContext->GetNumSetting("WOLsqlReconnectWaitTime", 0)) > 0)
         {
@@ -833,7 +827,7 @@
 
             if (!WOLsqlCommand.isEmpty())
             {
-                while (acttry <= retries && !gContext->OpenDatabase(db))
+                while (acttry <= retries && !(db = gContext->OpenDatabase()))
                 {
                     printf("Trying to wakeup SQLserver (Try %d of %d)\n",
                            acttry, retries);
Index: programs/mythlcd/main.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythlcd/main.cpp,v
retrieving revision 1.12
diff -u -w -B -r1.12 main.cpp
--- programs/mythlcd/main.cpp	23 Jan 2004 06:01:17 -0000	1.12
+++ programs/mythlcd/main.cpp	29 Aug 2004 12:32:42 -0000
@@ -30,8 +30,8 @@
     QApplication a(argc, argv);
     gContext = new MythContext(MYTH_BINARY_VERSION, FALSE);
 
-    db = QSqlDatabase::addDatabase("QMYSQL3");
-    if (!gContext->OpenDatabase(db))
+    db = gContext->OpenDatabase();
+    if (!db)
     {
         cerr << "Unable to open database:\n"
              << "Driver error was:" << endl
Index: programs/mythprogfind/main.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythprogfind/main.cpp,v
retrieving revision 1.7
diff -u -w -B -r1.7 main.cpp
--- programs/mythprogfind/main.cpp	23 Jan 2004 06:01:17 -0000	1.7
+++ programs/mythprogfind/main.cpp	29 Aug 2004 12:32:42 -0000
@@ -14,8 +14,8 @@
 
     gContext = new MythContext(MYTH_BINARY_VERSION);
 
-    QSqlDatabase *db = QSqlDatabase::addDatabase("QMYSQL3");
-    if (!gContext->OpenDatabase(db))
+    QSqlDatabase *db = gContext->OpenDatabase();
+    if (!db)
     {
         printf("couldn't open db\n");
         return -1;
Index: programs/mythtranscode/main.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythtranscode/main.cpp,v
retrieving revision 1.19
diff -u -w -B -r1.19 main.cpp
--- programs/mythtranscode/main.cpp	6 Aug 2004 17:20:30 -0000	1.19
+++ programs/mythtranscode/main.cpp	29 Aug 2004 12:32:42 -0000
@@ -228,19 +228,13 @@
          return -1;
     }
 
-    db = QSqlDatabase::addDatabase("QMYSQL3");
+    db = gContext->OpenDatabase();
     if (!db)
     {
         printf("Couldn't connect to database\n");
         return -1;
     }
 
-    if (!gContext->OpenDatabase(db))
-    {
-        printf("couldn't open db\n");
-        return -1;
-    }
-
     MythContext::KickDatabase(db);
 
     ProgramInfo *pginfo = NULL;
Index: programs/mythtv/main.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythtv/main.cpp,v
retrieving revision 1.30
diff -u -w -B -r1.30 main.cpp
--- programs/mythtv/main.cpp	19 Aug 2004 02:02:27 -0000	1.30
+++ programs/mythtv/main.cpp	29 Aug 2004 12:32:42 -0000
@@ -28,16 +28,12 @@
     
     gContext->LoadQtConfig();
 
-    QSqlDatabase *db = QSqlDatabase::addDatabase("QMYSQL3");
+    QSqlDatabase *db = gContext->OpenDatabase();
     if (!db)
     {
         printf("Couldn't connect to database\n");
         return 45; // exit(45)
-    }       
-    if (!gContext->OpenDatabase(db))
-    {
-        printf("couldn't open db\n");
-        return 46; // exit(46)
+        /* return 46 aka exit(46) used to be here */
     }
 
     gContext->LoadQtConfig();
Index: setup/main.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/setup/main.cpp,v
retrieving revision 1.46
diff -u -w -B -r1.46 main.cpp
--- setup/main.cpp	8 May 2004 19:58:31 -0000	1.46
+++ setup/main.cpp	29 Aug 2004 12:32:42 -0000
@@ -121,9 +121,8 @@
 
     gContext = new MythContext(MYTH_BINARY_VERSION, true);
     translator = new QTranslator(0);
-
-    db = QSqlDatabase::addDatabase("QMYSQL3");
-    if (!gContext->OpenDatabase(db))
+    db = gContext->OpenDatabase();
+    if (!db)
     {
         cerr << "Unable to open database:\n"
              << "Driver error was:" << endl


More information about the mythtv-dev mailing list