From b791ca8a048eef37c0bcaaa0343c59edfe143a2e Mon Sep 17 00:00:00 2001 From: Fred Gleason Date: Thu, 7 Jun 2018 14:01:38 +0000 Subject: [PATCH] 2018-06-07 Fred Gleason * Implemented show database status in rddbmgr(8). --- ChangeLog | 5 +- utils/rddbmgr/Makefile.am | 2 + utils/rddbmgr/modify.cpp | 75 ++--------------------- utils/rddbmgr/printstatus.cpp | 52 ++++++++++++++++ utils/rddbmgr/rddbmgr.cpp | 31 ++++++++-- utils/rddbmgr/rddbmgr.h | 14 ++++- utils/rddbmgr/schemamap.cpp | 112 ++++++++++++++++++++++++++++++++++ 7 files changed, 212 insertions(+), 79 deletions(-) create mode 100644 utils/rddbmgr/printstatus.cpp create mode 100644 utils/rddbmgr/schemamap.cpp diff --git a/ChangeLog b/ChangeLog index ab18b3ae..5eb68e90 100644 --- a/ChangeLog +++ b/ChangeLog @@ -10914,8 +10914,7 @@ 'rdairplay/list_log.h' to display hard times to 1/10 second. * Modified the ButtonLog widget in 'rdairplay/loglinebox.cpp' and 'rdairplay/loglinebox.h' to display hard times to 1/10 second. - * Modified the Edit Event dialog in 'rdairplay/edit_event.cpp' and - 'rdairplay/edit_event.h' to display hard times to 1/10 second. + * Modified the Edit Event dialog in 'rdairplay/edit_event.cpp' and 'rdairplay/edit_event.h' to display hard times to 1/10 second. * Modified the Edit Clock dialog in 'rdlogmanager/edit_clock.cpp' to display Start and End times to 1/10 second. * Modified the Edit Event Assignment dialog in @@ -17018,3 +17017,5 @@ new host with no exemplar. 2018-06-05 Fred Gleason * Implemented the '--modify' command in rddbmgr(8). +2018-06-07 Fred Gleason + * Implemented show database status in rddbmgr(8). diff --git a/utils/rddbmgr/Makefile.am b/utils/rddbmgr/Makefile.am index 528686d5..9be25339 100644 --- a/utils/rddbmgr/Makefile.am +++ b/utils/rddbmgr/Makefile.am @@ -32,8 +32,10 @@ sbin_PROGRAMS = rddbmgr dist_rddbmgr_SOURCES = check.cpp\ create.cpp\ modify.cpp\ + printstatus.cpp\ rddbmgr.cpp rddbmgr.h\ revertschema.cpp\ + schemamap.cpp\ updateschema.cpp rddbmgr_LDADD = @LIB_RDLIBS@ @LIBVORBIS@ diff --git a/utils/rddbmgr/modify.cpp b/utils/rddbmgr/modify.cpp index eeeea54b..ddb3af1f 100644 --- a/utils/rddbmgr/modify.cpp +++ b/utils/rddbmgr/modify.cpp @@ -18,37 +18,17 @@ // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // +#include + #include #include #include "rddbmgr.h" -bool MainObject::Modify(QString *err_msg,int set_schema, - const QString &set_version) const +bool MainObject::Modify(QString *err_msg,int set_schema) const { *err_msg="ok"; - // - // Determine Target Schema - // - if(set_schema>0) { - if((set_schema<242)||(set_schema>RD_VERSION_DATABASE)) { - *err_msg="unsupported schema"; - return false; - } - } - else { - if(set_version.isEmpty()) { - set_schema=RD_VERSION_DATABASE; - } - else { - if((set_schema=GetVersionSchema(set_version))==0) { - *err_msg="invalid/unsupported Rivendell version"; - return false; - } - } - } - // // Update/Revert // @@ -76,7 +56,7 @@ int MainObject::GetCurrentSchema() const int ret=0; QString sql=QString("select DB from VERSION"); - RDSqlQuery *q=new RDSqlQuery(sql); + QSqlQuery *q=new QSqlQuery(sql); if(q->first()) { ret=q->value(0).toInt(); } @@ -86,50 +66,3 @@ int MainObject::GetCurrentSchema() const } -int MainObject::GetVersionSchema(const QString &ver) const -{ - QString version=ver; - bool ok=false; - - // - // Version -> Schema Map - // - std::map version_map; - version_map["2.10"]=242; - version_map["2.11"]=245; - version_map["2.12"]=254; - version_map["2.13"]=255; - version_map["2.14"]=258; - version_map["2.15"]=259; - version_map["2.16"]=263; - version_map["2.17"]=268; - version_map["2.18"]=272; - version_map["2.19"]=275; - version_map["2.20"]=286; - - // - // Normalize String - // - if(version.left(1).lower()=="v") { - version=version.right(version.length()-1); - } - QStringList f0=f0.split(".",version); - if(f0.size()!=3) { - return 0; - } - for(int i=0;i<3;i++) { - f0[i].toInt(&ok); - if(!ok) { - return 0; - } - } - - // - // Lookup Schema - // - if(version_map.count(f0[0]+"."+f0[1])==0) { - return 0; - } - - return version_map[f0[0]+"."+f0[1]]; -} diff --git a/utils/rddbmgr/printstatus.cpp b/utils/rddbmgr/printstatus.cpp new file mode 100644 index 00000000..7d3521fd --- /dev/null +++ b/utils/rddbmgr/printstatus.cpp @@ -0,0 +1,52 @@ +// printstatus.cpp +// +// Print the status of a database. +// +// (C) Copyright 2018 Fred Gleason +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public +// License along with this program; if not, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#include + +#include + +#include "rddbmgr.h" + +bool MainObject::PrintStatus(QString *err_msg) const +{ + QString sql; + RDSqlQuery *q; + int schema; + + sql=QString("show tables"); + q=new RDSqlQuery(sql,false); + if(!q->first()) { + delete q; + printf("empty database\n"); + return true; + } + delete q; + + if((schema=GetCurrentSchema())<=0) { + printf("not a Rivendell database!\n"); + return true; + } + + printf("Rivendell database, schema %d [%s]\n",schema, + (const char *)GetSchemaVersion(schema)); + + *err_msg="ok"; + return true; +} diff --git a/utils/rddbmgr/rddbmgr.cpp b/utils/rddbmgr/rddbmgr.cpp index 2617ef0c..20e7d38b 100644 --- a/utils/rddbmgr/rddbmgr.cpp +++ b/utils/rddbmgr/rddbmgr.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include "rddbmgr.h" @@ -66,6 +67,8 @@ MainObject::MainObject(QObject *parent) db_mysql_collation=db_config->mysqlCollation(); station_name=db_config->stationName(); + InitializeSchemaMap(); + // // Process Command Switches // @@ -177,15 +180,32 @@ MainObject::MainObject(QObject *parent) exit(1); } } - delete cmd; // // Sanity Checks // - if(db_command==MainObject::NoCommand) { + if((cmd->keys()>0)&&(db_command==MainObject::NoCommand)) { fprintf(stderr,"rddbmgr: exactly one command must be specified\n"); exit(1); } + if(set_schema>0) { + if((set_schema<242)||(set_schema>RD_VERSION_DATABASE)) { + fprintf(stderr,"rddbmgr: unsupported schema\n"); + exit(1); + } + } + else { + if(set_version.isEmpty()) { + set_schema=RD_VERSION_DATABASE; + } + else { + if((set_schema=GetVersionSchema(set_version))==0) { + fprintf(stderr,"invalid/unsupported Rivendell version\n"); + exit(1); + } + } + } + delete cmd; if(db_verbose) { fprintf(stderr,"Using DB Credentials:\n"); @@ -231,14 +251,17 @@ MainObject::MainObject(QObject *parent) break; case MainObject::CreateCommand: - ok=Create(station_name,generate_audio,&err_msg); + if((ok=Create(station_name,generate_audio,&err_msg))) { + ok=Modify(&err_msg,set_schema); + } break; case MainObject::ModifyCommand: - ok=Modify(&err_msg,set_schema,set_version); + ok=Modify(&err_msg,set_schema); break; case MainObject::NoCommand: + ok=PrintStatus(&err_msg); break; } diff --git a/utils/rddbmgr/rddbmgr.h b/utils/rddbmgr/rddbmgr.h index 44ff547c..82fe6fd4 100644 --- a/utils/rddbmgr/rddbmgr.h +++ b/utils/rddbmgr/rddbmgr.h @@ -56,9 +56,8 @@ class MainObject : public QObject // // modify.cpp // - bool Modify(QString *err_msg,int set_schema,const QString &set_version) const; + bool Modify(QString *err_msg,int set_schema) const; int GetCurrentSchema() const; - int GetVersionSchema(const QString &ver) const; // // updateschema.cpp @@ -77,6 +76,17 @@ class MainObject : public QObject // bool RevertSchema(int cur_schema,int set_schema,QString *err_msg) const; + // + // schemamap.cpp + // + void InitializeSchemaMap(); + int GetVersionSchema(const QString &ver) const; + QString GetSchemaVersion(int schema) const; + + // + // printstatus.cpp + // + bool PrintStatus(QString *err_msg) const; Command db_command; QString db_mysql_hostname; diff --git a/utils/rddbmgr/schemamap.cpp b/utils/rddbmgr/schemamap.cpp new file mode 100644 index 00000000..0a96685c --- /dev/null +++ b/utils/rddbmgr/schemamap.cpp @@ -0,0 +1,112 @@ +// schemamap.cpp +// +// DB schema version <==> Rivendell version map +// +// (C) Copyright 2018 Fred Gleason +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public +// License along with this program; if not, write to the Free Software +// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// + +#include + +#include "rddbmgr.h" + +// +// Version -> Schema Map +// +std::map global_version_map; + +void MainObject::InitializeSchemaMap() { + global_version_map["1.0"]=159; + global_version_map["1.1"]=162; + global_version_map["1.2"]=169; + global_version_map["1.3"]=173; + global_version_map["1.4"]=177; + global_version_map["1.5"]=179; + global_version_map["1.6"]=182; + global_version_map["1.7"]=186; + global_version_map["2.0"]=202; + global_version_map["2.1"]=205; + global_version_map["2.2"]=207; + global_version_map["2.3"]=213; + global_version_map["2.4"]=216; + global_version_map["2.5"]=220; + global_version_map["2.6"]=224; + global_version_map["2.7"]=231; + global_version_map["2.8"]=234; + global_version_map["2.9"]=239; + global_version_map["2.10"]=242; + global_version_map["2.11"]=245; + global_version_map["2.12"]=254; + global_version_map["2.13"]=255; + global_version_map["2.14"]=258; + global_version_map["2.15"]=259; + global_version_map["2.16"]=263; + global_version_map["2.17"]=268; + global_version_map["2.18"]=272; + global_version_map["2.19"]=275; + global_version_map["2.20"]=286; +} + + +int MainObject::GetVersionSchema(const QString &ver) const +{ + QString version=ver; + bool ok=false; + + // + // Normalize String + // + if(version.left(1).lower()=="v") { + version=version.right(version.length()-1); + } + QStringList f0=f0.split(".",version); + if(f0.size()!=3) { + return 0; + } + for(int i=0;i<3;i++) { + f0[i].toInt(&ok); + if(!ok) { + return 0; + } + } + + // + // Lookup Schema + // + if(global_version_map.count(f0[0]+"."+f0[1])==0) { + return 0; + } + + return global_version_map[f0[0]+"."+f0[1]]; +} + + +QString MainObject::GetSchemaVersion(int schema) const +{ + QString prev_version="_preproduction"; + + for(std::map::const_iterator it=global_version_map.begin(); + it!=global_version_map.end();it++) { + if(it->second==schema) { + return "v"+it->first+".x"; + } + if(it->second>schema) { + return tr("between")+" v"+prev_version+".x "+tr("and")+ + " v"+it->first+".x"; + } + prev_version=it->first; + } + return QString("after")+" v"+prev_version+".x"; +}