From 739f1e63fd7882646d8b5994164ec728e9a279b3 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Wed, 17 Jun 2015 15:57:30 -0500 Subject: [PATCH] Allow table creation if database user only has access to the existing KBarcode table (cherry picked from commit 9e61fbb21fc9346113072c20ff3c440d30cc1aec) --- kbarcode/sqltables.cpp | 271 ++++++++++++++++++++--------------------- 1 file changed, 135 insertions(+), 136 deletions(-) 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() 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( !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("Can't create database ")+ database + i18n("
You can continue if the database exists already.
") - + firstquery.lastError().databaseText() ) == KMessageBox::Cancel ) { - dbase->close(); - TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database )); - return false; - } - } - } - dbase->close(); - TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database )); - - // 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; - } - - - 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," - " 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!") ); - } else { - TQSqlError dbError = dbase->lastError(); - dbase->close(); - TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database )); - KMessageBox::sorry(0, i18n("Can't connect to database.") + "\n" + dbError.driverText() + "\n" + dbError.databaseText()); - return false; - } - - return true; + 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("Can't create database ") + database + i18n("
You can continue if the database exists already.
") + + 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("Can't connect to database to create table.") + "

" + dbError.driverText() + "
" + dbError.databaseText() + i18n("
You can continue if the table exists already.")) == KMessageBox::Cancel) { + 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; + } + + 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," + " 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!") ); + + return true; } void SqlTables::importLabelDef()