ODBCSQLiteTest.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. //
  2. // ODBCSQLiteTest.cpp
  3. //
  4. // $Id: //poco/Main/Data/ODBC/testsuite/src/ODBCSQLiteTest.cpp#5 $
  5. //
  6. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  7. // and Contributors.
  8. //
  9. // SPDX-License-Identifier: BSL-1.0
  10. //
  11. #include "ODBCSQLiteTest.h"
  12. #include "CppUnit/TestCaller.h"
  13. #include "CppUnit/TestSuite.h"
  14. #include "Poco/String.h"
  15. #include "Poco/Format.h"
  16. #include "Poco/Exception.h"
  17. #include "Poco/Data/LOB.h"
  18. #include "Poco/Data/StatementImpl.h"
  19. #include "Poco/Data/ODBC/Connector.h"
  20. #include "Poco/Data/ODBC/Utility.h"
  21. #include "Poco/Data/ODBC/Diagnostics.h"
  22. #include "Poco/Data/ODBC/ODBCException.h"
  23. #include "Poco/Data/ODBC/ODBCStatementImpl.h"
  24. #include <sqltypes.h>
  25. #include <iostream>
  26. using namespace Poco::Data::Keywords;
  27. using Poco::Data::ODBC::Utility;
  28. using Poco::Data::ODBC::ConnectionException;
  29. using Poco::Data::ODBC::StatementException;
  30. using Poco::Data::ODBC::StatementDiagnostics;
  31. using Poco::format;
  32. using Poco::NotFoundException;
  33. #define SQLITE_ODBC_DRIVER "SQLite3 ODBC Driver"
  34. #define SQLITE_DSN "PocoDataSQLiteTest"
  35. #define SQLITE_DB "dummy.db"
  36. ODBCTest::SessionPtr ODBCSQLiteTest::_pSession;
  37. ODBCTest::ExecPtr ODBCSQLiteTest::_pExecutor;
  38. std::string ODBCSQLiteTest::_driver = SQLITE_ODBC_DRIVER;
  39. std::string ODBCSQLiteTest::_dsn = SQLITE_DSN;
  40. std::string ODBCSQLiteTest::_uid = "";
  41. std::string ODBCSQLiteTest::_pwd = "";
  42. std::string ODBCSQLiteTest::_connectString = "Driver=" SQLITE_ODBC_DRIVER
  43. ";Database=" SQLITE_DB ";";
  44. ODBCSQLiteTest::ODBCSQLiteTest(const std::string& name):
  45. ODBCTest(name, _pSession, _pExecutor, _dsn, _uid, _pwd, _connectString)
  46. {
  47. }
  48. ODBCSQLiteTest::~ODBCSQLiteTest()
  49. {
  50. }
  51. void ODBCSQLiteTest::testBareboneODBC()
  52. {
  53. std::string tableCreateString = "CREATE TABLE Test "
  54. "(First VARCHAR(30),"
  55. "Second VARCHAR(30),"
  56. "Third BLOB,"
  57. "Fourth INTEGER,"
  58. "Fifth REAL,"
  59. "Sixth TIMESTAMP)";
  60. executor().bareboneODBCTest(dbConnString(), tableCreateString, SQLExecutor::PB_IMMEDIATE, SQLExecutor::DE_MANUAL);
  61. executor().bareboneODBCTest(dbConnString(), tableCreateString, SQLExecutor::PB_IMMEDIATE, SQLExecutor::DE_BOUND);
  62. executor().bareboneODBCTest(dbConnString(), tableCreateString, SQLExecutor::PB_AT_EXEC, SQLExecutor::DE_MANUAL);
  63. executor().bareboneODBCTest(dbConnString(), tableCreateString, SQLExecutor::PB_AT_EXEC, SQLExecutor::DE_BOUND);
  64. tableCreateString = "CREATE TABLE Test "
  65. "(First VARCHAR(30),"
  66. "Second VARCHAR(30),"
  67. "Third BLOB,"
  68. "Fourth INTEGER,"
  69. "Fifth REAL,"
  70. "Sixth DATETIME)";
  71. executor().bareboneODBCTest(dbConnString(), tableCreateString, SQLExecutor::PB_IMMEDIATE, SQLExecutor::DE_MANUAL);
  72. executor().bareboneODBCTest(dbConnString(), tableCreateString, SQLExecutor::PB_IMMEDIATE, SQLExecutor::DE_BOUND);
  73. executor().bareboneODBCTest(dbConnString(), tableCreateString, SQLExecutor::PB_AT_EXEC, SQLExecutor::DE_MANUAL);
  74. executor().bareboneODBCTest(dbConnString(), tableCreateString, SQLExecutor::PB_AT_EXEC, SQLExecutor::DE_BOUND);
  75. tableCreateString = "CREATE TABLE Test "
  76. "(First VARCHAR(30),"
  77. "Second VARCHAR(30),"
  78. "Third BLOB,"
  79. "Fourth INTEGER,"
  80. "Fifth REAL,"
  81. "Sixth DATE)";
  82. executor().bareboneODBCTest(dbConnString(), tableCreateString, SQLExecutor::PB_IMMEDIATE, SQLExecutor::DE_MANUAL);
  83. executor().bareboneODBCTest(dbConnString(), tableCreateString, SQLExecutor::PB_IMMEDIATE, SQLExecutor::DE_BOUND);
  84. executor().bareboneODBCTest(dbConnString(), tableCreateString, SQLExecutor::PB_AT_EXEC, SQLExecutor::DE_MANUAL);
  85. executor().bareboneODBCTest(dbConnString(), tableCreateString, SQLExecutor::PB_AT_EXEC, SQLExecutor::DE_BOUND);
  86. }
  87. void ODBCSQLiteTest::testAffectedRows()
  88. {
  89. if (!_pSession) fail ("Test not available.");
  90. for (int i = 0; i < 8;)
  91. {
  92. recreateStringsTable();
  93. _pSession->setFeature("autoBind", bindValue(i));
  94. _pSession->setFeature("autoExtract", bindValue(i+1));
  95. // see SQLiteStatementImpl::affectedRows() documentation for explanation
  96. // why "WHERE 1" is necessary here
  97. _pExecutor->affectedRows("WHERE 1");
  98. i += 2;
  99. }
  100. }
  101. void ODBCSQLiteTest::testNull()
  102. {
  103. if (!_pSession) fail ("Test not available.");
  104. // test for NOT NULL violation exception
  105. for (int i = 0; i < 8;)
  106. {
  107. recreateNullsTable("NOT NULL");
  108. session().setFeature("autoBind", bindValue(i));
  109. session().setFeature("autoExtract", bindValue(i+1));
  110. _pExecutor->notNulls("HY000");
  111. i += 2;
  112. }
  113. }
  114. void ODBCSQLiteTest::dropObject(const std::string& type, const std::string& name)
  115. {
  116. try
  117. {
  118. session() << format("DROP %s %s", type, name), now;
  119. }
  120. catch (StatementException& ex)
  121. {
  122. bool ignoreError = false;
  123. const StatementDiagnostics::FieldVec& flds = ex.diagnostics().fields();
  124. StatementDiagnostics::Iterator it = flds.begin();
  125. for (; it != flds.end(); ++it)
  126. {
  127. if (1 == it->_nativeError)//(no such table)
  128. {
  129. ignoreError = true;
  130. break;
  131. }
  132. }
  133. if (!ignoreError)
  134. {
  135. std::cout << ex.toString() << std::endl;
  136. throw;
  137. }
  138. }
  139. }
  140. void ODBCSQLiteTest::recreateNullableTable()
  141. {
  142. dropObject("TABLE", "NullableTest");
  143. try { *_pSession << "CREATE TABLE NullableTest (EmptyString VARCHAR(30) NULL, EmptyInteger INTEGER NULL, EmptyFloat REAL NULL , EmptyDateTime TIMESTAMP NULL)", now; }
  144. catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail ("recreatePersonTable()"); }
  145. catch(StatementException& se){ std::cout << se.toString() << std::endl; fail ("recreatePersonTable()"); }
  146. }
  147. void ODBCSQLiteTest::recreatePersonTable()
  148. {
  149. dropObject("TABLE", "Person");
  150. try { session() << "CREATE TABLE Person (LastName VARCHAR(30), FirstName VARCHAR(30), Address VARCHAR(30), Age INTEGER)", now; }
  151. catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail ("recreatePersonTable()"); }
  152. catch(StatementException& se){ std::cout << se.toString() << std::endl; fail ("recreatePersonTable()"); }
  153. }
  154. void ODBCSQLiteTest::recreatePersonBLOBTable()
  155. {
  156. dropObject("TABLE", "Person");
  157. try { session() << "CREATE TABLE Person (LastName VARCHAR(30), FirstName VARCHAR(30), Address VARCHAR(30), Image BLOB)", now; }
  158. catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail ("recreatePersonBLOBTable()"); }
  159. catch(StatementException& se){ std::cout << se.toString() << std::endl; fail ("recreatePersonBLOBTable()"); }
  160. }
  161. void ODBCSQLiteTest::recreatePersonDateTimeTable()
  162. {
  163. dropObject("TABLE", "Person");
  164. try { session() << "CREATE TABLE Person (LastName VARCHAR(30), FirstName VARCHAR(30), Address VARCHAR(30), Born TIMESTAMP)", now; }
  165. catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail ("recreatePersonDateTimeTable()"); }
  166. catch(StatementException& se){ std::cout << se.toString() << std::endl; fail ("recreatePersonDateTimeTable()"); }
  167. }
  168. void ODBCSQLiteTest::recreateIntsTable()
  169. {
  170. dropObject("TABLE", "Strings");
  171. try { session() << "CREATE TABLE Strings (str INTEGER)", now; }
  172. catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail ("recreateIntsTable()"); }
  173. catch(StatementException& se){ std::cout << se.toString() << std::endl; fail ("recreateIntsTable()"); }
  174. }
  175. void ODBCSQLiteTest::recreateStringsTable()
  176. {
  177. dropObject("TABLE", "Strings");
  178. try { session() << "CREATE TABLE Strings (str VARCHAR(30))", now; }
  179. catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail ("recreateStringsTable()"); }
  180. catch(StatementException& se){ std::cout << se.toString() << std::endl; fail ("recreateStringsTable()"); }
  181. }
  182. void ODBCSQLiteTest::recreateFloatsTable()
  183. {
  184. dropObject("TABLE", "Strings");
  185. try { session() << "CREATE TABLE Strings (str REAL)", now; }
  186. catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail ("recreateFloatsTable()"); }
  187. catch(StatementException& se){ std::cout << se.toString() << std::endl; fail ("recreateFloatsTable()"); }
  188. }
  189. void ODBCSQLiteTest::recreateTuplesTable()
  190. {
  191. dropObject("TABLE", "Tuples");
  192. try { session() << "CREATE TABLE Tuples "
  193. "(int0 INTEGER, int1 INTEGER, int2 INTEGER, int3 INTEGER, int4 INTEGER, int5 INTEGER, int6 INTEGER, "
  194. "int7 INTEGER, int8 INTEGER, int9 INTEGER, int10 INTEGER, int11 INTEGER, int12 INTEGER, int13 INTEGER,"
  195. "int14 INTEGER, int15 INTEGER, int16 INTEGER, int17 INTEGER, int18 INTEGER, int19 INTEGER)", now; }
  196. catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail ("recreateTuplesTable()"); }
  197. catch(StatementException& se){ std::cout << se.toString() << std::endl; fail ("recreateTuplesTable()"); }
  198. }
  199. void ODBCSQLiteTest::recreateVectorsTable()
  200. {
  201. dropObject("TABLE", "Vectors");
  202. try { session() << "CREATE TABLE Vectors (int0 INTEGER, flt0 REAL, str0 VARCHAR)", now; }
  203. catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail ("recreateVectorsTable()"); }
  204. catch(StatementException& se){ std::cout << se.toString() << std::endl; fail ("recreateVectorsTable()"); }
  205. }
  206. void ODBCSQLiteTest::recreateAnysTable()
  207. {
  208. dropObject("TABLE", "Anys");
  209. try { session() << "CREATE TABLE Anys (int0 INTEGER, flt0 REAL, str0 VARCHAR)", now; }
  210. catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail ("recreateAnysTable()"); }
  211. catch(StatementException& se){ std::cout << se.toString() << std::endl; fail ("recreateAnysTable()"); }
  212. }
  213. void ODBCSQLiteTest::recreateNullsTable(const std::string& notNull)
  214. {
  215. dropObject("TABLE", "NullTest");
  216. try { session() << format("CREATE TABLE NullTest (i INTEGER %s, r REAL %s, v VARCHAR(30) %s)",
  217. notNull,
  218. notNull,
  219. notNull), now; }
  220. catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail ("recreateNullsTable()"); }
  221. catch(StatementException& se){ std::cout << se.toString() << std::endl; fail ("recreateNullsTable()"); }
  222. }
  223. void ODBCSQLiteTest::recreateMiscTable()
  224. {
  225. dropObject("TABLE", "MiscTest");
  226. try
  227. {
  228. // SQLite fails with BLOB bulk operations
  229. session() << "CREATE TABLE MiscTest "
  230. "(First VARCHAR(30),"
  231. //"Second BLOB,"
  232. "Third INTEGER,"
  233. "Fourth REAL,"
  234. "Fifth DATETIME)", now;
  235. } catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail ("recreateMiscTable()"); }
  236. catch(StatementException& se){ std::cout << se.toString() << std::endl; fail ("recreateMiscTable()"); }
  237. }
  238. void ODBCSQLiteTest::recreateLogTable()
  239. {
  240. dropObject("TABLE", "T_POCO_LOG");
  241. dropObject("TABLE", "T_POCO_LOG_ARCHIVE");
  242. try
  243. {
  244. std::string sql = "CREATE TABLE %s "
  245. "(Source VARCHAR,"
  246. "Name VARCHAR,"
  247. "ProcessId INTEGER,"
  248. "Thread VARCHAR, "
  249. "ThreadId INTEGER,"
  250. "Priority INTEGER,"
  251. "Text VARCHAR,"
  252. "DateTime DATETIME)";
  253. session() << sql, "T_POCO_LOG", now;
  254. session() << sql, "T_POCO_LOG_ARCHIVE", now;
  255. } catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail ("recreateLogTable()"); }
  256. catch(StatementException& se){ std::cout << se.toString() << std::endl; fail ("recreateLogTable()"); }
  257. }
  258. CppUnit::Test* ODBCSQLiteTest::suite()
  259. {
  260. if ((_pSession = init(_driver, _dsn, _uid, _pwd, _connectString)))
  261. {
  262. std::cout << "*** Connected to [" << _driver << "] test database." << std::endl;
  263. _pExecutor = new SQLExecutor(_driver + " SQL Executor", _pSession);
  264. CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ODBCSQLiteTest");
  265. CppUnit_addTest(pSuite, ODBCSQLiteTest, testBareboneODBC);
  266. CppUnit_addTest(pSuite, ODBCSQLiteTest, testZeroRows);
  267. CppUnit_addTest(pSuite, ODBCSQLiteTest, testSimpleAccess);
  268. CppUnit_addTest(pSuite, ODBCSQLiteTest, testComplexType);
  269. CppUnit_addTest(pSuite, ODBCSQLiteTest, testSimpleAccessVector);
  270. CppUnit_addTest(pSuite, ODBCSQLiteTest, testComplexTypeVector);
  271. CppUnit_addTest(pSuite, ODBCSQLiteTest, testSharedPtrComplexTypeVector);
  272. CppUnit_addTest(pSuite, ODBCSQLiteTest, testAutoPtrComplexTypeVector);
  273. CppUnit_addTest(pSuite, ODBCSQLiteTest, testInsertVector);
  274. CppUnit_addTest(pSuite, ODBCSQLiteTest, testInsertEmptyVector);
  275. CppUnit_addTest(pSuite, ODBCSQLiteTest, testSimpleAccessList);
  276. CppUnit_addTest(pSuite, ODBCSQLiteTest, testComplexTypeList);
  277. CppUnit_addTest(pSuite, ODBCSQLiteTest, testInsertList);
  278. CppUnit_addTest(pSuite, ODBCSQLiteTest, testInsertEmptyList);
  279. CppUnit_addTest(pSuite, ODBCSQLiteTest, testSimpleAccessDeque);
  280. CppUnit_addTest(pSuite, ODBCSQLiteTest, testComplexTypeDeque);
  281. CppUnit_addTest(pSuite, ODBCSQLiteTest, testInsertDeque);
  282. CppUnit_addTest(pSuite, ODBCSQLiteTest, testInsertEmptyDeque);
  283. CppUnit_addTest(pSuite, ODBCSQLiteTest, testAffectedRows);
  284. CppUnit_addTest(pSuite, ODBCSQLiteTest, testInsertSingleBulk);
  285. CppUnit_addTest(pSuite, ODBCSQLiteTest, testInsertSingleBulkVec);
  286. CppUnit_addTest(pSuite, ODBCSQLiteTest, testLimit);
  287. CppUnit_addTest(pSuite, ODBCSQLiteTest, testLimitOnce);
  288. CppUnit_addTest(pSuite, ODBCSQLiteTest, testLimitPrepare);
  289. CppUnit_addTest(pSuite, ODBCSQLiteTest, testLimitZero);
  290. CppUnit_addTest(pSuite, ODBCSQLiteTest, testPrepare);
  291. CppUnit_addTest(pSuite, ODBCSQLiteTest, testSetSimple);
  292. CppUnit_addTest(pSuite, ODBCSQLiteTest, testSetComplex);
  293. CppUnit_addTest(pSuite, ODBCSQLiteTest, testSetComplexUnique);
  294. CppUnit_addTest(pSuite, ODBCSQLiteTest, testMultiSetSimple);
  295. CppUnit_addTest(pSuite, ODBCSQLiteTest, testMultiSetComplex);
  296. CppUnit_addTest(pSuite, ODBCSQLiteTest, testMapComplex);
  297. CppUnit_addTest(pSuite, ODBCSQLiteTest, testMapComplexUnique);
  298. CppUnit_addTest(pSuite, ODBCSQLiteTest, testMultiMapComplex);
  299. CppUnit_addTest(pSuite, ODBCSQLiteTest, testSelectIntoSingle);
  300. CppUnit_addTest(pSuite, ODBCSQLiteTest, testSelectIntoSingleStep);
  301. CppUnit_addTest(pSuite, ODBCSQLiteTest, testSelectIntoSingleFail);
  302. CppUnit_addTest(pSuite, ODBCSQLiteTest, testLowerLimitOk);
  303. CppUnit_addTest(pSuite, ODBCSQLiteTest, testLowerLimitFail);
  304. CppUnit_addTest(pSuite, ODBCSQLiteTest, testCombinedLimits);
  305. CppUnit_addTest(pSuite, ODBCSQLiteTest, testCombinedIllegalLimits);
  306. CppUnit_addTest(pSuite, ODBCSQLiteTest, testRange);
  307. CppUnit_addTest(pSuite, ODBCSQLiteTest, testIllegalRange);
  308. CppUnit_addTest(pSuite, ODBCSQLiteTest, testSingleSelect);
  309. CppUnit_addTest(pSuite, ODBCSQLiteTest, testEmptyDB);
  310. CppUnit_addTest(pSuite, ODBCSQLiteTest, testBLOB);
  311. CppUnit_addTest(pSuite, ODBCSQLiteTest, testBLOBContainer);
  312. CppUnit_addTest(pSuite, ODBCSQLiteTest, testBLOBStmt);
  313. CppUnit_addTest(pSuite, ODBCSQLiteTest, testDateTime);
  314. CppUnit_addTest(pSuite, ODBCSQLiteTest, testFloat);
  315. CppUnit_addTest(pSuite, ODBCSQLiteTest, testDouble);
  316. CppUnit_addTest(pSuite, ODBCSQLiteTest, testTuple);
  317. CppUnit_addTest(pSuite, ODBCSQLiteTest, testTupleVector);
  318. CppUnit_addTest(pSuite, ODBCSQLiteTest, testInternalExtraction);
  319. CppUnit_addTest(pSuite, ODBCSQLiteTest, testFilter);
  320. CppUnit_addTest(pSuite, ODBCSQLiteTest, testInternalStorageType);
  321. CppUnit_addTest(pSuite, ODBCSQLiteTest, testNull);
  322. CppUnit_addTest(pSuite, ODBCSQLiteTest, testRowIterator);
  323. CppUnit_addTest(pSuite, ODBCSQLiteTest, testAsync);
  324. CppUnit_addTest(pSuite, ODBCSQLiteTest, testAny);
  325. CppUnit_addTest(pSuite, ODBCSQLiteTest, testDynamicAny);
  326. CppUnit_addTest(pSuite, ODBCSQLiteTest, testSQLChannel);
  327. CppUnit_addTest(pSuite, ODBCSQLiteTest, testSQLLogger);
  328. CppUnit_addTest(pSuite, ODBCSQLiteTest, testSessionTransaction);
  329. CppUnit_addTest(pSuite, ODBCSQLiteTest, testTransaction);
  330. CppUnit_addTest(pSuite, ODBCSQLiteTest, testTransactor);
  331. CppUnit_addTest(pSuite, ODBCSQLiteTest, testReconnect);
  332. return pSuite;
  333. }
  334. return 0;
  335. }