Branch: master

9e61fbb2 2015-06-17 15:57:30 Timothy Pearson
Allow table creation if database user only has access to the existing KBarcode table
M kbarcode/sqltables.cpp
diff --git a/kbarcode/sqltables.cpp b/kbarcode/sqltables.cpp
index 5efae3c..6a10b4a 100644
--- a/kbarcode/sqltables.cpp
+++ b/kbarcode/sqltables.cpp
@@ -141,142 +141,141 @@
 
 bool SqlTables::newTables( const TQString & username, const TQString & password, const TQString & hostname, const TQString & database, const TQString & driver )
 {
-    if( KMessageBox::warningContinueCancel( 0,
-                     i18n("We are going to re-create the tables '") +
-                          TQString( TABLE_BASIC "', '" TABLE_CUSTOMER "', '"
-                          TABLE_CUSTOMER_TEXT) +i18n("' and '") + TQString(TABLE_LABEL_DEF "'")) ==  KMessageBox::Cancel )
-        return false;
+	if( KMessageBox::warningContinueCancel( 0,
+			i18n("We are going to re-create the tables '") +
+				TQString( TABLE_BASIC "', '" TABLE_CUSTOMER "', '"
+				TABLE_CUSTOMER_TEXT) +i18n("' and '") + TQString(TABLE_LABEL_DEF "'")) ==  KMessageBox::Cancel )
+		return false;
+	
+	if( !drivers[driver] )
+		return false;
+	
+	TQSqlDatabase*dbase = TQSqlDatabase::addDatabase(driver, drivers[driver]->initdb( database ) );
+	dbase->setDatabaseName( drivers[driver]->initdb( database ) );
+	dbase->setUserName( username );
+	dbase->setPassword( password );
+	dbase->setHostName( hostname );
+	
+	if (dbase->open()) {
+		if ((driver != "QSQLITE") && (driver != "TQSQLITE"))
+		{
+			bool found = false;
+			TQSqlQuery existing("SHOW DATABASES LIKE '" + database + "';");
+			while( existing.next() )
+				found = true;
+		
+			TQSqlQuery firstquery( NULL, dbase );
+			if( !found && !firstquery.exec("CREATE DATABASE " + database + ";")) {
+				if( KMessageBox::warningContinueCancel( 0, i18n("<qt>Can't create database ") + database + i18n("<br>You can continue if the database exists already.</qt>")
+					+ firstquery.lastError().databaseText() ) == KMessageBox::Cancel ) {
+					dbase->close();
+					TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database ));
+					return false;
+				}
+			}
+		}
+		dbase->close();
+		TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database ));
+	} else {
+		TQSqlError dbError = dbase->lastError();
+		dbase->close();
+		TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database ));
+		if (KMessageBox::warningContinueCancel(0, i18n("<qt>Can't connect to database to create table.") + "<p>" + dbError.driverText() + "<br>" + dbError.databaseText() + i18n("<br>You can continue if the table exists already.</qt>")) == KMessageBox::Cancel) {
+			return false;
+		}
+	}
 
-    if( !drivers[driver] )
-        return false;
+	// The database is created, now connect to the one specified by the user
+	dbase = TQSqlDatabase::addDatabase(driver, database );
+	dbase->setDatabaseName( database );
+	dbase->setUserName( username );
+	dbase->setPassword( password );
+	dbase->setHostName( hostname );
+		if(!dbase->open() || !dbase->isOpen()) {
+		KMessageBox::error( 0, i18n("KBarcode could not create the required database. Please create it manually.") + dbase->lastError().databaseText() );
+		TQSqlDatabase::removeDatabase( database );
+		return false;
+	}
 
-    TQSqlDatabase*dbase = TQSqlDatabase::addDatabase(driver, drivers[driver]->initdb( database ) );
-    dbase->setDatabaseName( drivers[driver]->initdb( database ) );
-    dbase->setUserName( username );
-    dbase->setPassword( password );
-    dbase->setHostName( hostname );
+	TQSqlQuery query( NULL, dbase );
 
