Public Slots | Signals | Public Member Functions | Public Attributes

ServerEditor Class Reference

The ServerEditor widget is a fully fledged MySQL server browser. It is the primary component of SQL Master. This widgets contains a QTreeWidget to its left, and a QTabWidget to its right. The QtreeWidget Displays the server objects such as databases, tables and views. Selecting an object from this list will show more editing and interaction features to the right. More...

#include <servereditor.h>

List of all members.

Public Slots

void refreshDbList ()
void addNewDb (QString dbName)
 Adds a new database of the name dbName to the server.
void setCurrentDb (QString dbName)
 Sets the current database to dbName. All subsequent queries use this database.
void executeQuery (QString query)
void parseLargeQuery (QString query)
bool runQuery (QString query)
 Used to execute a query without showing results or a message.
void executeTypedQuery ()
 Used to parse the query typed in the queryEditor.
void addTableToScene ()
 **Depreciated** Used in earlier versions to add a table to the query designer. Use insertObjectToScene
void reloadCurrentDb ()
void selectTable (QString dbName, QString tableName)
void insertObjectToScene (QString, QString)
 Inserts the specified table or view into the query builder canvas.
void buildSceneQuery ()
void closeQueryTab (int)
 Closes the specified tab in the Query Browser tab.
void newQuery (QString)
 Creates a new tab in the Query Browser tab and executes the specified query in it.
void reloadServerInformation ()
 Reloads the server information list.
void dropView ()
 Drops the currentView.
void renameTable ()
 Renames the currentTable.
void truncateTable ()
 Truncates the currentTable.
void dropTable ()
 Drops the currentTable.
void openSQLFile ()
void dropDb ()
 Drops the currentDb.
void truncateDb ()
 Ennumerates all tables in currentDb and truncates them.
void emptyDb ()
 Drops all tables, views and stored procedures in currentDb.
void runSQLFile ()

Signals

void dbListChanged (QStringList newDbList)
void currentDbChanged (QString newDbName)
void callPopupMenu (QString tag)
void tableSelected (bool)
 Emitted when a table is selected in the object browser.
void viewSelected (bool)
 Emitted when a view is selected in the object browser.

Public Member Functions

 ServerEditor (QWidget *parent=0)
 ~ServerEditor ()
QAbstractItemModel * modelFromFile (const QString &fileName)
 Used to load keywords used in autocomplete.

Public Attributes

QSqlDatabase db
 The database on which all queries are executed. It must be Open at all times.
QsciScintilla * queryEditor
 The main query editor.
QCompleter * comp
 **Depreciated** Used in earlier versions for auto completion.
Highlighterhighlighter
QStringList dbList
 A QStringList used to store all of the databases in the current server.
QString currentDb
 A QString holding the current database name, if any.
QSqlTableModel * tableModel
 A used to hold data for the current table.
QString currentTable
 A QString holding the current selected table, if any.
QString lastQueryError
 A QString holding the last error that occurred during query execution, if any.
QString currentView
 A QString holding the current selected view, if any.

Detailed Description

The ServerEditor widget is a fully fledged MySQL server browser. It is the primary component of SQL Master. This widgets contains a QTreeWidget to its left, and a QTabWidget to its right. The QtreeWidget Displays the server objects such as databases, tables and views. Selecting an object from this list will show more editing and interaction features to the right.

Definition at line 26 of file servereditor.h.


Constructor & Destructor Documentation

ServerEditor::ServerEditor ( QWidget *  parent = 0 ) [explicit]

Definition at line 6 of file servereditor.cpp.

