Skip to content

Commit

Permalink
db createColumn()
Browse files Browse the repository at this point in the history
  • Loading branch information
ac001 committed Oct 5, 2010
1 parent 17ef4e1 commit 99bd96c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 40 deletions.
23 changes: 8 additions & 15 deletions sqlitebrowser/sqlitebrowser/editfieldform.cpp
Expand Up @@ -57,18 +57,7 @@ void editFieldForm::setInitialValues(bool is_new, QString table, QString fld_nam
return;
}
}
return;
typeBox->clear();
typeBox->insertItem(fld_type);
// QString tString = "";
// tString = "TEXT";
// if (type.compare(tString)!=0) typeBox->insertItem(tString);
// tString = "NUMERIC";
// if (type.compare(tString)!=0) typeBox->insertItem(tString);
// tString = "BLOB";
// if (type.compare(tString)!=0) typeBox->insertItem(tString);
// tString = "INTEGER PRIMARY KEY";
// if (type.compare(tString)!=0) typeBox->insertItem(tString);
//TODO enable custom
}

void editFieldForm::confirmEdit()
Expand All @@ -84,12 +73,16 @@ void editFieldForm::confirmEdit()
}
field_name = fieldname;
field_type = typeBox->currentText();
QString sql = QString("ALTER TABLE `%1` ");
qDebug(sql);
if(!pdb.executeSQL(sql)){
bool ok = pdb.createColumn(table_name, field_name, field_type);
if(!ok){
qDebug(pdb.lastErrorMessage);
return;
}
//qDebug("");
// if(!pdb.executeSQL(sql)){
// qDebug(pdb.lastErrorMessage);
// return;
// }
accept();
}

Expand Down
6 changes: 3 additions & 3 deletions sqlitebrowser/sqlitebrowser/editfieldform.h
Expand Up @@ -113,19 +113,19 @@ class Ui_editFieldForm
radioBLOB->setText(QApplication::translate("addFieldForm", "BLOB", 0, QApplication::UnicodeUTF8));
radioBLOB->setProperty("field_type", QVariant("BLOB"));
radioLayout->addWidget(radioBLOB);
//groupRadioTypes->addButton(radioBLOB);
groupRadioTypes->addButton(radioBLOB);

QRadioButton *radioINTPRIMARY = new QRadioButton();
radioINTPRIMARY->setText(QApplication::translate("addFieldForm", "INTEGER PRIMARY KEY", 0, QApplication::UnicodeUTF8));
radioINTPRIMARY->setProperty("field_type", QVariant("INTEGER PRIMARY KEY"));
radioLayout->addWidget(radioINTPRIMARY);
//groupRadioTypes->addButton(radioINTPRIMARY);
groupRadioTypes->addButton(radioINTPRIMARY);

QRadioButton *radioCustom = new QRadioButton();
radioCustom->setText(QApplication::translate("addFieldForm", "Custom", 0, QApplication::UnicodeUTF8));
radioCustom->setProperty("field_type", QVariant("__custom__"));
radioLayout->addWidget(radioCustom);
//groupRadioTypes->addButton(radioCustom);
groupRadioTypes->addButton(radioCustom);

