summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorworo <woro@e10066b5-e1e2-0310-b819-94efdf66514b>2008-02-12 13:59:19 +0000
committerworo <woro@e10066b5-e1e2-0310-b819-94efdf66514b>2008-02-12 13:59:19 +0000
commitdc0e6d48c36ed748705d985392133d05b9a5ad55 (patch)
tree4dd38feb208cd432c0b1af1d5317c37e77eccb58
parent5b9421a875952ea5047f0ca8774746042298e274 (diff)
downloadvdr-plugin-muggle-dc0e6d48c36ed748705d985392133d05b9a5ad55.tar.gz
vdr-plugin-muggle-dc0e6d48c36ed748705d985392133d05b9a5ad55.tar.bz2
restructure and simplify database connection&creation again
git-svn-id: https://vdr-muggle.svn.sourceforge.net/svnroot/vdr-muggle/trunk/muggle-plugin@1029 e10066b5-e1e2-0310-b819-94efdf66514b
-rw-r--r--mg_db.c46
-rw-r--r--mg_db.h11
-rw-r--r--mg_db_gd_mysql.c28
-rw-r--r--mg_db_gd_mysql.h4
-rw-r--r--mg_db_gd_pg.c28
-rw-r--r--mg_db_gd_pg.h4
-rw-r--r--mg_db_gd_sqlite.c22
-rw-r--r--mg_db_gd_sqlite.h3
-rw-r--r--po/de.po12
-rw-r--r--po/fr.po10
-rw-r--r--po/muggle.pot5
11 files changed, 110 insertions, 63 deletions
diff --git a/mg_db.c b/mg_db.c
index 42f9298..9d98612 100644
--- a/mg_db.c
+++ b/mg_db.c
@@ -309,24 +309,44 @@ mgDb::Connect ()
return false;
m_create_time=time(0);
m_database_found=ConnectDatabase();
- if (!m_database_found)
- mgWarning("database not found");
- if (the_setup.IsMugglei())
+ if (!m_database_found && !Creatable())
{
- if (the_setup.CreateMode)
- m_database_found = Create();
+ mgWarning("database not found");
+ return false;
}
+ bool clearwanted=false;
+ if (the_setup.IsMugglei())
+ clearwanted=the_setup.CreateMode;
else
- {
- if (!m_database_found)
+ if (!m_database_found)
{
extern bool create_question();
- if (create_question())
- m_database_found = Create();
+ clearwanted=create_question();
}
+ if (clearwanted)
+ {
+ if (!m_database_found && Creatable())
+ {
+ m_database_found = true; // avoid recursion
+ mgWarning("Dropping and recreating database %s",the_setup.DbName);
+ m_database_found = Create();
+ if (m_database_found)
+ {
+ m_database_found = true; // avoid recursion
+ m_database_found = ConnectDatabase();
+ }
+ }
}
if (m_database_found)
- m_database_found = SetCharset();
+ {
+ if (clearwanted || !FieldExists("tracks","id"))
+ {
+ if (Clear())
+ mgWarning(trdb("empty database created"));
+ }
+ if (m_database_found && exec_count("SELECT COUNT(1) FROM genre")==0)
+ FillTables();
+ }
return m_database_found;
}
@@ -344,12 +364,6 @@ mgDb::SyncStart()
return true;
}
-bool
-mgDb::DatabaseEmpty()
-{
- return (exec_count("SELECT COUNT FROM TRACKS")>0);
-}
-
void
mgDb::Sync(char * const * path_argv)
{
diff --git a/mg_db.h b/mg_db.h
index 6bb1c23..ff35401 100644
--- a/mg_db.h
+++ b/mg_db.h
@@ -21,6 +21,10 @@
#include <id3v2tag.h>
#include <fileref.h>
+#ifndef trdb
+#define trdb(a) (a)
+#endif
+
using namespace std;
class mgItem;
@@ -194,7 +198,6 @@ class mgDb {
virtual const char* DecadeExpr()=0;
virtual string Now() const =0;
virtual string Directory() const =0;
- bool DatabaseEmpty();
protected:
int m_rows;
int m_cols;
@@ -205,9 +208,9 @@ class mgDb {
time_t m_connect_time;
time_t m_create_time;
string get_col0(const string sql);
+ virtual bool Creatable() { return true; }
virtual bool Create() = 0;
- virtual bool SetCharset() = 0;
- void FillTables();
+ virtual bool Clear() = 0;
virtual void StartTransaction() {};
virtual void Commit() {};
virtual bool SyncStart();
@@ -227,7 +230,7 @@ class mgDb {
map<string,string> m_Genres;
map<string,string> m_GenreIds;
bool m_database_found;
-
+ void FillTables();
};
class mgKey {
diff --git a/mg_db_gd_mysql.c b/mg_db_gd_mysql.c
index d8eaae7..d34a958 100644
--- a/mg_db_gd_mysql.c
+++ b/mg_db_gd_mysql.c
@@ -432,22 +432,24 @@ mgDbGd::SetCharset()
bool
mgDbGd::Create()
{
- if (!ServerConnect())
- return false;
// create database and tables
- mgWarning("Dropping and recreating database %s",the_setup.DbName);
char buffer[500];
sprintf(buffer,"DROP DATABASE IF EXISTS %s",the_setup.DbName);
if (strlen(buffer)>400)
mgError("name of database too long: %s",the_setup.DbName);
mgQuery q(m_db,buffer);
if (!q.ErrorMessage().empty())
+ {
+ mgWarning(q.ErrorMessage().c_str());
return false;
-
+ }
sprintf(buffer,"CREATE DATABASE %s",the_setup.DbName);
mgQuery q1(m_db,buffer);
if (!q1.ErrorMessage().empty())
+ {
+ mgWarning(q.ErrorMessage().c_str());
return false;
+ }
if (!UsingEmbeddedMySQL())
sprintf(buffer,"grant all privileges on %s.* to vdr@localhost",
@@ -455,10 +457,13 @@ mgDbGd::Create()
Execute(buffer);
// ignore error. If we can create the data base, we can do everything
// with it anyway.
+ return true;
+}
- if (mysql_select_db(m_db,the_setup.DbName))
- mgError("mysql_select_db(%s) failed with %s",the_setup.DbName,mysql_error(m_db));
-
+bool
+mgDbGd::Clear()
+{
+ char buffer[500];
int len = sizeof( db_cmds ) / sizeof( char* );
for( int i=0; i < len; i ++ )
{
@@ -470,8 +475,6 @@ mgDbGd::Create()
return false;
}
}
- FillTables();
- mgWarning("new database successfully created");
return true;
}
@@ -535,7 +538,12 @@ UsingEmbeddedMySQL()
bool
mgDbGd::ConnectDatabase ()
{
- return mysql_select_db(m_db,the_setup.DbName)==0;
+ bool res=mysql_select_db(m_db,the_setup.DbName)==0;
+ if (res)
+ res=SetCharset();
+ else
+ mgError("mysql_select_db(%s) failed with %s",the_setup.DbName,mysql_error(m_db));
+ return res;
}
bool
diff --git a/mg_db_gd_mysql.h b/mg_db_gd_mysql.h
index b725d9a..d44445c 100644
--- a/mg_db_gd_mysql.h
+++ b/mg_db_gd_mysql.h
@@ -27,7 +27,7 @@ class mgDbGd : public mgDb {
bool ServerConnect();
bool ConnectDatabase();
bool Create();
- bool SetCharset();
+ bool Clear();
int AddToCollection( const string Name,const vector<mgItem*>&items,mgParts* what);
int RemoveFromCollection( const string Name,const vector<mgItem*>&items,mgParts* what);
@@ -51,7 +51,7 @@ class mgDbGd : public mgDb {
void CreateFolderFields();
MYSQL_RES* Query( const string sql);
bool sql_query(string sql);
-
+ bool SetCharset();
};
class mgDbServerMySQL : public mgDbServerImp {
diff --git a/mg_db_gd_pg.c b/mg_db_gd_pg.c
index 68a9482..fda72f2 100644
--- a/mg_db_gd_pg.c
+++ b/mg_db_gd_pg.c
@@ -266,19 +266,29 @@ mgDbGd::SetCharset()
}
bool
+mgDbGd::Creatable()
+{
+ return false;
+}
+
+bool
mgDbGd::Create()
{
- // create database and tables
- int len = sizeof( db_cmds ) / sizeof( char* );
- for( int i=0; i < len; i ++ )
+ return false;
+}
+
+bool
+mgDbGd::Clear()
+{
+ // create database and tables
+ int len = sizeof( db_cmds ) / sizeof( char* );
+ for( int i=0; i < len; i ++ )
{
mgQuery q(m_db,db_cmds[i],mgQueryWarnOnly);
if (!q.ErrorMessage().empty())
return false;
}
- FillTables();
- mgWarning("new database successfully created");
- return true;
+ return true;
}
bool
@@ -317,8 +327,7 @@ mgDbGd::ConnectDatabase ()
mgWarning("Failed to connect to postgres server using %s:%s",conninfo,PQerrorMessage(m_db));
return false;
}
- return atol (get_col0 ("SELECT COUNT(*) FROM information_schema.tables WHERE table_name='album'").c_str ())==1;
- // do not use exec_count because it calls Connect()
+ return SetCharset();
}
bool
@@ -337,7 +346,8 @@ bool
mgDbGd::FieldExists(string table, string field)
{
char *b;
- asprintf(&b,"SELECT COUNT(*) FROM information_schema.columns WHERE table_name='album' AND column_name='%s'",field.c_str());
+ asprintf(&b,"SELECT COUNT(*) FROM information_schema.columns WHERE table_name='%s' AND column_name='%s'",
+ table.c_str(),field.c_str());
bool result = exec_count(b)==1;
free(b);
return result;
diff --git a/mg_db_gd_pg.h b/mg_db_gd_pg.h
index 0c5fef1..5520767 100644
--- a/mg_db_gd_pg.h
+++ b/mg_db_gd_pg.h
@@ -49,8 +49,9 @@ class mgDbGd : public mgDb {
~mgDbGd();
bool ServerConnect();
bool ConnectDatabase();
+ bool Creatable();
bool Create();
- bool SetCharset();
+ bool Clear();
bool NeedGenre2();
long thread_id() { return -1; }
@@ -68,6 +69,7 @@ class mgDbGd : public mgDb {
private:
bool myCreate();
PGconn *m_db;
+ bool SetCharset();
};
#endif
diff --git a/mg_db_gd_sqlite.c b/mg_db_gd_sqlite.c
index 6fb3c22..09b986d 100644
--- a/mg_db_gd_sqlite.c
+++ b/mg_db_gd_sqlite.c
@@ -229,8 +229,20 @@ mgDbGd::Commit()
}
bool
+mgDbGd::Creatable()
+{
+ return true;
+}
+
+bool
mgDbGd::Create()
{
+ return true;
+}
+
+bool
+mgDbGd::Clear()
+{
// create database and tables
int len = sizeof( db_cmds ) / sizeof( char* );
for( int i=0; i < len; i ++ )
@@ -243,8 +255,6 @@ mgDbGd::Create()
return false;
}
}
- FillTables();
- mgWarning("new database successfully created");
return true;
}
@@ -290,12 +300,6 @@ mgSubstring(sqlite3_context *context, int argc, sqlite3_value **argv)
}
bool
-mgDbGd::SetCharset()
-{
- return true;
-}
-
-bool
mgDbGd::ConnectDatabase ()
{
struct stat stbuf;
@@ -333,7 +337,7 @@ mgDbGd::ConnectDatabase ()
mgWarning("Cannot define decade:%d/%s",rc,sqlite3_errmsg);
return false;
}
- return FieldExists("tracks","id");
+ return true;
}
bool
diff --git a/mg_db_gd_sqlite.h b/mg_db_gd_sqlite.h
index aaf8b3b..3bea6e0 100644
--- a/mg_db_gd_sqlite.h
+++ b/mg_db_gd_sqlite.h
@@ -47,8 +47,9 @@ class mgDbGd : public mgDb {
mgDbGd (bool SeparateThread=false);
~mgDbGd();
bool ConnectDatabase();
+ bool Creatable();
bool Create();
- bool SetCharset();
+ bool Clear();
bool NeedGenre2();
long thread_id() { return -1; }
diff --git a/po/de.po b/po/de.po
index 9fda8dc..222c3c8 100644
--- a/po/de.po
+++ b/po/de.po
@@ -10,8 +10,8 @@ msgid ""
msgstr ""
"Project-Id-Version: de\n"
"Report-Msgid-Bugs-To: <vdr-bugs@cadsoft.de>\n"
-"POT-Creation-Date: 2008-02-12 10:32+0100\n"
-"PO-Revision-Date: 2008-02-12 03:22+0100\n"
+"POT-Creation-Date: 2008-02-12 14:40+0100\n"
+"PO-Revision-Date: 2008-02-12 14:41+0100\n"
"Last-Translator: Wolfgang Rohdewald <wolfgang@rohdewald.de>\n"
"Language-Team: deutsch <vdr-muggle-i18n@sourceforge.net>\n"
"MIME-Version: 1.0\n"
@@ -20,6 +20,9 @@ msgstr ""
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: KBabel 1.11.4\n"
+msgid "empty database created"
+msgstr "leere Datenbank angelegt"
+
msgid "Order"
msgstr "Sortierung"
@@ -230,8 +233,3 @@ msgstr "DVB still picture benutzen"
msgid "Delete stale references"
msgstr "Einträge löschen, wenn Datei fehlt"
-#~ msgid "Unknown loop mode"
-#~ msgstr "Unbekannter Endlosmodus"
-
-#~ msgid "Unknown shuffle mode"
-#~ msgstr "Unbekannter Zufallsmodus"
diff --git a/po/fr.po b/po/fr.po
index be326ae..23f852b 100644
--- a/po/fr.po
+++ b/po/fr.po
@@ -9,8 +9,8 @@ msgid ""
msgstr ""
"Project-Id-Version: fr\n"
"Report-Msgid-Bugs-To: <vdr-bugs@cadsoft.de>\n"
-"POT-Creation-Date: 2008-02-12 10:32+0100\n"
-"PO-Revision-Date: 2008-02-12 04:22+0100\n"
+"POT-Creation-Date: 2008-02-12 14:40+0100\n"
+"PO-Revision-Date: 2008-02-12 14:43+0100\n"
"Last-Translator: Wolfgang Rohdewald <wolfgang@rohdewald.de>\n"
"Language-Team: deutsch <vdr-muggle-i18n@sourceforge.net>\n"
"MIME-Version: 1.0\n"
@@ -19,6 +19,9 @@ msgstr ""
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"X-Generator: KBabel 1.11.4\n"
+msgid "empty database created"
+msgstr "créé base des données vide"
+
msgid "Order"
msgstr "Ordre"
@@ -135,7 +138,7 @@ msgid "Key %d"
msgstr "Key %d"
msgid "Sort by count"
-msgstr ""
+msgstr "Ordre par quantité"
#, c-format
msgid "Create database %s?"
@@ -228,3 +231,4 @@ msgstr ""
msgid "Delete stale references"
msgstr ""
+
diff --git a/po/muggle.pot b/po/muggle.pot
index 711c36f..83c7add 100644
--- a/po/muggle.pot
+++ b/po/muggle.pot
@@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: <vdr-bugs@cadsoft.de>\n"
-"POT-Creation-Date: 2008-02-12 10:33+0100\n"
+"POT-Creation-Date: 2008-02-12 14:52+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -16,6 +16,9 @@ msgstr ""
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
+msgid "empty database created"
+msgstr ""
+
msgid "Order"
msgstr ""