References closeQueryTab(), comp, db, modelFromFile(), queryEditor, and tableModel.

                                          :
        QWidget(parent),
        ui(new Ui::ServerEditor)
{
    ui->setupUi(this);
    //
    tableModel = new QSqlTableModel(this, db);
    tableModel->setEditStrategy(QSqlTableModel::OnRowChange);
    //
    //set up query editor wirh auto complete, syntax highlighting

    //and line numbers
    queryEditor = new QsciScintilla;
    queryEditor->setAutoCompletionSource(QsciScintilla::AcsAPIs);
    queryEditor->setAutoCompletionThreshold(2);
    lex = new QsciLexerSQL(queryEditor);
    lex->setFoldComments(true);
    queryEditor->setLexer(lex);
    queryEditor->setFolding(QsciScintilla::CircledTreeFoldStyle, 2);

    queryEditor->setAutoCompletionFillupsEnabled(true);
    queryEditor->setBraceMatching(QsciScintilla::StrictBraceMatch);
    queryEditor->setCallTipsVisible(true);
    queryEditor->setCallTipsStyle(QsciScintilla::CallTipsNoContext);
    queryEditor->setCaretLineVisible(true);
    queryEditor->setMarginWidth(1, 40);
    queryEditor->setMarginLineNumbers(1, true);
    ui->queryArea->addWidget(queryEditor);

    ui->txtSceneQuery->setLexer(new QsciLexerSQL);
    ui->txtSceneQuery->setFolding(QsciScintilla::PlainFoldStyle, 2);
    ui->txtSceneQuery->setAutoCompletionSource(QsciScintilla::AcsAll);
    ui->txtSceneQuery->setAutoCompletionFillupsEnabled(true);
    ui->txtSceneQuery->setBraceMatching(QsciScintilla::StrictBraceMatch);
    ui->txtSceneQuery->setCallTipsVisible(true);
    ui->txtSceneQuery->setCallTipsStyle(QsciScintilla::CallTipsNoContext);
    ui->txtSceneQuery->setCaretLineVisible(true);
    ui->txtSceneQuery->setMarginLineNumbers(1, true);
    //create auto complete
    comp = new QCompleter(queryEditor);
    comp->setModel(modelFromFile(":/dat/datafiles/keywords.dat"));
    //create syntax highlighter
    //icon set
    ico_folder = QIcon(QApplication::style()->standardIcon(QApplication::style()->SP_DirIcon));
    ico_db = QIcon("://icons/Icon_45.ico");
    ico_table = QIcon("://icons/Icon_44.ico");
    ico_view = QIcon("://icons/Icon_96.ico");
    ico_proc = QIcon("://icons/Icon_100.ico");
    ico_col = QIcon("://icons/Icon_42.ico");
    ico_key = QIcon("://icons/Icon_43.ico");
    ico_key2 = QIcon("://icons/key.ico");
    ico_gears = QIcon("://icons/Icon_79.ico");
    ico_vars = QIcon("://icons/Icon_120.ico");
    //
    QWidget *widget = ui->trvServerExplorer;
    QSizePolicy policy = widget->sizePolicy();
    policy.setHorizontalStretch(0);
    policy.setVerticalStretch(1);
    widget->setSizePolicy(policy);
    //
    ui->tabWidget->setCurrentIndex(0);
    //
    variablesLoaded = false;
    processesLoaded = false;
    //
    scene = new QGraphicsScene(this);
    scene->addText("Double click on tables and views on the database explorer to load them here.", QFont("Verdana", 20, 1, false));
    ui->graphicsView->setScene(scene);
    //
    connect (ui->tabQueryResults, SIGNAL(tabCloseRequested(int)), this, SLOT(closeQueryTab(int)));
    //
    postMessage("Connected to server.");
    //
    ui->trvServerDetails->setFirstColumnSpanned(0, QModelIndex(), true);
    ui->trvServerDetails->setFirstColumnSpanned(1, QModelIndex(), true);
    ui->trvServerDetails->expandAll();
    ui->trvServerDetails->resizeColumnToContents(0);
}
ServerEditor::~ServerEditor (  )

Definition at line 85 of file servereditor.cpp.

{
    delete ui;
}

Member Function Documentation

void ServerEditor::addNewDb ( QString  dbName ) [slot]

Adds a new database of the name dbName to the server.

Definition at line 296 of file servereditor.cpp.

References db, and refreshDbList().

                                          {
    QSqlQuery *qu = new QSqlQuery(tr("create database %1").arg(dbName), db);
    if (qu->lastError().isValid()) {
        QMessageBox::critical(this, "Error", qu->lastError().text());
    } else
    {
        QMessageBox::information(this, "Success", tr("The database %1 has been successfully created.").arg(dbName));
        refreshDbList();
    }
}
void ServerEditor::addTableToScene (  ) [slot]

