Add new function to DBBrowserDB which creates a new table

Add a new function to the DBBrowserDB class which creates a new table.

Use this function in most places where a table needs to be created.
This commit is contained in:
Martin Kleusberg
2013-01-08 19:41:07 +01:00
parent f8c629ca4e
commit 684b825a9e
3 changed files with 32 additions and 25 deletions

View File

@@ -68,21 +68,18 @@ void editTableForm::accept()
{
// Creation of new table
// Build SQL statement from what the use entered
QString sql = QString("CREATE TABLE `%1` (").arg(ui->editTableName->text());
// Prepare creation of the table
QList<DBBrowserField> tbl_structure;
for(int i=0;i<ui->treeWidget->topLevelItemCount();i++)
sql.append(QString("`%1` %2,").arg(ui->treeWidget->topLevelItem(i)->text(0)).arg(ui->treeWidget->topLevelItem(i)->text(1)));
sql.remove(sql.count() - 1, 1); // Remove last comma
sql.append(");");
tbl_structure.push_back(DBBrowserField(ui->treeWidget->topLevelItem(i)->text(0), ui->treeWidget->topLevelItem(i)->text(1)));
// Execute it
modified = true;
if (!pdb->executeSQL(sql)){
QString error("Error creating table. Message from database engine:\n");
error.append(pdb->lastErrorMessage).append("\n\n").append(sql);
QMessageBox::warning( this, QApplication::applicationName(), error );
// Create table
if(!pdb->createTable(ui->editTableName->text(), tbl_structure))
{
QMessageBox::warning(this, QApplication::applicationName(), QString("Error creating table. Message from database engine:\n%1").arg(pdb->lastErrorMessage));
return;
}
modified = true;
} else {
// Editing of old table

View File

@@ -414,8 +414,21 @@ bool DBBrowserDB::browseTable( const QString & tablename, const QString& orderby
return hasValidBrowseSet;
}
bool DBBrowserDB::createColumn( QString tablename, QString fieldname, QString fieldtype ){
qDebug("create column");
bool DBBrowserDB::createTable(QString name, const QList<DBBrowserField>& structure)
{
// Build SQL statement
QString sql = QString("CREATE TABLE `%1` (").arg(name);
for(int i=0;i<structure.count();i++)
sql.append(QString("`%1` %2,").arg(structure.at(i).getname()).arg(structure.at(i).gettype()));
sql.remove(sql.count() - 1, 1); // Remove last comma
sql.append(");");
// Execute it
return executeSQL(sql);
}
bool DBBrowserDB::createColumn(QString tablename, QString fieldname, QString fieldtype)
{
QString sql = QString("ALTER TABLE `%1` ADD COLUMN `%2` %3").arg(tablename).arg(fieldname).arg(fieldtype);
return executeSQL(sql);
}
@@ -446,18 +459,16 @@ bool DBBrowserDB::renameColumn(QString tablename, QString from, QString to, QStr
// Create a new table with a name that hopefully doesn't exist yet. Its layout is exactly the same as the one of the table to change - except for the column to change
// of course
QString sql = QString("CREATE TABLE sqlitebrowser_rename_column_new_table (");
QList<DBBrowserField> new_table_structure;
for(int i=0;i<table.fldmap.count();i++)
{
// Is this the column to rename?
if(table.fldmap.value(i).getname() == from)
sql.append(QString("`%1` %2,").arg(to).arg(type));
new_table_structure.push_back(DBBrowserField(to, type));
else
sql.append(QString("`%1` %2,").arg(table.fldmap.value(i).getname()).arg(table.fldmap.value(i).gettype()));
new_table_structure.push_back(DBBrowserField(table.fldmap.value(i).getname(), table.fldmap.value(i).gettype()));
}
sql.remove(sql.count() - 1, 1); // Remove last comma
sql.append(");");
if(!executeSQL(sql))
if(!createTable("sqlitebrowser_rename_column_new_table", new_table_structure))
{
lastErrorMessage = QString("renameColumn: creating new table failed. DB says: %1").arg(lastErrorMessage);
qDebug(lastErrorMessage.toStdString().c_str());
@@ -528,27 +539,25 @@ bool DBBrowserDB::dropColumn(QString tablename, QString column)
// Create a new table with a name that hopefully doesn't exist yet. Its layout is exactly the same as the one of the table to change - except for the column to drop
// of course. Also prepare the columns to be copied in a later step now.
QString sql = QString("CREATE TABLE sqlitebrowser_drop_column_new_table (");
QList<DBBrowserField> new_table_structure;
QString select_cols;
for(int i=0;i<table.fldmap.count();i++)
{
// Only add this if it is not the column to drop?
if(table.fldmap.value(i).getname() != column)
{
sql.append(QString("`%1` %2,").arg(table.fldmap.value(i).getname()).arg(table.fldmap.value(i).gettype()));
new_table_structure.push_back(DBBrowserField(table.fldmap.value(i).getname(), table.fldmap.value(i).gettype()));
select_cols.append(QString("`%1`,").arg(table.fldmap.value(i).getname()));
}
}
sql.remove(sql.count() - 1, 1); // Remove last comma
select_cols.remove(select_cols.count() - 1, 1);
sql.append(");");
if(!executeSQL(sql))
if(!createTable("sqlitebrowser_drop_column_new_table", new_table_structure))
{
lastErrorMessage = QString("dropColumn: creating new table failed. DB says: %1").arg(lastErrorMessage);
qDebug(lastErrorMessage.toStdString().c_str());
executeSQL("ROLLBACK TO SAVEPOINT sqlitebrowser_drop_column;");
return false;
}
select_cols.remove(select_cols.count() - 1, 1);
// Copy the data from the old table to the new one
if(!executeSQL(QString("INSERT INTO sqlitebrowser_drop_column_new_table SELECT %1 FROM `%2`;").arg(select_cols).arg(tablename)))

View File

@@ -103,6 +103,7 @@ public:
bool updateRecord(int wrow, int wcol, const QString & wtext);
bool browseTable( const QString & tablename, const QString& orderby = "rowid" );
bool createTable(QString name, const QList<DBBrowserField>& structure);
bool renameTable(QString from_table, QString to_table);
bool createColumn(QString table, QString field, QString type);
bool renameColumn(QString tablename, QString from, QString to, QString type);