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 struct apr_dbd_transaction_t
{
41 apr_dbd_transaction_t
*trans
;
43 apr_dbd_prepared_t
*prep
;
53 struct apr_dbd_row_t
{
54 apr_dbd_results_t
*res
;
55 apr_dbd_column_t
**columns
;
56 apr_dbd_row_t
*next_row
;
61 struct apr_dbd_results_t
{
65 apr_dbd_row_t
*next_row
;
71 struct apr_dbd_prepared_t
{
73 apr_dbd_prepared_t
*next
;
76 #define dbd_sqlite3_is_success(x) (((x) == SQLITE_DONE ) \
77 || ((x) == SQLITE_OK ))
79 static int dbd_sqlite3_select(apr_pool_t
* pool
, apr_dbd_t
* sql
, apr_dbd_results_t
** results
, const char *query
, int seek
)
81 sqlite3_stmt
*stmt
= NULL
;
82 const char *tail
= NULL
;
83 int i
, ret
, retry_count
= 0;
84 size_t num_tuples
= 0;
86 apr_dbd_row_t
*row
= NULL
;
87 apr_dbd_row_t
*lastrow
= NULL
;
88 apr_dbd_column_t
*column
;
91 if (sql
->trans
&& sql
->trans
->errnum
) {
92 return sql
->trans
->errnum
;
97 ret
= sqlite3_prepare(sql
->conn
, query
, strlen(query
), &stmt
, &tail
);
98 if (!dbd_sqlite3_is_success(ret
)) {
99 apr_dbd_mutex_unlock();
103 column_count
= sqlite3_column_count(stmt
);
105 *results
= apr_pcalloc(pool
, sizeof(apr_dbd_results_t
));
107 (*results
)->stmt
= stmt
;
108 (*results
)->sz
= column_count
;
109 (*results
)->random
= seek
;
110 (*results
)->next_row
= 0;
111 (*results
)->tuples
= 0;
112 (*results
)->col_names
= apr_pcalloc(pool
,
113 column_count
* sizeof(char *));
115 ret
= sqlite3_step(stmt
);
116 if (ret
== SQLITE_BUSY
) {
117 if (retry_count
++ > MAX_RETRY_COUNT
) {
120 apr_dbd_mutex_unlock();
121 apr_sleep(MAX_RETRY_SLEEP
);
122 apr_dbd_mutex_lock();
124 } else if (ret
== SQLITE_ROW
) {
126 apr_dbd_column_t
*col
;
127 row
= apr_palloc(pool
, sizeof(apr_dbd_row_t
));
129 increment
= sizeof(apr_dbd_column_t
*);
130 length
= increment
* (*results
)->sz
;
131 row
->columns
= apr_palloc(pool
, length
);
132 row
->columnCount
= column_count
;
133 for (i
= 0; i
< (*results
)->sz
; i
++) {
134 column
= apr_palloc(pool
, sizeof(apr_dbd_column_t
));
135 row
->columns
[i
] = column
;
136 /* copy column name once only */
137 if ((*results
)->col_names
[i
] == NULL
) {
138 (*results
)->col_names
[i
] =
139 apr_pstrdup(pool
, sqlite3_column_name(stmt
, i
));
141 column
->name
= (*results
)->col_names
[i
];
142 column
->size
= sqlite3_column_bytes(stmt
, i
);
143 column
->type
= sqlite3_column_type(stmt
, i
);
144 column
->value
= NULL
;
145 switch (column
->type
) {
150 hold
= (char *) sqlite3_column_text(stmt
, i
);
152 column
->value
= apr_palloc(pool
, column
->size
+ 1);
153 strncpy(column
->value
, hold
, column
->size
+ 1);
161 col
= row
->columns
[i
];
163 row
->rownum
= num_tuples
++;
165 (*results
)->tuples
= num_tuples
;
166 if ((*results
)->next_row
== 0) {
167 (*results
)->next_row
= row
;
170 lastrow
->next_row
= row
;
173 } else if (ret
== SQLITE_DONE
) {
176 } while (ret
== SQLITE_ROW
|| ret
== SQLITE_BUSY
);
178 ret
= sqlite3_finalize(stmt
);
179 apr_dbd_mutex_unlock();
182 sql
->trans
->errnum
= ret
;
187 static const char *dbd_sqlite3_get_name(const apr_dbd_results_t
*res
, int n
)
189 if ((n
< 0) || (n
>= res
->sz
)) {
193 return res
->next_row
->columns
[n
]->name
;
196 static int dbd_sqlite3_get_row(apr_pool_t
*pool
, apr_dbd_results_t
*res
,
197 apr_dbd_row_t
**rowp
, int rownum
)
202 *rowp
= res
->next_row
;
205 res
->next_row
= (*rowp
)->next_row
;
208 if (rownum
> res
->tuples
) {
212 *rowp
= res
->next_row
;
213 for (; *rowp
!= 0; i
++, *rowp
= (*rowp
)->next_row
) {
223 static const char *dbd_sqlite3_get_entry(const apr_dbd_row_t
*row
, int n
)
225 apr_dbd_column_t
*column
;
227 if ((n
< 0) || (n
>= row
->columnCount
)) {
230 column
= row
->columns
[n
];
231 value
= column
->value
;
235 static const char *dbd_sqlite3_error(apr_dbd_t
*sql
, int n
)
237 return sqlite3_errmsg(sql
->conn
);
240 static int dbd_sqlite3_query(apr_dbd_t
*sql
, int *nrows
, const char *query
)
242 sqlite3_stmt
*stmt
= NULL
;
243 const char *tail
= NULL
;
244 int ret
= -1, length
= 0;
246 if (sql
->trans
&& sql
->trans
->errnum
) {
247 return sql
->trans
->errnum
;
250 length
= strlen(query
);
251 apr_dbd_mutex_lock();
256 ret
= sqlite3_prepare(sql
->conn
, query
, length
, &stmt
, &tail
);
257 if (ret
!= SQLITE_OK
) {
258 sqlite3_finalize(stmt
);
262 while(retry_count
++ <= MAX_RETRY_COUNT
) {
263 ret
= sqlite3_step(stmt
);
264 if (ret
!= SQLITE_BUSY
)
267 apr_dbd_mutex_unlock();
268 apr_sleep(MAX_RETRY_SLEEP
);
269 apr_dbd_mutex_lock();
272 *nrows
= sqlite3_changes(sql
->conn
);
273 sqlite3_finalize(stmt
);
274 length
-= (tail
- query
);
276 } while (length
> 0);
278 if (dbd_sqlite3_is_success(ret
)) {
281 apr_dbd_mutex_unlock();
283 sql
->trans
->errnum
= ret
;
288 static apr_status_t
free_mem(void *data
)
294 static const char *dbd_sqlite3_escape(apr_pool_t
*pool
, const char *arg
,
297 char *ret
= sqlite3_mprintf("%q", arg
);
298 apr_pool_cleanup_register(pool
, ret
, free_mem
,
299 apr_pool_cleanup_null
);
303 static int dbd_sqlite3_prepare(apr_pool_t
*pool
, apr_dbd_t
*sql
,
304 const char *query
, const char *label
,
305 apr_dbd_prepared_t
**statement
)
308 char *p
, *slquery
= apr_pstrdup(pool
, query
);
309 const char *tail
= NULL
, *q
;
312 for (p
= slquery
, q
= query
; *q
; ++q
) {
318 else if (q
[1] == '%') {
332 apr_dbd_mutex_lock();
334 ret
= sqlite3_prepare(sql
->conn
, slquery
, strlen(query
), &stmt
, &tail
);
335 if (ret
== SQLITE_OK
) {
336 apr_dbd_prepared_t
*prep
;
338 prep
= apr_pcalloc(sql
->pool
, sizeof(*prep
));
340 prep
->next
= sql
->prep
;
342 /* link new statement to the handle */
347 sqlite3_finalize(stmt
);
350 apr_dbd_mutex_unlock();
355 static int dbd_sqlite3_pquery(apr_pool_t
*pool
, apr_dbd_t
*sql
,
356 int *nrows
, apr_dbd_prepared_t
*statement
,
357 int nargs
, const char **values
)
359 sqlite3_stmt
*stmt
= statement
->stmt
;
360 int ret
= -1, retry_count
= 0, i
;
362 if (sql
->trans
&& sql
->trans
->errnum
) {
363 return sql
->trans
->errnum
;
366 apr_dbd_mutex_lock();
368 ret
= sqlite3_reset(stmt
);
369 if (ret
== SQLITE_OK
) {
370 for (i
=0; i
< nargs
; i
++) {
371 sqlite3_bind_text(stmt
, i
+ 1, values
[i
], strlen(values
[i
]),
375 while(retry_count
++ <= MAX_RETRY_COUNT
) {
376 ret
= sqlite3_step(stmt
);
377 if (ret
!= SQLITE_BUSY
)
380 apr_dbd_mutex_unlock();
381 apr_sleep(MAX_RETRY_SLEEP
);
382 apr_dbd_mutex_lock();
385 *nrows
= sqlite3_changes(sql
->conn
);
390 if (dbd_sqlite3_is_success(ret
)) {
393 apr_dbd_mutex_unlock();
395 sql
->trans
->errnum
= ret
;
401 static int dbd_sqlite3_pvquery(apr_pool_t
*pool
, apr_dbd_t
*sql
, int *nrows
,
402 apr_dbd_prepared_t
*statement
, va_list args
)
405 int i
, nargs
= sqlite3_bind_parameter_count(statement
->stmt
);
407 if (sql
->trans
&& sql
->trans
->errnum
) {
408 return sql
->trans
->errnum
;
411 values
= apr_palloc(pool
, sizeof(*values
) * nargs
);
413 for (i
= 0; i
< nargs
; i
++) {
414 values
[i
] = apr_pstrdup(pool
, va_arg(args
, const char*));
417 return dbd_sqlite3_pquery(pool
, sql
, nrows
, statement
, nargs
, values
);
420 static int dbd_sqlite3_pselect(apr_pool_t
*pool
, apr_dbd_t
*sql
,
421 apr_dbd_results_t
**results
,
422 apr_dbd_prepared_t
*statement
, int seek
,
423 int nargs
, const char **values
)
425 sqlite3_stmt
*stmt
= statement
->stmt
;
426 int i
, ret
, retry_count
= 0;
427 size_t num_tuples
= 0;
429 apr_dbd_row_t
*row
= NULL
;
430 apr_dbd_row_t
*lastrow
= NULL
;
431 apr_dbd_column_t
*column
;
434 if (sql
->trans
&& sql
->trans
->errnum
) {
435 return sql
->trans
->errnum
;
438 apr_dbd_mutex_lock();
440 ret
= sqlite3_reset(stmt
);
441 if (ret
== SQLITE_OK
) {
444 for (i
=0; i
< nargs
; i
++) {
445 sqlite3_bind_text(stmt
, i
+ 1, values
[i
], strlen(values
[i
]),
449 column_count
= sqlite3_column_count(stmt
);
451 *results
= apr_pcalloc(pool
, sizeof(apr_dbd_results_t
));
453 (*results
)->stmt
= stmt
;
454 (*results
)->sz
= column_count
;
455 (*results
)->random
= seek
;
456 (*results
)->next_row
= 0;
457 (*results
)->tuples
= 0;
458 (*results
)->col_names
= apr_pcalloc(pool
,
459 column_count
* sizeof(char *));
461 ret
= sqlite3_step(stmt
);
462 if (ret
== SQLITE_BUSY
) {
463 if (retry_count
++ > MAX_RETRY_COUNT
) {
466 apr_dbd_mutex_unlock();
467 apr_sleep(MAX_RETRY_SLEEP
);
468 apr_dbd_mutex_lock();
470 } else if (ret
== SQLITE_ROW
) {
472 apr_dbd_column_t
*col
;
473 row
= apr_palloc(pool
, sizeof(apr_dbd_row_t
));
475 increment
= sizeof(apr_dbd_column_t
*);
476 length
= increment
* (*results
)->sz
;
477 row
->columns
= apr_palloc(pool
, length
);
478 row
->columnCount
= column_count
;
479 for (i
= 0; i
< (*results
)->sz
; i
++) {
480 column
= apr_palloc(pool
, sizeof(apr_dbd_column_t
));
481 row
->columns
[i
] = column
;
482 /* copy column name once only */
483 if ((*results
)->col_names
[i
] == NULL
) {
484 (*results
)->col_names
[i
] =
485 apr_pstrdup(pool
, sqlite3_column_name(stmt
, i
));
487 column
->name
= (*results
)->col_names
[i
];
488 column
->size
= sqlite3_column_bytes(stmt
, i
);
489 column
->type
= sqlite3_column_type(stmt
, i
);
490 column
->value
= NULL
;
491 switch (column
->type
) {
496 hold
= (char *) sqlite3_column_text(stmt
, i
);
498 column
->value
= apr_palloc(pool
, column
->size
+ 1);
499 strncpy(column
->value
, hold
, column
->size
+ 1);
507 col
= row
->columns
[i
];
509 row
->rownum
= num_tuples
++;
511 (*results
)->tuples
= num_tuples
;
512 if ((*results
)->next_row
== 0) {
513 (*results
)->next_row
= row
;
516 lastrow
->next_row
= row
;
519 } else if (ret
== SQLITE_DONE
) {
522 } while (ret
== SQLITE_ROW
|| ret
== SQLITE_BUSY
);
526 apr_dbd_mutex_unlock();
529 sql
->trans
->errnum
= ret
;
534 static int dbd_sqlite3_pvselect(apr_pool_t
*pool
, apr_dbd_t
*sql
,
535 apr_dbd_results_t
**results
,
536 apr_dbd_prepared_t
*statement
, int seek
,
540 int i
, nargs
= sqlite3_bind_parameter_count(statement
->stmt
);
542 if (sql
->trans
&& sql
->trans
->errnum
) {
543 return sql
->trans
->errnum
;
546 values
= apr_palloc(pool
, sizeof(*values
) * nargs
);
548 for (i
= 0; i
< nargs
; i
++) {
549 values
[i
] = apr_pstrdup(pool
, va_arg(args
, const char*));
552 return dbd_sqlite3_pselect(pool
, sql
, results
, statement
,
553 seek
, nargs
, values
);
556 static int dbd_sqlite3_start_transaction(apr_pool_t
*pool
,
558 apr_dbd_transaction_t
**trans
)
563 ret
= dbd_sqlite3_query(handle
, &nrows
, "BEGIN");
565 *trans
= apr_pcalloc(pool
, sizeof(apr_dbd_transaction_t
));
566 (*trans
)->handle
= handle
;
567 handle
->trans
= *trans
;
573 static int dbd_sqlite3_end_transaction(apr_dbd_transaction_t
*trans
)
575 int ret
= -1; /* ending transaction that was never started is an error */
581 ret
= dbd_sqlite3_query(trans
->handle
, &nrows
, "ROLLBACK");
583 ret
= dbd_sqlite3_query(trans
->handle
, &nrows
, "COMMIT");
585 trans
->handle
->trans
= NULL
;
591 static apr_dbd_t
*dbd_sqlite3_open(apr_pool_t
*pool
, const char *params
)
593 apr_dbd_t
*sql
= NULL
;
594 sqlite3
*conn
= NULL
;
598 sqlres
= sqlite3_open(params
, &conn
);
599 if (sqlres
!= SQLITE_OK
) {
603 /* should we register rand or power functions to the sqlite VM? */
604 sql
= apr_pcalloc(pool
, sizeof(*sql
));
612 static apr_status_t
dbd_sqlite3_close(apr_dbd_t
*handle
)
614 apr_dbd_prepared_t
*prep
= handle
->prep
;
616 /* finalize all prepared statements, or we'll get SQLITE_BUSY on close */
618 sqlite3_finalize(prep
->stmt
);
622 sqlite3_close(handle
->conn
);
626 static apr_status_t
dbd_sqlite3_check_conn(apr_pool_t
*pool
,
629 return (handle
->conn
!= NULL
) ? APR_SUCCESS
: APR_EGENERAL
;
632 static int dbd_sqlite3_select_db(apr_pool_t
*pool
, apr_dbd_t
*handle
,
638 static void *dbd_sqlite3_native(apr_dbd_t
*handle
)
643 static int dbd_sqlite3_num_cols(apr_dbd_results_t
*res
)
648 static int dbd_sqlite3_num_tuples(apr_dbd_results_t
*res
)
653 APU_DECLARE_DATA
const apr_dbd_driver_t apr_dbd_sqlite3_driver
= {
658 dbd_sqlite3_check_conn
,
660 dbd_sqlite3_select_db
,
661 dbd_sqlite3_start_transaction
,
662 dbd_sqlite3_end_transaction
,
665 dbd_sqlite3_num_cols
,
666 dbd_sqlite3_num_tuples
,
668 dbd_sqlite3_get_entry
,
673 dbd_sqlite3_pvselect
,