| 1 | // |
| 2 | // SQLTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "SQLTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "Poco/SQL/Session.h" |
| 15 | #include "Poco/SQL/SessionFactory.h" |
| 16 | #include "Poco/SQL/LOB.h" |
| 17 | #include "Poco/SQL/LOBStream.h" |
| 18 | #include "Poco/SQL/MetaColumn.h" |
| 19 | #include "Poco/SQL/Column.h" |
| 20 | #include "Poco/SQL/Date.h" |
| 21 | #include "Poco/SQL/Time.h" |
| 22 | #include "Poco/SQL/SimpleRowFormatter.h" |
| 23 | #include "Poco/SQL/JSONRowFormatter.h" |
| 24 | #include "Poco/SQL/SQLException.h" |
| 25 | #include "Connector.h" |
| 26 | #include "Poco/BinaryReader.h" |
| 27 | #include "Poco/BinaryWriter.h" |
| 28 | #include "Poco/DateTime.h" |
| 29 | #include "Poco/Types.h" |
| 30 | #include "Poco/Dynamic/Var.h" |
| 31 | #include "Poco/SQL/DynamicLOB.h" |
| 32 | #include "Poco/SQL/DynamicDateTime.h" |
| 33 | #include "Poco/Exception.h" |
| 34 | #include <cstring> |
| 35 | #include <sstream> |
| 36 | #include <iomanip> |
| 37 | #include <set> |
| 38 | #include <tuple> |
| 39 | |
| 40 | |
| 41 | using namespace Poco::SQL::Keywords; |
| 42 | |
| 43 | |
| 44 | using Poco::BinaryReader; |
| 45 | using Poco::BinaryWriter; |
| 46 | using Poco::UInt32; |
| 47 | using Poco::Int64; |
| 48 | using Poco::UInt64; |
| 49 | using Poco::DateTime; |
| 50 | using Poco::Dynamic::Var; |
| 51 | using Poco::InvalidAccessException; |
| 52 | using Poco::IllegalStateException; |
| 53 | using Poco::RangeException; |
| 54 | using Poco::NotFoundException; |
| 55 | using Poco::InvalidArgumentException; |
| 56 | using Poco::NotImplementedException; |
| 57 | using Poco::SQL::Session; |
| 58 | using Poco::SQL::SessionFactory; |
| 59 | using Poco::SQL::Statement; |
| 60 | using Poco::SQL::NotSupportedException; |
| 61 | using Poco::SQL::CLOB; |
| 62 | using Poco::SQL::CLOBInputStream; |
| 63 | using Poco::SQL::CLOBOutputStream; |
| 64 | using Poco::SQL::MetaColumn; |
| 65 | using Poco::SQL::Column; |
| 66 | using Poco::SQL::Row; |
| 67 | using Poco::SQL::RowFormatter; |
| 68 | using Poco::SQL::SimpleRowFormatter; |
| 69 | using Poco::SQL::JSONRowFormatter; |
| 70 | using Poco::SQL::Date; |
| 71 | using Poco::SQL::Time; |
| 72 | using Poco::SQL::AbstractExtraction; |
| 73 | using Poco::SQL::AbstractExtractionVec; |
| 74 | using Poco::SQL::AbstractExtractionVecVec; |
| 75 | using Poco::SQL::AbstractBinding; |
| 76 | using Poco::SQL::AbstractBindingVec; |
| 77 | using Poco::SQL::NotConnectedException; |
| 78 | |
| 79 | |
| 80 | SQLTest::SQLTest(const std::string& name): CppUnit::TestCase(name) |
| 81 | { |
| 82 | Poco::SQL::Test::Connector::addToFactory(); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | SQLTest::~SQLTest() |
| 87 | { |
| 88 | Poco::SQL::Test::Connector::removeFromFactory(); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | void SQLTest::testSession() |
| 93 | { |
| 94 | Session sess(SessionFactory::instance().create("test" , "cs" )); |
| 95 | assertTrue ("test" == sess.impl()->connectorName()); |
| 96 | assertTrue (sess.connector() == sess.impl()->connectorName()); |
| 97 | assertTrue ("cs" == sess.impl()->connectionString()); |
| 98 | assertTrue ("test:///cs" == sess.uri()); |
| 99 | |
| 100 | assertTrue (sess.getLoginTimeout() == Session::LOGIN_TIMEOUT_DEFAULT); |
| 101 | sess.setLoginTimeout(123); |
| 102 | assertTrue (sess.getLoginTimeout() == 123); |
| 103 | |
| 104 | Session sess2(SessionFactory::instance().create("TeSt:///Cs" )); |
| 105 | assertTrue ("test" == sess2.impl()->connectorName()); |
| 106 | assertTrue ("Cs" == sess2.impl()->connectionString()); |
| 107 | assertTrue ("test:///Cs" == sess2.uri()); |
| 108 | |
| 109 | sess << "DROP TABLE IF EXISTS Test" , now; |
| 110 | int count; |
| 111 | sess << "SELECT COUNT(*) FROM PERSON" , into(count), now; |
| 112 | |
| 113 | std::string str; |
| 114 | Statement stmt = (sess << "SELECT * FROM Strings" , into(str), limit(50)); |
| 115 | stmt.execute(); |
| 116 | |
| 117 | sess.close(); |
| 118 | assertTrue (!sess.getFeature("connected" )); |
| 119 | assertTrue (!sess.isConnected()); |
| 120 | |
| 121 | try |
| 122 | { |
| 123 | stmt.execute(); |
| 124 | fail ("must fail" ); |
| 125 | } catch (NotConnectedException&) { } |
| 126 | |
| 127 | try |
| 128 | { |
| 129 | sess << "SELECT * FROM Strings" , now; |
| 130 | fail ("must fail" ); |
| 131 | } catch (NotConnectedException&) { } |
| 132 | |
| 133 | sess.open(); |
| 134 | assertTrue (sess.getFeature("connected" )); |
| 135 | assertTrue (sess.isConnected()); |
| 136 | |
| 137 | sess << "SELECT * FROM Strings" , now; |
| 138 | stmt.execute(); |
| 139 | |
| 140 | sess.reconnect(); |
| 141 | assertTrue (sess.getFeature("connected" )); |
| 142 | assertTrue (sess.isConnected()); |
| 143 | |
| 144 | sess << "SELECT * FROM Strings" , now; |
| 145 | stmt.execute(); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | void SQLTest::testStatementFormatting() |
| 150 | { |
| 151 | Session sess(SessionFactory::instance().create("test" , "cs" )); |
| 152 | |
| 153 | Statement stmt = (sess << "SELECT %s%c%s,%d,%u,%f,%s FROM Person WHERE Name LIKE 'Simp%%'" , |
| 154 | "'" ,'a',"'" ,-1, 1u, 1.5, "42" , now); |
| 155 | |
| 156 | assertTrue ("SELECT 'a',-1,1,1.500000,42 FROM Person WHERE Name LIKE 'Simp%'" == stmt.toString()); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | void SQLTest::testFeatures() |
| 161 | { |
| 162 | Session sess(SessionFactory::instance().create("test" , "cs" )); |
| 163 | |
| 164 | sess.setFeature("f1" , true); |
| 165 | assertTrue (sess.getFeature("f1" )); |
| 166 | assertTrue (sess.getFeature("f2" )); |
| 167 | |
| 168 | try |
| 169 | { |
| 170 | sess.setFeature("f2" , false); |
| 171 | } |
| 172 | catch (NotImplementedException&) |
| 173 | { |
| 174 | } |
| 175 | |
| 176 | sess.setFeature("f3" , false); |
| 177 | assertTrue (!sess.getFeature("f2" )); |
| 178 | |
| 179 | try |
| 180 | { |
| 181 | sess.setFeature("f3" , true); |
| 182 | } |
| 183 | catch (NotImplementedException&) |
| 184 | { |
| 185 | } |
| 186 | |
| 187 | try |
| 188 | { |
| 189 | sess.setFeature("f4" , false); |
| 190 | } |
| 191 | catch (NotSupportedException&) |
| 192 | { |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | |
| 197 | void SQLTest::testProperties() |
| 198 | { |
| 199 | Session sess(SessionFactory::instance().create("test" , "cs" )); |
| 200 | |
| 201 | sess.setProperty("p1" , 1); |
| 202 | Poco::Any v1 = sess.getProperty("p1" ); |
| 203 | assertTrue (Poco::AnyCast<int>(v1) == 1); |
| 204 | Poco::Any v2 = sess.getProperty("p2" ); |
| 205 | assertTrue (Poco::AnyCast<int>(v2) == 1); |
| 206 | |
| 207 | try |
| 208 | { |
| 209 | sess.setProperty("p2" , 2); |
| 210 | } |
| 211 | catch (NotImplementedException&) |
| 212 | { |
| 213 | } |
| 214 | |
| 215 | sess.setProperty("p3" , 2); |
| 216 | v1 = sess.getProperty("p2" ); |
| 217 | assertTrue (Poco::AnyCast<int>(v1) == 2); |
| 218 | |
| 219 | try |
| 220 | { |
| 221 | sess.setProperty("p3" , 3); |
| 222 | } |
| 223 | catch (NotImplementedException&) |
| 224 | { |
| 225 | } |
| 226 | |
| 227 | try |
| 228 | { |
| 229 | sess.setProperty("p4" , 4); |
| 230 | } |
| 231 | catch (NotSupportedException&) |
| 232 | { |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | |
| 237 | void SQLTest::testLOB() |
| 238 | { |
| 239 | std::vector<int> vec; |
| 240 | vec.push_back(0); |
| 241 | vec.push_back(1); |
| 242 | vec.push_back(2); |
| 243 | vec.push_back(3); |
| 244 | vec.push_back(4); |
| 245 | vec.push_back(5); |
| 246 | vec.push_back(6); |
| 247 | vec.push_back(7); |
| 248 | vec.push_back(8); |
| 249 | vec.push_back(9); |
| 250 | |
| 251 | Poco::SQL::LOB<int> lobNum1(&vec[0], vec.size()); |
| 252 | assertTrue (lobNum1.size() == vec.size()); |
| 253 | assertTrue (0 == std::memcmp(&vec[0], lobNum1.rawContent(), lobNum1.size() * sizeof(int))); |
| 254 | assertTrue (*lobNum1.begin() == 0); |
| 255 | Poco::SQL::LOB<int>::Iterator it1 = lobNum1.end(); |
| 256 | assertTrue (*(--it1) == 9); |
| 257 | |
| 258 | Poco::SQL::LOB<int> lobNum2(lobNum1); |
| 259 | assertTrue (lobNum2.size() == lobNum1.size()); |
| 260 | assertTrue (lobNum2 == lobNum1); |
| 261 | lobNum1.swap(lobNum2); |
| 262 | assertTrue (lobNum2 == lobNum1); |
| 263 | } |
| 264 | |
| 265 | |
| 266 | void SQLTest::testCLOB() |
| 267 | { |
| 268 | std::string strDigit = "1234567890" ; |
| 269 | std::string strAlpha = "abcdefghijklmnopqrstuvwxyz" ; |
| 270 | std::vector<char> vecAlpha(strAlpha.begin(), strAlpha.end()); |
| 271 | std::vector<char> vecDigit(strDigit.begin(), strDigit.end()); |
| 272 | |
| 273 | CLOB blobNumStr(strDigit.c_str(), strDigit.size()); |
| 274 | assertTrue (blobNumStr.size() == strDigit.size()); |
| 275 | assertTrue (0 == std::strncmp(strDigit.c_str(), blobNumStr.rawContent(), blobNumStr.size())); |
| 276 | assertTrue (*blobNumStr.begin() == '1'); |
| 277 | CLOB::Iterator itNumStr = blobNumStr.end(); |
| 278 | assertTrue (*(--itNumStr) == '0'); |
| 279 | CLOB blobNumVec(vecDigit); |
| 280 | assertTrue (blobNumVec.size() == vecDigit.size()); |
| 281 | assertTrue (blobNumVec == blobNumStr); |
| 282 | blobNumVec.swap(blobNumStr); |
| 283 | assertTrue (blobNumVec.size() == blobNumStr.size()); |
| 284 | assertTrue (blobNumVec == blobNumStr); |
| 285 | |
| 286 | CLOB blobChrStr(strAlpha.c_str(), strAlpha.size()); |
| 287 | CLOB blobChrVec(vecAlpha); |
| 288 | assertTrue (blobChrStr.size() == strAlpha.size()); |
| 289 | assertTrue (0 == std::strncmp(strAlpha.c_str(), blobChrStr.rawContent(), blobChrStr.size())); |
| 290 | assertTrue (*blobChrStr.begin() == 'a'); |
| 291 | CLOB::Iterator itChrStr = blobChrStr.end(); |
| 292 | assertTrue (*(--itChrStr) == 'z'); |
| 293 | assertTrue (blobChrStr == blobChrVec); |
| 294 | |
| 295 | blobNumStr.swap(blobChrStr); |
| 296 | assertTrue (blobNumStr != blobChrStr); |
| 297 | assertTrue (&blobNumStr != &blobChrStr); |
| 298 | assertTrue (blobNumStr.content() != blobChrStr.content()); |
| 299 | assertTrue (&blobNumStr.content() != &blobChrStr.content()); |
| 300 | assertTrue (blobNumStr == blobChrVec); |
| 301 | |
| 302 | Poco::SQL::swap(blobNumStr, blobChrVec); |
| 303 | assertTrue (blobNumStr == blobChrVec); |
| 304 | std::swap(blobNumStr, blobChrVec); |
| 305 | assertTrue (blobNumStr == blobChrVec); |
| 306 | |
| 307 | assertTrue (blobChrStr != blobNumStr); |
| 308 | Var vLOB = blobNumStr; |
| 309 | std::string sss = vLOB.convert<std::string>(); |
| 310 | blobChrStr = CLOB(sss); |
| 311 | assertTrue (blobChrStr == blobNumStr); |
| 312 | |
| 313 | std::string xyz = "xyz" ; |
| 314 | vLOB = xyz; |
| 315 | blobChrStr = sss = vLOB.convert<std::string>(); |
| 316 | assertTrue (0 == std::strncmp(xyz.c_str(), blobChrStr.rawContent(), blobChrStr.size())); |
| 317 | } |
| 318 | |
| 319 | |
| 320 | void SQLTest::testCLOBStreams() |
| 321 | { |
| 322 | CLOB blob; |
| 323 | assertTrue (0 == blob.size()); |
| 324 | |
| 325 | CLOBOutputStream bos(blob); |
| 326 | BinaryWriter bw(bos); |
| 327 | |
| 328 | assertTrue (0 == blob.size()); |
| 329 | writeToCLOB(bw); |
| 330 | assertTrue (blob.size() > 0); |
| 331 | |
| 332 | CLOBInputStream bis(blob); |
| 333 | BinaryReader br(bis); |
| 334 | |
| 335 | readFromCLOB(br); |
| 336 | } |
| 337 | |
| 338 | |
| 339 | void SQLTest::writeToCLOB(BinaryWriter& writer) |
| 340 | { |
| 341 | writer << true; |
| 342 | writer << false; |
| 343 | writer << 'a'; |
| 344 | |
| 345 | writer << (short) -100; |
| 346 | writer << (unsigned short) 50000; |
| 347 | writer << -123456; |
| 348 | writer << (unsigned) 123456; |
| 349 | #ifndef POCO_LONG_IS_64_BIT |
| 350 | writer << (long) -1234567890; |
| 351 | writer << (unsigned long) 1234567890; |
| 352 | #endif // POCO_LONG_IS_64_BIT |
| 353 | writer << (Int64) -1234567890; |
| 354 | writer << (UInt64) 1234567890; |
| 355 | |
| 356 | writer << (float) 1.5; |
| 357 | writer << (double) -1.5; |
| 358 | |
| 359 | writer << "foo" ; |
| 360 | writer << "" ; |
| 361 | |
| 362 | writer << std::string("bar" ); |
| 363 | writer << std::string(); |
| 364 | |
| 365 | writer.write7BitEncoded((UInt32) 100); |
| 366 | writer.write7BitEncoded((UInt32) 1000); |
| 367 | writer.write7BitEncoded((UInt32) 10000); |
| 368 | writer.write7BitEncoded((UInt32) 100000); |
| 369 | writer.write7BitEncoded((UInt32) 1000000); |
| 370 | |
| 371 | writer.write7BitEncoded((UInt64) 100); |
| 372 | writer.write7BitEncoded((UInt64) 1000); |
| 373 | writer.write7BitEncoded((UInt64) 10000); |
| 374 | writer.write7BitEncoded((UInt64) 100000); |
| 375 | writer.write7BitEncoded((UInt64) 1000000); |
| 376 | |
| 377 | writer.writeRaw("RAW" ); |
| 378 | } |
| 379 | |
| 380 | |
| 381 | void SQLTest::readFromCLOB(BinaryReader& reader) |
| 382 | { |
| 383 | bool b = false; |
| 384 | reader >> b; |
| 385 | assertTrue (b); |
| 386 | reader >> b; |
| 387 | assertTrue (!b); |
| 388 | |
| 389 | char c = ' '; |
| 390 | reader >> c; |
| 391 | assertTrue (c == 'a'); |
| 392 | |
| 393 | short shortv = 0; |
| 394 | reader >> shortv; |
| 395 | assertTrue (shortv == -100); |
| 396 | |
| 397 | unsigned short ushortv = 0; |
| 398 | reader >> ushortv; |
| 399 | assertTrue (ushortv == 50000); |
| 400 | |
| 401 | int intv = 0; |
| 402 | reader >> intv; |
| 403 | assertTrue (intv == -123456); |
| 404 | |
| 405 | unsigned uintv = 0; |
| 406 | reader >> uintv; |
| 407 | assertTrue (uintv == 123456); |
| 408 | #ifndef POCO_LONG_IS_64_BIT |
| 409 | long longv = 0; |
| 410 | reader >> longv; |
| 411 | assertTrue (longv == -1234567890); |
| 412 | |
| 413 | unsigned long ulongv = 0; |
| 414 | reader >> ulongv; |
| 415 | assertTrue (ulongv == 1234567890); |
| 416 | #endif // POCO_LONG_IS_64_BIT |
| 417 | Int64 int64v = 0; |
| 418 | reader >> int64v; |
| 419 | assertTrue (int64v == -1234567890); |
| 420 | |
| 421 | UInt64 uint64v = 0; |
| 422 | reader >> uint64v; |
| 423 | assertTrue (uint64v == 1234567890); |
| 424 | |
| 425 | float floatv = 0.0; |
| 426 | reader >> floatv; |
| 427 | assertTrue (floatv == 1.5); |
| 428 | |
| 429 | double doublev = 0.0; |
| 430 | reader >> doublev; |
| 431 | assertTrue (doublev == -1.5); |
| 432 | |
| 433 | std::string str; |
| 434 | reader >> str; |
| 435 | assertTrue (str == "foo" ); |
| 436 | reader >> str; |
| 437 | assertTrue (str == "" ); |
| 438 | |
| 439 | reader >> str; |
| 440 | assertTrue (str == "bar" ); |
| 441 | reader >> str; |
| 442 | assertTrue (str == "" ); |
| 443 | |
| 444 | UInt32 uint32v; |
| 445 | reader.read7BitEncoded(uint32v); |
| 446 | assertTrue (uint32v == 100); |
| 447 | reader.read7BitEncoded(uint32v); |
| 448 | assertTrue (uint32v == 1000); |
| 449 | reader.read7BitEncoded(uint32v); |
| 450 | assertTrue (uint32v == 10000); |
| 451 | reader.read7BitEncoded(uint32v); |
| 452 | assertTrue (uint32v == 100000); |
| 453 | reader.read7BitEncoded(uint32v); |
| 454 | assertTrue (uint32v == 1000000); |
| 455 | |
| 456 | reader.read7BitEncoded(uint64v); |
| 457 | assertTrue (uint64v == 100); |
| 458 | reader.read7BitEncoded(uint64v); |
| 459 | assertTrue (uint64v == 1000); |
| 460 | reader.read7BitEncoded(uint64v); |
| 461 | assertTrue (uint64v == 10000); |
| 462 | reader.read7BitEncoded(uint64v); |
| 463 | assertTrue (uint64v == 100000); |
| 464 | reader.read7BitEncoded(uint64v); |
| 465 | assertTrue (uint64v == 1000000); |
| 466 | |
| 467 | reader.readRaw(3, str); |
| 468 | assertTrue (str == "RAW" ); |
| 469 | } |
| 470 | |
| 471 | |
| 472 | void SQLTest::testColumnVector() |
| 473 | { |
| 474 | MetaColumn mc(0, "mc" , MetaColumn::FDT_DOUBLE, 2, 3, true); |
| 475 | |
| 476 | assertTrue (mc.name() == "mc" ); |
| 477 | assertTrue (mc.position() == 0); |
| 478 | assertTrue (mc.length() == 2); |
| 479 | assertTrue (mc.precision() == 3); |
| 480 | assertTrue (mc.type() == MetaColumn::FDT_DOUBLE); |
| 481 | assertTrue (mc.isNullable()); |
| 482 | |
| 483 | std::vector<int>* pData = new std::vector<int>; |
| 484 | pData->push_back(1); |
| 485 | pData->push_back(2); |
| 486 | pData->push_back(3); |
| 487 | pData->push_back(4); |
| 488 | pData->push_back(5); |
| 489 | |
| 490 | Column<std::vector<int> > c(mc, pData); |
| 491 | |
| 492 | assertTrue (c.rowCount() == 5); |
| 493 | assertTrue (c[0] == 1); |
| 494 | assertTrue (c[1] == 2); |
| 495 | assertTrue (c[2] == 3); |
| 496 | assertTrue (c[3] == 4); |
| 497 | assertTrue (c[4] == 5); |
| 498 | assertTrue (c.name() == "mc" ); |
| 499 | assertTrue (c.position() == 0); |
| 500 | assertTrue (c.length() == 2); |
| 501 | assertTrue (c.precision() == 3); |
| 502 | assertTrue (c.type() == MetaColumn::FDT_DOUBLE); |
| 503 | |
| 504 | try |
| 505 | { |
| 506 | POCO_UNUSED int i; i = c[100]; // to silence gcc |
| 507 | fail ("must fail" ); |
| 508 | } |
| 509 | catch (RangeException&) { } |
| 510 | |
| 511 | Column<std::vector<int> > c1 = c; |
| 512 | |
| 513 | assertTrue (c1.rowCount() == 5); |
| 514 | assertTrue (c1[0] == 1); |
| 515 | assertTrue (c1[1] == 2); |
| 516 | assertTrue (c1[2] == 3); |
| 517 | assertTrue (c1[3] == 4); |
| 518 | assertTrue (c1[4] == 5); |
| 519 | |
| 520 | Column<std::vector<int> > c2(c1); |
| 521 | |
| 522 | assertTrue (c2.rowCount() == 5); |
| 523 | assertTrue (c2[0] == 1); |
| 524 | assertTrue (c2[1] == 2); |
| 525 | assertTrue (c2[2] == 3); |
| 526 | assertTrue (c2[3] == 4); |
| 527 | assertTrue (c2[4] == 5); |
| 528 | |
| 529 | std::vector<int> vi; |
| 530 | vi.assign(c.begin(), c.end()); |
| 531 | assertTrue (vi.size() == 5); |
| 532 | assertTrue (vi[0] == 1); |
| 533 | assertTrue (vi[1] == 2); |
| 534 | assertTrue (vi[2] == 3); |
| 535 | assertTrue (vi[3] == 4); |
| 536 | assertTrue (vi[4] == 5); |
| 537 | |
| 538 | c.reset(); |
| 539 | assertTrue (c.rowCount() == 0); |
| 540 | assertTrue (c1.rowCount() == 0); |
| 541 | assertTrue (c2.rowCount() == 0); |
| 542 | |
| 543 | std::vector<int>* pV1 = new std::vector<int>; |
| 544 | pV1->push_back(1); |
| 545 | pV1->push_back(2); |
| 546 | pV1->push_back(3); |
| 547 | pV1->push_back(4); |
| 548 | pV1->push_back(5); |
| 549 | std::vector<int>* pV2 = new std::vector<int>; |
| 550 | pV2->push_back(5); |
| 551 | pV2->push_back(4); |
| 552 | pV2->push_back(3); |
| 553 | pV2->push_back(2); |
| 554 | pV2->push_back(1); |
| 555 | Column<std::vector<int> > c3(mc, pV1); |
| 556 | Column<std::vector<int> > c4(mc, pV2); |
| 557 | |
| 558 | Poco::SQL::swap(c3, c4); |
| 559 | assertTrue (c3[0] == 5); |
| 560 | assertTrue (c3[1] == 4); |
| 561 | assertTrue (c3[2] == 3); |
| 562 | assertTrue (c3[3] == 2); |
| 563 | assertTrue (c3[4] == 1); |
| 564 | |
| 565 | assertTrue (c4[0] == 1); |
| 566 | assertTrue (c4[1] == 2); |
| 567 | assertTrue (c4[2] == 3); |
| 568 | assertTrue (c4[3] == 4); |
| 569 | assertTrue (c4[4] == 5); |
| 570 | |
| 571 | std::swap(c3, c4); |
| 572 | assertTrue (c3[0] == 1); |
| 573 | assertTrue (c3[1] == 2); |
| 574 | assertTrue (c3[2] == 3); |
| 575 | assertTrue (c3[3] == 4); |
| 576 | assertTrue (c3[4] == 5); |
| 577 | |
| 578 | assertTrue (c4[0] == 5); |
| 579 | assertTrue (c4[1] == 4); |
| 580 | assertTrue (c4[2] == 3); |
| 581 | assertTrue (c4[3] == 2); |
| 582 | assertTrue (c4[4] == 1); |
| 583 | } |
| 584 | |
| 585 | |
| 586 | void SQLTest::testColumnVectorBool() |
| 587 | { |
| 588 | MetaColumn mc(0, "mc" , MetaColumn::FDT_BOOL); |
| 589 | |
| 590 | std::vector<bool>* pData = new std::vector<bool>; |
| 591 | pData->push_back(true); |
| 592 | pData->push_back(false); |
| 593 | pData->push_back(true); |
| 594 | pData->push_back(false); |
| 595 | pData->push_back(true); |
| 596 | |
| 597 | Column<std::vector<bool> > c(mc, pData); |
| 598 | |
| 599 | assertTrue (c.rowCount() == 5); |
| 600 | assertTrue (c[0] == true); |
| 601 | assertTrue (c[1] == false); |
| 602 | assertTrue (c[2] == true); |
| 603 | assertTrue (c[3] == false); |
| 604 | assertTrue (c[4] == true); |
| 605 | assertTrue (c.type() == MetaColumn::FDT_BOOL); |
| 606 | |
| 607 | try |
| 608 | { |
| 609 | POCO_UNUSED bool b; b = c[100]; // to silence gcc |
| 610 | fail ("must fail" ); |
| 611 | } |
| 612 | catch (RangeException&) { } |
| 613 | |
| 614 | Column<std::vector<bool> > c1 = c; |
| 615 | |
| 616 | assertTrue (c1.rowCount() == 5); |
| 617 | assertTrue (c1[0] == true); |
| 618 | assertTrue (c1[1] == false); |
| 619 | assertTrue (c1[2] == true); |
| 620 | assertTrue (c1[3] == false); |
| 621 | assertTrue (c1[4] == true); |
| 622 | |
| 623 | Column<std::vector<bool> > c2(c1); |
| 624 | |
| 625 | assertTrue (c2.rowCount() == 5); |
| 626 | assertTrue (c2[0] == true); |
| 627 | assertTrue (c2[1] == false); |
| 628 | assertTrue (c2[2] == true); |
| 629 | assertTrue (c2[3] == false); |
| 630 | assertTrue (c2[4] == true); |
| 631 | |
| 632 | std::vector<bool> vi; |
| 633 | vi.assign(c.begin(), c.end()); |
| 634 | assertTrue (vi.size() == 5); |
| 635 | assertTrue (vi[0] == true); |
| 636 | assertTrue (vi[1] == false); |
| 637 | assertTrue (vi[2] == true); |
| 638 | assertTrue (vi[3] == false); |
| 639 | assertTrue (vi[4] == true); |
| 640 | |
| 641 | c.reset(); |
| 642 | assertTrue (c.rowCount() == 0); |
| 643 | assertTrue (c1.rowCount() == 0); |
| 644 | assertTrue (c2.rowCount() == 0); |
| 645 | } |
| 646 | |
| 647 | |
| 648 | void SQLTest::testColumnDeque() |
| 649 | { |
| 650 | typedef std::deque<int> ContainerType; |
| 651 | typedef Column<ContainerType> ColumnType; |
| 652 | |
| 653 | MetaColumn mc(0, "mc" , MetaColumn::FDT_DOUBLE, 2, 3, true); |
| 654 | |
| 655 | assertTrue (mc.name() == "mc" ); |
| 656 | assertTrue (mc.position() == 0); |
| 657 | assertTrue (mc.length() == 2); |
| 658 | assertTrue (mc.precision() == 3); |
| 659 | assertTrue (mc.type() == MetaColumn::FDT_DOUBLE); |
| 660 | assertTrue (mc.isNullable()); |
| 661 | |
| 662 | ContainerType* pData = new ContainerType; |
| 663 | pData->push_back(1); |
| 664 | pData->push_back(2); |
| 665 | pData->push_back(3); |
| 666 | pData->push_back(4); |
| 667 | pData->push_back(5); |
| 668 | |
| 669 | ColumnType c(mc, pData); |
| 670 | |
| 671 | assertTrue (c.rowCount() == 5); |
| 672 | assertTrue (c[0] == 1); |
| 673 | assertTrue (c[1] == 2); |
| 674 | assertTrue (c[2] == 3); |
| 675 | assertTrue (c[3] == 4); |
| 676 | assertTrue (c[4] == 5); |
| 677 | assertTrue (c.name() == "mc" ); |
| 678 | assertTrue (c.position() == 0); |
| 679 | assertTrue (c.length() == 2); |
| 680 | assertTrue (c.precision() == 3); |
| 681 | assertTrue (c.type() == MetaColumn::FDT_DOUBLE); |
| 682 | |
| 683 | try |
| 684 | { |
| 685 | POCO_UNUSED int i; i = c[100]; // to silence gcc |
| 686 | fail ("must fail" ); |
| 687 | } |
| 688 | catch (RangeException&) { } |
| 689 | |
| 690 | ColumnType c1 = c; |
| 691 | |
| 692 | assertTrue (c1.rowCount() == 5); |
| 693 | assertTrue (c1[0] == 1); |
| 694 | assertTrue (c1[1] == 2); |
| 695 | assertTrue (c1[2] == 3); |
| 696 | assertTrue (c1[3] == 4); |
| 697 | assertTrue (c1[4] == 5); |
| 698 | |
| 699 | ColumnType c2(c1); |
| 700 | |
| 701 | assertTrue (c2.rowCount() == 5); |
| 702 | assertTrue (c2[0] == 1); |
| 703 | assertTrue (c2[1] == 2); |
| 704 | assertTrue (c2[2] == 3); |
| 705 | assertTrue (c2[3] == 4); |
| 706 | assertTrue (c2[4] == 5); |
| 707 | |
| 708 | ContainerType vi; |
| 709 | vi.assign(c.begin(), c.end()); |
| 710 | assertTrue (vi.size() == 5); |
| 711 | assertTrue (vi[0] == 1); |
| 712 | assertTrue (vi[1] == 2); |
| 713 | assertTrue (vi[2] == 3); |
| 714 | assertTrue (vi[3] == 4); |
| 715 | assertTrue (vi[4] == 5); |
| 716 | |
| 717 | c.reset(); |
| 718 | assertTrue (c.rowCount() == 0); |
| 719 | assertTrue (c1.rowCount() == 0); |
| 720 | assertTrue (c2.rowCount() == 0); |
| 721 | |
| 722 | ContainerType* pV1 = new ContainerType; |
| 723 | pV1->push_back(1); |
| 724 | pV1->push_back(2); |
| 725 | pV1->push_back(3); |
| 726 | pV1->push_back(4); |
| 727 | pV1->push_back(5); |
| 728 | ContainerType* pV2 = new ContainerType; |
| 729 | pV2->push_back(5); |
| 730 | pV2->push_back(4); |
| 731 | pV2->push_back(3); |
| 732 | pV2->push_back(2); |
| 733 | pV2->push_back(1); |
| 734 | Column<ContainerType> c3(mc, pV1); |
| 735 | Column<ContainerType> c4(mc, pV2); |
| 736 | |
| 737 | Poco::SQL::swap(c3, c4); |
| 738 | assertTrue (c3[0] == 5); |
| 739 | assertTrue (c3[1] == 4); |
| 740 | assertTrue (c3[2] == 3); |
| 741 | assertTrue (c3[3] == 2); |
| 742 | assertTrue (c3[4] == 1); |
| 743 | |
| 744 | assertTrue (c4[0] == 1); |
| 745 | assertTrue (c4[1] == 2); |
| 746 | assertTrue (c4[2] == 3); |
| 747 | assertTrue (c4[3] == 4); |
| 748 | assertTrue (c4[4] == 5); |
| 749 | |
| 750 | std::swap(c3, c4); |
| 751 | assertTrue (c3[0] == 1); |
| 752 | assertTrue (c3[1] == 2); |
| 753 | assertTrue (c3[2] == 3); |
| 754 | assertTrue (c3[3] == 4); |
| 755 | assertTrue (c3[4] == 5); |
| 756 | |
| 757 | assertTrue (c4[0] == 5); |
| 758 | assertTrue (c4[1] == 4); |
| 759 | assertTrue (c4[2] == 3); |
| 760 | assertTrue (c4[3] == 2); |
| 761 | assertTrue (c4[4] == 1); |
| 762 | } |
| 763 | |
| 764 | |
| 765 | void SQLTest::testColumnList() |
| 766 | { |
| 767 | typedef std::list<int> ContainerType; |
| 768 | typedef Column<ContainerType> ColumnType; |
| 769 | |
| 770 | MetaColumn mc(0, "mc" , MetaColumn::FDT_DOUBLE, 2, 3, true); |
| 771 | |
| 772 | assertTrue (mc.name() == "mc" ); |
| 773 | assertTrue (mc.position() == 0); |
| 774 | assertTrue (mc.length() == 2); |
| 775 | assertTrue (mc.precision() == 3); |
| 776 | assertTrue (mc.type() == MetaColumn::FDT_DOUBLE); |
| 777 | assertTrue (mc.isNullable()); |
| 778 | |
| 779 | ContainerType* pData = new ContainerType; |
| 780 | pData->push_back(1); |
| 781 | pData->push_back(2); |
| 782 | pData->push_back(3); |
| 783 | pData->push_back(4); |
| 784 | pData->push_back(5); |
| 785 | |
| 786 | ColumnType c(mc, pData); |
| 787 | |
| 788 | assertTrue (c.rowCount() == 5); |
| 789 | assertTrue (c[0] == 1); |
| 790 | assertTrue (c[1] == 2); |
| 791 | assertTrue (c[2] == 3); |
| 792 | assertTrue (c[3] == 4); |
| 793 | assertTrue (c[4] == 5); |
| 794 | assertTrue (c.name() == "mc" ); |
| 795 | assertTrue (c.position() == 0); |
| 796 | assertTrue (c.length() == 2); |
| 797 | assertTrue (c.precision() == 3); |
| 798 | assertTrue (c.type() == MetaColumn::FDT_DOUBLE); |
| 799 | |
| 800 | try |
| 801 | { |
| 802 | POCO_UNUSED int i; i = c[100]; // to silence gcc |
| 803 | fail ("must fail" ); |
| 804 | } |
| 805 | catch (RangeException&) { } |
| 806 | |
| 807 | ColumnType c1 = c; |
| 808 | |
| 809 | assertTrue (c1.rowCount() == 5); |
| 810 | assertTrue (c1[0] == 1); |
| 811 | assertTrue (c1[1] == 2); |
| 812 | assertTrue (c1[2] == 3); |
| 813 | assertTrue (c1[3] == 4); |
| 814 | assertTrue (c1[4] == 5); |
| 815 | |
| 816 | ColumnType c2(c1); |
| 817 | assertTrue (c2.rowCount() == 5); |
| 818 | assertTrue (c2[0] == 1); |
| 819 | assertTrue (c2[1] == 2); |
| 820 | assertTrue (c2[2] == 3); |
| 821 | assertTrue (c2[3] == 4); |
| 822 | assertTrue (c2[4] == 5); |
| 823 | |
| 824 | ContainerType vi; |
| 825 | vi.assign(c.begin(), c.end()); |
| 826 | assertTrue (vi.size() == 5); |
| 827 | ContainerType::const_iterator it = vi.begin(); |
| 828 | ContainerType::const_iterator end = vi.end(); |
| 829 | for (int i = 1; it != end; ++it, ++i) |
| 830 | assertTrue (*it == i); |
| 831 | |
| 832 | c.reset(); |
| 833 | assertTrue (c.rowCount() == 0); |
| 834 | assertTrue (c1.rowCount() == 0); |
| 835 | assertTrue (c2.rowCount() == 0); |
| 836 | |
| 837 | ContainerType* pV1 = new ContainerType; |
| 838 | pV1->push_back(1); |
| 839 | pV1->push_back(2); |
| 840 | pV1->push_back(3); |
| 841 | pV1->push_back(4); |
| 842 | pV1->push_back(5); |
| 843 | ContainerType* pV2 = new ContainerType; |
| 844 | pV2->push_back(5); |
| 845 | pV2->push_back(4); |
| 846 | pV2->push_back(3); |
| 847 | pV2->push_back(2); |
| 848 | pV2->push_back(1); |
| 849 | Column<ContainerType> c3(mc, pV1); |
| 850 | Column<ContainerType> c4(mc, pV2); |
| 851 | |
| 852 | Poco::SQL::swap(c3, c4); |
| 853 | assertTrue (c3[0] == 5); |
| 854 | assertTrue (c3[1] == 4); |
| 855 | assertTrue (c3[2] == 3); |
| 856 | assertTrue (c3[3] == 2); |
| 857 | assertTrue (c3[4] == 1); |
| 858 | |
| 859 | assertTrue (c4[0] == 1); |
| 860 | assertTrue (c4[1] == 2); |
| 861 | assertTrue (c4[2] == 3); |
| 862 | assertTrue (c4[3] == 4); |
| 863 | assertTrue (c4[4] == 5); |
| 864 | |
| 865 | std::swap(c3, c4); |
| 866 | assertTrue (c3[0] == 1); |
| 867 | assertTrue (c3[1] == 2); |
| 868 | assertTrue (c3[2] == 3); |
| 869 | assertTrue (c3[3] == 4); |
| 870 | assertTrue (c3[4] == 5); |
| 871 | |
| 872 | assertTrue (c4[0] == 5); |
| 873 | assertTrue (c4[1] == 4); |
| 874 | assertTrue (c4[2] == 3); |
| 875 | assertTrue (c4[3] == 2); |
| 876 | assertTrue (c4[4] == 1); |
| 877 | } |
| 878 | |
| 879 | |
| 880 | void SQLTest::testRow() |
| 881 | { |
| 882 | Row row; |
| 883 | row.append("field0" , 0); |
| 884 | row.append("field1" , 1); |
| 885 | row.append("field2" , 2); |
| 886 | row.append("field3" , 3); |
| 887 | row.append("field4" , 4); |
| 888 | |
| 889 | assertTrue (row["field0" ] == 0); |
| 890 | assertTrue (row["field1" ] == 1); |
| 891 | assertTrue (row["field2" ] == 2); |
| 892 | assertTrue (row["field3" ] == 3); |
| 893 | assertTrue (row["field4" ] == 4); |
| 894 | |
| 895 | assertTrue (row["FIELD0" ] == 0); |
| 896 | assertTrue (row["FIELD1" ] == 1); |
| 897 | assertTrue (row["FIELD2" ] == 2); |
| 898 | assertTrue (row["FIELD3" ] == 3); |
| 899 | assertTrue (row["FIELD4" ] == 4); |
| 900 | |
| 901 | assertTrue (row[0] == 0); |
| 902 | assertTrue (row[1] == 1); |
| 903 | assertTrue (row[2] == 2); |
| 904 | assertTrue (row[3] == 3); |
| 905 | assertTrue (row[4] == 4); |
| 906 | |
| 907 | const Row& cr = row; |
| 908 | assertTrue (cr["field0" ] == 0); |
| 909 | assertTrue (cr[0] == 0); |
| 910 | assertTrue (cr.get(0) == 0); |
| 911 | |
| 912 | try |
| 913 | { |
| 914 | POCO_UNUSED int i; i = row[5].convert<int>(); // to silence gcc |
| 915 | fail ("must fail" ); |
| 916 | }catch (RangeException&) {} |
| 917 | |
| 918 | try |
| 919 | { |
| 920 | POCO_UNUSED int i; i = row["a bad name" ].convert<int>(); // to silence gcc |
| 921 | fail ("must fail" ); |
| 922 | }catch (NotFoundException&) {} |
| 923 | |
| 924 | assertTrue (5 == row.fieldCount()); |
| 925 | assertTrue (row[0] == 0); |
| 926 | assertTrue (row["field0" ] == 0); |
| 927 | assertTrue (row[1] == 1); |
| 928 | assertTrue (row["field1" ] == 1); |
| 929 | assertTrue (row[2] == 2); |
| 930 | assertTrue (row["field2" ] == 2); |
| 931 | assertTrue (row[3] == 3); |
| 932 | assertTrue (row["field3" ] == 3); |
| 933 | assertTrue (row[4] == 4); |
| 934 | assertTrue (row["field4" ] == 4); |
| 935 | |
| 936 | Row row2; |
| 937 | |
| 938 | row2.append("field0" , 5); |
| 939 | row2.append("field1" , 4); |
| 940 | row2.append("field2" , 3); |
| 941 | row2.append("field3" , 2); |
| 942 | row2.append("field4" , 1); |
| 943 | |
| 944 | assertTrue (row != row2); |
| 945 | |
| 946 | Row row3; |
| 947 | |
| 948 | row3.append("field0" , 0); |
| 949 | row3.append("field1" , 1); |
| 950 | row3.append("field2" , 2); |
| 951 | row3.append("field3" , 3); |
| 952 | row3.append("field4" , 4); |
| 953 | |
| 954 | assertTrue (row3 == row); |
| 955 | assertTrue (!(row < row3 || row3 < row)); |
| 956 | |
| 957 | Row row4(row3.names()); |
| 958 | try |
| 959 | { |
| 960 | row4.set("badfieldname" , 0); |
| 961 | fail ("must fail" ); |
| 962 | }catch (NotFoundException&) {} |
| 963 | |
| 964 | row4.set("field0" , 0); |
| 965 | row4.set("field1" , 1); |
| 966 | row4.set("field2" , 2); |
| 967 | row4.set("field3" , 3); |
| 968 | row4.set("field4" , 4); |
| 969 | assertTrue (row3 == row4); |
| 970 | try |
| 971 | { |
| 972 | row4.set(5, 0); |
| 973 | fail ("must fail" ); |
| 974 | }catch (RangeException&) {} |
| 975 | row4.set("field0" , 1); |
| 976 | assertTrue (row3 != row4); |
| 977 | assertTrue (row3 < row4); |
| 978 | } |
| 979 | |
| 980 | |
| 981 | void SQLTest::testRowSort() |
| 982 | { |
| 983 | Row row1; |
| 984 | row1.append("0" , 0); |
| 985 | row1.append("1" , 1); |
| 986 | row1.append("2" , 2); |
| 987 | row1.append("3" , 3); |
| 988 | row1.append("4" , 4); |
| 989 | |
| 990 | Row row2; |
| 991 | row2.append("0" , 0); |
| 992 | row2.append("1" , 1); |
| 993 | row2.append("2" , 2); |
| 994 | row2.append("3" , 3); |
| 995 | row2.append("4" , 4); |
| 996 | |
| 997 | std::multiset<Row> rowSet1; |
| 998 | rowSet1.insert(row1); |
| 999 | rowSet1.insert(row2); |
| 1000 | std::multiset<Row>::iterator it1 = rowSet1.begin(); |
| 1001 | assertTrue (row1 == *it1); |
| 1002 | ++it1; |
| 1003 | assertTrue (row2 == *it1); |
| 1004 | |
| 1005 | Row row3; |
| 1006 | row3.append("0" , 1); |
| 1007 | row3.append("1" , 1); |
| 1008 | row3.append("2" , 2); |
| 1009 | row3.append("3" , 3); |
| 1010 | row3.append("4" , 4); |
| 1011 | |
| 1012 | Row row4; |
| 1013 | row4.append("0" , 0); |
| 1014 | row4.append("1" , 1); |
| 1015 | row4.append("2" , 2); |
| 1016 | row4.append("3" , 3); |
| 1017 | row4.append("4" , 4); |
| 1018 | |
| 1019 | std::set<Row> rowSet2; |
| 1020 | rowSet2.insert(row4); |
| 1021 | rowSet2.insert(row3); |
| 1022 | std::set<Row>::iterator it2 = rowSet2.begin(); |
| 1023 | assertTrue (row4 == *it2); |
| 1024 | ++it2; |
| 1025 | assertTrue (row3 == *it2); |
| 1026 | |
| 1027 | Row row5; |
| 1028 | row5.append("0" , 2); |
| 1029 | row5.append("1" , 2); |
| 1030 | row5.append("2" , 0); |
| 1031 | row5.append("3" , 3); |
| 1032 | row5.append("4" , 4); |
| 1033 | row5.addSortField("1" ); |
| 1034 | |
| 1035 | Row row6; |
| 1036 | row6.append("0" , 1); |
| 1037 | row6.append("1" , 0); |
| 1038 | row6.append("2" , 1); |
| 1039 | row6.append("3" , 3); |
| 1040 | row6.append("4" , 4); |
| 1041 | row6.addSortField("1" ); |
| 1042 | |
| 1043 | Row row7; |
| 1044 | row7.append("0" , 0); |
| 1045 | row7.append("1" , 1); |
| 1046 | row7.append("2" , 2); |
| 1047 | row7.append("3" , 3); |
| 1048 | row7.append("4" , 4); |
| 1049 | |
| 1050 | std::set<Row> rowSet3; |
| 1051 | rowSet3.insert(row5); |
| 1052 | rowSet3.insert(row6); |
| 1053 | try |
| 1054 | { |
| 1055 | rowSet3.insert(row7);//has no same sort criteria |
| 1056 | fail ("must fail" ); |
| 1057 | } catch (InvalidAccessException&) {} |
| 1058 | |
| 1059 | row7.addSortField("1" ); |
| 1060 | testRowStrictWeak(row7, row6, row5); |
| 1061 | rowSet3.insert(row7); |
| 1062 | |
| 1063 | std::set<Row>::iterator it3 = rowSet3.begin(); |
| 1064 | assertTrue (row7 == *it3); |
| 1065 | ++it3; |
| 1066 | assertTrue (row6 == *it3); |
| 1067 | ++it3; |
| 1068 | assertTrue (row5 == *it3); |
| 1069 | |
| 1070 | row5.replaceSortField("0" , "2" ); |
| 1071 | row6.replaceSortField("0" , "2" ); |
| 1072 | row7.replaceSortField("0" , "2" ); |
| 1073 | |
| 1074 | rowSet3.clear(); |
| 1075 | rowSet3.insert(row7); |
| 1076 | rowSet3.insert(row6); |
| 1077 | rowSet3.insert(row5); |
| 1078 | |
| 1079 | it3 = rowSet3.begin(); |
| 1080 | assertTrue (row5 == *it3); |
| 1081 | ++it3; |
| 1082 | assertTrue (row6 == *it3); |
| 1083 | ++it3; |
| 1084 | assertTrue (row7 == *it3); |
| 1085 | |
| 1086 | row5.resetSort(); |
| 1087 | row6.resetSort(); |
| 1088 | row7.resetSort(); |
| 1089 | |
| 1090 | rowSet3.clear(); |
| 1091 | rowSet3.insert(row5); |
| 1092 | rowSet3.insert(row6); |
| 1093 | rowSet3.insert(row7); |
| 1094 | |
| 1095 | it3 = rowSet3.begin(); |
| 1096 | assertTrue (row7 == *it3); |
| 1097 | ++it3; |
| 1098 | assertTrue (row6 == *it3); |
| 1099 | ++it3; |
| 1100 | assertTrue (row5 == *it3); |
| 1101 | |
| 1102 | Row row8; |
| 1103 | row8.append("0" , "2" ); |
| 1104 | row8.append("1" , "2" ); |
| 1105 | row8.append("2" , "0" ); |
| 1106 | row8.append("3" , "3" ); |
| 1107 | row8.append("4" , "4" ); |
| 1108 | row8.addSortField("1" ); |
| 1109 | |
| 1110 | Row row9; |
| 1111 | row9.append("0" , "1" ); |
| 1112 | row9.append("1" , "0" ); |
| 1113 | row9.append("2" , "1" ); |
| 1114 | row9.append("3" , "3" ); |
| 1115 | row9.append("4" , "4" ); |
| 1116 | row9.addSortField("1" ); |
| 1117 | |
| 1118 | Row row10; |
| 1119 | row10.append("0" , "0" ); |
| 1120 | row10.append("1" , "1" ); |
| 1121 | row10.append("2" , "2" ); |
| 1122 | row10.append("3" , "3" ); |
| 1123 | row10.append("4" , "4" ); |
| 1124 | row10.addSortField("1" ); |
| 1125 | |
| 1126 | testRowStrictWeak(row10, row9, row8); |
| 1127 | |
| 1128 | |
| 1129 | Row row11; |
| 1130 | row11.append("0" , 2.5); |
| 1131 | row11.append("1" , 2.5); |
| 1132 | row11.append("2" , 0.5); |
| 1133 | row11.append("3" , 3.5); |
| 1134 | row11.append("4" , 4.5); |
| 1135 | row11.addSortField("1" ); |
| 1136 | |
| 1137 | Row row12; |
| 1138 | row12.append("0" , 1.5); |
| 1139 | row12.append("1" , 0.5); |
| 1140 | row12.append("2" , 1.5); |
| 1141 | row12.append("3" , 3.5); |
| 1142 | row12.append("4" , 4.5); |
| 1143 | row12.addSortField("1" ); |
| 1144 | |
| 1145 | Row row13; |
| 1146 | row13.append("0" , 0.5); |
| 1147 | row13.append("1" , 1.5); |
| 1148 | row13.append("2" , 2.5); |
| 1149 | row13.append("3" , 3.5); |
| 1150 | row13.append("4" , 4.5); |
| 1151 | row13.addSortField("1" ); |
| 1152 | |
| 1153 | testRowStrictWeak(row13, row12, row11); |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | void SQLTest::testRowStrictWeak(const Row& row1, const Row& row2, const Row& row3) |
| 1158 | { |
| 1159 | assertTrue (row1 < row2 && !(row2 < row1)); // antisymmetric |
| 1160 | assertTrue (row1 < row2 && row2 < row3 && row1 < row3); // transitive |
| 1161 | assertTrue (!(row1 < row1)); // irreflexive |
| 1162 | } |
| 1163 | |
| 1164 | |
| 1165 | void SQLTest::testSimpleRowFormatter() |
| 1166 | { |
| 1167 | Row row1; |
| 1168 | row1.append("field0" , 0); |
| 1169 | row1.append("field1" , 1); |
| 1170 | row1.append("field2" , 2); |
| 1171 | row1.append("field3" , 3); |
| 1172 | row1.append("field4" , 4); |
| 1173 | |
| 1174 | SimpleRowFormatter rf; |
| 1175 | std::streamsize sz = rf.getColumnWidth(); |
| 1176 | std::streamsize sp = rf.getSpacing(); |
| 1177 | |
| 1178 | std::string line(std::string::size_type(sz * 5 + sp * 4), '-'); |
| 1179 | std::string spacer(static_cast<std::size_t>(sp), ' '); |
| 1180 | std::ostringstream os; |
| 1181 | os << std::left |
| 1182 | << std::setw(sz) << "field0" |
| 1183 | << spacer |
| 1184 | << std::setw(sz) << "field1" |
| 1185 | << spacer |
| 1186 | << std::setw(sz) << "field2" |
| 1187 | << spacer |
| 1188 | << std::setw(sz) << "field3" |
| 1189 | << spacer |
| 1190 | << std::setw(sz) << "field4" << std::endl |
| 1191 | << line << std::endl; |
| 1192 | assertTrue (row1.namesToString() == os.str()); |
| 1193 | |
| 1194 | os.str("" ); |
| 1195 | os << std::right |
| 1196 | << std::setw(sz) << "0" |
| 1197 | << spacer |
| 1198 | << std::setw(sz) << "1" |
| 1199 | << spacer |
| 1200 | << std::setw(sz) << "2" |
| 1201 | << spacer |
| 1202 | << std::setw(sz) << "3" |
| 1203 | << spacer |
| 1204 | << std::setw(sz) << "4" << std::endl; |
| 1205 | assertTrue (row1.valuesToString() == os.str()); |
| 1206 | } |
| 1207 | |
| 1208 | |
| 1209 | void SQLTest::testJSONRowFormatter() |
| 1210 | { |
| 1211 | Row row1; |
| 1212 | row1.append("field0" , 0); |
| 1213 | row1.append("field1" , "1" ); |
| 1214 | row1.append("field2" , DateTime(2007, 3, 13, 8, 12, 15)); |
| 1215 | row1.append("field3" , Var()); |
| 1216 | row1.append("field4" , 4); |
| 1217 | row1.setFormatter(new JSONRowFormatter); |
| 1218 | |
| 1219 | assertTrue (row1.getFormatter().prefix() == "{" ); |
| 1220 | assertTrue (row1.getFormatter().postfix() == "]}" ); |
| 1221 | assertTrue (row1.getFormatter().getMode() == RowFormatter::FORMAT_PROGRESSIVE); |
| 1222 | assertTrue (row1.namesToString() == "\"names\":[\"field0\",\"field1\",\"field2\",\"field3\",\"field4\"]" ); |
| 1223 | assertTrue (row1.valuesToString() == ",\"values\":[[0,\"1\",\"2007-03-13T08:12:15Z\",null,4]" ); |
| 1224 | |
| 1225 | row1.setFormatter(new JSONRowFormatter(JSONRowFormatter::JSON_FMT_MODE_SMALL)); |
| 1226 | assertTrue (row1.getFormatter().getMode() == RowFormatter::FORMAT_PROGRESSIVE); |
| 1227 | assertTrue (row1.namesToString() == "" ); |
| 1228 | assertTrue (row1.valuesToString() == "[[0,\"1\",\"2007-03-13T08:12:15Z\",null,4]" ); |
| 1229 | assertTrue (row1.valuesToString() == ",[0,\"1\",\"2007-03-13T08:12:15Z\",null,4]" ); |
| 1230 | |
| 1231 | row1.setFormatter(new JSONRowFormatter(JSONRowFormatter::JSON_FMT_MODE_FULL)); |
| 1232 | assertTrue (row1.getFormatter().prefix() == "{\"count\":0,[" ); |
| 1233 | assertTrue (row1.getFormatter().postfix() == "]}" ); |
| 1234 | assertTrue (row1.getFormatter().getMode() == RowFormatter::FORMAT_PROGRESSIVE); |
| 1235 | assertTrue (row1.namesToString() == "" ); |
| 1236 | assertTrue (row1.valuesToString() == "{\"field0\":0,\"field1\":\"1\",\"field2\":\"2007-03-13T08:12:15Z\",\"field3\":null,\"field4\":4}" ); |
| 1237 | assertTrue (row1.valuesToString() == ",{\"field0\":0,\"field1\":\"1\",\"field2\":\"2007-03-13T08:12:15Z\",\"field3\":null,\"field4\":4}" ); |
| 1238 | } |
| 1239 | |
| 1240 | |
| 1241 | void SQLTest::testDateAndTime() |
| 1242 | { |
| 1243 | DateTime dt; |
| 1244 | Date d(dt); |
| 1245 | Time t(dt); |
| 1246 | |
| 1247 | assertTrue (dt.year() == d.year()); |
| 1248 | assertTrue (dt.month() == d.month()); |
| 1249 | assertTrue (dt.day() == d.day()); |
| 1250 | |
| 1251 | assertTrue (dt.hour() == t.hour()); |
| 1252 | assertTrue (dt.minute() == t.minute()); |
| 1253 | assertTrue (dt.second() == t.second()); |
| 1254 | |
| 1255 | Date d1(2007, 6, 15); |
| 1256 | d1.assign(d.year() - 1, d.month(), (d.month() == 2 && d.day() == 29) ? 28 : d.day()); |
| 1257 | assertTrue (d1 < d); assertTrue (d1 != d); |
| 1258 | |
| 1259 | d1.assign(d.year() - 1, 12, d.day()); |
| 1260 | assertTrue (d1 < d); assertTrue (d1 != d); |
| 1261 | |
| 1262 | if (d.day() > 1) |
| 1263 | { |
| 1264 | d1.assign(d.year(), d.month(), d.day() - 1); |
| 1265 | assertTrue (d1 < d); assertTrue (d1 != d); |
| 1266 | } |
| 1267 | |
| 1268 | d1.assign(d.year() + 1, d.month(), (d.month() == 2 && d.day() == 29) ? 28 : d.day()); |
| 1269 | assertTrue (d1 > d); assertTrue (d1 != d); |
| 1270 | |
| 1271 | d1.assign(d.year() + 1, 1, d.day()); |
| 1272 | assertTrue (d1 > d); assertTrue (d1 != d); |
| 1273 | |
| 1274 | if (d.day() < dt.daysOfMonth(dt.year(), dt.month())) |
| 1275 | { |
| 1276 | d1.assign(d.year(), d.month(), d.day() + 1); |
| 1277 | assertTrue (d1 > d); assertTrue (d1 != d); |
| 1278 | } |
| 1279 | |
| 1280 | d1.assign(d.year(), d.month(), d.day()); |
| 1281 | assertTrue (d1 == d); |
| 1282 | |
| 1283 | try { d1.assign(-1, 1, 1); fail ("must fail" ); } |
| 1284 | catch (InvalidArgumentException&) { } |
| 1285 | try { d1.assign(1, 0, 1); fail ("must fail" ); } |
| 1286 | catch (InvalidArgumentException&) { } |
| 1287 | try { d1.assign(1, 1, 0); fail ("must fail" ); } |
| 1288 | catch (InvalidArgumentException&) { } |
| 1289 | |
| 1290 | Time t1(12, 30, 15); |
| 1291 | |
| 1292 | if (t.hour() > 1) |
| 1293 | { |
| 1294 | t1.assign(t.hour() - 1, t.minute(), t.second()); |
| 1295 | assertTrue (t1 < t); assertTrue (t1 != t); |
| 1296 | } |
| 1297 | |
| 1298 | if (t.minute() > 1) |
| 1299 | { |
| 1300 | t1.assign(t.hour(), t.minute() - 1, t.second()); |
| 1301 | assertTrue (t1 < t); assertTrue (t1 != t); |
| 1302 | } |
| 1303 | |
| 1304 | if (t.second() > 1) |
| 1305 | { |
| 1306 | t1.assign(t.hour(), t.minute(), t.second() - 1); |
| 1307 | assertTrue (t1 < t); assertTrue (t1 != t); |
| 1308 | } |
| 1309 | |
| 1310 | if (t.hour() < 23) |
| 1311 | { |
| 1312 | t1.assign(t.hour() + 1, t.minute(), t.second()); |
| 1313 | assertTrue (t1 > t); assertTrue (t1 != t); |
| 1314 | } |
| 1315 | |
| 1316 | if (t.minute() < 59) |
| 1317 | { |
| 1318 | t1.assign(t.hour(), t.minute() + 1, t.second()); |
| 1319 | assertTrue (t1 > t); assertTrue (t1 != t); |
| 1320 | } |
| 1321 | |
| 1322 | if (t.second() < 59) |
| 1323 | { |
| 1324 | t1.assign(t.hour(), t.minute(), t.second() + 1); |
| 1325 | assertTrue (t1 > t); assertTrue (t1 != t); |
| 1326 | } |
| 1327 | |
| 1328 | t1.assign(t.hour(), t.minute(), t.second()); |
| 1329 | assertTrue (t1 == t); |
| 1330 | |
| 1331 | try { t1.assign(-1, 0, 0); fail ("must fail" ); } |
| 1332 | catch (InvalidArgumentException&) { } |
| 1333 | try { t1.assign(0, -1, 0); fail ("must fail" ); } |
| 1334 | catch (InvalidArgumentException&) { } |
| 1335 | try { t1.assign(0, 0, -1); fail ("must fail" ); } |
| 1336 | catch (InvalidArgumentException&) { } |
| 1337 | |
| 1338 | d1 = dt; |
| 1339 | assertTrue (d1 == dt); |
| 1340 | |
| 1341 | t1 = dt; |
| 1342 | assertTrue (t1 == dt); |
| 1343 | |
| 1344 | d.assign(2007, 6, 15); |
| 1345 | d1.assign(2007, 6, 16); |
| 1346 | assertTrue (d != d1); |
| 1347 | Var vDate = d; |
| 1348 | d1 = vDate; |
| 1349 | assertTrue (d == d1); |
| 1350 | |
| 1351 | t.assign(12, 30, 15); |
| 1352 | t1.assign(12, 30, 16); |
| 1353 | assertTrue (t != t1); |
| 1354 | Var vTime = t; |
| 1355 | t1 = vTime; |
| 1356 | assertTrue (t == t1); |
| 1357 | } |
| 1358 | |
| 1359 | |
| 1360 | void SQLTest::testExternalBindingAndExtraction() |
| 1361 | { |
| 1362 | Session tmp (Poco::SQL::Test::Connector::KEY, "dummy.db" ); |
| 1363 | |
| 1364 | int i; |
| 1365 | AbstractExtraction::Ptr pExt1 = into(i); |
| 1366 | AbstractExtraction::Ptr pExt2 = into(i); |
| 1367 | assertTrue (1 == pExt1.referenceCount()); |
| 1368 | assertTrue (1 == pExt2.referenceCount()); |
| 1369 | { |
| 1370 | Statement stmt(tmp); |
| 1371 | stmt.addExtract(pExt1); |
| 1372 | assertTrue (2 == pExt1.referenceCount()); |
| 1373 | } |
| 1374 | assertTrue (1 == pExt1.referenceCount()); |
| 1375 | assertTrue (1 == pExt2.referenceCount()); |
| 1376 | |
| 1377 | AbstractBinding::Ptr pBind1 = use(i, "mybind1" ); |
| 1378 | AbstractBinding::Ptr pBind2 = use(i, "mybind2" ); |
| 1379 | AbstractBinding::Ptr pBind3 = use(i, "mybind3" ); |
| 1380 | assertTrue (1 == pBind1.referenceCount()); |
| 1381 | assertTrue (1 == pBind2.referenceCount()); |
| 1382 | assertTrue (1 == pBind3.referenceCount()); |
| 1383 | { |
| 1384 | Statement stmt(tmp); |
| 1385 | stmt.addBind(pBind1); |
| 1386 | assertTrue (2 == pBind1.referenceCount()); |
| 1387 | stmt.removeBind(pBind1->name()); |
| 1388 | assertTrue (1 == pBind1.referenceCount()); |
| 1389 | stmt.addBind(pBind2); |
| 1390 | assertTrue (2 == pBind2.referenceCount()); |
| 1391 | stmt.addBind(pBind3); |
| 1392 | assertTrue (2 == pBind3.referenceCount()); |
| 1393 | |
| 1394 | try { stmt.removeBind("a bad name" ); fail("must fail" ); } |
| 1395 | catch (NotFoundException&) { } |
| 1396 | } |
| 1397 | assertTrue (1 == pBind1.referenceCount()); |
| 1398 | assertTrue (1 == pBind2.referenceCount()); |
| 1399 | assertTrue (1 == pBind3.referenceCount()); |
| 1400 | } |
| 1401 | |
| 1402 | |
| 1403 | void SQLTest::testStdTuple() |
| 1404 | { |
| 1405 | using Row = std::tuple<std::string, std::string, int>; |
| 1406 | |
| 1407 | Session sess(SessionFactory::instance().create("test" , "cs" )); |
| 1408 | Row person = std::make_tuple(std::string("Scott" ), std::string("Washington, DC" ), 42); |
| 1409 | sess << "INSERT INTO Person(name, address, age) VALUES (?, ?, ?)" , use(person), now; |
| 1410 | std::vector<Row> rows; |
| 1411 | sess << "SELECT name, address, age FROM Person" , into(rows) , now; |
| 1412 | } |
| 1413 | |
| 1414 | |
| 1415 | void SQLTest::setUp() |
| 1416 | { |
| 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | void SQLTest::tearDown() |
| 1421 | { |
| 1422 | } |
| 1423 | |
| 1424 | |
| 1425 | CppUnit::Test* SQLTest::suite() |
| 1426 | { |
| 1427 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SQLTest" ); |
| 1428 | |
| 1429 | CppUnit_addTest(pSuite, SQLTest, testSession); |
| 1430 | CppUnit_addTest(pSuite, SQLTest, testStatementFormatting); |
| 1431 | CppUnit_addTest(pSuite, SQLTest, testFeatures); |
| 1432 | CppUnit_addTest(pSuite, SQLTest, testProperties); |
| 1433 | CppUnit_addTest(pSuite, SQLTest, testLOB); |
| 1434 | CppUnit_addTest(pSuite, SQLTest, testCLOB); |
| 1435 | CppUnit_addTest(pSuite, SQLTest, testCLOBStreams); |
| 1436 | CppUnit_addTest(pSuite, SQLTest, testColumnVector); |
| 1437 | CppUnit_addTest(pSuite, SQLTest, testColumnVectorBool); |
| 1438 | CppUnit_addTest(pSuite, SQLTest, testColumnDeque); |
| 1439 | CppUnit_addTest(pSuite, SQLTest, testColumnList); |
| 1440 | CppUnit_addTest(pSuite, SQLTest, testRow); |
| 1441 | CppUnit_addTest(pSuite, SQLTest, testRowSort); |
| 1442 | CppUnit_addTest(pSuite, SQLTest, testSimpleRowFormatter); |
| 1443 | CppUnit_addTest(pSuite, SQLTest, testJSONRowFormatter); |
| 1444 | CppUnit_addTest(pSuite, SQLTest, testDateAndTime); |
| 1445 | CppUnit_addTest(pSuite, SQLTest, testExternalBindingAndExtraction); |
| 1446 | CppUnit_addTest(pSuite, SQLTest, testStdTuple); |
| 1447 | |
| 1448 | |
| 1449 | return pSuite; |
| 1450 | } |
| 1451 | |