1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "apr_pools.h"
22 #include "apr_strings.h"
24 static void test_dbd_init(abts_case *tc, void *data)
29 rv = apr_dbd_init(pool);
30 ABTS_ASSERT(tc, "failed to init apr_dbd", rv == APR_SUCCESS);
33 #if APU_HAVE_SQLITE2 || APU_HAVE_SQLITE3
34 static void test_statement(abts_case *tc, apr_dbd_t* handle,
35 const apr_dbd_driver_t* driver, const char* sql)
40 rv = apr_dbd_query(driver, handle, &nrows, sql);
42 ABTS_ASSERT(tc, sql, rv == APR_SUCCESS);
45 static void create_table(abts_case *tc, apr_dbd_t* handle,
46 const apr_dbd_driver_t* driver)
48 const char *sql = "CREATE TABLE apr_dbd_test ("
49 "col1 varchar(40) not null,"
53 test_statement(tc, handle, driver, sql);
56 static void drop_table(abts_case *tc, apr_dbd_t* handle,
57 const apr_dbd_driver_t* driver)
59 const char *sql = "DROP TABLE apr_dbd_test";
60 test_statement(tc, handle, driver, sql);
63 static void delete_rows(abts_case *tc, apr_dbd_t* handle,
64 const apr_dbd_driver_t* driver)
66 const char *sql = "DELETE FROM apr_dbd_test";
67 test_statement(tc, handle, driver, sql);
71 static void insert_data(abts_case *tc, apr_dbd_t* handle,
72 const apr_dbd_driver_t* driver, int count)
75 const char* sql = "INSERT INTO apr_dbd_test VALUES('%d', '%d', %d)";
81 for (i=0; i<count; i++) {
82 sqf = apr_psprintf(pool, sql, i, i, i);
83 rv = apr_dbd_query(driver, handle, &nrows, sqf);
84 ABTS_ASSERT(tc, sqf, rv == APR_SUCCESS);
85 ABTS_ASSERT(tc, sqf, 1 == nrows);
89 static void select_rows(abts_case *tc, apr_dbd_t* handle,
90 const apr_dbd_driver_t* driver, int count)
95 const char* sql = "SELECT * FROM apr_dbd_test ORDER BY col1";
96 apr_dbd_results_t *res = NULL;
97 apr_dbd_row_t *row = NULL;
100 rv = apr_dbd_select(driver, pool, handle, &res, sql, 0);
101 ABTS_ASSERT(tc, sql, rv == APR_SUCCESS);
102 ABTS_PTR_NOTNULL(tc, res);
104 apr_pool_create(&tpool, pool);
108 rv = apr_dbd_get_row(driver, pool, res, &row, -1);
109 ABTS_ASSERT(tc, sql, rv == APR_SUCCESS);
110 ABTS_PTR_NOTNULL(tc, row);
111 apr_pool_clear(tpool);
114 ABTS_ASSERT(tc, "Missing Rows!", i == 0);
119 rv = apr_dbd_select(driver, pool, handle, &res, sql, 1);
120 ABTS_ASSERT(tc, sql, rv == APR_SUCCESS);
121 ABTS_PTR_NOTNULL(tc, res);
123 rv = apr_dbd_num_tuples(driver, res);
124 ABTS_ASSERT(tc, "invalid row count", rv == count);
128 rv = apr_dbd_get_row(driver, pool, res, &row, i);
129 ABTS_ASSERT(tc, sql, rv == APR_SUCCESS);
130 ABTS_PTR_NOTNULL(tc, row);
131 apr_pool_clear(tpool);
134 ABTS_ASSERT(tc, "Missing Rows!", i == 0);
135 rv = apr_dbd_get_row(driver, pool, res, &row, count+100);
136 ABTS_ASSERT(tc, "If we overseek, get_row should return -1", rv == -1);
139 static void test_escape(abts_case *tc, apr_dbd_t *handle,
140 const apr_dbd_driver_t *driver)
142 const char *escaped = apr_dbd_escape(driver, p, "foo'bar", handle);
144 ABTS_STR_EQUAL(tc, "foo''bar", escaped);
147 static void test_dbd_generic(abts_case *tc, apr_dbd_t* handle,
148 const apr_dbd_driver_t* driver)
151 apr_pool_t *pool = p;
154 native = apr_dbd_native_handle(driver, handle);
155 ABTS_PTR_NOTNULL(tc, native);
157 rv = apr_dbd_check_conn(driver, pool, handle);
159 create_table(tc, handle, driver);
160 select_rows(tc, handle, driver, 0);
161 insert_data(tc, handle, driver, 5);
162 select_rows(tc, handle, driver, 5);
163 delete_rows(tc, handle, driver);
164 select_rows(tc, handle, driver, 0);
165 drop_table(tc, handle, driver);
167 test_escape(tc, handle, driver);
169 rv = apr_dbd_close(driver, handle);
170 ABTS_ASSERT(tc, "failed to close database", rv == APR_SUCCESS);
175 static void test_dbd_sqlite2(abts_case *tc, void *data)
177 apr_pool_t *pool = p;
179 const apr_dbd_driver_t* driver = NULL;
180 apr_dbd_t* handle = NULL;
182 rv = apr_dbd_get_driver(pool, "sqlite2", &driver);
183 ABTS_ASSERT(tc, "failed to fetch sqlite2 driver", rv == APR_SUCCESS);
184 ABTS_PTR_NOTNULL(tc, driver);
189 ABTS_STR_EQUAL(tc, "sqlite2", apr_dbd_name(driver));
191 rv = apr_dbd_open(driver, pool, "data/sqlite2.db:600", &handle);
192 ABTS_ASSERT(tc, "failed to open sqlite2 atabase", rv == APR_SUCCESS);
193 ABTS_PTR_NOTNULL(tc, handle);
198 test_dbd_generic(tc, handle, driver);
203 static void test_dbd_sqlite3(abts_case *tc, void *data)
205 apr_pool_t *pool = p;
207 const apr_dbd_driver_t* driver = NULL;
208 apr_dbd_t* handle = NULL;
210 rv = apr_dbd_get_driver(pool, "sqlite3", &driver);
211 ABTS_ASSERT(tc, "failed to fetch sqlite3 driver", rv == APR_SUCCESS);
212 ABTS_PTR_NOTNULL(tc, driver);
217 ABTS_STR_EQUAL(tc, "sqlite3", apr_dbd_name(driver));
219 rv = apr_dbd_open(driver, pool, "data/sqlite3.db", &handle);
220 ABTS_ASSERT(tc, "failed to open sqlite3 database", rv == APR_SUCCESS);
221 ABTS_PTR_NOTNULL(tc, handle);
226 test_dbd_generic(tc, handle, driver);
230 abts_suite *testdbd(abts_suite *suite)
232 suite = ADD_SUITE(suite);
235 abts_run_test(suite, test_dbd_init, NULL);
238 abts_run_test(suite, test_dbd_sqlite2, NULL);
242 abts_run_test(suite, test_dbd_sqlite3, NULL);