QLineEdit *txtCustomType = new QLineEdit();
radioLayout->addWidget(txtCustomType);
Expand Down
24 changes: 22 additions & 2 deletions sqlitebrowser/sqlitebrowser/form1.cpp
Expand Up @@ -1229,6 +1229,7 @@ void mainForm::on_tree_selection_changed(){
if (!dbTreeWidget->selectionModel()->hasSelection()){
editDeleteTableActionPopup->setEnabled(false);
editModifyTableActionPopup->setEnabled(false);
editAddFieldActionPopup->setEnabled(false);
editModifyFieldActionPopup->setEnabled(false);
editDeleteFieldActionPopup->setEnabled(false);
return;
Expand All @@ -1237,10 +1238,12 @@ void mainForm::on_tree_selection_changed(){
if(dbTreeWidget->currentItem()->text(1) == "table"){
editDeleteTableActionPopup->setEnabled(true);
editModifyTableActionPopup->setEnabled(true);
editAddFieldActionPopup->setEnabled(true);
editModifyFieldActionPopup->setEnabled(false);
editDeleteFieldActionPopup->setEnabled(false);

}else if(dbTreeWidget->currentItem()->text(1) == "field"){
editAddFieldActionPopup->setEnabled(false);
editDeleteTableActionPopup->setEnabled(false);
editModifyTableActionPopup->setEnabled(false);
editModifyFieldActionPopup->setEnabled(true);
Expand All @@ -1249,8 +1252,25 @@ void mainForm::on_tree_selection_changed(){
}




void mainForm::on_add_field(){
qDebug("YES");
//if( !dbTreeWidget->currentItem() ){
// return;
//}
//QTreeWidgetItem *item = dbTreeWidget->currentItem();
editFieldForm *fieldForm = new editFieldForm( this, "editfield", true );
//qDebug(item->text(2));
fieldForm->setInitialValues(true, "TABLE_NAME", "", "NUMERIC");
fieldForm->setDB(this->db);
if (fieldForm->exec())
{
//modified = true;
//do the sql rename here
qDebug(fieldForm->field_name + fieldForm->field_type);
//item->setText(0,fieldForm->field_name);
//item->setText(2,fieldForm->field_type);
}
}
void mainForm::on_edit_field(){
qDebug("YES");
if( !dbTreeWidget->currentItem() ){
Expand Down
16 changes: 11 additions & 5 deletions sqlitebrowser/sqlitebrowser/form1.h
Expand Up @@ -270,18 +270,21 @@ class Ui_mainForm
editModifyTableActionPopup->setIcon(QIcon(":/icons/table_modify"));


//** Modify Field
//** Add, Modify, Delete Field

editAddFieldActionPopup = new QAction(mainForm);
editAddFieldActionPopup->setText("Add Field");
editAddFieldActionPopup->setDisabled(true);
editAddFieldActionPopup->setIcon(QIcon(":/icons/field_add"));

editModifyFieldActionPopup = new QAction(mainForm);
editModifyFieldActionPopup->setText("Modify Field");
editModifyFieldActionPopup->setText("Modify Field");
editModifyFieldActionPopup->setDisabled(true);
editModifyFieldActionPopup->setIcon(QIcon(":/icons/field_edit"));

editDeleteFieldActionPopup = new QAction(mainForm);
editDeleteFieldActionPopup->setText("Delete Field");
editDeleteFieldActionPopup->setDisabled(true);
editDeleteFieldActionPopup->setIcon(QIcon(":/icons/field_delete"));

//** Create Index
Expand Down Expand Up @@ -317,9 +320,11 @@ class Ui_mainForm
mainTab->setObjectName(QString::fromUtf8("mainTab"));
structure = new QWidget();
structure->setObjectName(QString::fromUtf8("structure"));


vboxLayout1 = new QVBoxLayout(structure);
vboxLayout1->setSpacing(6);
vboxLayout1->setContentsMargins(11, 11, 11, 11);
vboxLayout1->setSpacing(0);
vboxLayout1->setContentsMargins(0,0,0,0);
vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1"));

//**** Structure ***********************************
Expand Down Expand Up @@ -643,9 +648,9 @@ class Ui_mainForm
QObject::connect(editModifyTableAction, SIGNAL(activated()), mainForm, SLOT(editTable()));
QObject::connect(editDeleteTableActionPopup, SIGNAL(activated()), mainForm, SLOT(deleteTablePopup()));
QObject::connect(editModifyTableActionPopup, SIGNAL(activated()), mainForm, SLOT(editTablePopup()));
QObject::connect(editAddFieldActionPopup, SIGNAL(activated()), mainForm, SLOT(on_add_field()));
QObject::connect(editModifyFieldActionPopup, SIGNAL(activated()), mainForm, SLOT(on_edit_field()));


QObject::connect(fileExportSQLAction, SIGNAL(activated()), mainForm, SLOT(exportDatabaseToSQL()));
QObject::connect(fileImportSQLAction, SIGNAL(activated()), mainForm, SLOT(importDatabaseFromSQL()));
QObject::connect(editPreferencesAction, SIGNAL(activated()), mainForm, SLOT(openPreferences()));
Expand Down Expand Up @@ -958,6 +963,7 @@ class mainForm : public QMainWindow, public Ui::mainForm
public slots:
virtual void on_tree_context_menu(const QPoint & qPoint);
virtual void on_tree_selection_changed();
virtual void on_add_field();
virtual void on_edit_field();

virtual void fileOpen( const QString & fileName );
Expand Down
41 changes: 26 additions & 15 deletions sqlitebrowser/sqlitebrowser/sqlitedb.cpp
Expand Up @@ -275,26 +275,25 @@ bool DBBrowserDB::dump( const QString & filename)

bool DBBrowserDB::executeSQL ( const QString & statement, bool dirtyDB, bool logsql)
{
char *errmsg;
bool ok=false;
char *errmsg;
bool ok = false;

if (!isOpen()) return false;
if (!isOpen()) return false;

if (_db){
if (logsql) logSQL(statement, kLogMsg_App);
if (dirtyDB) setDirty(true);
if (SQLITE_OK==sqlite3_exec(_db,GetEncodedQString(statement),
NULL,NULL,&errmsg)){
ok=true;
}
if (_db){
if (logsql) logSQL(statement, kLogMsg_App);
if (dirtyDB) setDirty(true);
if (SQLITE_OK==sqlite3_exec(_db,GetEncodedQString(statement),
NULL,NULL,&errmsg)){
ok=true;
}
}

if (!ok){
lastErrorMessage = QString(errmsg);
return false;
}else{
if (!ok){
lastErrorMessage = QString(errmsg);
return false;
}
return true;
}
}


Expand Down Expand Up @@ -426,6 +425,18 @@ bool DBBrowserDB::browseTable( const QString & tablename )
return hasValidBrowseSet;
}

bool DBBrowserDB::createColumn( QString tablename, QString fieldname, QString fieldtype ){
qDebug("create column");
QString sql = QString("ALTER TABLE `%1` ADD COLUMN `%2` %3").arg(tablename).arg(fieldname).arg(fieldtype);
qDebug(sql);
return executeSQL(sql);
}

bool DBBrowserDB::renameTable(QString from_table, QString to_table){
qDebug("renameTable column");
return true;
}

void DBBrowserDB::getTableRecords( const QString & tablename )
{
sqlite3_stmt *vm;
Expand Down
4 changes: 4 additions & 0 deletions sqlitebrowser/sqlitebrowser/sqlitedb.h
Expand Up @@ -106,6 +106,10 @@ class DBBrowserDB
bool deleteRecord(int wrow);
bool updateRecord(int wrow, int wcol, const QString & wtext);
bool browseTable( const QString & tablename );

bool renameTable(QString from_table, QString to_table);
bool createColumn(QString table, QString field, QString type);

QStringList getTableFields(const QString & tablename);
QStringList getTableTypes(const QString & tablename);
QStringList getTableNames();
Expand Down

0 comments on commit 99bd96c

Please sign in to comment.