-    if(dbase->open()) {
+	// barcode_basic
+	query.exec("DROP TABLE " TABLE_BASIC );
+	exec( &query, "CREATE TABLE " TABLE_BASIC " ("
+		"    uid " + drivers[driver]->autoIncrement() + ","
+		"    article_no varchar(50) DEFAULT NULL,"
+		"    article_desc varchar(50) DEFAULT NULL,"
+		"    barcode_no TEXT DEFAULT NULL,"
+		"    encoding_type varchar(50) DEFAULT NULL,"
+		"    field0 varchar(50) DEFAULT NULL,"
+		"    field1 varchar(50) DEFAULT NULL,"
+		"    field2 varchar(50) DEFAULT NULL,"
+		"    field3 varchar(50) DEFAULT NULL,"
+		"    field4 varchar(50) DEFAULT NULL,"
+		"    field5 varchar(50) DEFAULT NULL,"
+		"    field6 varchar(50) DEFAULT NULL,"
+		"    field7 varchar(50) DEFAULT NULL,"
+		"    field8 varchar(50) DEFAULT NULL,"
+		"    field9 varchar(50) DEFAULT NULL,"
+		"    PRIMARY KEY  (uid)"
+		");" );
 
-        if ((driver != "QSQLITE") && (driver != "TQSQLITE"))
-        {
-            bool found = false;
-            TQSqlQuery existing("SHOW DATABASES LIKE '" + database + "';");
-            while( existing.next() )
-                found = true;
+	// customer
+	query.exec("DROP TABLE " TABLE_CUSTOMER );
+	exec( &query, "CREATE TABLE " TABLE_CUSTOMER " ("
+		"    uid " + drivers[driver]->autoIncrement() + " ,"
+		"    customer_no varchar(20) DEFAULT NULL,"
+		"    customer_name varchar(20) DEFAULT NULL,"
+		"    PRIMARY KEY  (uid)"
+		");" );
 
-            TQSqlQuery firstquery( NULL, dbase );
-            if( !found && !firstquery.exec("CREATE DATABASE " + database + ";")) {
-                if( KMessageBox::warningContinueCancel( 0, i18n("<qt>Can't create database ")+ database + i18n("<br>You can continue if the database exists already.</qt>")
-                    + firstquery.lastError().databaseText() ) == KMessageBox::Cancel ) {
-                    dbase->close();
-                    TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database ));
-                    return false;
-                }
-            }
-        }
-        dbase->close();
-        TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database ));
+	// customer_text
+	query.exec("DROP TABLE " TABLE_CUSTOMER_TEXT );
+	exec( &query, "CREATE TABLE " TABLE_CUSTOMER_TEXT " ("
+		"    uid " + drivers[driver]->autoIncrement() + ","
+		"    customer_no varchar(20) DEFAULT NULL,"
+		"    encoding_type varchar(50) DEFAULT NULL,"
+		"    article_no varchar(50) DEFAULT NULL,"
+		"    article_no_customer varchar(50) NULL,"
+		"    barcode_no TEXT DEFAULT NULL,"
+		"    line0 varchar(50) DEFAULT NULL,"
+		"    line1 varchar(50) DEFAULT NULL,"
+		"    line2 varchar(50) DEFAULT NULL,"
+		"    line3 varchar(50) DEFAULT NULL,"
+		"    line4 varchar(50) DEFAULT NULL,"
+		"    line5 varchar(50) DEFAULT NULL,"
+		"    line6 varchar(50) DEFAULT NULL,"
+		"    line7 varchar(50) DEFAULT NULL,"
+		"    line8 varchar(50) DEFAULT NULL,"
+		"    line9 varchar(50) DEFAULT NULL,"
+		"    PRIMARY KEY  (uid)"
+		");" );
 
-        // The database is created, now connect to the one specified by the user
-        dbase = TQSqlDatabase::addDatabase(driver, database );
-        dbase->setDatabaseName( database );
-        dbase->setUserName( username );
-        dbase->setPassword( password );
-        dbase->setHostName( hostname );
-        if(!dbase->open() || !dbase->isOpen()) {
-            KMessageBox::error( 0, i18n("KBarcode could not create the required database. Please create it manually.") + dbase->lastError().databaseText() );
-            TQSqlDatabase::removeDatabase( database );
-            return false;
-        }
+	// label_def
+	query.exec("DROP TABLE " TABLE_LABEL_DEF );
+	exec( &query, "CREATE TABLE " TABLE_LABEL_DEF " ("
+		"    label_no " + drivers[driver]->autoIncrement() + ","
+		"    manufacture varchar(255) DEFAULT NULL,"
+		"    type varchar(255) DEFAULT NULL,"
+		"    paper char(1) DEFAULT NULL,"
+		"    gap_top NUMERIC(10,4) NULL,"
+		"    gap_left NUMERIC(10,4) NULL,"
+		"    height NUMERIC(10,4) NULL,"
+		"    width NUMERIC(10,4) NULL,"
+		"    gap_v NUMERIC(10,4) NULL,"
+		"    gap_h NUMERIC(10,4) NULL,"
+		"    number_h int DEFAULT NULL," //smalint(6)
+		"    number_v int DEFAULT NULL," //smalint(6)
+		"    paper_type varchar(30) DEFAULT NULL,"
+		"    compatibility varchar(10) DEFAULT NULL," // keep compatibility with older versions, was "remark text"
+		"    PRIMARY KEY  (label_no)"
+		");" );
 
