/**
 * Creates a new instance of the DataAccess class. Only one static connection
 * is made to the database in each program using this class, which is shared by
 * all instances. It is important to abort or commit transactions when they are
 * finished with to prevent database deadlock.
 */
DataAccess::DataAccess() {
    const char* routine = "DataAccess::DataAccess";

	// For each instance increment counter. We can only close the connection
	// when the last instance is destroyed.
    DataAccess::instanceCount++;

	// If this is the first instance create the connection to database
    if (DataAccess::instanceCount == 1) {
        try {
			// Create connection
			C = new PqxxConnection( getConnectionString() );
            L_INFO(LOG_DB,"Connected to database " + std::string(C->dbname()));
            L_INFO(LOG_DB," -> Backend version: " 
                            + dps_itoa(C->server_version()));
            L_INFO(LOG_DB," -> Protocal version: " 
                            + dps_itoa(C->protocol_version()));
			// Init the transaction mutex
            t_routine_mutex = new pthread_mutex_t;
            pthread_mutex_init(t_routine_mutex,NULL);
			W = 0;
        }
        catch (const std::exception &e) {
            L_ERROR(LOG_DB,"Exception: " + std::string(e.what()));
            throw;
        }
        catch (...) {
			// Whoops! We can't connect to the DB
            L_CRITICAL(LOG_DB,"An unexpected error occured");
            throw;
        }
    }
}
Exemple #2
0
//-----------------------------------------------------------------------------
void Server::createDatabase(DatabasePtr db, int pagesize, int dialect)
{
    QString extra_params;
    if (pagesize)
        extra_params += (QString::fromLatin1(" PAGE_SIZE ") + pagesize);

    QString charset(db->getConnectionCharset());
    if (!charset.isEmpty())
        extra_params += (QString::fromLatin1(" DEFAULT CHARACTER SET ") + charset);

    IBPP::Database db1;
    db1 = IBPP::DatabaseFactory(wx2std(getConnectionString()),
                                wx2std(db->getPath()), wx2std(db->getUsername()),
                                wx2std(db->getDecryptedPassword()), "", wx2std(charset),
                                wx2std(extra_params));
    db1->Create(dialect);
}
Exemple #3
0
//-----------------------------------------------------------------------------
bool Server::getService(IBPP::Service& svc, ProgressIndicator* progressind,
                        bool sysdba)
{
    if (progressind)
    {
        progressind->initProgress(_("Connecting..."),
                                  databasesM.size() + 2, 0, 1);
    }

    // check if we already had some successful connections
    if (!serviceSysdbaPasswordM.isEmpty())  // we have sysdba pass
    {
        if (progressind)
        {
            progressind->setProgressMessage(_("Using current SYSDBA password"));
            progressind->stepProgress();
        }
        try
        {
            svc = IBPP::ServiceFactory(wx2std(getConnectionString()),
                                       "SYSDBA", wx2std(serviceSysdbaPasswordM));
            svc->Connect();
            return true;
        }
        catch(IBPP::Exception&)   // keep going if connect fails
        {
            serviceSysdbaPasswordM.clear();
        }
    }
    if (progressind && progressind->isCanceled())
        return false;
    // check if we have non-sysdba connection
    if (!sysdba && !serviceUserM.isEmpty())
    {
        if (progressind)
        {
            progressind->setProgressMessage(QString::fromLatin1("Using current %1 password")
                                            .arg(serviceUserM));
            progressind->stepProgress();
        }
        try
        {
            svc = IBPP::ServiceFactory(wx2std(getConnectionString()),
                                       wx2std(serviceUserM), wx2std(servicePasswordM));
            svc->Connect();
            return true;
        }
        catch(IBPP::Exception&)   // keep going if connect fails
        {
            serviceUserM.clear();
            servicePasswordM.clear();
        }
    }

    // first try connected databases
    for (DatabasePtrs::iterator ci = databasesM.begin();
            ci != databasesM.end(); ++ci)
    {
        if (progressind && progressind->isCanceled())
            return false;
        if (!(*ci)->isConnected())
            continue;
        // Use the user name and password of the connected user
        // instead of the stored ones.
        IBPP::Database& db = (*ci)->getIBPPDatabase();
        if (sysdba && std2wx(db->Username()).toUpper() != QString::fromLatin1("SYSDBA"))
            continue;
        if (progressind)
        {
            progressind->setProgressMessage(_("Using password of: ") +
                                            std2wx(db->Username()) + QString::fromLatin1("@") + (*ci)->getName_());
            progressind->stepProgress();
        }
        try
        {
            svc = IBPP::ServiceFactory(wx2std(getConnectionString()),
                                       db->Username(), db->UserPassword());
            svc->Connect();
            if (sysdba)
                serviceSysdbaPasswordM = std2wx(db->UserPassword());
            else
            {
                serviceUserM = std2wx(db->Username());
                servicePasswordM = std2wx(db->UserPassword());
            }
            return true;
        }
        catch(IBPP::Exception&)   // keep going if connect fails
        {
        }
    }

    // when the operation is not canceled try to user/pass of disconnected DBs
    for (DatabasePtrs::const_iterator ci = databasesM.begin();
            ci != databasesM.end(); ++ci)
    {
        if (progressind && progressind->isCanceled())
            return false;
        if ((*ci)->isConnected())
            continue;
        QString user = (*ci)->getUsername();
        QString pwd = (*ci)->getDecryptedPassword();
        if (pwd.isEmpty() || sysdba && user.toUpper() != QString::fromLatin1("SYSDBA"))
            continue;
        if (progressind)
        {
            progressind->setProgressMessage(_("Using password of: ") +
                                            user + QString::fromLatin1("@") + (*ci)->getName_());
            progressind->stepProgress();
        }
        try
        {
            svc = IBPP::ServiceFactory(wx2std(getConnectionString()),
                                       wx2std(user), wx2std(pwd));
            svc->Connect();
            if (sysdba)
                serviceSysdbaPasswordM = pwd;
            else
            {
                serviceUserM = user;
                servicePasswordM = pwd;
            }
            return true;
        }
        catch(IBPP::Exception&)   // keep going if connect fails
        {
        }
    }
    return false;
}