1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
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
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.
26 #include "apr_strings.h"
29 #include "apr_dbd_internal.h"
31 #define MAX_RETRY_COUNT 15
32 #define MAX_RETRY_SLEEP 100000
34 #define QUERY_MAX_ARGS 40
36 struct apr_dbd_transaction_t
{
43 apr_dbd_transaction_t
*trans
;
45 apr_dbd_prepared_t
*prep
;
55 struct apr_dbd_row_t
{
56 apr_dbd_results_t
*res
;
57 apr_dbd_column_t
**columns
;
58 apr_dbd_row_t
*next_row
;
63 struct apr_dbd_results_t
{
67 apr_dbd_row_t
*next_row
;
73 struct apr_dbd_prepared_t
{
75 apr_dbd_prepared_t
*next
;
78 #define dbd_sqlite3_is_success(x) (((x) == SQLITE_DONE ) \
79 || ((x) == SQLITE_OK ))
81 static int dbd_sqlite3_select(apr_pool_t
* pool
, apr_dbd_t
* sql
, apr_dbd_results_t
** results
, const char *query
, int seek
)
83 sqlite3_stmt
*stmt
= NULL
;
84 const char *tail
= NULL
;
85 int i
, ret
, retry_count
= 0;
86 size_t num_tuples
= 0;
88 apr_dbd_row_t
*row
= NULL
;
89 apr_dbd_row_t
*lastrow
= NULL
;
90 apr_dbd_column_t
*column
;
93 if (sql
->trans
&& sql
->trans
->errnum
) {
94 return sql
->trans
->errnum
;
99 ret
= sqlite3_prepare(sql
->conn
, query
, strlen(query
), &stmt
, &tail
);
100 if (!dbd_sqlite3_is_success(ret
)) {
101 apr_dbd_mutex_unlock();
105 column_count
= sqlite3_column_count(stmt
);
107 *results
= apr_pcalloc(pool
, sizeof(apr_dbd_results_t
));
109 (*results
)->stmt
= stmt
;
110 (*results
)->sz
= column_count
;
111 (*results
)->random
= seek
;
112 (*results
)->next_row
= 0;
113 (*results
)->tuples
= 0;
114 (*results
)->col_names
= apr_pcalloc(pool
,
115 column_count
* sizeof(char *));
117 ret
= sqlite3_step(stmt
);
118 if (ret
== SQLITE_BUSY
) {
119 if (retry_count
++ > MAX_RETRY_COUNT
) {
122 apr_dbd_mutex_unlock();
123 apr_sleep(MAX_RETRY_SLEEP
);
124 apr_dbd_mutex_lock();
126 } else if (ret
== SQLITE_ROW
) {
128 apr_dbd_column_t
*col
;
129 row
= apr_palloc(pool
, sizeof(apr_dbd_row_t
));
131 increment
= sizeof(apr_dbd_column_t
*);
132 length
= increment
* (*results
)->sz
;
133 row
->columns
= apr_palloc(pool
, length
);
134 row
->columnCount
= column_count
;
135 for (i
= 0; i
< (*results
)->sz
; i
++) {
136 column
= apr_palloc(pool
, sizeof(apr_dbd_column_t
));
137 row
->columns
[i
] = column
;
138 /* copy column name once only */
139 if ((*results
)->col_names
[i
] == NULL
) {
140 (*results
)->col_names
[i
] =
141 apr_pstrdup(pool
, sqlite3_column_name(stmt
, i
));
143 column
->name
= (*results
)->col_names
[i
];
144 column
->size
= sqlite3_column_bytes(stmt
, i
);
145 column
->type
= sqlite3_column_type(stmt
, i
);
146 column
->value
= NULL
;
147 switch (column
->type
) {
152 hold
= (char *) sqlite3_column_text(stmt
, i
);
154 column
->value
= apr_palloc(pool
, column
->size
+ 1);
155 strncpy(column
->value
, hold
, column
->size
+ 1);
163 col
= row
->columns
[i
];
165 row
->rownum
= num_tuples
++;
167 (*results
)->tuples
= num_tuples
;
168 if ((*results
)->next_row
== 0) {
169 (*results
)->next_row
= row
;
172 lastrow
->next_row
= row
;
175 } else if (ret
== SQLITE_DONE
) {
178 } while (ret
== SQLITE_ROW
|| ret
== SQLITE_BUSY
);
180 ret
= sqlite3_finalize(stmt
);
181 apr_dbd_mutex_unlock();
184 sql
->trans
->errnum
= ret
;
189 static const char *dbd_sqlite3_get_name(const apr_dbd_results_t
*res
, int n
)
191 if ((n
< 0) || (n
>= res
->sz
)) {
195 return res
->next_row
->columns
[n
]->name
;
198 static int dbd_sqlite3_get_row(apr_pool_t
*pool
, apr_dbd_results_t
*res
,
199 apr_dbd_row_t
**rowp
, int rownum
)
204 *rowp
= res
->next_row
;
207 res
->next_row
= (*rowp
)->next_row
;
210 if (rownum
> res
->tuples
) {
214 *rowp
= res
->next_row
;
215 for (; *rowp
!= 0; i
++, *rowp
= (*rowp
)->next_row
) {
225 static const char *dbd_sqlite3_get_entry(const apr_dbd_row_t
*row
, int n
)
227 apr_dbd_column_t
*column
;
229 if ((n
< 0) || (n
>= row
->columnCount
)) {
232 column
= row
->columns
[n
];
233 value
= column
->value
;
237 static const char *dbd_sqlite3_error(apr_dbd_t
*sql
, int n
)
239 return sqlite3_errmsg(sql
->conn
);
242 static int dbd_sqlite3_query(apr_dbd_t
*sql
, int *nrows
, const char *query
)
244 sqlite3_stmt
*stmt
= NULL
;
245 const char *tail
= NULL
;
246 int ret
= -1, length
= 0;
248 if (sql
->trans
&& sql
->trans
->errnum
) {
249 return sql
->trans
->errnum
;
252 length
= strlen(query
);
253 apr_dbd_mutex_lock();
258 ret
= sqlite3_prepare(sql
->conn
, query
, length
, &stmt
, &tail
);
259 if (ret
!= SQLITE_OK
) {
260 sqlite3_finalize(stmt
);
264 while(retry_count
++ <= MAX_RETRY_COUNT
) {
265 ret
= sqlite3_step(stmt
);
266 if (ret
!= SQLITE_BUSY
)
269 apr_dbd_mutex_unlock();
270 apr_sleep(MAX_RETRY_SLEEP
);
271 apr_dbd_mutex_lock();
274 *nrows
= sqlite3_changes(sql
->conn
);
275 sqlite3_finalize(stmt
);
276 length
-= (tail
- query
);
278 } while (length
> 0);
280 if (dbd_sqlite3_is_success(ret
)) {
283 apr_dbd_mutex_unlock();
285 sql
->trans
->errnum
= ret
;
290 static apr_status_t
free_mem(void *data
)
296 static const char *dbd_sqlite3_escape(apr_pool_t
*pool
, const char *arg
,
299 char *ret
= sqlite3_mprintf("%q", arg
);
300 apr_pool_cleanup_register(pool
, ret
, free_mem
,
301 apr_pool_cleanup_null
);
305 static int dbd_sqlite3_prepare(apr_pool_t
*pool
, apr_dbd_t
*sql
,
306 const char *query
, const char *label
,
307 apr_dbd_prepared_t
**statement
)
310 char *p
, *slquery
= apr_pstrdup(pool
, query
);
311 const char *tail
= NULL
, *q
;
314 for (p
= slquery
, q
= query
; *q
; ++q
) {
320 else if (q
[1] == '%') {
334 apr_dbd_mutex_lock();
336 ret
= sqlite3_prepare(sql
->conn
, slquery
, strlen(query
), &stmt
, &tail
);
337 if (ret
== SQLITE_OK
) {
338 apr_dbd_prepared_t
*prep
;
340 prep
= apr_pcalloc(sql
->pool
, sizeof(*prep
));
342 prep
->next
= sql
->prep
;
344 /* link new statement to the handle */
349 sqlite3_finalize(stmt
);
352 apr_dbd_mutex_unlock();
357 static int dbd_sqlite3_pquery(apr_pool_t
*pool
, apr_dbd_t
*sql
,
358 int *nrows
, apr_dbd_prepared_t
*statement
,
359 int nargs
, const char **values
)
361 sqlite3_stmt
*stmt
= statement
->stmt
;
362 int ret
= -1, retry_count
= 0, i
;
364 if (sql
->trans
&& sql
->trans
->errnum
) {
365 return sql
->trans
->errnum
;
368 apr_dbd_mutex_lock();
370 ret
= sqlite3_reset(stmt
);
371 if (ret
== SQLITE_OK
) {
372 for (i
=0; i
< nargs
; i
++) {
373 sqlite3_bind_text(stmt
, i
+ 1, values
[i
], strlen(values
[i
]),
377 while(retry_count
++ <= MAX_RETRY_COUNT
) {
378 ret
= sqlite3_step(stmt
);
379 if (ret
!= SQLITE_BUSY
)
382 apr_dbd_mutex_unlock();
383 apr_sleep(MAX_RETRY_SLEEP
);
384 apr_dbd_mutex_lock();
387 *nrows
= sqlite3_changes(sql
->conn
);
392 if (dbd_sqlite3_is_success(ret
)) {
395 apr_dbd_mutex_unlock();
397 sql
->trans
->errnum
= ret
;
403 static int dbd_sqlite3_pvquery(apr_pool_t
*pool
, apr_dbd_t
*sql
, int *nrows
,
404 apr_dbd_prepared_t
*statement
, va_list args
)
406 const char *arg
, *values
[QUERY_MAX_ARGS
];
409 if (sql
->trans
&& sql
->trans
->errnum
) {
410 return sql
->trans
->errnum
;
413 while (arg
= va_arg(args
, const char*), arg
) {
414 if (nargs
>= QUERY_MAX_ARGS
) {
418 values
[nargs
++] = apr_pstrdup(pool
, arg
);
420 values
[nargs
] = NULL
;
422 return dbd_sqlite3_pquery(pool
, sql
, nrows
, statement
, nargs
, values
);
425 static int dbd_sqlite3_pselect(apr_pool_t
*pool
, apr_dbd_t
*sql
,
426 apr_dbd_results_t
**results
,
427 apr_dbd_prepared_t
*statement
, int seek
,
428 int nargs
, const char **values
)
430 sqlite3_stmt
*stmt
= statement
->stmt
;
431 int i
, ret
, retry_count
= 0;
432 size_t num_tuples
= 0;
434 apr_dbd_row_t
*row
= NULL
;
435 apr_dbd_row_t
*lastrow
= NULL
;
436 apr_dbd_column_t
*column
;
439 if (sql
->trans
&& sql
->trans
->errnum
) {
440 return sql
->trans
->errnum
;
443 apr_dbd_mutex_lock();
445 ret
= sqlite3_reset(stmt
);
446 if (ret
== SQLITE_OK
) {
449 for (i
=0; i
< nargs
; i
++) {
450 sqlite3_bind_text(stmt
, i
+ 1, values
[i
], strlen(values
[i
]),
454 column_count
= sqlite3_column_count(stmt
);
456 *results
= apr_pcalloc(pool
, sizeof(apr_dbd_results_t
));
458 (*results
)->stmt
= stmt
;
459 (*results
)->sz
= column_count
;
460 (*results
)->random
= seek
;
461 (*results
)->next_row
= 0;
462 (*results
)->tuples
= 0;
463 (*results
)->col_names
= apr_pcalloc(pool
,
464 column_count
* sizeof(char *));
466 ret
= sqlite3_step(stmt
);
467 if (ret
== SQLITE_BUSY
) {
468 if (retry_count
++ > MAX_RETRY_COUNT
) {
471 apr_dbd_mutex_unlock();
472 apr_sleep(MAX_RETRY_SLEEP
);
473 apr_dbd_mutex_lock();
475 } else if (ret
== SQLITE_ROW
) {
477 apr_dbd_column_t
*col
;
478 row
= apr_palloc(pool
, sizeof(apr_dbd_row_t
));
480 increment
= sizeof(apr_dbd_column_t
*);
481 length
= increment
* (*results
)->sz
;
482 row
->columns
= apr_palloc(pool
, length
);
483 row
->columnCount
= column_count
;
484 for (i
= 0; i
< (*results
)->sz
; i
++) {
485 column
= apr_palloc(pool
, sizeof(apr_dbd_column_t
));
486 row
->columns
[i
] = column
;
487 /* copy column name once only */
488 if ((*results
)->col_names
[i
] == NULL
) {
489 (*results
)->col_names
[i
] =
490 apr_pstrdup(pool
, sqlite3_column_name(stmt
, i
));
492 column
->name
= (*results
)->col_names
[i
];
493 column
->size
= sqlite3_column_bytes(stmt
, i
);
494 column
->type
= sqlite3_column_type(stmt
, i
);
495 column
->value
= NULL
;
496 switch (column
->type
) {
501 hold
= (char *) sqlite3_column_text(stmt
, i
);
503 column
->value
= apr_palloc(pool
, column
->size
+ 1);
504 strncpy(column
->value
, hold
, column
->size
+ 1);
512 col
= row
->columns
[i
];
514 row
->rownum
= num_tuples
++;
516 (*results
)->tuples
= num_tuples
;
517 if ((*results
)->next_row
== 0) {
518 (*results
)->next_row
= row
;
521 lastrow
->next_row
= row
;
524 } else if (ret
== SQLITE_DONE
) {
527 } while (ret
== SQLITE_ROW
|| ret
== SQLITE_BUSY
);
531 apr_dbd_mutex_unlock();
534 sql
->trans
->errnum
= ret
;
539 static int dbd_sqlite3_pvselect(apr_pool_t
*pool
, apr_dbd_t
*sql
,
540 apr_dbd_results_t
**results
,
541 apr_dbd_prepared_t
*statement
, int seek
,
544 const char *arg
, *values
[QUERY_MAX_ARGS
];
547 if (sql
->trans
&& sql
->trans
->errnum
) {
548 return sql
->trans
->errnum
;
551 while (arg
= va_arg(args
, const char*), arg
) {
552 if (nargs
>= QUERY_MAX_ARGS
) {
556 values
[nargs
++] = apr_pstrdup(pool
, arg
);
558 values
[nargs
] = NULL
;
560 return dbd_sqlite3_pselect(pool
, sql
, results
, statement
,
561 seek
, nargs
, values
);
564 static int dbd_sqlite3_start_transaction(apr_pool_t
*pool
,
566 apr_dbd_transaction_t
**trans
)
571 ret
= dbd_sqlite3_query(handle
, &nrows
, "BEGIN");
573 *trans
= apr_pcalloc(pool
, sizeof(apr_dbd_transaction_t
));
574 (*trans
)->handle
= handle
;
575 handle
->trans
= *trans
;
581 static int dbd_sqlite3_end_transaction(apr_dbd_transaction_t
*trans
)
583 int ret
= -1; /* ending transaction that was never started is an error */
589 ret
= dbd_sqlite3_query(trans
->handle
, &nrows
, "ROLLBACK");
591 ret
= dbd_sqlite3_query(trans
->handle
, &nrows
, "COMMIT");
593 trans
->handle
->trans
= NULL
;
599 static apr_dbd_t
*dbd_sqlite3_open(apr_pool_t
*pool
, const char *params
)
601 apr_dbd_t
*sql
= NULL
;
602 sqlite3
*conn
= NULL
;
606 sqlres
= sqlite3_open(params
, &conn
);
607 if (sqlres
!= SQLITE_OK
) {
611 /* should we register rand or power functions to the sqlite VM? */
612 sql
= apr_pcalloc(pool
, sizeof(*sql
));
620 static apr_status_t
dbd_sqlite3_close(apr_dbd_t
*handle
)
622 apr_dbd_prepared_t
*prep
= handle
->prep
;
624 /* finalize all prepared statements, or we'll get SQLITE_BUSY on close */
626 sqlite3_finalize(prep
->stmt
);
630 sqlite3_close(handle
->conn
);
634 static apr_status_t
dbd_sqlite3_check_conn(apr_pool_t
*pool
,
637 return (handle
->conn
!= NULL
) ? APR_SUCCESS
: APR_EGENERAL
;
640 static int dbd_sqlite3_select_db(apr_pool_t
*pool
, apr_dbd_t
*handle
,
646 static void *dbd_sqlite3_native(apr_dbd_t
*handle
)
651 static int dbd_sqlite3_num_cols(apr_dbd_results_t
*res
)
656 static int dbd_sqlite3_num_tuples(apr_dbd_results_t
*res
)
661 APU_DECLARE_DATA
const apr_dbd_driver_t apr_dbd_sqlite3_driver
= {
666 dbd_sqlite3_check_conn
,
668 dbd_sqlite3_select_db
,
669 dbd_sqlite3_start_transaction
,
670 dbd_sqlite3_end_transaction
,
673 dbd_sqlite3_num_cols
,
674 dbd_sqlite3_num_tuples
,
676 dbd_sqlite3_get_entry
,
681 dbd_sqlite3_pvselect
,