Allow table creation if database user only has access to the existing KBarcode table

(cherry picked from commit 9e61fbb21f)
r14.0.x r14.0.3
Timothy Pearson 9 years ago committed by Slávek Banko
parent a38797c8c9
commit 739f1e63fd

@ -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 ) bool SqlTables::newTables( const TQString & username, const TQString & password, const TQString & hostname, const TQString & database, const TQString & driver )
{ {
if( KMessageBox::warningContinueCancel( 0, if( KMessageBox::warningContinueCancel( 0,
i18n("We are going to re-create the tables '") + i18n("We are going to re-create the tables '") +
TQString( TABLE_BASIC "', '" TABLE_CUSTOMER "', '" TQString( TABLE_BASIC "', '" TABLE_CUSTOMER "', '"
TABLE_CUSTOMER_TEXT) +i18n("' and '") + TQString(TABLE_LABEL_DEF "'")) == KMessageBox::Cancel ) TABLE_CUSTOMER_TEXT) +i18n("' and '") + TQString(TABLE_LABEL_DEF "'")) == KMessageBox::Cancel )
return false; return false;
if( !drivers[driver] ) if( !drivers[driver] )
return false; return false;
TQSqlDatabase*dbase = TQSqlDatabase::addDatabase(driver, drivers[driver]->initdb( database ) ); TQSqlDatabase*dbase = TQSqlDatabase::addDatabase(driver, drivers[driver]->initdb( database ) );
dbase->setDatabaseName( drivers[driver]->initdb( database ) ); dbase->setDatabaseName( drivers[driver]->initdb( database ) );
dbase->setUserName( username ); dbase->setUserName( username );
dbase->setPassword( password ); dbase->setPassword( password );
dbase->setHostName( hostname ); dbase->setHostName( hostname );
if(dbase->open()) { if (dbase->open()) {
if ((driver != "QSQLITE") && (driver != "TQSQLITE"))
if ((driver != "QSQLITE") && (driver != "TQSQLITE")) {
{ bool found = false;
bool found = false; TQSqlQuery existing("SHOW DATABASES LIKE '" + database + "';");
TQSqlQuery existing("SHOW DATABASES LIKE '" + database + "';"); while( existing.next() )
while( existing.next() ) found = true;
found = true;
TQSqlQuery firstquery( NULL, dbase );
TQSqlQuery firstquery( NULL, dbase ); if( !found && !firstquery.exec("CREATE DATABASE " + database + ";")) {
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>")
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 ) {
+ firstquery.lastError().databaseText() ) == KMessageBox::Cancel ) { dbase->close();
dbase->close(); TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database ));
TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database )); return false;
return false; }
} }
} }
} dbase->close();
dbase->close(); TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database ));
TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database )); } else {
TQSqlError dbError = dbase->lastError();
// The database is created, now connect to the one specified by the user dbase->close();
dbase = TQSqlDatabase::addDatabase(driver, database ); TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database ));
dbase->setDatabaseName( 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) {
dbase->setUserName( username ); return false;
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() ); // The database is created, now connect to the one specified by the user
TQSqlDatabase::removeDatabase( database ); dbase = TQSqlDatabase::addDatabase(driver, database );
return false; dbase->setDatabaseName( database );
} dbase->setUserName( username );
dbase->setPassword( password );
dbase->setHostName( hostname );
TQSqlQuery query( NULL, dbase ); if(!dbase->open() || !dbase->isOpen()) {
KMessageBox::error( 0, i18n("KBarcode could not create the required database. Please create it manually.") + dbase->lastError().databaseText() );
// barcode_basic TQSqlDatabase::removeDatabase( database );
query.exec("DROP TABLE " TABLE_BASIC ); return false;
exec( &query, "CREATE TABLE " TABLE_BASIC " (" }
" uid " + drivers[driver]->autoIncrement() + ","
" article_no varchar(50) DEFAULT NULL," TQSqlQuery query( NULL, dbase );
" article_desc varchar(50) DEFAULT NULL,"
" barcode_no TEXT DEFAULT NULL," // barcode_basic
" encoding_type varchar(50) DEFAULT NULL," query.exec("DROP TABLE " TABLE_BASIC );
" field0 varchar(50) DEFAULT NULL," exec( &query, "CREATE TABLE " TABLE_BASIC " ("
" field1 varchar(50) DEFAULT NULL," " uid " + drivers[driver]->autoIncrement() + ","
" field2 varchar(50) DEFAULT NULL," " article_no varchar(50) DEFAULT NULL,"
" field3 varchar(50) DEFAULT NULL," " article_desc varchar(50) DEFAULT NULL,"
" field4 varchar(50) DEFAULT NULL," " barcode_no TEXT DEFAULT NULL,"
" field5 varchar(50) DEFAULT NULL," " encoding_type varchar(50) DEFAULT NULL,"
" field6 varchar(50) DEFAULT NULL," " field0 varchar(50) DEFAULT NULL,"
" field7 varchar(50) DEFAULT NULL," " field1 varchar(50) DEFAULT NULL,"
" field8 varchar(50) DEFAULT NULL," " field2 varchar(50) DEFAULT NULL,"
" field9 varchar(50) DEFAULT NULL," " field3 varchar(50) DEFAULT NULL,"
" PRIMARY KEY (uid)" " field4 varchar(50) DEFAULT NULL,"
");" ); " field5 varchar(50) DEFAULT NULL,"
" field6 varchar(50) DEFAULT NULL,"
// customer " field7 varchar(50) DEFAULT NULL,"
query.exec("DROP TABLE " TABLE_CUSTOMER ); " field8 varchar(50) DEFAULT NULL,"
exec( &query, "CREATE TABLE " TABLE_CUSTOMER " (" " field9 varchar(50) DEFAULT NULL,"
" uid " + drivers[driver]->autoIncrement() + " ," " PRIMARY KEY (uid)"
" customer_no varchar(20) DEFAULT NULL," ");" );
" customer_name varchar(20) DEFAULT NULL,"
" PRIMARY KEY (uid)" // customer
");" ); query.exec("DROP TABLE " TABLE_CUSTOMER );
exec( &query, "CREATE TABLE " TABLE_CUSTOMER " ("
// customer_text " uid " + drivers[driver]->autoIncrement() + " ,"
query.exec("DROP TABLE " TABLE_CUSTOMER_TEXT ); " customer_no varchar(20) DEFAULT NULL,"
exec( &query, "CREATE TABLE " TABLE_CUSTOMER_TEXT " (" " customer_name varchar(20) DEFAULT NULL,"
" uid " + drivers[driver]->autoIncrement() + "," " PRIMARY KEY (uid)"
" customer_no varchar(20) DEFAULT NULL," ");" );
" encoding_type varchar(50) DEFAULT NULL,"
" article_no varchar(50) DEFAULT NULL," // customer_text
" article_no_customer varchar(50) NULL," query.exec("DROP TABLE " TABLE_CUSTOMER_TEXT );
" barcode_no TEXT DEFAULT NULL," exec( &query, "CREATE TABLE " TABLE_CUSTOMER_TEXT " ("
" line0 varchar(50) DEFAULT NULL," " uid " + drivers[driver]->autoIncrement() + ","
" line1 varchar(50) DEFAULT NULL," " customer_no varchar(20) DEFAULT NULL,"
" line2 varchar(50) DEFAULT NULL," " encoding_type varchar(50) DEFAULT NULL,"
" line3 varchar(50) DEFAULT NULL," " article_no varchar(50) DEFAULT NULL,"
" line4 varchar(50) DEFAULT NULL," " article_no_customer varchar(50) NULL,"
" line5 varchar(50) DEFAULT NULL," " barcode_no TEXT DEFAULT NULL,"
" line6 varchar(50) DEFAULT NULL," " line0 varchar(50) DEFAULT NULL,"
" line7 varchar(50) DEFAULT NULL," " line1 varchar(50) DEFAULT NULL,"
" line8 varchar(50) DEFAULT NULL," " line2 varchar(50) DEFAULT NULL,"
" line9 varchar(50) DEFAULT NULL," " line3 varchar(50) DEFAULT NULL,"
" PRIMARY KEY (uid)" " line4 varchar(50) DEFAULT NULL,"
");" ); " line5 varchar(50) DEFAULT NULL,"
" line6 varchar(50) DEFAULT NULL,"
// label_def " line7 varchar(50) DEFAULT NULL,"
query.exec("DROP TABLE " TABLE_LABEL_DEF ); " line8 varchar(50) DEFAULT NULL,"
exec( &query, "CREATE TABLE " TABLE_LABEL_DEF " (" " line9 varchar(50) DEFAULT NULL,"
" label_no " + drivers[driver]->autoIncrement() + "," " PRIMARY KEY (uid)"
" manufacture varchar(255) DEFAULT NULL," ");" );
" type varchar(255) DEFAULT NULL,"
" paper char(1) DEFAULT NULL," // label_def
" gap_top NUMERIC(10,4) NULL," query.exec("DROP TABLE " TABLE_LABEL_DEF );
" gap_left NUMERIC(10,4) NULL," exec( &query, "CREATE TABLE " TABLE_LABEL_DEF " ("
" height NUMERIC(10,4) NULL," " label_no " + drivers[driver]->autoIncrement() + ","
" width NUMERIC(10,4) NULL," " manufacture varchar(255) DEFAULT NULL,"
" gap_v NUMERIC(10,4) NULL," " type varchar(255) DEFAULT NULL,"
" gap_h NUMERIC(10,4) NULL," " paper char(1) DEFAULT NULL,"
" number_h int DEFAULT NULL," //smalint(6) " gap_top NUMERIC(10,4) NULL,"
" number_v int DEFAULT NULL," //smalint(6) " gap_left NUMERIC(10,4) NULL,"
" paper_type varchar(30) DEFAULT NULL," " height NUMERIC(10,4) NULL,"
" compatibility varchar(10) DEFAULT NULL," // keep compatibility with older versions, was "remark text" " width NUMERIC(10,4) NULL,"
" PRIMARY KEY (label_no)" " gap_v NUMERIC(10,4) NULL,"
");" ); " gap_h NUMERIC(10,4) NULL,"
" number_h int DEFAULT NULL," //smalint(6)
dbase->close(); " number_v int DEFAULT NULL," //smalint(6)
TQSqlDatabase::removeDatabase( database ); " paper_type varchar(30) DEFAULT NULL,"
KMessageBox::information( 0, i18n("Created table ")+database+i18n(" successfully!") ); " compatibility varchar(10) DEFAULT NULL," // keep compatibility with older versions, was "remark text"
} else { " PRIMARY KEY (label_no)"
TQSqlError dbError = dbase->lastError(); ");" );
dbase->close();
TQSqlDatabase::removeDatabase(drivers[driver]->initdb( database )); dbase->close();
KMessageBox::sorry(0, i18n("Can't connect to database.") + "\n" + dbError.driverText() + "\n" + dbError.databaseText()); TQSqlDatabase::removeDatabase( database );
return false; KMessageBox::information( 0, i18n("Created table ")+database+i18n(" successfully!") );
}
return true;
return true;
} }
void SqlTables::importLabelDef() void SqlTables::importLabelDef()

Loading…
Cancel
Save