Beispiel #1
0
void
QtSqlConnectionBackend::open(SqlDialect *dialect, const SqlSource &source)
{
    close();
    ScopedLock lock(drv_->conn_mux_);
    own_handle_ = true;
    conn_name_ = dialect->get_name() + _T("_") + source.db()
        + _T("_") + to_string(drv_->seq_);
    ++drv_->seq_;
    String driver = source.driver();
    bool eat_slash = false;
    if (driver == _T("QTSQL"))
    {
        if (dialect->get_name() == _T("MYSQL"))
            driver = _T("QMYSQL");
        else if (dialect->get_name() == _T("POSTGRES"))
            driver = _T("QPSQL");
        else if (dialect->get_name() == _T("ORACLE"))
            driver = _T("QOCI");
        else if (dialect->get_name() == _T("INTERBASE"))
            driver = _T("QIBASE");
        else if (dialect->get_name() == _T("SQLITE"))
            driver = _T("QSQLITE");
        if (dialect->native_driver_eats_slash()
                && !str_empty(source.db())
                && char_code(source.db()[0]) == '/')
            eat_slash = true;
    }
    conn_ = new QSqlDatabase(QSqlDatabase::addDatabase(driver, conn_name_));
    if (eat_slash)
        conn_->setDatabaseName(str_substr(source.db(), 1));
    else
        conn_->setDatabaseName(source.db());
    conn_->setUserName(source.user());
    conn_->setPassword(source.passwd());
    if (source.port() > 0)
        conn_->setPort(source.port());
    if (!str_empty(source.host()))
        conn_->setHostName(source.host());
    String options;
    Strings keys = source.options();
    for (size_t i = 0; i < keys.size(); ++i) {
        if (!str_empty(options))
            options += _T(";");
        options += keys[i] + _T("=") + source[keys[i]];
    }
    if (!str_empty(options))
        conn_->setConnectOptions(options);
    if (!conn_->open())
        throw DBError(conn_->lastError().text());
}
Beispiel #2
0
void
SQLiteConnectionBackend::open(SqlDialect *dialect, const SqlSource &source)
{
    close();
    ScopedLock lock(drv_->conn_mux_);
    own_handle_ = true;
    sqlite3_open(NARROW(source.db()).c_str(), &conn_);
    if (SQLITE_OK != sqlite3_errcode(conn_)) {
        const char *err = sqlite3_errmsg(conn_);
        throw DBError(WIDEN(err));
    }
}
Beispiel #3
0
void
SOCIConnectionBackend::open(SqlDialect *dialect, const SqlSource &source)
{
    close();
    own_handle_ = true;
    try {
        String driver = source.driver();
        std::string soci_backend = "odbc";
        if (driver == _T("SOCI"))
            soci_backend = soci_convert_dialect(dialect->get_name());
        conn_ = new soci::session(soci_backend, NARROW(source.db()));
#ifdef YB_SOCI_DEBUG
        conn_->set_log_stream(&cerr);
#endif
    }
    catch (const soci::soci_error &e) {
        throw DBError(WIDEN(e.what()));
    }
}