diff --git a/Data/MySQL/src/SessionImpl.cpp b/Data/MySQL/src/SessionImpl.cpp index 85b06d86f..6884fd0f6 100644 --- a/Data/MySQL/src/SessionImpl.cpp +++ b/Data/MySQL/src/SessionImpl.cpp @@ -129,8 +129,9 @@ void SessionImpl::open(const std::string& connect) if (options["user"] == "") throw MySQLException("create session: specify user name"); - if (options["db"] == "") - throw MySQLException("create session: specify database"); + 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) @@ -154,7 +155,7 @@ void SessionImpl::open(const std::string& connect) _handle.connect(options["host"].c_str(), options["user"].c_str(), options["password"].c_str(), - options["db"].c_str(), + db, port); addFeature("autoCommit", diff --git a/Data/MySQL/testsuite/src/MySQLTest.cpp b/Data/MySQL/testsuite/src/MySQLTest.cpp index c9b7b7420..6db543d09 100644 --- a/Data/MySQL/testsuite/src/MySQLTest.cpp +++ b/Data/MySQL/testsuite/src/MySQLTest.cpp @@ -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() { if (!_pSession) fail ("Test not available."); @@ -778,6 +799,7 @@ CppUnit::Test* MySQLTest::suite() CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MySQLTest"); + CppUnit_addTest(pSuite, MySQLTest, testConnectNoDB); CppUnit_addTest(pSuite, MySQLTest, testBareboneMySQL); CppUnit_addTest(pSuite, MySQLTest, testSimpleAccess); CppUnit_addTest(pSuite, MySQLTest, testComplexType); diff --git a/Data/MySQL/testsuite/src/MySQLTest.h b/Data/MySQL/testsuite/src/MySQLTest.h index 27065c105..b84b94d7c 100644 --- a/Data/MySQL/testsuite/src/MySQLTest.h +++ b/Data/MySQL/testsuite/src/MySQLTest.h @@ -57,6 +57,7 @@ public: MySQLTest(const std::string& name); ~MySQLTest(); + void testConnectNoDB(); void testBareboneMySQL(); void testSimpleAccess();