**Depreciated** Used in earlier versions to add a table to the query designer. Use insertObjectToScene

Definition at line 489 of file servereditor.cpp.

                                   {
    QListWidget *newtable = new QListWidget(0);
    newtable->insertItem(0, "Column 1");
    newtable->insertItem(1, "Column 2");
    scene->addWidget(newtable, Qt::Tool);
}
void ServerEditor::buildSceneQuery (  ) [slot]

Constructs an SQL query based on the selected items in the uery builder. This query is displayed in the txtSceneQuery QsciScintilla.

Definition at line 533 of file servereditor.cpp.

Referenced by insertObjectToScene().

                                   {
    sceneQuery = "";
    sceneQuery.append("SELECT");

    //add columns
    QTreeWidgetItem *paren = ui->trvSceneColumns->invisibleRootItem();
    for (int i = 0; i < paren->childCount(); i++) {
        QTreeWidgetItem *x = paren->child(i);
        QString attatch = "\n";
        QString colName =  x->text(2);
        if (colName != "*") {
            colName = tr("`%1`").arg(colName);
        }
        attatch.append(tr("`%1`.`%2`.%3,").arg(x->text(0), x->text(1), colName));
        sceneQuery.append(attatch);
    }
    if (paren->childCount() > 0) {
        sceneQuery = sceneQuery.left(sceneQuery.length() - 1);
    }
    //

    //add tableList
    sceneQuery.append("\nFROM");
    for (int i = 0; i < sceneObjects.count(); i++) {
        sceneQuery.append("\n");
        sceneQuery.append(sceneObjects.at(i));
        sceneQuery.append(",");
    }
    sceneQuery = sceneQuery.left(sceneQuery.length() - 1);
    //

    ui->txtSceneQuery->setText(sceneQuery);
}
void ServerEditor::callPopupMenu ( QString  tag ) [signal]

When this signal is emitted, it tells the Parent Window to display a popup menu. The tag is used to 'tell' the Parent Window what sort of menu to display. An empty tag will display the default popup menu.

void ServerEditor::closeQueryTab ( int  tabIndex ) [slot]

Closes the specified tab in the Query Browser tab.

Definition at line 567 of file servereditor.cpp.

Referenced by ServerEditor().

                                             {
    ui->tabQueryResults->removeTab(tabIndex);
}
void ServerEditor::currentDbChanged ( QString  newDbName ) [signal]

Emitted when the current database changes. This occurs when the user selects a new database from the Object Browser. The new name is returned via newDbName as a QString.

Referenced by setCurrentDb().

void ServerEditor::dbListChanged ( QStringList  newDbList ) [signal]

Emitted when the database list changed. This occurs when a new database is added, a database is dropped or the database list is reloaded. The new list is returned via newDbList as a QStringList

Referenced by refreshDbList().

void ServerEditor::dropDb (  ) [slot]

Drops the currentDb.

Definition at line 735 of file servereditor.cpp.

References currentDb, refreshDbList(), and runQuery().

                          {
    if (currentDb != "")   {
        int res = 0;
        res = QMessageBox::question(this, "Drop Database", tr("Are you sure you want to drop the database <b>%1</b>? \n\nWARNING!!! \nYou will lose all data.").arg(currentDb), QMessageBox::Yes, QMessageBox::No);
        if (res == QMessageBox::Yes) {
            if(runQuery(tr("drop database `%1`").arg(currentDb))) {
                refreshDbList();
            } else {
                QMessageBox::critical(this, "Error", "Could not drop the database.");
            }
        }
    }
}
void ServerEditor::dropTable (  ) [slot]

Drops the currentTable.

Definition at line 663 of file servereditor.cpp.

References currentDb, currentTable, db, and refreshDbList().

                             {
    if (currentTable != "") {
        int x = QMessageBox::question(this, "Confirm", tr(""
                                                          "Do you really want to delete the table(%1)?"
                                                          "\n\n"
                                                          "Warning: You will lose all data in this table.").arg(currentTable),
                                      QMessageBox::Yes, QMessageBox::No, QMessageBox::Help);

        if (x == QMessageBox::Yes) {
            QSqlQuery truncate = QSqlQuery(db);
            truncate.exec(tr("drop table `%1`.`%2`").arg(currentDb, currentTable));
            QMessageBox::information(this, "Succes", tr("The table %1 has been deleted.").arg(currentTable));
            refreshDbList();
        }else if (x == QMessageBox::Help) {
            QMessageBox::information(this, "Delete Table table", tr("The drop table command will delete the selected table."));
        }
    }
}
void ServerEditor::dropView (  ) [slot]