+	dbase->close();
+	TQSqlDatabase::removeDatabase( database );
+	KMessageBox::information( 0, i18n("Created table ")+database+i18n(" successfully!") );
 
-        TQSqlQuery query( NULL, dbase );
-
-        // barcode_basic
-        query.exec("DROP TABLE " TABLE_BASIC );
-        exec( &query, "CREATE TABLE " TABLE_BASIC " ("
-                   "    uid " + drivers[driver]->autoIncrement() + ","
-                   "    article_no varchar(50) DEFAULT NULL,"
-                   "    article_desc varchar(50) DEFAULT NULL,"
-                   "    barcode_no TEXT DEFAULT NULL,"
-                   "    encoding_type varchar(50) DEFAULT NULL,"
-                   "    field0 varchar(50) DEFAULT NULL,"
-                   "    field1 varchar(50) DEFAULT NULL,"
-                   "    field2 varchar(50) DEFAULT NULL,"
-                   "    field3 varchar(50) DEFAULT NULL,"
-                   "    field4 varchar(50) DEFAULT NULL,"
-                   "    field5 varchar(50) DEFAULT NULL,"
-                   "    field6 varchar(50) DEFAULT NULL,"
-                   "    field7 varchar(50) DEFAULT NULL,"
-                   "    field8 varchar(50) DEFAULT NULL,"
-                   "    field9 varchar(50) DEFAULT NULL,"
-                   "    PRIMARY KEY  (uid)"
-                   ");" );
-
-        // customer
-        query.exec("DROP TABLE " TABLE_CUSTOMER );
-        exec( &query, "CREATE TABLE " TABLE_CUSTOMER " ("
-                   "    uid " + drivers[driver]->autoIncrement() + " ,"
-                   "    customer_no varchar(20) DEFAULT NULL,"
-                   "    customer_name varchar(20) DEFAULT NULL,"
-                   "    PRIMARY KEY  (uid)"
-                   ");" );
-
-        // customer_text
-        query.exec("DROP TABLE " TABLE_CUSTOMER_TEXT );
-        exec( &query, "CREATE TABLE " TABLE_CUSTOMER_TEXT " ("
-                   "    uid " + drivers[driver]->autoIncrement() + ","
-                   "    customer_no varchar(20) DEFAULT NULL,"
-                   "    encoding_type varchar(50) DEFAULT NULL,"
-                   "    article_no varchar(50) DEFAULT NULL,"
-                   "    article_no_customer varchar(50) NULL,"
-                   "    barcode_no TEXT DEFAULT NULL,"
-                   "    line0 varchar(50) DEFAULT NULL,"
-                   "    line1 varchar(50) DEFAULT NULL,"
-                   "    line2 varchar(50) DEFAULT NULL,"
-                   "    line3 varchar(50) DEFAULT NULL,"
-                   "    line4 varchar(50) DEFAULT NULL,"
-                   "    line5 varchar(50) DEFAULT NULL,"
-                   "    line6 varchar(50) DEFAULT NULL,"
-                   "    line7 varchar(50) DEFAULT NULL,"
-                   "    line8 varchar(50) DEFAULT NULL,"
-                   "    line9 varchar(50) DEFAULT NULL,"
-                   "    PRIMARY KEY  (uid)"
-                   ");" );
-
-        // label_def
-        query.exec("DROP TABLE " TABLE_LABEL_DEF );
-        exec( &query, "CREATE TABLE " TABLE_LABEL_DEF " ("
-                   "    label_no " + drivers[driver]->autoIncrement() + ","
-                   "    manufacture varchar(255) DEFAULT NULL,"
-                   "    type varchar(255) DEFAULT NULL,"
-                   "    paper char(1) DEFAULT NULL,"
-                   "    gap_top NUMERIC(10,4) NULL,"
-                   "    gap_left NUMERIC(10,4) NULL,"
-                   "    height NUMERIC(10,4) NULL,"
-                   "    width NUMERIC(10,4) NULL,"
-                   "    gap_v NUMERIC(10,4) NULL,"
 ** Diff limit reached (max: 250 lines) **