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

E:/sqlmaster/src/sqldump.cpp

Go to the documentation of this file.
00001 #include "sqldump.h"
00002 #include "highlighter.h"
00003 #include "ui_sqldump.h"
00004 //--------------------------------------------------------------------------------
00005 SQLDump::SQLDump(QWidget *parent) :
00006         QWidget(parent),
00007         ui(new Ui::SQLDump)
00008 {
00009     ui->setupUi(this);
00010     comboChanged = false;
00011     ico_folder = QIcon(QApplication::style()->standardIcon(QApplication::style()->SP_DirIcon));
00012     ico_db = QIcon("://icons/Icon_45.ico");
00013     ico_table = QIcon("://icons/Icon_44.ico");
00014     ico_view = QIcon("://icons/Icon_96.ico");
00015     ico_proc = QIcon("://icons/Icon_100.ico");
00016 }
00017 //--------------------------------------------------------------------------------
00018 SQLDump::~SQLDump()
00019 {
00020     delete ui;
00021 }
00022 //--------------------------------------------------------------------------------
00023 void SQLDump::setDb(QString newDbName) {
00024     dbName = newDbName;
00025     this->setWindowTitle(tr("SQL Dump (Dumping objects in %1)").arg(dbName));
00026     ui->trvObjects->clear();
00027     QTreeWidgetItem *folder;
00028     QTreeWidgetItem *child;
00029     QSqlQuery query = QSqlQuery(db);
00030     //show table list
00031     folder = new QTreeWidgetItem(ui->trvObjects);
00032     folder->setText(0, "Tables");
00033     folder->setText(1, "folTables");
00034     folder->setIcon(0, ico_folder);
00035     folder->setCheckState(0, Qt::Checked);
00036     query.exec(tr("show full tables in `%1` WHERE TABLE_TYPE = 'BASE TABLE'").arg(dbName));
00037     while (query.next()) {
00038         child = new QTreeWidgetItem(folder);
00039         child->setText(0, query.value(0).toString());
00040         child->setText(1, "chilTable");
00041         child->setIcon(0, ico_table);
00042         child->setCheckState(0, Qt::Checked);
00043     }
00044     //show view list
00045     folder = new QTreeWidgetItem(ui->trvObjects);
00046     folder->setText(0, "Views");
00047     folder->setText(1, "folViews");
00048     folder->setIcon(0, ico_folder);
00049     folder->setCheckState(0, Qt::Checked);
00050     query.exec(tr("show full tables in `%1` WHERE TABLE_TYPE = 'VIEW'").arg(dbName));
00051     while (query.next()) {
00052         child = new QTreeWidgetItem(folder);
00053         child->setText(0, query.value(0).toString());
00054         child->setText(1, "chilView");
00055         child->setIcon(0, ico_view);
00056         child->setCheckState(0, Qt::Checked);
00057     }
00058     //show stored procedure list
00059     folder = new QTreeWidgetItem(ui->trvObjects);
00060     folder->setText(0, "Stored Procedures");
00061     folder->setText(1, "folProcedures");
00062     folder->setIcon(0, ico_folder);
00063     folder->setCheckState(0, Qt::Checked);
00064     query.exec(tr("SELECT `SPECIFIC_NAME` FROM `INFORMATION_SCHEMA`.`ROUTINES` WHERE `ROUTINE_SCHEMA` = '%1' AND `ROUTINE_TYPE` = 'PROCEDURE'").arg(dbName));
00065     while (query.next()) {
00066         child = new QTreeWidgetItem(folder);
00067         child->setText(0, query.value(0).toString());
00068         child->setText(1, "chilProc");
00069         child->setIcon(0, ico_proc);
00070         child->setCheckState(0, Qt::Checked);
00071     }
00072     //show function list
00073     folder = new QTreeWidgetItem(ui->trvObjects);
00074     folder->setText(0, "Functions");
00075     folder->setText(1, "folFunctions");
00076     folder->setIcon(0, ico_folder);
00077     folder->setCheckState(0, Qt::Checked);
00078     query.exec(tr("SELECT `SPECIFIC_NAME` FROM `INFORMATION_SCHEMA`.`ROUTINES` WHERE `ROUTINE_SCHEMA` = '%1' AND `ROUTINE_TYPE` = 'FUNCTION'").arg(dbName));
00079     while (query.next()) {
00080         child = new QTreeWidgetItem(folder);
00081         child->setText(0, query.value(0).toString());
00082         child->setText(1, "chilFunction");
00083         child->setIcon(0, ico_proc);
00084         child->setCheckState(0, Qt::Checked);
00085     }
00086     //show trigger list
00087     folder = new QTreeWidgetItem(ui->trvObjects);
00088     folder->setText(0, "Triggers");
00089     folder->setText(1, "folTriggers");
00090     folder->setIcon(0, ico_folder);
00091     folder->setCheckState(0, Qt::Checked);
00092     query.exec(tr("SELECT `TRIGGER_NAME` FROM `INFORMATION_SCHEMA`.`TRIGGERS` WHERE `ROUTINE_SCHEMA` = '%1'").arg(dbName));
00093     while (query.next()) {
00094         child = new QTreeWidgetItem(folder);
00095         child->setText(0, query.value(0).toString());
00096         child->setText(1, "chilTrigger");
00097         child->setIcon(0, ico_view);
00098         child->setCheckState(0, Qt::Checked);
00099     }
00100 }
00101 //--------------------------------------------------------------------------------
00102 void SQLDump::startBackup() {
00103     ui->cboDbList->clear();
00104     QString originalDbName = dbName;
00105     for (int i = 0; i < dbList.length(); i++) {
00106         ui->cboDbList->insertItem(i, ico_db ,dbList.at(i));
00107     }
00108     for (int i = 0; i < dbList.length(); i++) {
00109         if (dbList.at(i) == originalDbName) {
00110             ui->cboDbList->setCurrentIndex(i);
00111             setDb(originalDbName);
00112         }
00113     }
00114 }
00115 //--------------------------------------------------------------------------------
00116 void SQLDump::on_cmdSelectFile_clicked()
00117 {
00118     QString fileName = QFileDialog::getSaveFileName(this, "Enter the SQL file to dump to.", QString(),"SQL Files (*.sql)");
00119     if (fileName != "") {
00120         ui->txtFileName->setText(fileName);
00121     }
00122 }
00123 //--------------------------------------------------------------------------------
00124 void SQLDump::on_cboDbList_currentIndexChanged(QString newDbName)
00125 {
00126     setDb(newDbName);
00127 }
00128 //--------------------------------------------------------------------------------
00129 void SQLDump::on_cmdExport_clicked()
00130 {
00131     backup();
00132 }
00133 //--------------------------------------------------------------------------------
00134 void SQLDump::generateQuery() {
00135 }
00136 //--------------------------------------------------------------------------------
00137 void SQLDump::on_cmdClose_clicked()
00138 {
00139     emit closeWindow();
00140 }
00141 //--------------------------------------------------------------------------------
00142 void SQLDump::on_trvObjects_itemClicked(QTreeWidgetItem* item, int /*column*/)
00143 {
00144     if (item->text(1).left(3) == "fol") {
00145         for (int i = 0; i < item->childCount(); i++) {
00146             item->child(i)->setCheckState(0, item->checkState(0));
00147         }
00148     }
00149 }
00150 //--------------------------------------------------------------------------------
00151 void SQLDump::objectCountChanged(int newCount) {
00152     objectCount = newCount;
00153     updateProgress();
00154 }
00155 //--------------------------------------------------------------------------------
00156 void SQLDump::objectNameChanged(QString newName) {
00157     objectName = newName;
00158     updateProgress();
00159 }
00160 //--------------------------------------------------------------------------------
00161 void SQLDump::objectPositionChanged(int newPos) {
00162     objectPosition = newPos;
00163     updateProgress();
00164 }
00165 //--------------------------------------------------------------------------------
00166 void SQLDump::objectTypeChanged(QString newType) {
00167     objectType = newType;
00168     updateProgress();
00169 }
00170 //--------------------------------------------------------------------------------
00171 void SQLDump::updateProgress() {
00172     ui->lblProgress->setText(tr(""
00173                                 "Scripting %1 '%2'").arg(objectType, objectName));
00174     int perc;
00175     if (objectCount > 0) {
00176         perc = (objectPosition / objectCount) * 100;
00177     } else {
00178         perc = 0;
00179     }
00180     ui->prgProgress->setValue(perc);
00181 }
00182 //--------------------------------------------------------------------------------
00183 void SQLDump::backupComplete() {
00184     ui->prgProgress->setValue(100);
00185 }
00186 //--------------------------------------------------------------------------------
00187 void SQLDump::backupComplete(QString query) {
00188     if (ui->txtFileName->text() != "") {
00189         QFile file(ui->txtFileName->text());
00190         if (!file.open(QFile::WriteOnly | QFile::Text)) {
00191             QMessageBox::warning(this, tr("Error"),
00192                                  tr("Cannot write file %1:\n%2.")
00193                                  .arg(file.fileName())
00194                                  .arg(file.errorString()));
00195             return;
00196         }
00197         QTextStream out(&file);
00198         QApplication::setOverrideCursor(Qt::WaitCursor);
00199         out << query;
00200         QApplication::restoreOverrideCursor();
00201         // QMessageBox::information(this, "Success", tr("The backup for objects in %1 has been completed.").arg(dbName));
00202     } else {
00203         // QMessageBox::critical(this, "Error", "You did not specify a file to save into. Backup failed");
00204     }
00205 }
00206 //--------------------------------------------------------------------------------
00207 void SQLDump::backup() {
00208     QStringList tableList;
00209     QStringList viewList;
00210     QStringList procedureList;
00211     for (int fols = 0; fols < ui->trvObjects->topLevelItemCount(); fols++) {
00212         //TABLES
00213         if (ui->trvObjects->topLevelItem(fols)->text(1) == "folTables") {
00214             QTreeWidgetItem *folder = ui->trvObjects->topLevelItem(fols);
00215             for (int tables = 0; tables < folder->childCount(); tables++) {
00216                 QTreeWidgetItem *table = folder->child(tables);
00217                 QString tableName = table->text(0);
00218                 if (table->checkState(0) == Qt::Checked) {
00219                     tableList << tableName;
00220                 }
00221             }
00222         }
00223         //VIEWS
00224         else if (ui->trvObjects->topLevelItem(fols)->text(1) == "folViews") {
00225             QTreeWidgetItem *folder = ui->trvObjects->topLevelItem(fols);
00226             for (int tables = 0; tables < folder->childCount(); tables++) {
00227                 QTreeWidgetItem *table = folder->child(tables);
00228                 QString tableName = table->text(0);
00229                 if (table->checkState(0) == Qt::Checked) {
00230                     viewList << tableName;
00231 
00232                 }
00233             }
00234         }
00235         //PROCEDURES
00236         else if (ui->trvObjects->topLevelItem(fols)->text(1) == "folProcedures") {
00237             QTreeWidgetItem *folder = ui->trvObjects->topLevelItem(fols);
00238             for (int tables = 0; tables < folder->childCount(); tables++) {
00239                 QTreeWidgetItem *table = folder->child(tables);
00240                 QString tableName = table->text(0);
00241                 if (table->checkState(0) == Qt::Checked) {
00242                     procedureList << tableName;
00243                 }
00244             }
00245         }
00246     }
00247     bool structure = true;
00248     bool data = false;
00249     bool useDb = ui->chkUseDb->isChecked();
00250     bool createDb = ui->chkCreateDb->isChecked();
00251     bool drop = ui->chkIncludeDrop->isChecked();
00252     if (ui->optDataOnly->isChecked())  {
00253         data = true;
00254         structure = false;
00255     }
00256     if (ui->optStructureAndData->isChecked()) {
00257         data = true;
00258         structure = true;
00259     }
00260     //create thread
00261     QThread newThread(0);
00262     dumpThread = new SqlDumpThread(0);
00263     dumpThread->moveToThread(&newThread);
00264     //setup connection
00265     connect (dumpThread, SIGNAL(currentObjectName(QString)), this, SLOT(objectNameChanged(QString)));
00266     connect (dumpThread, SIGNAL(currentObjectType(QString)), this, SLOT(objectTypeChanged(QString)));
00267     connect (dumpThread, SIGNAL(currentPosition(int)), this, SLOT(objectPositionChanged(int)));
00268     connect (dumpThread, SIGNAL(objectValueCount(int)), this, SLOT(objectCountChanged(int)));
00269     connect (dumpThread, SIGNAL(scriptingComplete()), this, SLOT(backupComplete()));
00270     connect (dumpThread, SIGNAL(generatedQuery(QString)), this, SLOT(backupComplete(QString)));
00271     connect (dumpThread, SIGNAL(totalProgress(int)), this, SLOT(totalProgress(int)));
00272     //start thread
00273     dumpThread->db = db;
00274     dumpThread->dbName = dbName;
00275     dumpThread->tableList = tableList;
00276     dumpThread->viewList = viewList;
00277     dumpThread->procedureList = procedureList;
00278     dumpThread->drop = drop;
00279     dumpThread->data = data;
00280     dumpThread->structure = structure;
00281     dumpThread->createDb = createDb;
00282     dumpThread->useDb = useDb;
00283     dumpThread->start();
00284 }
00285 //--------------------------------------------------------------------------------
00286 void SQLDump::totalProgress(int prog) {
00287     ui->prgOverall->setValue(prog);
00288     dumpThread->wait(100);
00289 }

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