DBBrowserDB: Remove getTableSQL() function

Delete the getTableSQL() method in DBBrowserDB. Because we reload and
store the SQL statements for tables after every change to the DB layout
we can just use these strings instead of having an extra function doing
an extra DB query. This simplifies the code a bit and also reduces the
number of SQL statements executed by the application.
This commit is contained in:
Martin Kleusberg
2013-06-13 18:11:18 +02:00
parent 7bf3b87411
commit bd169e034f
3 changed files with 4 additions and 37 deletions

View File

@@ -30,7 +30,7 @@ EditTableDialog::EditTableDialog(DBBrowserDB* db, const QString& tableName, QWid
m_bNewTable = false; // we are editing an existing table
// Existing table, so load and set the current layout
QString sTablesql = pdb->getTableSQL(curTable);
QString sTablesql = pdb->getObjectByName(curTable).getsql();
m_table = sqlb::Table::parseSQL(sTablesql);
populateFields();
}
@@ -366,7 +366,7 @@ void EditTableDialog::removeField()
QMessageBox::warning(0, QApplication::applicationName(), pdb->lastErrorMessage);
} else {
//relayout
QString sTablesql = pdb->getTableSQL(curTable);
QString sTablesql = pdb->getObjectByName(curTable).getsql();
m_table = sqlb::Table::parseSQL(sTablesql);
populateFields();
}

View File

@@ -424,7 +424,7 @@ int DBBrowserDB::addRecord(const QString& sTableName)
// add record is seldom called, for now this is ok
// but if we ever going to add a lot of records
// we should cache the parsed tables somewhere
sqlb::Table table = sqlb::Table::parseSQL(getTableSQL(sTableName));
sqlb::Table table = sqlb::Table::parseSQL(getObjectByName(sTableName).getsql());
QString sInsertstmt = table.emptyInsertStmt();
lastErrorMessage = "";
logSQL(sInsertstmt, kLogMsg_App);
@@ -531,7 +531,7 @@ bool DBBrowserDB::renameColumn(const QString& tablename, const QString& name, sq
//return executeSQL(sql);
// Collect information on the current DB layout
QString tableSql = getTableSQL(tablename);
QString tableSql = getObjectByName(tablename).getsql();
if(tableSql.isEmpty())
{
lastErrorMessage = QObject::tr("renameColumn: cannot find table %1.").arg(tablename);
@@ -740,32 +740,6 @@ void DBBrowserDB::logSQL(QString statement, int msgtype)
}
}
QString DBBrowserDB::getTableSQL(const QString& sTable)
{
sqlite3_stmt *vm;
const char *tail;
int err;
QString sTableSQL;
QString statement = QString("SELECT sql FROM sqlite_master WHERE name='%1';").arg(sTable);
QByteArray utf8Statement = statement.toUtf8();
err=sqlite3_prepare_v2(_db, utf8Statement, utf8Statement.length(),
&vm, &tail);
if (err == SQLITE_OK){
logSQL(statement, kLogMsg_App);
if( sqlite3_step(vm) == SQLITE_ROW )
{
sTableSQL = QString::fromUtf8((const char*)sqlite3_column_text(vm, 0));
}
sqlite3_finalize(vm);
}else{
qCritical() << "Unable to get sql for table: " << sTable;
}
return sTableSQL;
}
void DBBrowserDB::updateSchema( )
{
sqlite3_stmt *vm;

View File

@@ -65,13 +65,6 @@ public:
bool executeSQL ( const QString & statement, bool dirtyDB=true, bool logsql=true);
bool executeMultiSQL(const QString& statement, bool dirty = true, bool log = false);
/**
* @brief getTableSQL Returns the create table statement for the given table.
* @param sTable Table name
* @return An empty string if the table does not exist
* or the create table statement.
*/
QString getTableSQL(const QString& sTable);
void updateSchema();
int addRecord(const QString& sTableName);
bool deleteRecord(const QString& table, int rowid);