Ejemplo n.º 1
0
/**
  * The constructor creates the db and the tables if required. There's a single instance of db for all
  * the ServerDatabase instances.
  */
ServerDatabase::ServerDatabase() {
#ifdef _VERBOSE_DATABASE
    qDebug() << "Creating a new instance of the db connector: " << m_dbref;
#endif
    if (!m_dbref++) {
        // manually load the driver (for some reason I couldn't figure out how to have it loaded automagically)
        QPluginLoader loader("libqsqlmysql.so");
        if (loader.load()) {
#ifdef _VERBOSE_DATABASE
            qDebug() << "Loaded mysql drivers plugin";
#endif
        } else {
            qDebug() << QObject::tr("Failed to load mysql drivers plugin: ") + loader.errorString();
            return;
        }

        QSqlDriverPlugin *sqlPlugin  = qobject_cast<QSqlDriverPlugin *>(loader.instance());
#ifdef _VERBOSE_DATABASE
        qDebug() << "Available sql drivers: " << sqlPlugin->keys();
#endif
        QSqlDriver *sqlDriver = sqlPlugin->create(DB_TYPE);
        if (!sqlDriver) {
            qDebug() << QObject::tr("Failed to instantiate mysql driver");
            return;
        }

        sqlDriver->open(DB_NAME, DB_USR, DB_PWD, DB_HOST);
        if (sqlDriver->isOpenError()) {
            qDebug() << QObject::tr("Failed to connect to (") + DB_TYPE + QObject::tr(") DB ") + DB_NAME + QObject::tr(" on host ") + DB_HOST + QObject::tr(" with usr/pwd '") + DB_USR + "/" + DB_PWD + "'";
            qDebug() << QObject::tr("ERROR: ") + sqlDriver->lastError().text();
            return;
        }
        m_db = QSqlDatabase::addDatabase(sqlDriver);

        // create tables if non existing
        createTables();
    }
}
Ejemplo n.º 2
0
QSqlDriver *QSqlDriverPluginPrivate::create( const QString &key )
{
    return plugin->create( key );
}