testdbd.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "testutil.h"
  17. #include "apr.h"
  18. #include "apu.h"
  19. #include "apr_pools.h"
  20. #include "apr_dbd.h"
  21. #include "apr_strings.h"
  22. static void test_dbd_init(abts_case *tc, void *data)
  23. {
  24. apr_pool_t *pool = p;
  25. apr_status_t rv;
  26. rv = apr_dbd_init(pool);
  27. ABTS_ASSERT(tc, "failed to init apr_dbd", rv == APR_SUCCESS);
  28. }
  29. #if APU_HAVE_SQLITE2 || APU_HAVE_SQLITE3
  30. static void test_statement(abts_case *tc, apr_dbd_t* handle,
  31. const apr_dbd_driver_t* driver, const char* sql)
  32. {
  33. int nrows;
  34. apr_status_t rv;
  35. rv = apr_dbd_query(driver, handle, &nrows, sql);
  36. ABTS_ASSERT(tc, sql, rv == APR_SUCCESS);
  37. }
  38. static void create_table(abts_case *tc, apr_dbd_t* handle,
  39. const apr_dbd_driver_t* driver)
  40. {
  41. const char *sql = "CREATE TABLE apr_dbd_test ("
  42. "col1 varchar(40) not null,"
  43. "col2 varchar(40),"
  44. "col3 integer)";
  45. test_statement(tc, handle, driver, sql);
  46. }
  47. static void drop_table(abts_case *tc, apr_dbd_t* handle,
  48. const apr_dbd_driver_t* driver)
  49. {
  50. const char *sql = "DROP TABLE apr_dbd_test";
  51. test_statement(tc, handle, driver, sql);
  52. }
  53. static void delete_rows(abts_case *tc, apr_dbd_t* handle,
  54. const apr_dbd_driver_t* driver)
  55. {
  56. const char *sql = "DELETE FROM apr_dbd_test";
  57. test_statement(tc, handle, driver, sql);
  58. }
  59. static void insert_data(abts_case *tc, apr_dbd_t* handle,
  60. const apr_dbd_driver_t* driver, int count)
  61. {
  62. apr_pool_t* pool = p;
  63. const char* sql = "INSERT INTO apr_dbd_test VALUES('%d', '%d', %d)";
  64. char* sqf = NULL;
  65. int i;
  66. int nrows;
  67. apr_status_t rv;
  68. for (i=0; i<count; i++) {
  69. sqf = apr_psprintf(pool, sql, i, i, i);
  70. rv = apr_dbd_query(driver, handle, &nrows, sqf);
  71. ABTS_ASSERT(tc, sqf, rv == APR_SUCCESS);
  72. ABTS_ASSERT(tc, sqf, 1 == nrows);
  73. }
  74. }
  75. static void select_rows(abts_case *tc, apr_dbd_t* handle,
  76. const apr_dbd_driver_t* driver, int count)
  77. {
  78. apr_status_t rv;
  79. apr_pool_t* pool = p;
  80. apr_pool_t* tpool;
  81. const char* sql = "SELECT * FROM apr_dbd_test ORDER BY col1";
  82. apr_dbd_results_t *res = NULL;
  83. apr_dbd_row_t *row = NULL;
  84. int i;
  85. rv = apr_dbd_select(driver, pool, handle, &res, sql, 0);
  86. ABTS_ASSERT(tc, sql, rv == APR_SUCCESS);
  87. ABTS_PTR_NOTNULL(tc, res);
  88. apr_pool_create(&tpool, pool);
  89. i = count;
  90. while (i > 0) {
  91. row = NULL;
  92. rv = apr_dbd_get_row(driver, pool, res, &row, -1);
  93. ABTS_ASSERT(tc, sql, rv == APR_SUCCESS);
  94. ABTS_PTR_NOTNULL(tc, row);
  95. apr_pool_clear(tpool);
  96. i--;
  97. }
  98. ABTS_ASSERT(tc, "Missing Rows!", i == 0);
  99. res = NULL;
  100. i = count;
  101. rv = apr_dbd_select(driver, pool, handle, &res, sql, 1);
  102. ABTS_ASSERT(tc, sql, rv == APR_SUCCESS);
  103. ABTS_PTR_NOTNULL(tc, res);
  104. rv = apr_dbd_num_tuples(driver, res);
  105. ABTS_ASSERT(tc, "invalid row count", rv == count);
  106. while (i > 0) {
  107. row = NULL;
  108. rv = apr_dbd_get_row(driver, pool, res, &row, i);
  109. ABTS_ASSERT(tc, sql, rv == APR_SUCCESS);
  110. ABTS_PTR_NOTNULL(tc, row);
  111. apr_pool_clear(tpool);
  112. i--;
  113. }
  114. ABTS_ASSERT(tc, "Missing Rows!", i == 0);
  115. rv = apr_dbd_get_row(driver, pool, res, &row, count+100);
  116. ABTS_ASSERT(tc, "If we overseek, get_row should return -1", rv == -1);
  117. }
  118. static void test_escape(abts_case *tc, apr_dbd_t *handle,
  119. const apr_dbd_driver_t *driver)
  120. {
  121. const char *escaped = apr_dbd_escape(driver, p, "foo'bar", handle);
  122. ABTS_STR_EQUAL(tc, "foo''bar", escaped);
  123. }
  124. static void test_dbd_generic(abts_case *tc, apr_dbd_t* handle,
  125. const apr_dbd_driver_t* driver)
  126. {
  127. void* native;
  128. apr_pool_t *pool = p;
  129. apr_status_t rv;
  130. native = apr_dbd_native_handle(driver, handle);
  131. ABTS_PTR_NOTNULL(tc, native);
  132. rv = apr_dbd_check_conn(driver, pool, handle);
  133. create_table(tc, handle, driver);
  134. select_rows(tc, handle, driver, 0);
  135. insert_data(tc, handle, driver, 5);
  136. select_rows(tc, handle, driver, 5);
  137. delete_rows(tc, handle, driver);
  138. select_rows(tc, handle, driver, 0);
  139. drop_table(tc, handle, driver);
  140. test_escape(tc, handle, driver);
  141. rv = apr_dbd_close(driver, handle);
  142. ABTS_ASSERT(tc, "failed to close database", rv == APR_SUCCESS);
  143. }
  144. #endif
  145. #if APU_HAVE_SQLITE2
  146. static void test_dbd_sqlite2(abts_case *tc, void *data)
  147. {
  148. apr_pool_t *pool = p;
  149. apr_status_t rv;
  150. const apr_dbd_driver_t* driver = NULL;
  151. apr_dbd_t* handle = NULL;
  152. rv = apr_dbd_get_driver(pool, "sqlite2", &driver);
  153. ABTS_ASSERT(tc, "failed to fetch driver", rv == APR_SUCCESS);
  154. ABTS_PTR_NOTNULL(tc, driver);
  155. ABTS_STR_EQUAL(tc, apr_dbd_name(driver), "sqlite2");
  156. rv = apr_dbd_open(driver, pool, "data/sqlite2.db:600", &handle);
  157. ABTS_ASSERT(tc, "failed to open database", rv == APR_SUCCESS);
  158. ABTS_PTR_NOTNULL(tc, handle);
  159. test_dbd_generic(tc, handle, driver);
  160. }
  161. #endif
  162. #if APU_HAVE_SQLITE3
  163. static void test_dbd_sqlite3(abts_case *tc, void *data)
  164. {
  165. apr_pool_t *pool = p;
  166. apr_status_t rv;
  167. const apr_dbd_driver_t* driver = NULL;
  168. apr_dbd_t* handle = NULL;
  169. rv = apr_dbd_get_driver(pool, "sqlite3", &driver);
  170. ABTS_ASSERT(tc, "failed to fetch driver", rv == APR_SUCCESS);
  171. ABTS_PTR_NOTNULL(tc, driver);
  172. ABTS_STR_EQUAL(tc, apr_dbd_name(driver), "sqlite3");
  173. rv = apr_dbd_open(driver, pool, "data/sqlite3.db", &handle);
  174. ABTS_ASSERT(tc, "failed to open database", rv == APR_SUCCESS);
  175. ABTS_PTR_NOTNULL(tc, handle);
  176. test_dbd_generic(tc, handle, driver);
  177. }
  178. #endif
  179. abts_suite *testdbd(abts_suite *suite)
  180. {
  181. suite = ADD_SUITE(suite);
  182. abts_run_test(suite, test_dbd_init, NULL);
  183. #if APU_HAVE_SQLITE2
  184. abts_run_test(suite, test_dbd_sqlite2, NULL);
  185. #endif
  186. #if APU_HAVE_SQLITE3
  187. abts_run_test(suite, test_dbd_sqlite3, NULL);
  188. #endif
  189. return suite;
  190. }