• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

E:/sqlmaster/src/tableeditor.cpp

Go to the documentation of this file.
00001 #include "tableeditor.h"
00002 #include "ui_tableeditor.h"
00003 #include "delegate.h"
00004 #include "addcolumntotable.h"
00005 #include "gpublics.h"
00006 #include "highlighter.h"
00007 tableEditor::tableEditor(QWidget *parent) :
00008         QMainWindow(parent),
00009         ui(new Ui::tableEditor)
00010 {
00011     ui->setupUi(this);
00012     isEditing = false;
00013     ui->txtTableName->setText("New Table");
00014     Highlighter *high;
00015     high = new Highlighter(ui->txtQuery->document());
00016 }
00017 //------------------------------------------------------
00018 tableEditor::~tableEditor()
00019 {
00020     delete ui;
00021 }
00022 //------------------------------------------------------
00023 void tableEditor::changeEvent(QEvent *e)
00024 {
00025     QMainWindow::changeEvent(e);
00026     switch (e->type()) {
00027     case QEvent::LanguageChange:
00028         ui->retranslateUi(this);
00029         break;
00030     default:
00031         break;
00032     }
00033 }
00034 //------------------------------------------------------
00035 void tableEditor::loadDatabaseList(QString defaultDb) {
00036     QSqlQuery *qu = new QSqlQuery("show databases", db);
00037     if (qu->lastError().isValid() == false) {
00038         while (qu->next()) {
00039             ui->comboBox->insertItem(ui->comboBox->count(), QIcon("://icons/Icon_45.ico"), qu->value(0).toString());
00040         }
00041         ui->comboBox->setEditText(defaultDb);
00042     }
00043 }
00044 //------------------------------------------------------
00045 void tableEditor::editTable() {
00046     for (int i = 0; i < ui->tblColumns->rowCount(); i++) {
00047         if (i < m_originalName.count()) {
00048             QString m_strColName = "";
00049             QString m_strDataType = "";
00050             QString m_strLength = "";
00051             QString m_strDefault = "";
00052             QString m_strNotNull = "";
00053             QString m_strAutoIncrement = "";
00054             QString m_strPrimaryKey;
00055             QTableWidgetItem *m_itemColName = ui->tblColumns->item(i, 0);
00056             QComboBox *m_cboDataTypes = dataTypes[i];
00057             QCheckBox *m_chkNull = notNulls[i];
00058             QCheckBox *m_chkAuto = autoIncrements[i];
00059             QCheckBox *m_chkPrim = primaryKeys[i]; m_strColName = m_itemColName->text();
00060             m_strDataType = m_cboDataTypes->currentText();
00061             m_strLength = ui->tblColumns->item(i, 2)->text();
00062             m_strDefault = ui->tblColumns->item(i, 3)->text();
00063             if (m_strDefault.length() < 1) {
00064                 m_strDefault = "";
00065             } else { m_strDefault = tr("DEFAULT `%1`").arg(m_strDefault); }
00066             if (m_strDataType.toUpper() == "VARCHAR") {
00067                 if (m_strLength.length() < 1) { m_strLength = "10"; }
00068                 m_strDataType = tr("VARCHAR(%1)").arg(m_strLength);
00069             } else { m_strDataType = m_strDataType; }
00070             int m_chkNotNull  = m_chkNull->checkState();
00071             if (m_chkNotNull == 2) {
00072                 m_strNotNull = "NOT NULL";
00073             } else { m_strNotNull = ""; }
00074             int m_chkAutoIncr = m_chkAuto->checkState();
00075             if (m_chkAutoIncr == 2) {
00076                 m_strAutoIncrement = "AUTO_INCREMENT";
00077             } else { m_strAutoIncrement ="" ; }
00078             int m_intPrim = m_chkPrim->checkState();
00079             if (m_intPrim == 2) {
00080                 m_strPrimaryKey = m_strColName;
00081             } else  {m_strPrimaryKey = ""; }
00082         }
00083     }
00084 }
00085 //------------------------------------------------------
00086 
00087 void tableEditor::saveTable() {
00088     QString m_createTable = "";
00089     QString m_primaryKeys = "";
00090     if (isEditing) { saveEditedTable(); }  else {
00091         m_createTable = tr("CREATE TABLE `%1`.`%2` (\n").arg(ui->comboBox->currentText(), ui->txtTableName->text());
00092 
00093         for (int i = 0; i < ui->tblColumns->rowCount(); i++) {
00094             QString m_strColName = "";
00095             QString m_strDataType = "";
00096             QString m_strLength = "";
00097             QString m_strDefault = "";
00098             QString m_strNotNull = "";
00099             QString m_strAutoIncrement = "";
00100             QString m_strPrimaryKey;
00101             QTableWidgetItem *m_itemColName = ui->tblColumns->item(i, 0);
00102             QComboBox *m_cboDataTypes = dataTypes[i];
00103             QCheckBox *m_chkNull = notNulls[i];
00104             QCheckBox *m_chkAuto = autoIncrements[i];
00105             QCheckBox *m_chkPrim = primaryKeys[i];
00106             m_strColName = m_itemColName->text();
00107             m_strDataType = m_cboDataTypes->currentText();
00108             m_strLength = ui->tblColumns->item(i, 2)->text();
00109             m_strDefault = ui->tblColumns->item(i, 3)->text();
00110             if (m_strDefault.length() < 1) {
00111                 m_strDefault = "";
00112             } else { m_strDefault = tr("DEFAULT `%1`").arg(m_strDefault); }
00113 
00114             if (m_strDataType.toLower() == "varchar") {
00115                 if (m_strLength.length() < 1) { m_strLength = "10"; }
00116                 m_strDataType = tr("varchar(%1)").arg(m_strLength);
00117             } else { m_strDataType = m_strDataType; }
00118 
00119             int m_chkNotNull  = m_chkNull->checkState();
00120             if (m_chkNotNull == 2) {
00121                 m_strNotNull = "NOT NULL";
00122             } else { m_strNotNull = ""; }
00123             int m_chkAutoIncr = m_chkAuto->checkState();
00124             if (m_chkAutoIncr == 2) {
00125                 m_strAutoIncrement = "AUTO_INCREMENT";
00126             } else { m_strAutoIncrement ="" ; }
00127             int m_intPrim = m_chkPrim->checkState();
00128             if (m_intPrim == 2) {
00129                 m_strPrimaryKey = m_strColName;
00130             } else  {m_strPrimaryKey = ""; }
00131 
00132             if (m_strPrimaryKey.length() > 0) {
00133                 if (m_primaryKeys.length() > 0) {
00134                     m_strPrimaryKey = tr(", %1").arg(m_strPrimaryKey);
00135                 }
00136                 m_primaryKeys = tr("%1 %2").arg(m_primaryKeys, m_strPrimaryKey);
00137             }
00138             m_createTable = tr("%1 %2").arg(m_createTable, tr("`%1` %2 %3 %4 %5,\n").arg(m_strColName, m_strDataType, m_strNotNull, m_strAutoIncrement, m_strDefault));
00139         }
00140     }
00141 
00142     if (ui->txtTableName->text().length() < 1) {
00143         QMessageBox::warning(this, "Error", "Enter the table name.");
00144         ui->txtTableName->setFocus();
00145     }
00146     else {
00147         m_createTable = tr("%1 PRIMARY KEY(%2) )Engine=InnoDb;").arg(m_createTable, m_primaryKeys);
00148 
00149 
00150         ui->txtQuery->setText(m_createTable);
00151     }
00152     isEditing = false;
00153 }
00154 //------------------------------------------------------
00155 
00156 
00157 //------------------------------------------------------
00158 void tableEditor::saveEditedTable() {
00159     QString m_createTable = "";
00160     QString m_primaryKeys = "";
00161     m_createTable = tr("ALTER TABLE `%1`.`%2` \n").arg(ui->comboBox->currentText(), ui->txtTableName->text());
00162 
00163     for (int i = 0; i < ui->tblColumns->rowCount(); i++) {
00164         if (i < m_originalName.count()) {
00165             QString m_strColName = "";
00166             QString m_strDataType = "";
00167             QString m_strLength = "";
00168             QString m_strDefault = "";
00169             QString m_strNotNull = "";
00170             QString m_strAutoIncrement = "";
00171             QString m_strPrimaryKey;
00172             QTableWidgetItem *m_itemColName = ui->tblColumns->item(i, 0);
00173             QComboBox *m_cboDataTypes = dataTypes[i];
00174             QCheckBox *m_chkNull = notNulls[i];
00175             QCheckBox *m_chkAuto = autoIncrements[i];
00176             QCheckBox *m_chkPrim = primaryKeys[i];
00177             //==4
00178             m_strColName = m_itemColName->text();
00179             m_strDataType = m_cboDataTypes->currentText();
00180             m_strLength = ui->tblColumns->item(i, 2)->text();
00181             m_strDefault = ui->tblColumns->item(i, 3)->text();
00182             if (m_strDefault.length() < 1) {
00183                 m_strDefault = "";
00184             } else { m_strDefault = tr("DEFAULT '%1'").arg(m_strDefault); }
00185 
00186             if (m_strDataType.toLower() == "varchar") {
00187                 if (m_strLength.length() < 1) { m_strLength = "10"; }
00188                 m_strDataType = tr("varchar(%1)").arg(m_strLength);
00189             } else { m_strDataType = m_strDataType; }
00190 
00191             int m_chkNotNull  = m_chkNull->checkState();
00192             if (m_chkNotNull == 2) {
00193                 m_strNotNull = "NOT NULL";
00194             } else { m_strNotNull = ""; }
00195             int m_chkAutoIncr = m_chkAuto->checkState();
00196             if (m_chkAutoIncr == 2) {
00197                 m_strAutoIncrement = "AUTO_INCREMENT";
00198             } else { m_strAutoIncrement ="" ; }
00199             int m_intPrim = m_chkPrim->checkState();
00200             if (m_intPrim == 2) {
00201                 m_strPrimaryKey = m_strColName;
00202             } else  {m_strPrimaryKey = ""; }
00203 
00204             if (m_strPrimaryKey.length() > 0) {
00205                 if (m_primaryKeys.length() > 0) {
00206                     m_strPrimaryKey = tr(", %1").arg(m_strPrimaryKey);
00207                 }
00208                 m_primaryKeys = tr("%1 %2").arg(m_primaryKeys, m_strPrimaryKey);
00209             }
00210             QTableWidgetItem *it = ui->tblColumns->item(i, 0);
00211             if (it->backgroundColor() == Qt::red){
00212                 //column deleted
00213                 QString dropText = tr(" DROP COLUMN `%1`").arg(m_originalName[i]);
00214                 m_createTable = tr("%1 %2,\n").arg(m_createTable, dropText);
00215             } else if (it->backgroundColor() == Qt::lightGray) {
00216                 QTableWidgetItem *it2 = ui->tblColumns->item(i - 1, 0);
00217                 m_createTable = tr("%1 %2").arg(m_createTable,
00218                                                 tr(" ADD COLUMN `%1` %2 %3 %4 %5 AFTER `%6`,\n")
00219                                                 .arg(m_strColName, m_strDataType, m_strNotNull, m_strAutoIncrement,
00220                                                      m_strDefault, it2->text()));
00221             } else {
00222                 m_createTable = tr("%1 %2").arg(m_createTable, tr(" CHANGE `%1` `%2` %3 %4 %5 %6,\n").arg(m_originalName[i] ,m_strColName, m_strDataType, m_strNotNull, m_strAutoIncrement, m_strDefault));
00223             }
00224         }
00225     }
00226     if (ui->txtTableName->text().length() < 1) {
00227         QMessageBox::warning(this, "Error", "Enter the table name.");
00228         ui->txtTableName->setFocus();
00229     }
00230     else {
00231         m_createTable = tr("%1 DROP PRIMARY KEY,\n ADD PRIMARY KEY(%2)\n\n/* Query Generated by Smart MySQL Tool*/\n/*(c)Joshua Wambua 2010 */").arg(m_createTable, m_primaryKeys);
00232         ui->txtQuery->setText(m_createTable);
00233     }
00234 }
00235 //------------------------------------------------------
00236 
00237 
00238 
00239 void tableEditor::addColumn(QString strColName, QString length, QString datType,
00240                             QString isNull, QString strAutoInc, QString primaryKey,
00241                             QString strDefault, bool specifiedPosition, int position)
00242 {
00243     int rowNum = ui->tblColumns->rowCount();
00244     if (specifiedPosition == true) {
00245         QMessageBox::information(this, "", "Specific");
00246         rowNum = position;
00247     }
00248     ui->tblColumns->insertRow(rowNum);    
00249     ComboBoxDelegate delegate;
00250     ui->tblColumns->setItemDelegateForColumn(1, &delegate);
00251     QString m_strColName, m_strDataType;
00252     //data type combo box
00253     QComboBox *cbo = new QComboBox(this);
00254     cbo->setEditable(true);
00255     GPublics *pubs = new GPublics();
00256     cbo->insertItems(0, pubs->mysqlDatatypes());
00257     //not null check box
00258     QCheckBox *m_chkNotNull = new QCheckBox(this);
00259     m_chkNotNull->setChecked(false);
00260     if (isNull == "NO") {  m_chkNotNull->setChecked(true); }
00261     //auto increment check box
00262     QCheckBox *m_chkAutoInc = new QCheckBox(this);
00263     m_chkAutoInc->setChecked(false);
00264     if (strAutoInc == "YES") { m_chkAutoInc->setChecked(true); }
00265     //primary key check box
00266     QCheckBox *m_chkPrimaryKey = new QCheckBox(this);
00267     m_chkPrimaryKey->setChecked(false);
00268     if (primaryKey == "YES") { m_chkPrimaryKey->setChecked(true); }
00269     //
00270     m_strDataType = datType;
00271     cbo->setEditText(datType);
00272     m_strColName = strColName;
00273 
00274     QTableWidgetItem *colName = new QTableWidgetItem(m_strColName, 1);
00275     ui->tblColumns->setItem(rowNum, 0, colName);
00276     QTableWidgetItem *dataType = new QTableWidgetItem(m_strDataType, 1);
00277     dataType->text() = datType;
00278     ui->tblColumns->setItem(rowNum, 1, dataType);
00279 
00280     QTableWidgetItem *m_itemLength = new QTableWidgetItem(length, 1);
00281     ui->tblColumns->setItem(rowNum, 2, m_itemLength);
00282     QTableWidgetItem *m_itemDefault = new QTableWidgetItem(strDefault, 1);
00283     ui->tblColumns->setItem(rowNum, 3, m_itemDefault);
00284     QTableWidgetItem *notNull = new QTableWidgetItem("", 1);
00285     ui->tblColumns->setItem(rowNum, 4, notNull);
00286     QTableWidgetItem *autoInc = new QTableWidgetItem("", 1);
00287     ui->tblColumns->setItem(rowNum, 5, autoInc);
00288     QTableWidgetItem *m_itemPrimaryKey = new QTableWidgetItem("", 1);
00289     ui->tblColumns->setItem(rowNum, 6, m_itemPrimaryKey);
00290     dataTypes.insert(rowNum, cbo);
00291     notNulls.insert(rowNum, m_chkNotNull);
00292     autoIncrements.insert(rowNum, m_chkAutoInc);
00293     primaryKeys.insert(rowNum, m_chkPrimaryKey);
00294     ui->tblColumns->setCellWidget(rowNum, 1, cbo);
00295     ui->tblColumns->setCellWidget(rowNum, 4, m_chkNotNull);
00296     ui->tblColumns->setCellWidget(rowNum, 5, m_chkAutoInc);
00297     ui->tblColumns->setCellWidget(rowNum, 6, m_chkPrimaryKey);
00298     ui->tblColumns->resizeRowToContents(rowNum);
00299 
00300     if (specifiedPosition) {
00301         for (int i = 0; i < ui->tblColumns->columnCount(); i++) {
00302             QTableWidgetItem *it = ui->tblColumns->item(rowNum, i);
00303             it->setBackgroundColor(Qt::lightGray);
00304         }
00305     } else {
00306         for (int i = 0; i < ui->tblColumns->columnCount(); i++) {
00307             QTableWidgetItem *it = ui->tblColumns->item(rowNum, i);
00308             it->setBackgroundColor(Qt::white);
00309         }
00310     }
00311 }
00312 //------------------------------------------------------
00313 void tableEditor::on_btn_AddColumn_clicked()
00314 {
00315     if (isEditing) {
00316         if (ui->tblColumns->selectedItems().count() > 0) {
00317             int rowNum = ui->tblColumns->row(ui->tblColumns->selectedItems().first()) + 1;
00318             int originalCount =  m_originalName.count();
00319             //
00320             m_originalName.insert(originalCount, "");
00321             m_originalDataType.insert(originalCount, "");
00322             m_originalLength.insert(originalCount, "");
00323             m_originalDefault.insert(originalCount, "");
00324             m_originalPrimKey.insert(originalCount, "");
00325             m_originalNotNull.insert(originalCount, "");
00326             m_originalAutoInc.insert(originalCount, "");
00327             //
00328             dataTypes.insert(originalCount, new QComboBox(this));
00329             notNulls.insert(originalCount, new QCheckBox(this));
00330             autoIncrements.insert(originalCount, new QCheckBox(this));
00331             primaryKeys.insert(originalCount, new QCheckBox(this));
00332             //nudge the m_originals.
00333             //all those after rowNum should be +1
00334             for (int i = originalCount - 1; i >= rowNum; i--) {
00335                 m_originalName[i + 1] = m_originalName[i];
00336                 m_originalDataType[i + 1] = m_originalDataType[i];
00337                 m_originalLength[i + 1] = m_originalLength[i];
00338                 m_originalDefault[i + 1] = m_originalDefault[i];
00339                 m_originalPrimKey[i + 1] = m_originalPrimKey[i];
00340                 m_originalNotNull[i + 1] = m_originalNotNull[i];
00341                 m_originalAutoInc[i + 1] = m_originalAutoInc[i];
00342             }
00343             //
00344             //nudge the combo box and checkboxes
00345             for (int i = originalCount - 1; i >= rowNum; i--) {
00346                 dataTypes[i + 1] = dataTypes[i];
00347                 notNulls[i + 1] = notNulls[i];
00348                 autoIncrements[i + 1] = autoIncrements[i];
00349                 primaryKeys[i + 1] = primaryKeys[i];
00350             }
00351             //
00352             m_originalName[rowNum] = "New Column";
00353             m_originalDataType[rowNum] = "varchar";
00354             m_originalLength[rowNum] = "50";
00355             m_originalDefault[rowNum] = "";
00356             m_originalPrimKey[rowNum] = "NO";
00357             m_originalNotNull[rowNum] = "NO";
00358             m_originalAutoInc[rowNum] = "NO";
00359             addColumn("New Column", "50", "varchar", "NO", "NO", "NO", "", true, rowNum);
00360             updateEdit();
00361             saveEditedTable();
00362         }
00363     } else {
00364         if (ui->tblColumns->rowCount() == 0) {
00365             addColumn("ID", "", "int", "NO", "YES", "YES", "", false, 0);
00366         }
00367         addColumn(tr("Column%1").arg(ui->tblColumns->rowCount()), "50", "varchar", "YES", "NO", "NO", "", false, 0);
00368         saveTable();
00369     }
00370 }
00371 //------------------------------------------------------
00372 void tableEditor::on_cmdCreateTable_clicked()
00373 {
00374     if (ui->txtTableName->text() != "") {
00375         if (isEditing) {
00376             saveEditedTable();
00377         } else {
00378             saveTable();
00379         }
00380         QSqlQuery *qu = new QSqlQuery(ui->txtQuery->toPlainText(), db);
00381         if (qu->lastError().isValid()) {
00382             //error occurred
00383             QMessageBox::critical(this, "Error", tr("An error has occurred:\n\n%1").arg(qu->lastError().text()));
00384             return;
00385         } else {
00386             emit closeMe();
00387         }
00388     } else {
00389         QMessageBox::critical(this, "Error", tr("An error has occurred:\n\n%1").arg("The table name was not specified"));
00390         return;
00391     }
00392 }
00393 //------------------------------------------------------
00394 void tableEditor::on_cmdCancel_clicked()
00395 {
00396     emit closeMe();
00397 }
00398 //------------------------------------------------------
00399 void tableEditor::startEdit(QString tableName, QString dbName) {
00400     ui->comboBox->setEditText(dbName);
00401     ui->comboBox->setEnabled(false);
00402     ui->txtTableName->setText(tableName);
00403     ui->txtTableName->setEnabled(false);
00404     isEditing = true;
00405     QSqlQuery *qu = new QSqlQuery(tr("describe `%1`.`%2`").arg(dbName, tableName), db);
00406     this->setWindowTitle(tr("Editing table '%1' in '%2' ").arg(tableName, dbName));
00407     int i = 0;
00408     int k = 0;
00409     while (qu->next()) {
00410         QString m_strColumnName = "";
00411         QString m_strDataType = "";
00412         QString m_strLength = "";
00413         QString m_strNull = "";
00414         QString m_strKey = "";
00415         QString m_strDefault = "";
00416         QString m_strExtra = "";
00417         m_strColumnName = qu->value(0).toString();
00418         m_strDataType = qu->value(1).toString();
00419         m_strNull = qu->value(2).toString();
00420         m_strKey = qu->value(3).toString();
00421         m_strDefault = qu->value(4).toString();
00422         m_strExtra = qu->value(5).toString();
00423         if (m_strDataType.contains("("))  {
00424             QStringList list = m_strDataType.split("(");
00425             m_strDataType = list.at(0);
00426             m_strLength = list.at(1);
00427             QStringList len = m_strLength.split(")");
00428             m_strLength = len.at(0);
00429         }
00430         if (m_strExtra.contains("auto")) {
00431             m_strExtra = "YES";
00432         }
00433         if (m_strKey.contains("PRI")) {
00434             m_strKey = "YES";
00435         }
00436         m_strDataType = m_strDataType.toLower();
00437         //
00438         m_originalName.insert(k, m_strColumnName);
00439         m_originalDataType.insert(k, m_strDataType);
00440         m_originalLength.insert(k, m_strLength);
00441         m_originalDefault.insert(k, m_strDefault);
00442         m_originalPrimKey.insert(k, m_strKey);
00443         m_originalNotNull.insert(k, m_strNull);
00444         m_originalAutoInc.insert(k, m_strExtra);
00445         //
00446         addColumn(m_strColumnName, m_strLength, m_strDataType, m_strNull, m_strExtra, m_strKey, m_strDefault, false, 0);
00447 
00448         i = i + 1;
00449         k = k + 1;
00450     }
00451     saveEditedTable();
00452 }
00453 //------------------------------------------------------
00454 void tableEditor::on_cmdDeleteColumn_clicked()
00455 {
00456     if (ui->tblColumns->selectedItems().count() > 0) {
00457         if (isEditing) {
00458             int row =   ui->tblColumns->row(ui->tblColumns->selectedItems().first());
00459             QTableWidgetItem *it = ui->tblColumns->item(row, 0);
00460             QColor col = Qt::red;
00461             if (it->backgroundColor() == Qt::red) {
00462                 col = Qt::white;
00463                 for (int i = 0; i < ui->tblColumns->columnCount(); i++) {
00464                     QTableWidgetItem *it = ui->tblColumns->item(row, i);
00465                     it->setBackgroundColor(col);
00466                 }
00467             } else if (it->backgroundColor() == Qt::lightGray) {
00468                 ui->tblColumns->removeRow(ui->tblColumns->selectedItems().first()->row());
00469                 int row = ui->tblColumns->selectedItems().first()->row();
00470                 //remove originals
00471                 m_originalName.remove(row);
00472                 m_originalDataType.remove(row);
00473                 m_originalLength.remove(row);
00474                 m_originalDefault.remove(row);
00475                 m_originalPrimKey.remove(row);
00476                 m_originalNotNull.remove(row);
00477                 m_originalAutoInc.remove(row);
00478                 //
00479                 //remove controls
00480                 dataTypes.remove(row);
00481                 notNulls.remove(row);
00482                 autoIncrements.remove(row);
00483                 primaryKeys.remove(row);
00484                 //
00485             } else {
00486                 col = Qt::red;
00487                 for (int i = 0; i < ui->tblColumns->columnCount(); i++) {
00488                     QTableWidgetItem *it = ui->tblColumns->item(row, i);
00489                     it->setBackgroundColor(col);
00490                 }
00491             }
00492             saveEditedTable();
00493         }
00494         else {
00495             ui->tblColumns->removeRow((ui->tblColumns->selectedItems().first()->row()));
00496             saveTable();
00497         }
00498     }
00499 
00500 }
00501 //------------------------------------------------------
00502 void tableEditor::on_tblColumns_itemClicked(QTableWidgetItem* item)
00503 {
00504     if (ui->tblColumns->selectedItems().count() > 0) {
00505         if (isEditing) {
00506             int row = ui->tblColumns->row(item);
00507             QTableWidgetItem *it = ui->tblColumns->item(row, 0);
00508             if (it->backgroundColor() == Qt::red) {
00509                 ui->cmdDeleteColumn->setText("Undo Delete");
00510             } else {
00511                 ui->cmdDeleteColumn->setText("Delete Column");
00512             }
00513         }
00514         else {
00515             saveTable();
00516         }
00517     }
00518 
00519 }
00520 //------------------------------------------------------
00521 void tableEditor::on_tblColumns_itemChanged(QTableWidgetItem* item)
00522 {
00523 
00524     if (ui->tblColumns->selectedItems().count() > 0) {
00525         if (isEditing) {
00526             int row = ui->tblColumns->row(item);
00527             QTableWidgetItem *it = ui->tblColumns->item(row, 0);
00528             if (it->backgroundColor() == Qt::red) {
00529                 ui->cmdDeleteColumn->setText("Undo Delete");
00530             } else {
00531                 ui->cmdDeleteColumn->setText("Delete Column");
00532             }
00533         }
00534         else {
00535             saveTable();
00536         }
00537     }
00538 
00539 }
00540 //-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00541 void tableEditor::on_tblColumns_currentItemChanged(QTableWidgetItem* current, QTableWidgetItem* previous)
00542 {
00543     if (ui->tblColumns->selectedItems().count() > 0) {
00544         if (isEditing) {
00545             int row = ui->tblColumns->row(current);
00546             QTableWidgetItem *it = ui->tblColumns->item(row, 0);
00547             if (it->backgroundColor() == Qt::red) {
00548                 ui->cmdDeleteColumn->setText("Undo Delete");
00549             } else {
00550                 ui->cmdDeleteColumn->setText("Delete Column");
00551             }
00552         }
00553         else {
00554         }
00555     }
00556 }
00557 //------------------------------------------------------
00558 void tableEditor::generateQuery() {
00559     //capture column changes and create a query
00560     QString m_strQuery = tr("ALTER TABLE `%1`.`%2` ").arg(ui->comboBox->currentText(), ui->txtTableName->text());
00561     for (int i = 0; i <  ui->tblColumns->rowCount(); i++) {
00562         QTableWidgetItem *it = ui->tblColumns->item(i, 0);
00563         if (it->backgroundColor() == Qt::red) {
00564             m_strQuery.append(tr("\n\tDROP COLUMN `%1`,").arg(it->text()));
00565         } else if (it->backgroundColor() == Qt::yellow) {
00566             QString m_strColName = "";
00567             QString m_strDataType = "";
00568             QString m_strLength = "";
00569             QString m_strDefault = "";
00570             QString m_strNotNull = "";
00571             QString m_strAutoIncrement = "";
00572             QString m_strPrimaryKey;
00573             QTableWidgetItem *m_itemColName = ui->tblColumns->item(i, 0);
00574             QComboBox *m_cboDataTypes = dataTypes[i];
00575             QCheckBox *m_chkNull = notNulls[i];
00576             QCheckBox *m_chkAuto = autoIncrements[i];
00577             QCheckBox *m_chkPrim = primaryKeys[i];
00578             m_strColName = m_itemColName->text();
00579             m_strDataType = m_cboDataTypes->currentText();
00580             m_strLength = ui->tblColumns->item(i, 2)->text();
00581             m_strDefault = ui->tblColumns->item(i, 3)->text();
00582             if (m_strDefault.length() < 1) {
00583                 m_strDefault = "";
00584             } else { m_strDefault = tr("DEFAULT `%1`").arg(m_strDefault); }
00585 
00586             if (m_strDataType.toLower() == "varchar") {
00587                 if (m_strLength.length() < 1) { m_strLength = "10"; }
00588                 m_strDataType = tr("varchar(%1)").arg(m_strLength);
00589             } else { m_strDataType = m_strDataType; }
00590 
00591             int m_chkNotNull  = m_chkNull->checkState();
00592             if (m_chkNotNull == 2) {
00593                 m_strNotNull = "NOT NULL";
00594             } else { m_strNotNull = ""; }
00595             int m_chkAutoIncr = m_chkAuto->checkState();
00596             if (m_chkAutoIncr == 2) {
00597                 m_strAutoIncrement = "AUTO_INCREMENT";
00598             } else { m_strAutoIncrement ="" ; }
00599             int m_intPrim = m_chkPrim->checkState();
00600             if (m_intPrim == 2) {
00601                 m_strPrimaryKey = m_strColName;
00602             } else  {m_strPrimaryKey = ""; }
00603 
00604             //            if (m_strPrimaryKey.length() > 0) {
00605             //                if (m_primaryKeys.length() > 0) {
00606             //                    m_strPrimaryKey = tr(", %1").arg(m_strPrimaryKey);
00607             //                }
00608             //                m_primaryKeys = tr("%1 %2").arg(m_primaryKeys, m_strPrimaryKey);
00609             //            }
00610             QString  m_createTable = tr("%1 %2").arg(m_createTable, tr("`%1` %2 %3 %4 %5,").arg(m_strColName, m_strDataType, m_strNotNull, m_strAutoIncrement, m_strDefault));
00611 
00612 
00613             m_strQuery.append(tr("\n\tALTER `%1` %2").arg(m_originalName[i], m_createTable));
00614         }
00615     }
00616     m_strQuery = m_strQuery.left(m_strQuery.length() - 1);
00617     m_strQuery.append(";");
00618 
00619 
00620     ui->txtQuery->setText(m_strQuery);
00621 }
00622 //------------------------------------------------------
00623 void tableEditor::on_tblColumns_cellChanged(int /*row*/, int /*column*/)
00624 {
00625 
00626 }
00627 //------------------------------------------------------
00628 void tableEditor::updateEdit() {
00629     QColor col = Qt::white;
00630     for (int j = 0; j < ui->tblColumns->rowCount(); j++) {
00631         if (j < m_originalName.count()) {
00632             col = Qt::white;
00633             QTableWidgetItem *it = ui->tblColumns->item(j, 0);
00634             //check column name
00635             if (m_originalName[j] != it->text()) {
00636                 col = Qt::yellow;
00637             }
00638             //check length
00639             it= ui->tblColumns->item(j, 2);
00640             if (m_originalLength[j] != it->text()) {
00641                 col = Qt::yellow;
00642             }
00643             //check default
00644             it= ui->tblColumns->item(j, 3);
00645             if (m_originalDefault[j] != it->text()) {
00646                 col = Qt::yellow;
00647             }
00648             if (it->backgroundColor() == Qt::red) {
00649                 col = Qt::red;
00650             } else if (it->backgroundColor() == Qt::lightGray) {
00651                 col = Qt::lightGray;
00652             }
00653             for (int k = 0; k < ui->tblColumns->columnCount(); k++) {
00654                 QTableWidgetItem *it = ui->tblColumns->item(j, k);
00655                 it->setBackgroundColor(col);
00656             }
00657         }
00658     }
00659 }
00660 //------------------------------------------------------
00661 void tableEditor::on_tblColumns_currentCellChanged(int /*currentRow*/, int /*currentColumn*/, int /*previousRow*/, int /*previousColumn*/)
00662 {
00663     if (isEditing ) {
00664         updateEdit();
00665         saveEditedTable();
00666     } else {
00667     }
00668 }
00669 //------------------------------------------------------
00670 void tableEditor::insertColumn(QString after) {
00671     if (after != "") {
00672 
00673     } else {
00674 
00675     }
00676 }
00677 
00678 //------------------------------------------------------

Generated on Wed Dec 1 2010 08:43:40 for SQL Master by  doxygen 1.7.2