Skip to content

Commit 248d521

Browse files
committed
pocoproject#310 Connect to MySQL db without specifying database name.
1 parent 9c66edf commit 248d521

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎Data/MySQL/src/SessionImpl.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ void SessionImpl::open(const std::string& connect)
129129
if (options["user"] == "")
130130
throw MySQLException("create session: specify user name");
131131

132-
if (options["db"] == "")
133-
throw MySQLException("create session: specify database");
132+
const char * db = NULL;
133+
if (!options["db"].empty())
134+
db = options["db"].c_str();
134135

135136
unsigned int port = 0;
136137
if (!NumberParser::tryParseUnsigned(options["port"], port) || 0 == port || port > 65535)
@@ -154,7 +155,7 @@ void SessionImpl::open(const std::string& connect)
154155
_handle.connect(options["host"].c_str(),
155156
options["user"].c_str(),
156157
options["password"].c_str(),
157-
options["db"].c_str(),
158+
db,
158159
port);
159160

160161
addFeature("autoCommit",

‎Data/MySQL/testsuite/src/MySQLTest.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,27 @@ MySQLTest::~MySQLTest()
9090
}
9191

9292

93+
void MySQLTest::testConnectNoDB()
94+
{
95+
std::string dbConnString = "host=" MYSQL_HOST
96+
";user=" MYSQL_USER
97+
";password=" MYSQL_PWD
98+
";compress=true;auto-reconnect=true";
99+
100+
try
101+
{
102+
Session session(MySQL::Connector::KEY, dbConnString);
103+
std::cout << "Connected to [" << "MySQL" << "] without database. Disconnecting ..." << std::endl;
104+
session.close();
105+
std::cout << "Disconnected." << std::endl;
106+
}
107+
catch (ConnectionFailedException& ex)
108+
{
109+
std::cout << ex.displayText() << std::endl;
110+
}
111+
}
112+
113+
93114
void MySQLTest::testBareboneMySQL()
94115
{
95116
if (!_pSession) fail ("Test not available.");
@@ -778,6 +799,7 @@ CppUnit::Test* MySQLTest::suite()
778799

779800
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MySQLTest");
780801

802+
CppUnit_addTest(pSuite, MySQLTest, testConnectNoDB);
781803
CppUnit_addTest(pSuite, MySQLTest, testBareboneMySQL);
782804
CppUnit_addTest(pSuite, MySQLTest, testSimpleAccess);
783805
CppUnit_addTest(pSuite, MySQLTest, testComplexType);

‎Data/MySQL/testsuite/src/MySQLTest.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class MySQLTest: public CppUnit::TestCase
5757
MySQLTest(const std::string& name);
5858
~MySQLTest();
5959

60+
void testConnectNoDB();
6061
void testBareboneMySQL();
6162

6263
void testSimpleAccess();

0 commit comments

Comments
 (0)