Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ce29abee15
Fetching contributors…

Cannot retrieve contributors at this time

executable file 327 lines (298 sloc) 12.163 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
/*
LICENSE AND COPYRIGHT INFORMATION - Please read carefully.

Copyright (c) 2011-2012, davyjones <davyjones@github>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include "database.h"
#include "connectionproperties.h"

ulong Database::dbViewObjectId = 0;

Database::Database(MainWin *mainwin, int database_id)
{
    if(database_id == 0)
        this->database_id = ++dbViewObjectId;
    else
        this->database_id = database_id;
    this->mainwin = mainwin;
    setDatabaseStatus(false);
    setDatabaseCollapsed(true);
    setFlags(QGraphicsItem::ItemIsSelectable | ItemSendsGeometryChanges);
    setToolTip(getName());
    setCacheMode(DeviceCoordinateCache);
    setZValue(-1);
    connect(this, SIGNAL(setMainWinTitle(QString)), mainwin, SLOT(setWindowTitle(QString)));
    connect(this, SIGNAL(expandDatabase(Database*)), mainwin, SLOT(showSchemas()));
    connect(this, SIGNAL(collapseDatabase(Database*)), mainwin, SLOT(hideSchemas()));
    connect(this, SIGNAL(expandAll(Database*)), mainwin, SLOT(explodeAndShowSchemas()));
    connect(this, SIGNAL(expandAllVertically(Database*)), mainwin, SLOT(explodeAndShowSchemasVertically()));
}

void Database::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    //if(!getDatabaseStatus()) {
        emit summonPropertyDialog(this);
        //return;
    //}
    /*
if(getDatabaseStatus() && getDatabaseCollapsed()) {
setDatabaseCollapsed(false);
emit expandDatabase(this);
}
else if(getDatabaseStatus() && !getDatabaseCollapsed()) {
setDatabaseCollapsed(true);
emit collapseDatabase(this);
}*/
    //update();
}

void Database::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    QMenu menu;
    if(getDatabaseStatus()) {
        if(getDatabaseCollapsed())
            menu.addAction(tr("Explode"));
        else
            menu.addAction(tr("Collapse"));
        //menu.addAction("Explode all");
        //menu.addAction("Explode all vertically");
        menu.addSeparator();
    }
    if(getDatabaseStatus())
        menu.addAction(tr("Refresh"));
    menu.addSeparator();
    menu.addAction(QIcon(":/icons/properties.png"), tr("Properties"));

    QAction *a = menu.exec(event->screenPos());
    if(a && QString::compare(a->text(), tr("Explode")) == 0) {
        emit expandDatabase(this);
        setDatabaseCollapsed(false);
        update();
    }
    else if(a && QString::compare(a->text(), tr("Collapse")) == 0) {
        emit collapseDatabase(this);
        setDatabaseCollapsed(true);
        update();
    }
    else if(a && QString::compare(a->text(), tr("Explode all"))==0) {
        emit expandAll(this);
        setDatabaseCollapsed(false);
        update();
    }
    else if(a && QString::compare(a->text(), tr("Explode all vertically"))==0) {
        emit expandAllVertically(this);
        setDatabaseCollapsed(false);
        update();
    }
    else if(a && QString::compare(a->text(), tr("Properties"))==0) {
        emit summonPropertyDialog(this);
    }
    else if(a && QString::compare(a->text(), tr("Refresh"))==0) {
        populateDatabase();
        if(mainwin->getSearchBox()->isVisible() && !mainwin->getSearchBox()->text().isEmpty()) {
            QString search_term = mainwin->getSearchBox()->text();
            if(mainwin->displayMode() == MainWin::Tables) {
                foreach(Schema *schema, getSchemaList()) {
                    foreach(Table *table, schema->getTableList()) {
                        if(table->getName().contains(search_term))
                            table->setSearched(true);
                        else
                            table->setSearched(false);
                        update();
                    }
                }
            }
            else if(mainwin->displayMode() == MainWin::Views) {
                foreach(Schema *schema, getSchemaList()) {
                    foreach(View *view, schema->getViewList()) {
                        if(view->getName().contains(search_term))
                            view->setSearched(true);
                        else
                            view->setSearched(false);
                        update();
                    }
                }
            }
            else if(mainwin->displayMode() == MainWin::Functions) {
                foreach(Schema *schema, getSchemaList()) {
                    foreach(Function *function, schema->getFunctionList()) {
                        if(function->getName().contains(search_term))
                            function->setSearched(true);
                        else
                            function->setSearched(false);
                        update();
                    }
                }
            }
        }
    }
}

void Database::delDatabase(Database *db)
{
    delete db;
}

