66// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
77// and Contributors.
88//
9- // SPDX-License-Identifier: BSL-1.0
9+ // Permission is hereby granted, free of charge, to any person or organization
10+ // obtaining a copy of the software and accompanying documentation covered by
11+ // this license (the "Software") to use, reproduce, display, distribute,
12+ // execute, and transmit the Software, and to prepare derivative works of the
13+ // Software, and to permit third-parties to whom the Software is furnished to
14+ // do so, all subject to the following:
15+ //
16+ // The copyright notices in the Software and this entire statement, including
17+ // the above license grant, this restriction and the following disclaimer,
18+ // must be included in all copies of the Software, in whole or in part, and
19+ // all derivative works of the Software, unless such copies or derivative
20+ // works are solely in the form of machine-executable object code generated by
21+ // a source language processor.
22+ //
23+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+ // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
26+ // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
27+ // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
28+ // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29+ // DEALINGS IN THE SOFTWARE.
1030//
1131
1232
@@ -48,20 +68,20 @@ Poco::SharedPtr<SQLExecutor> MySQLTest::_pExecutor = 0;
4868#define MYSQL_PWD " poco"
4969#define MYSQL_HOST " localhost"
5070#define MYSQL_PORT 3306
51- #define MYSQL_DB " test "
71+ #define MYSQL_DB " pocotestdb "
5272
5373//
5474// Connection string
5575std::string MySQLTest::_dbConnString = " host=" MYSQL_HOST
5676 " ;user=" MYSQL_USER
57- " ;password=" MYSQL_PWD
58- " ;db=" MYSQL_DB
77+ " ;password=" MYSQL_PWD
78+ " ;db=" MYSQL_DB
5979 " ;compress=true"
6080 " ;auto-reconnect=true"
6181 " ;secure-auth=true" ;
6282
6383
64- MySQLTest::MySQLTest (const std::string& name):
84+ MySQLTest::MySQLTest (const std::string& name):
6585 CppUnit::TestCase(name)
6686{
6787 MySQL::Connector::registerConnector ();
@@ -76,25 +96,25 @@ MySQLTest::~MySQLTest()
7696
7797void MySQLTest::dbInfo (Session& session)
7898{
79-
80- std::cout << " Server Info: " << Utility::serverInfo (session) << std::endl;
81- std::cout << " Server Version: " << Utility::serverVersion (session) << std::endl;
82- std::cout << " Host Info: " << Utility::hostInfo (session) << std::endl;
99+ std::cout << " Server Info: " << Utility::serverInfo (session) << std::endl;
100+ std::cout << " Server Version: " << Utility::serverVersion (session) << std::endl;
101+ std::cout << " Host Info: " << Utility::hostInfo (session) << std::endl;
83102}
84103
85104
86- void MySQLTest::testConnectNoDB ()
105+ void MySQLTest::connectNoDB ()
87106{
88107 std::string dbConnString = " host=" MYSQL_HOST
89108 " ;user=" MYSQL_USER
90109 " ;password=" MYSQL_PWD
91110 " ;compress=true;auto-reconnect=true" ;
92-
111+
93112 try
94113 {
95114 Session session (MySQL::Connector::KEY, dbConnString);
96115 std::cout << " Connected to [" << " MySQL" << " ] without database." << std::endl;
97116 dbInfo (session);
117+ session << " CREATE DATABASE IF NOT EXISTS " MYSQL_DB " ;" , now;
98118 std::cout << " Disconnecting ..." << std::endl;
99119 session.close ();
100120 std::cout << " Disconnected." << std::endl;
@@ -104,8 +124,8 @@ void MySQLTest::testConnectNoDB()
104124 std::cout << ex.displayText () << std::endl;
105125 }
106126}
107-
108-
127+
128+
109129void MySQLTest::testBareboneMySQL ()
110130{
111131 if (!_pSession) fail (" Test not available." );
@@ -217,7 +237,6 @@ void MySQLTest::testLimitOnce()
217237
218238 recreateIntsTable ();
219239 _pExecutor->limitOnce ();
220-
221240}
222241
223242
@@ -428,7 +447,7 @@ void MySQLTest::testDateTime()
428447void MySQLTest::testBLOB ()
429448{
430449 if (!_pSession) fail (" Test not available." );
431-
450+
432451 recreatePersonBLOBTable ();
433452 _pExecutor->blob ();
434453
@@ -441,7 +460,7 @@ void MySQLTest::testBLOB()
441460 _pExecutor->blob (maxFldSize);
442461 fail (" must fail" );
443462 }
444- catch (DataException&)
463+ catch (DataException&)
445464 {
446465 _pSession->setProperty (" maxFieldSize" , Poco::Any (maxFldSize));
447466 }
@@ -648,7 +667,7 @@ void MySQLTest::testTupleWithNullable()
648667
649668 Info info (0 , std::string (" Address" ), 10 );
650669 *_pSession << " INSERT INTO NullableStringTest VALUES(?, ?, ?)" , use (info), now;
651-
670+
652671 info.set <0 >(info.get <0 >()++);
653672 info.set <1 >(null);
654673 *_pSession << " INSERT INTO NullableStringTest VALUES(?, ?, ?)" , use (info), now;
@@ -692,7 +711,6 @@ void MySQLTest::testTupleWithNullable()
692711
693712void MySQLTest::dropTable (const std::string& tableName)
694713{
695-
696714 try { *_pSession << format (" DROP TABLE IF EXISTS %s" , tableName), now; }
697715 catch (ConnectionException& ce){ std::cout << ce.displayText () << std::endl; fail (" dropTable()" ); }
698716 catch (StatementException& se){ std::cout << se.displayText () << std::endl; fail (" dropTable()" ); }
@@ -795,7 +813,7 @@ void MySQLTest::recreateTuplesTable()
795813void MySQLTest::recreateNullableIntTable ()
796814{
797815 dropTable (" NullableIntTest" );
798- try {
816+ try {
799817 *_pSession << " CREATE TABLE NullableIntTest (Id INTEGER(10), Value INTEGER(10))" , now;
800818 }
801819 catch (ConnectionException& ce){ std::cout << ce.displayText () << std::endl; fail (" recreateNullableIntTable()" ); }
@@ -806,7 +824,7 @@ void MySQLTest::recreateNullableIntTable()
806824void MySQLTest::recreateNullableStringTable ()
807825{
808826 dropTable (" NullableStringTest" );
809- try {
827+ try {
810828 *_pSession << " CREATE TABLE NullableStringTest (Id INTEGER(10), Address VARCHAR(30), Age INTEGER(10))" , now;
811829 }
812830 catch (ConnectionException& ce){ std::cout << ce.displayText () << std::endl; fail (" recreateNullableStringTable()" ); }
@@ -846,7 +864,17 @@ CppUnit::Test* MySQLTest::suite()
846864 catch (ConnectionFailedException& ex)
847865 {
848866 std::cout << ex.displayText () << std::endl;
849- return 0 ;
867+ std::cout << " Trying to connect without DB and create one ..." << std::endl;
868+ connectNoDB ();
869+ try
870+ {
871+ _pSession = new Session (MySQL::Connector::KEY, _dbConnString);
872+ }
873+ catch (ConnectionFailedException& ex)
874+ {
875+ std::cout << ex.displayText () << std::endl;
876+ return 0 ;
877+ }
850878 }
851879
852880 std::cout << " *** Connected to [" << " MySQL" << " ] test database." << std::endl;
@@ -856,7 +884,6 @@ CppUnit::Test* MySQLTest::suite()
856884
857885 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite (" MySQLTest" );
858886
859- CppUnit_addTest (pSuite, MySQLTest, testConnectNoDB);
860887 CppUnit_addTest (pSuite, MySQLTest, testBareboneMySQL);
861888 CppUnit_addTest (pSuite, MySQLTest, testSimpleAccess);
862889 CppUnit_addTest (pSuite, MySQLTest, testComplexType);
0 commit comments