예제 #1
0
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
DatabaseService::pDatabaseConnection_type
DatabaseService::connect(const std::string &_name, config_type& _config, bool _asynchronous)
{
    Threading::CriticalSection lock(m_pConnectionsGuard);

    connections_types::iterator iter = m_namedConnections.find(_name);
    if (iter != m_namedConnections.end())
    {
        pDatabaseConnection_type pConn(iter->second);

        onConnectedEvent(pConn);
        return pConn;
    }

    // TODO clean up parsing of config and check for errors
    // Get File Name
    std::string fileName;
    {
        config_type::const_iterator iter = _config.find("fileName");
        if( iter != _config.end() )
        {
            fileName = iter->second;
        }
        else
        {
            throw Utility::runtime_exception("ZSQLite connect requires \"fileName\" as a parameter.");
        }
    }

    // TODO Possibly allow for other optional configuration parameters

    sqlite3* pConnection;

    int result = sqlite3_open(fileName.c_str(), &pConnection);

    if (result != SQLITE_OK)
    {
        std::stringstream errMsg;
        errMsg << "Error opening SQLite database " << fileName << ":";
        if (pConnection != NULL)
        {
             errMsg << sqlite3_errmsg(pConnection);
        }
        else
        {
            errMsg << "Unknown error.  Possibly out of memory.";
        }

        throw Utility::runtime_exception(errMsg.str());
    }

    pDatabaseConnection_type pConn(new DatabaseConnection(getSelfReference().lock(), _name, pConnection), boost::bind(&DatabaseService::onDestroy, this, _1));

    wpDatabaseConnection_type pWeakConn(pConn);
    m_namedConnections[_name] = pWeakConn;

    onConnectedEvent(pConn);

    return pConn;
}
예제 #2
0
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
DatabaseService::pDatabaseConnection_type
DatabaseService::connect(const std::string &_name, config_type& _config, bool _asynchronous)
{
    Threading::CriticalSection lock(m_pConnectionsGuard);

    connections_types::iterator iter = m_namedConnections.find(_name);
    if (iter != m_namedConnections.end())
    {
        pDatabaseConnection_type pConn(iter->second);

        onConnectedEvent(pConn);
        return pConn;
    }

    try
    {
        DatabaseConnection* pRawDatabaseConnection = 
            new DatabaseConnection(getSelfReference().lock(), _name, _config);

        pDatabaseConnection_type pConn(pRawDatabaseConnection, boost::bind(&DatabaseService::onDestroy, this, _1));

        wpDatabaseConnection_type pWeakConn(pConn);
        m_namedConnections[_name] = pWeakConn;

        onConnectedEvent(pConn);

        return pConn;
    }
    catch(...)      /// TODO : Create custom exception types and handle specific exception.
    {
        return pDatabaseConnection_type();
    }
}
예제 #3
0
connection connect(destination* pDest)
{
    std::shared_ptr<connection> pConn(new connection(pDest->ip, pDest->port));
    std::cout << "creating connection(" << pConn.use_count() << ")"
              << std::endl;
    return *pConn;
}