bool Database::populateDatabase()
{
    mainwin->clearSchemas();
    table_names_list.clear();
    view_names_list.clear();
    function_names_list.clear();

    QList<Schema*> schema_list;
    {
        QSqlQuery *query = new QSqlQuery(database_connection);
        query->exec("SELECT DISTINCT nspname FROM pg_namespace WHERE \
nspname NOT LIKE 'pg_%' AND nspname<>'information_schema' \
ORDER BY nspname;");
        if(query->lastError().isValid())
        {
            QMessageBox *error_message = new QMessageBox(QMessageBox::Critical,
                                        tr("Database error"),
                                        tr("Unable to retrieve database schemas.\n"
                                           "Check your database connection or permissions.\n"),
                                        QMessageBox::Cancel,0,Qt::Dialog);
            error_message->setWindowModality(Qt::NonModal);
            error_message->show();
            return false;
        }
        number_of_schemas = query->size();
        while (query->next())
        {
            QString schema_name = query->value(0).toString();
            Schema *schema = new Schema(mainwin, this, schema_name, schema_list.size(), number_of_schemas);
            schema->resetPos();

            /*SchemaLink *schema_link = new SchemaLink(this, schema);
if(mainwin->isColumnView())
schema_link->hide();
else
schema_link->show();
schema_link->show();*/

            QObject::connect(schema, SIGNAL(expandSchemaTables(Schema*)), mainwin, SLOT(showTables(Schema*)));
            QObject::connect(schema, SIGNAL(collapseSchemaTables(Schema*)), mainwin, SLOT(hideTables(Schema*)));
            QObject::connect(schema, SIGNAL(collapseOtherSchemas(Schema*)), mainwin, SLOT(hideOtherTables(Schema*)));
            QObject::connect(schema, SIGNAL(expandSchemaViews(Schema*)), mainwin, SLOT(showViews(Schema*)));
            QObject::connect(schema, SIGNAL(collapseSchemaViews(Schema*)), mainwin, SLOT(hideViews(Schema*)));
            QObject::connect(schema, SIGNAL(expandSchemaFunctions(Schema*)), mainwin, SLOT(showFunctions(Schema*)));
            QObject::connect(schema, SIGNAL(collapseSchemaFunctions(Schema*)), mainwin, SLOT(hideFunctions(Schema*)));
            QObject::connect(schema, SIGNAL(newTable(Schema*)), mainwin, SLOT(newTable(Schema*)));
            //QObject::connect(schema, SIGNAL(newView(Schema*)), mainwin, SLOT(newView(Schema*)));
            QObject::connect(schema, SIGNAL(newFunction(Schema*)), mainwin, SLOT(newFunction(Schema*)));
            connect(this, SIGNAL(collapseSchemaTables(Schema*)), mainwin, SLOT(hideTables(Schema*)));
            connect(this, SIGNAL(collapseSchemaFunctions(Schema*)), mainwin, SLOT(hideFunctions(Schema*)));
            if(!mainwin->isColumnView()) {
                emit collapseSchemaTables(schema);
            }
            emit collapseSchemaViews(schema);
            emit collapseSchemaFunctions(schema);
            schema->update();
            schema_list.append(schema);
        }

        query->exec("SELECT oid, * FROM pg_type;");
        if(query->lastError().isValid())
        {
            QMessageBox *error_message = new QMessageBox(QMessageBox::Critical,
                                        tr("Database error"),
                                        tr("Unable to retrieve data types.\n"
                                           "Check your database connection or permissions.\n"),
                                        QMessageBox::Cancel,0,Qt::Dialog);
            error_message->setWindowModality(Qt::NonModal);
            error_message->show();
            return false;
        }
        while (query->next())
        {
            types_hash.insert(query->value(0).toInt(),query->value(1).toString());
        }
    }
    setSchemaList(schema_list);
    setDatabaseStatus(true);
    if(mainwin->displayMode() == MainWin::Tables)
        mainwin->showAllTables();
    else if(mainwin->displayMode() == MainWin::Views)
        mainwin->showAllViews();
    else if(mainwin->displayMode() == MainWin::Functions)
        mainwin->showAllFunctions();
    return true;
}

/*void Database::refresh()
{

}*/

bool Database::setConnectionProperties(const QString srv,
                            const qint32 port,
                            const QString dbname,
                            const QString user,
                            const QString pass)
{
    QSqlDatabase::removeDatabase(QString("base").append(QString::number(database_id)));
    QStringList drivers(database_connection.drivers());
    if(!drivers.contains("QPSQL")) {
        QMessageBox::critical(mainwin, tr("Database error"),
            tr("Unable to establish a database connection.\n"
               "No PostgreSQL support.\n"), QMessageBox::Cancel);
        return false;
    }

    setHost(srv);
    setPort(QString::number(port));
    setName(dbname);
    setUser(user);
    setPassword(pass);

    database_connection = QSqlDatabase::addDatabase("QPSQL", QString("base").append(QString::number(database_id)));

    database_connection.setHostName(srv);
    database_connection.setPort(port);
    database_connection.setDatabaseName(dbname);
    database_connection.setUserName(user);
    database_connection.setPassword(pass);
    setName(database_connection.databaseName());
    if (!database_connection.open()) {
        QMessageBox::critical(mainwin, tr("Database error"),
            tr("Couldn't connect to database.\n"
               "Check connection parameters.\n"), QMessageBox::Cancel);
        MainWin::session_unsaved = false;
        return false;
    }

    if (!populateDatabase())
        return false;
    setDatabaseCollapsed(false);
    update();
    return true;
}

bool Database::advance()
{
    if (newPos == pos())
        return false;
    setPos(newPos);
    return true;
}

void Database::arrangeHorizontally()
{
    populateDatabase();
    QList<Schema*> schema_list = getSchemaList();
    foreach(Schema *schema, schema_list)
    {
        schema->horizontalPosition();
    }
    //scene()->setSceneRect(QRectF());
}

/*void Database::newSchema(QString new_schema_name)
{

}*/

QVariant Database::itemChange(GraphicsItemChange change, const QVariant &value)
{
    switch (change) {
    case ItemPositionHasChanged:
        foreach (SchemaLink *edge, edgeList)
            edge->adjust();
        break;
    default:
        break;
    }

    return QGraphicsItem::itemChange(change, value);
}

void Database::addEdge(SchemaLink *edge)
{
    edgeList << edge;
    edge->adjust();
}
Something went wrong with that request. Please try again.