Beispiel #1
0
void SessionImpl::open(const std::string& connect)
{
	if (connect != connectionString())
	{
		if (isConnected())
			throw InvalidAccessException("Session already connected");

		if (!connect.empty())
			setConnectionString(connect);
	}

	poco_assert_dbg (!connectionString().empty());

	_handle.init();
	
	unsigned int timeout = static_cast<unsigned int>(getLoginTimeout());
	_handle.options(MYSQL_OPT_CONNECT_TIMEOUT, timeout);

	std::map<std::string, std::string> options;

	// Default values
	options["host"] = "localhost";
	options["port"] = "3306";
	options["user"] = "";
	options["password"] = "";
	options["db"] = "";
	options["compress"] = "";
	options["auto-reconnect"] = "";
	options["secure-auth"] = "";
	options["character-set"] = "utf8";

	const std::string& connString = connectionString();
	for (std::string::const_iterator start = connString.begin();;) 
	{
		std::string::const_iterator finish = std::find(start, connString.end(), ';');
		std::string::const_iterator middle = std::find(start, finish, '=');

		if (middle == finish)
			throw MySQLException("create session: bad connection string format, can not find '='");

		options[copyStripped(start, middle)] = copyStripped(middle + 1, finish);

		if ((finish == connString.end()) || (finish + 1 == connString.end())) break;

		start = finish + 1;
	} 

	if (options["user"].empty())
		throw MySQLException("create session: specify user name");

	const char * db = NULL;
	if (!options["db"].empty())
		db = options["db"].c_str();

	unsigned int port = 0;
	if (!NumberParser::tryParseUnsigned(options["port"], port) || 0 == port || port > 65535)
		throw MySQLException("create session: specify correct port (numeric in decimal notation)");

	if (options["compress"] == "true")
		_handle.options(MYSQL_OPT_COMPRESS);
	else if (options["compress"] == "false")
		;
	else if (!options["compress"].empty())
		throw MySQLException("create session: specify correct compress option (true or false) or skip it");

	if (options["auto-reconnect"] == "true")
		_handle.options(MYSQL_OPT_RECONNECT, true);
	else if (options["auto-reconnect"] == "false")
		_handle.options(MYSQL_OPT_RECONNECT, false);
	else if (!options["auto-reconnect"].empty())
		throw MySQLException("create session: specify correct auto-reconnect option (true or false) or skip it");

	if (options["secure-auth"] == "true")
		_handle.options(MYSQL_SECURE_AUTH, true);
	else if (options["secure-auth"] == "false")
		_handle.options(MYSQL_SECURE_AUTH, false);
	else if (!options["secure-auth"].empty())
		throw MySQLException("create session: specify correct secure-auth option (true or false) or skip it");

	if (!options["character-set"].empty())
		_handle.options(MYSQL_SET_CHARSET_NAME, options["character-set"].c_str());

	// Real connect
	_handle.connect(options["host"].c_str(), 
			options["user"].c_str(), 
			options["password"].c_str(), 
			db, 
			port);

	addFeature("autoCommit", 
		&SessionImpl::autoCommit, 
		&SessionImpl::isAutoCommit);

	_connected = true;
}
Beispiel #2
0
SessionImpl::SessionImpl(const std::string& connectionString) : _mysql(0), _connected(false), _inTransaction(0)
{
    addProperty("insertId",
                &SessionImpl::setInsertId,
                &SessionImpl::getInsertId);

    std::map<std::string, std::string> options;

    // Default values
    options["host"] = "localhost";
    options["port"] = "3306";
    options["user"] = "";
    options["password"] = "";
    options["db"] = "";
    options["compress"] = "";
    options["auto-reconnect"] = "";

    //
    // Parse string
    //

    for (std::string::const_iterator start = connectionString.begin();;)
    {
        // find next ';'
        std::string::const_iterator finish = std::find(start, connectionString.end(), ';');

        // find '='
        std::string::const_iterator middle = std::find(start, finish, '=');

        if (middle == finish)
        {
            throw MySQLException("create session: bad connection string format, can not find '='");
        }

        // Parse name and value, skip all spaces
        options[copyStripped(start, middle)] = copyStripped(middle + 1, finish);

        if (finish == connectionString.end())
        {
            // end of parse
            break;
        }

        // move start position after ';'
        start = finish + 1;
    }

    //
    // Checking
    //

    if (options["user"] == "")
    {
        throw MySQLException("create session: specify user name");
    }

    if (options["db"] == "")
    {
        throw MySQLException("create session: specify database");
    }

    unsigned int port = 0;
    if (!NumberParser::tryParseUnsigned(options["port"], port) || 0 == port || port > 65535)
    {
        throw MySQLException("create session: specify correct port (numeric in decimal notation)");
    }

    //
    // Options
    //

    if (options["compress"] == "true")
    {
        _mysql.options(MYSQL_OPT_COMPRESS);
    }
    else if (options["compress"] == "false")
    {
        // do nothing
    }
    else if (options["compress"] != "")
    {
        throw MySQLException("create session: specify correct compress option (true or false) or skip it");
    }

    if (options["auto-reconnect"] == "true")
    {
        _mysql.options(MYSQL_OPT_RECONNECT, true);
    }
    else if (options["auto-reconnect"] == "false")
    {
        _mysql.options(MYSQL_OPT_RECONNECT, false);
    }
    else if (options["auto-reconnect"] != "")
    {
        throw MySQLException("create session: specify correct auto-reconnect option (true or false) or skip it");
    }

    //
    // Real connect
    //

    _mysql.connect(
        options["host"].c_str(),
        options["user"].c_str(),
        options["password"].c_str(),
        options["db"].c_str(),
        port);

    _connected = true;
}