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

E:/sqlmaster/src/indexeditor.cpp

Go to the documentation of this file.
00001 #include "indexeditor.h"
00002 #include "ui_indexeditor.h"
00003 #include <QMessageBox>
00004 IndexEditor::IndexEditor(QWidget *parent) :
00005         QDialog(parent),
00006         ui(new Ui::IndexEditor)
00007 {
00008     ui->setupUi(this);
00009 }
00010 
00011 IndexEditor::~IndexEditor()
00012 {
00013     delete ui;
00014 }
00015 
00016 void IndexEditor::loadColumns() {
00017     db.open();
00018     ui->txtTableName->setText(tableName);
00019     ui->txtIndexName->setText(indexName);
00020     if (db.open()) {
00021         QSqlQuery qu(db);
00022         if (qu.exec(tr("show full fields from `%1`.`%2`").arg(dbName, tableName))) {
00023             while (qu.next()) {
00024                 QSqlRecord rec = qu.record();
00025                 QTreeWidgetItem *it = new QTreeWidgetItem(ui->lstColumns);
00026                 it->setText(0, rec.value("Field").toString());
00027                 it->setText(1, rec.value("Type").toString());
00028                 it->setCheckState(0, Qt::Unchecked);
00029                 it->setIcon(0, QIcon("://icons/Icon_42.ico"));
00030             }
00031         }
00032     }
00033 }
00034 
00035 void IndexEditor::newIndex() {
00036     loadColumns();
00037     isNew = true;
00038     ui->txtIndexName->setText("New Index");
00039 }
00040 
00041 void IndexEditor::editIndex() {
00042     loadColumns();
00043     isNew = false;
00044     ui->txtIndexName->setReadOnly(true);
00045     QTreeWidgetItem *par = ui->lstColumns->invisibleRootItem();
00046     for (int i = 0; i < par->childCount(); i++) {
00047         QTreeWidgetItem *it = par->child(i);
00048         QSqlQuery qu(db);
00049         if (qu.exec(tr("show keys from `%1`.`%2`").arg(dbName, tableName))) {
00050             while (qu.next()) {
00051                 QSqlRecord rec = qu.record();
00052                 if (rec.value("Key_name") == indexName && rec.value("Column_name") == it->text(0)) {
00053                     it->setCheckState(0, Qt::Checked);
00054                 }
00055             }
00056         }
00057     }
00058 }
00059 
00060 void IndexEditor::on_cmdApplyChanges_clicked()
00061 {
00062     if (isNew) {
00063         QString keyType = "index";
00064         if (ui->chkFullText->isChecked())
00065             keyType = "fulltext";
00066         if (ui->chkUnique->isChecked())
00067             keyType = "unique";
00068         QString  mainquery = tr("alter table `%1`.`%2` add %4 `%3` (")
00069                              .arg(dbName, tableName, ui->txtIndexName->text(), keyType);
00070         QString subQuery = "";
00071         QTreeWidgetItem *par = ui->lstColumns->invisibleRootItem();
00072         for (int i = 0; i < par->childCount(); i++) {
00073             QTreeWidgetItem *it = par->child(i);
00074             if (it->checkState(0) == Qt::Checked) {
00075                 subQuery.append(tr("`%1`,").arg(it->text(0)));
00076             }
00077         }
00078         if (subQuery.length() == 0) {
00079             QMessageBox::critical(this, "No columns Selected", "You have not selected any columns.");
00080             return;
00081         } else {
00082             subQuery = subQuery.left(subQuery.length() - 1);
00083             mainquery.append(subQuery);
00084             mainquery.append(")");
00085             QSqlQuery save(db);
00086             if (save.exec(mainquery)) {
00087                 QMessageBox::information(this, "Success", "The index was successfully saved.");
00088                 this->accept();
00089             } else
00090             {
00091                 QMessageBox::critical(this, "Error", tr("There was an error while saving the index.\n\n%1").arg(save.lastError().text()));
00092                 return;
00093             }
00094         }
00095 
00096     } else {
00097         QString keyType = "index";
00098         if (ui->chkFullText->isChecked())
00099             keyType = "fulltext";
00100         if (ui->chkUnique->isChecked())
00101             keyType = "unique";
00102         QString  mainquery = tr("alter table `%1`.`%2` drop key `%3`, add %4 `%3` (")
00103                              .arg(dbName, tableName, indexName, keyType);
00104         QString subQuery = "";
00105         QTreeWidgetItem *par = ui->lstColumns->invisibleRootItem();
00106         for (int i = 0; i < par->childCount(); i++) {
00107             QTreeWidgetItem *it = par->child(i);
00108             if (it->checkState(0) == Qt::Checked) {
00109                 subQuery.append(tr("`%1`,").arg(it->text(0)));
00110             }
00111         }
00112         if (subQuery.length() == 0) {
00113             QMessageBox::critical(this, "No columns Selected", "You have not selected any columns.");
00114             return;
00115         } else {
00116             subQuery = subQuery.left(subQuery.length() - 1);
00117             mainquery.append(subQuery);
00118             mainquery.append(")");
00119             QSqlQuery save(db);
00120             if (save.exec(mainquery)) {
00121                 QMessageBox::information(this, "Success", "The index was successfully modified.");
00122                 this->accept();
00123             } else
00124             {
00125                 QMessageBox::critical(this, "Error", tr("There was an error while saving the index.\n\n%1").arg(save.lastError().text()));
00126                 return;
00127             }
00128         }
00129     }
00130 }

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