Drops the currentView.

Definition at line 608 of file servereditor.cpp.

References currentDb, currentView, db, and reloadCurrentDb().

                            {
    if (currentView != "") {
        if (QMessageBox::question(this, "Conform drop", tr("Are you sure you want to drop the view <b>%1</b> from %2?").arg(currentView, currentDb),QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
            QSqlQuery qu(db);
            if (qu.exec(tr("drop view if exists `%1`.`%2`").arg(currentDb, currentView))) {
                QMessageBox::information(this, "Success", "View Dropped");
                reloadCurrentDb();
            } else {
                QMessageBox::critical(this, "Error", tr("There was an error in dropping thew view.\n\n").arg(qu.lastError().text()));
            }
        }
    }
}
void ServerEditor::emptyDb (  ) [slot]

Drops all tables, views and stored procedures in currentDb.

Definition at line 769 of file servereditor.cpp.

References currentDb, and db.

                           {
    if (currentDb != "")   {
        int res = 0;
        res = QMessageBox::question(this, "Empty Database", tr("Are you sure you want to empty the database <b>%1</b>? \n\nWARNING!!! \nYou will lose all data.").arg(currentDb), QMessageBox::Yes, QMessageBox::No);
        if (res == QMessageBox::Yes) {
            QSqlQuery qu(db);
            QSqlQuery qu2(db);
            if (qu.exec(tr("show full tables from `%1` where table_type = 'BASE TABLE'").arg(currentDb))){
                while (qu.next()) {
                    if (qu2.exec(tr("drop table if exists `%1`.`%2`").arg(currentDb, qu.value(0).toString()))) {
                        postMessage(tr("Dropped table `%1`.`%2`").arg(currentDb, qu.value(0).toString()));
                    } else {
                        postMessage(tr("Could not drop table `%1`.`%2`. Error: %3").arg(currentDb, qu.value(0).toString(), qu.lastError().text()));
                    }
                }
                qu.clear();
            }
            if (qu.exec(tr("select `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` where `TABLE_SCHEMA` = '%1' and `TABLE_TYPE` = 'VIEW'").arg(currentDb))){
                while (qu.next()) {
                    if (qu2.exec(tr("drop view if exists `%1`.`%2`").arg(currentDb, qu.value(0).toString()))) {
                        postMessage(tr("Dropped view `%1`.`%2`").arg(currentDb, qu.value(0).toString()));
                    } else {
                        postMessage(tr("Could not drop view `%1`.`%2`. Error: %3").arg(currentDb, qu.value(0).toString(), qu.lastError().text()));
                    }
                }
                qu.clear();
            }
        }
    }
}
void ServerEditor::executeQuery ( QString  query ) [slot]

Executes the specified query. If the query returns a result, a new QueryResults is added to the query browser tab, otherwise postMessage(); is called.

Definition at line 384 of file servereditor.cpp.

References currentDb, QueryResults::db, db, QueryResults::dbName, QueryResults::loadModel(), QueryResults::model, QueryResults::query, and QueryResults::tab.

Referenced by parseLargeQuery(), and runQuery().

                                             {
    if (query != "") {
        QSqlQueryModel *model = new QSqlQueryModel(this);
        QueryResults *res = new QueryResults(this);
        model->setQuery(query, db);
        if (model->lastError().isValid()) {
            postMessage(tr("Query failed. Error: %1").arg(model->lastError().text()));
            ui->tabObjectBrowser->setCurrentIndex(4);
            return;
        }

        if (model->query().isSelect()) {
            res->query = query;
            res->model = model;
            res->loadModel();
            res->db = db;
            res->dbName = currentDb;
            ui->tabQueryResults->insertTab(0, res, ico_table, "Query");
            ui->tabQueryResults->setCurrentIndex(0);
            res->tab = ui->tabQueryResults;
            ui->tabObjectBrowser->setCurrentIndex(2);
        } else {
            postMessage(tr("Query succeeded. %1 rows affected").arg(model->rowCount()));
            ui->tabObjectBrowser->setCurrentIndex(4);
        }
    }
}
void ServerEditor::executeTypedQuery (  ) [slot]

Used to parse the query typed in the queryEditor.

Definition at line 439 of file servereditor.cpp.

References parseLargeQuery(), and queryEditor.

void ServerEditor::insertObjectToScene ( QString  dbname,
QString  tableName 
) [slot]

Inserts the specified table or view into the query builder canvas.

Definition at line 505 of file servereditor.cpp.

References buildSceneQuery(), db, ColumnList::db, ColumnList::dbName, ColumnList::loadColumns(), and ColumnList::tableName.

                                                                        {
    ColumnList *list = new ColumnList(0);
    list->db = db;
    list->setWindowIcon(ico_table);
    list->dbName = dbname;
    list->tableName = tableName;
    connect (list, SIGNAL(objectDoubleClicked(QString,QString,QString)), this, SLOT(sceneItemDoubleClicked(QString,QString,QString)));
    list->loadColumns();
    scene->addWidget(list, Qt::Tool);
    sceneObjects << tr("`%1`.`%2`").arg(list->dbName, list->tableName);
    buildSceneQuery();
}
QAbstractItemModel * ServerEditor::modelFromFile ( const QString &  fileName )

Used to load keywords used in autocomplete.

Definition at line 140 of file servereditor.cpp.

References comp.

Referenced by ServerEditor().

                                                                       {
    QFile file(fileName);
    if (!file.open(QFile::ReadOnly))
        return new QStringListModel(comp);

#ifndef QT_NO_CURSOR
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
#endif
    QStringList words;

    while (!file.atEnd()) {
        QByteArray line = file.readLine();
        if (!line.isEmpty())
            words << line.trimmed();
    }

#ifndef QT_NO_CURSOR
    QApplication::restoreOverrideCursor();
#endif
    return new QStringListModel(words, comp);
}
void ServerEditor::newQuery ( QString  query ) [slot]

Creates a new tab in the Query Browser tab and executes the specified query in it.

Definition at line 575 of file servereditor.cpp.

References queryEditor.

                                         {
    queryEditor->setText(query);
}
void ServerEditor::openSQLFile (  ) [slot]

Shows the file open dialog box where the user can select an SQL file. This file is then displayed in the queryEditor.

See also:
runSQLFile()

Definition at line 722 of file servereditor.cpp.

References queryEditor.

                               {
    QFileDialog *op = new QFileDialog(this);
    QString fileName = op->getOpenFileName(this, "Select an SQL File", qApp->applicationDirPath(), tr("SQL Files (*.sql)"));

    if (fileName != "") {
        QFile sqlFile(fileName);
        if (!sqlFile.open(QIODevice::ReadOnly | QIODevice::Text))
            return;
        queryEditor->setText(sqlFile.readAll());;
        sqlFile.close();
    }
}
void ServerEditor::parseLargeQuery ( QString  query ) [slot]

Used to process 'Large' Queries. The queries are split and then passed to executeQuery one by one

See also:
executeQuery()
runQuery()
executeTypedQuery()

Definition at line 412 of file servereditor.cpp.

References executeQuery().

Referenced by executeTypedQuery(), and runSQLFile().

                                                {
    if (query.contains(";")) {
        QStringList queries = query.split(";");
        for (int i = 0; i < queries.length(); i++) {
            if (queries.at(i) != "")
                executeQuery(queries.at(i));
        }
    } else
    {
        executeQuery(query);
    }
}
void ServerEditor::refreshDbList (  ) [slot]

Reloads the Object Browser. All items are cleared and the database list is loaded. The signal dbListChanged is emitted, and the database objects are loaded later as each database is expanded.

Definition at line 90 of file servereditor.cpp.

References db, dbList, dbListChanged(), queryEditor, and tableSelected().

Referenced by addNewDb(), dropDb(), and dropTable().

                                 {
    QSqlQuery *qu = new QSqlQuery("show databases", db);
    ui->trvServerExplorer->clear();
    dbList.clear();
    apis = new QsciAPIs(lex);
    //GPublics *pubs = new GPublics();
    //QStringList lstKeys = pubs->mysqlKeywords();
    //apis->updateAutoCompletionList(QStringList(), lstKeys);
    //emit signalse
    emit tableSelected(false);
    //end emit signals
    while (qu->next()) {
        dbList << qu->value(0).toString();
        QTreeWidgetItem *it = new QTreeWidgetItem(ui->trvServerExplorer);
        it->setText(0, qu->value(0).toString());
        it->setIcon(0, ico_db);
        it->setText(1, "db");
        it->setText(2, "new");
        apis->add(qu->value(0).toString());
        QTreeWidgetItem *tablesFolder = new QTreeWidgetItem(it);
        tablesFolder->setText(0, "Tables");
        tablesFolder->setText(1, "folTables");
        tablesFolder->setIcon(0, ico_folder);
        tablesFolder = new QTreeWidgetItem(it);
        tablesFolder->setText(0, "Views");
        tablesFolder->setText(1, "folViews");
        tablesFolder->setIcon(0, ico_folder);
        tablesFolder = new QTreeWidgetItem(it);
        tablesFolder->setText(0, "Stored Procs");
        tablesFolder->setText(1, "folProcs");
        tablesFolder->setIcon(0, ico_folder);
        tablesFolder = new QTreeWidgetItem(it);
        tablesFolder->setText(0, "Functions");
        tablesFolder->setText(1, "folFunctions");
        tablesFolder->setIcon(0, ico_folder);
        tablesFolder = new QTreeWidgetItem(it);
        tablesFolder->setText(0, "Triggers");
        tablesFolder->setText(1, "folTriggers");
        tablesFolder->setIcon(0, ico_folder);
        tablesFolder = new QTreeWidgetItem(it);
        tablesFolder->setText(0, "Events");
        tablesFolder->setText(1, "folEvents");
        tablesFolder->setIcon(0, ico_folder);
    }
    apis->prepare();
    queryEditor->setLexer(lex);
    emit dbListChanged(dbList);
    postMessage("Database List Reloaded");
}
void ServerEditor::reloadCurrentDb (  ) [slot]

Reloads the current database. This just removes all the tables, views and stored procedure so that they can be reloaded on their parents' expansion

Definition at line 443 of file servereditor.cpp.

References currentDb.

Referenced by dropView(), and renameTable().

                                   {
    QString dbName = currentDb;
    if (ui->trvServerExplorer->findItems(dbName, Qt::MatchExactly, 0).count() > 0) {
        QTreeWidgetItem *it =  (ui->trvServerExplorer->findItems(dbName, Qt::MatchExactly, 0).at(0));
        it->setText(2, "new");
        it->setExpanded(false);
        it->setExpanded(true);
    }
}
void ServerEditor::reloadServerInformation (  ) [slot]

Reloads the server information list.

Definition at line 584 of file servereditor.cpp.

References db.

                                           {
    QTreeWidgetItem *inv = ui->trvServerDetails->invisibleRootItem();
    for (int i = 0; i < inv->childCount(); i++) {
        QTreeWidgetItem *topLevel = inv->child(i);
        for (int j = 0; j < topLevel->childCount(); j++) {
            QTreeWidgetItem *childCol = topLevel->child(j);
            //variables
            if (childCol->text(0) == "Variables") {
                QSqlQuery var(db);
                if (var.exec("show variables")) {
                    childCol->takeChildren();
                    while (var.next()) {
                        QTreeWidgetItem *varItem = new QTreeWidgetItem(childCol);
                        varItem->setText(0, var.value(0).toString());
                        varItem->setText(1, var.value(1).toString());
                        varItem->setIcon(0, ico_vars);
                    }
                }
            }
        }
    }
}
void ServerEditor::renameTable (  ) [slot]

Renames the currentTable.

Definition at line 622 of file servereditor.cpp.

References currentDb, currentTable, db, reloadCurrentDb(), and selectTable().

                               {
    if (currentTable != "") {
        bool ok;
        QString newTableName =  QInputDialog::getText(this, "Rename table",
                                                      "Enter the new table name", QLineEdit::Normal,
                                                      currentTable, &ok
                                                      );
        if (ok && !newTableName.isEmpty()) {
            QString query = tr("rename table `%1`.`%2` to `%1`.`%3`").arg(currentDb,currentTable,newTableName);
            QSqlQuery qu(db);
            if (qu.exec(query)) {
                //rename successful
                reloadCurrentDb();
                selectTable(currentDb, newTableName);
            } else {
                //rename failed
                QMessageBox::critical(this, "Error", tr("There was an error while renaming table %1.\n\n")
                                      .arg(currentTable, qu.lastError().text()));
            }
        }
    }
}
bool ServerEditor::runQuery ( QString  query ) [slot]

Used to execute a query without showing results or a message.

Definition at line 425 of file servereditor.cpp.

References db, executeQuery(), and lastQueryError.

Referenced by dropDb().

                                         {
    QSqlQuery qu(query, db);
    if (qu.lastError().isValid()) {
        if (qu.isSelect()) {
            executeQuery(query);
            return true;
        }
        lastQueryError = qu.lastError().text();
        return false;
    } else {
        return true;
    }
}
void ServerEditor::runSQLFile (  ) [slot]

Opens an SQL file and executes its contents without loading it into the queryEditor.

See also:
openSQLFile()

Definition at line 800 of file servereditor.cpp.

References currentDb, db, and parseLargeQuery().

                              {
    QFileDialog *op = new QFileDialog(this);
    QString fileName = op->getOpenFileName(this, "Select an SQL File", qApp->applicationDirPath(), tr("SQL Files (*.sql)"));

    if (fileName != "") {
        QFile sqlFile(fileName);
        if (!sqlFile.open(QIODevice::ReadOnly | QIODevice::Text))
            return;
        QString queryText = sqlFile.readAll();
        sqlFile.close();
        QSqlQuery qu(db);
        qu.exec(tr("use `%1`").arg(currentDb));
        parseLargeQuery(queryText);
    }
}
void ServerEditor::selectTable ( QString  dbName,
QString  tableName 
) [slot]

Sets the specified tableName as selected. This makes tableName the currentTable, and also makes dbName the currentDb.

See also:
setCurrentDb()

Definition at line 453 of file servereditor.cpp.

Referenced by renameTable().

                                                                {
    if (ui->trvServerExplorer->findItems(dbName, Qt::MatchExactly, 0).count() > 0) {
        QTreeWidgetItem *it =  (ui->trvServerExplorer->findItems(dbName, Qt::MatchExactly, 0).at(0));
        it->setExpanded(true);
        for (int j = 0; j < it->childCount(); j++) {
            QTreeWidgetItem *fol = it->child(j);
            if (fol->text(0) == "Tables") {
                for (int k = 0; k < fol->childCount(); k++) {
                    QTreeWidgetItem *tab = fol->child(k);
                    if (tab->text(0) == tableName) {
                        fol->setExpanded(true);
                        tab->setSelected(true);
                        return;
                    }
                }
            }
        }
    }
}
void ServerEditor::setCurrentDb ( QString  dbName ) [slot]

Sets the current database to dbName. All subsequent queries use this database.

Definition at line 367 of file servereditor.cpp.

References currentDb, currentDbChanged(), and db.

                                              {
    this->currentDb = dbName;
    emit currentDbChanged(currentDb);
    if (ui->trvServerExplorer->findItems(dbName, Qt::MatchExactly, 0).count() > 0) {
        ui->trvServerExplorer->setCurrentItem(ui->trvServerExplorer->findItems(dbName, Qt::MatchExactly, 0).at(0));
    }
    QSqlQuery *use = new QSqlQuery(tr("use %1").arg(currentDb), db);
    use->clear();
}
void ServerEditor::tableSelected ( bool   ) [signal]

Emitted when a table is selected in the object browser.

Referenced by refreshDbList().

void ServerEditor::truncateDb (  ) [slot]

Ennumerates all tables in currentDb and truncates them.

Definition at line 749 of file servereditor.cpp.

References currentDb, and db.

                              {
    if (currentDb != "")   {
        int res = 0;
        res = QMessageBox::question(this, "Truncate Database", tr("Are you sure you want to truncate the database <b>%1</b>? \n\nWARNING!!! \nYou will lose all data.").arg(currentDb), QMessageBox::Yes, QMessageBox::No);
        if (res == QMessageBox::Yes) {
            QSqlQuery qu(db);
            QSqlQuery qu2(db);
            if (qu.exec(tr("show full tables from `%1` where table_type = 'BASE TABLE'").arg(currentDb))){
                while (qu.next()) {
                    if (qu2.exec(tr("truncate table `%1`.`%2`").arg(currentDb, qu.value(0).toString()))) {
                        postMessage(tr("Truncated data for table `%1`.`%2`").arg(currentDb, qu.value(0).toString()));
                    } else {
                        postMessage(tr("Could not truncate data for table `%1`.`%2`. Error: %3").arg(currentDb, qu.value(0).toString(), qu.lastError().text()));
                    }
                }
            }
        }
    }
}
void ServerEditor::truncateTable (  ) [slot]

Truncates the currentTable.

Definition at line 645 of file servereditor.cpp.

References currentDb, currentTable, and db.

                                 {
    if (currentTable != "") {
        int x = QMessageBox::question(this, "Confirm", tr(""
                                                          "Do you really want to truncate the table(%1)?"
                                                          "\n\n"
                                                          "Warning: You will lose all data.").arg(currentTable),
                                      QMessageBox::Yes, QMessageBox::No, QMessageBox::Help);
        if (x == QMessageBox::Yes) {
            QSqlQuery truncate = QSqlQuery(db);
            truncate.exec(tr("truncate table `%1`.`%2`").arg(currentDb, currentTable));
            QMessageBox::information(this, "Succes", tr("The data in the table %1 has been truncated.").arg(currentTable));
        }else if (x == QMessageBox::Help) {
            QMessageBox::information(this, "Truncate table", tr(""
                                                                "The truncate table command will delete all data in the selected table."));
        }
    }
}
void ServerEditor::viewSelected ( bool   ) [signal]

Emitted when a view is selected in the object browser.


Member Data Documentation

QCompleter* ServerEditor::comp

**Depreciated** Used in earlier versions for auto completion.

See also:
highlighter

Definition at line 43 of file servereditor.h.

Referenced by modelFromFile(), and ServerEditor().

A QString holding the current database name, if any.

Definition at line 53 of file servereditor.h.

Referenced by dropDb(), dropTable(), dropView(), emptyDb(), executeQuery(), reloadCurrentDb(), renameTable(), runSQLFile(), setCurrentDb(), truncateDb(), and truncateTable().

A QString holding the current selected table, if any.

Definition at line 59 of file servereditor.h.

Referenced by dropTable(), renameTable(), and truncateTable().

A QString holding the current selected view, if any.

Definition at line 65 of file servereditor.h.

Referenced by dropView().

QSqlDatabase ServerEditor::db

The database on which all queries are executed. It must be Open at all times.

Definition at line 34 of file servereditor.h.

Referenced by addNewDb(), dropTable(), dropView(), emptyDb(), executeQuery(), insertObjectToScene(), refreshDbList(), reloadServerInformation(), renameTable(), runQuery(), runSQLFile(), ServerEditor(), setCurrentDb(), truncateDb(), and truncateTable().

QStringList ServerEditor::dbList

A QStringList used to store all of the databases in the current server.

Definition at line 50 of file servereditor.h.

Referenced by refreshDbList().

**Depreciated** Used in earlier versions for syntax highlighting. Earlier versions used a QTextEdit for the query editor, but now a QsciScintilla is used.

See also:
comp.

Definition at line 47 of file servereditor.h.

A QString holding the last error that occurred during query execution, if any.

Definition at line 62 of file servereditor.h.

Referenced by runQuery().

QsciScintilla* ServerEditor::queryEditor

The main query editor.

Definition at line 40 of file servereditor.h.

Referenced by executeTypedQuery(), newQuery(), openSQLFile(), refreshDbList(), and ServerEditor().

QSqlTableModel* ServerEditor::tableModel

A used to hold data for the current table.

Todo:
Create a custom table editor

Definition at line 56 of file servereditor.h.

Referenced by ServerEditor().


The documentation for this class was generated from the following files: