#310 Connect to MySQL db without specifying database name.

This commit is contained in:
Aleksandar Fabijanic 2012-09-23 20:52:02 +00:00
parent 9c66edfb6f
commit 248d52164d
3 changed files with 27 additions and 3 deletions

View File

@ -129,8 +129,9 @@ void SessionImpl::open(const std::string& connect)
if (options["user"] == "") if (options["user"] == "")
throw MySQLException("create session: specify user name"); throw MySQLException("create session: specify user name");
if (options["db"] == "") const char * db = NULL;
throw MySQLException("create session: specify database"); if (!options["db"].empty())
db = options["db"].c_str();
unsigned int port = 0; unsigned int port = 0;
if (!NumberParser::tryParseUnsigned(options["port"], port) || 0 == port || port > 65535) if (!NumberParser::tryParseUnsigned(options["port"], port) || 0 == port || port > 65535)
@ -154,7 +155,7 @@ void SessionImpl::open(const std::string& connect)
_handle.connect(options["host"].c_str(), _handle.connect(options["host"].c_str(),
options["user"].c_str(), options["user"].c_str(),
options["password"].c_str(), options["password"].c_str(),
options["db"].c_str(), db,
port); port);
addFeature("autoCommit", addFeature("autoCommit",

View File

@ -90,6 +90,27 @@ MySQLTest::~MySQLTest()
} }
void MySQLTest::testConnectNoDB()
{
std::string dbConnString = "host=" MYSQL_HOST
";user=" MYSQL_USER
";password=" MYSQL_PWD
";compress=true;auto-reconnect=true";
try
{
Session session(MySQL::Connector::KEY, dbConnString);
std::cout << "Connected to [" << "MySQL" << "] without database. Disconnecting ..." << std::endl;
session.close();
std::cout << "Disconnected." << std::endl;
}
catch (ConnectionFailedException& ex)
{
std::cout << ex.displayText() << std::endl;
}
}
void MySQLTest::testBareboneMySQL() void MySQLTest::testBareboneMySQL()
{ {
if (!_pSession) fail ("Test not available."); if (!_pSession) fail ("Test not available.");
@ -778,6 +799,7 @@ CppUnit::Test* MySQLTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MySQLTest"); CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MySQLTest");
CppUnit_addTest(pSuite, MySQLTest, testConnectNoDB);
CppUnit_addTest(pSuite, MySQLTest, testBareboneMySQL); CppUnit_addTest(pSuite, MySQLTest, testBareboneMySQL);
CppUnit_addTest(pSuite, MySQLTest, testSimpleAccess); CppUnit_addTest(pSuite, MySQLTest, testSimpleAccess);
CppUnit_addTest(pSuite, MySQLTest, testComplexType); CppUnit_addTest(pSuite, MySQLTest, testComplexType);

View File

@ -57,6 +57,7 @@ public:
MySQLTest(const std::string& name); MySQLTest(const std::string& name);
~MySQLTest(); ~MySQLTest();
void testConnectNoDB();
void testBareboneMySQL(); void testBareboneMySQL();
void testSimpleAccess(); void testSimpleAccess();