DboImplTest.C 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (C) 2009 Emweb bvba, Kessel-Lo, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <boost/test/unit_test.hpp>
  7. #include <boost/version.hpp>
  8. #include <Wt/Dbo/Dbo>
  9. namespace dbo = Wt::Dbo;
  10. #define SQL(...) #__VA_ARGS__
  11. namespace {
  12. void parseSql(const std::string& sql, int listsCount,
  13. int fieldsCount,
  14. bool simpleSelect)
  15. {
  16. dbo::Impl::SelectFieldLists result;
  17. bool simpleSelectCount;
  18. dbo::Impl::parseSql(sql, result, simpleSelectCount);
  19. BOOST_REQUIRE(result.size() == (unsigned)listsCount);
  20. int fields = 0;
  21. for (unsigned i = 0; i < result.size(); ++i) {
  22. fields += static_cast<int>(result[i].size());
  23. for (unsigned j = 0; j < result[i].size(); ++j) {
  24. dbo::Impl::SelectField& f = result[i][j];
  25. std::cerr << "Field: '" << sql.substr(f.begin, f.end - f.begin)
  26. << "'" << std::endl;
  27. }
  28. }
  29. BOOST_REQUIRE(fields == fieldsCount);
  30. BOOST_REQUIRE(simpleSelect == simpleSelectCount);
  31. }
  32. }
  33. BOOST_AUTO_TEST_CASE( DboImplTest_test1 )
  34. {
  35. parseSql("select 1", 1, 1, true);
  36. parseSql("select a, b from foo", 1, 2, true);
  37. parseSql("select distinct a, b from foo", 1, 2, false);
  38. parseSql("select '1'", 1, 1, true);
  39. parseSql("select distinct '1'", 1, 1, false);
  40. parseSql("select 'Barts'' car'", 1, 1, true);
  41. #if BOOST_VERSION >= 104100
  42. // These ones only work correctly with our new spirit-based parser
  43. parseSql("select 'Barts'', car', bike from depot", 1, 2, true);
  44. parseSql("SELECT cast(round(number, 2) as text) AS column_number FROM table",
  45. 1, 1, true);
  46. parseSql
  47. (SQL
  48. (WITH
  49. regional_sales AS
  50. (
  51. SELECT region, SUM(amount) AS total_sales
  52. FROM orders
  53. GROUP BY region
  54. ),
  55. top_regions AS
  56. (
  57. SELECT region
  58. FROM regional_sales
  59. WHERE total_sales > (SELECT SUM(total_sales)/10 FROM
  60. regional_sales)
  61. )
  62. SELECT region,
  63. product,
  64. SUM(quantity) AS product_units,
  65. SUM(amount) AS product_sales
  66. FROM orders
  67. WHERE region IN (SELECT region FROM top_regions)
  68. GROUP BY region, product, result, simpleSelectCount
  69. ),
  70. 1, 4, false
  71. );
  72. parseSql
  73. (SQL
  74. (select a from foo
  75. intersect
  76. select b, c from bar),
  77. 2, 3, false);
  78. #endif // BOOST_VERSION
  79. }