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.
18 #include "apu_config.h"
20 /* COMPILE_STUBS: compile stubs for unimplemented functions.
22 * This is required to compile in /trunk/, but can be
23 * undefined to compile a driver for httpd-2.2 and other
24 * APR-1.2 applications
33 #include "apr_strings.h"
36 #include "apr_pools.h"
37 #include "apr_dbd_internal.h"
39 #ifdef HAVE_FREETDS_SYBDB_H
40 #include <freetds/sybdb.h>
47 #include <sys/types.h>
50 /* This probably needs to change for different applications */
51 #define MAX_COL_LEN 256
53 typedef struct freetds_cell_t {
59 struct apr_dbd_transaction_t {
67 apr_dbd_transaction_t *trans;
73 struct apr_dbd_results_t {
81 struct apr_dbd_row_t {
82 apr_dbd_results_t *res;
83 BYTE buf[MAX_COL_LEN];
86 struct apr_dbd_prepared_t {
93 #define dbd_freetds_is_success(x) (x == SUCCEED)
95 static int labelnum = 0; /* FIXME */
96 static regex_t dbd_freetds_find_arg;
98 /* execute a query that doesn't return a result set, mop up,
99 * and return and APR-flavoured status
101 static RETCODE freetds_exec(DBPROCESS *proc, const char *query,
102 int want_results, int *nrows)
105 RETCODE rv = dbcmd(proc, query);
109 rv = dbsqlexec(proc);
114 while (dbresults(proc) != NO_MORE_RESULTS) {
120 static apr_status_t clear_result(void *data)
123 return (dbcanquery((DBPROCESS*)data) == SUCCEED)
128 static int dbd_freetds_select(apr_pool_t *pool, apr_dbd_t *sql,
129 apr_dbd_results_t **results,
130 const char *query, int seek)
132 apr_dbd_results_t *res;
133 if (sql->trans && (sql->trans->errnum != SUCCEED)) {
136 /* the core of this is
137 * dbcmd(proc, query);
139 * while (dbnextrow(dbproc) != NO_MORE_ROWS) {
146 sql->err = freetds_exec(sql->proc, query, 1, NULL);
147 if (!dbd_freetds_is_success(sql->err)) {
149 sql->trans->errnum = sql->err;
154 sql->err = dbresults(sql->proc);
155 if (sql->err != SUCCEED) {
157 sql->trans->errnum = sql->err;
163 *results = apr_pcalloc(pool, sizeof(apr_dbd_results_t));
166 res->proc = sql->proc;
169 res->ntuples = dblastrow(sql->proc);
170 res->sz = dbnumcols(sql->proc);
171 apr_pool_cleanup_register(pool, sql->proc, clear_result,
172 apr_pool_cleanup_null);
175 /* Now we have a result set. We need to bind to its vars */
176 res->vars = apr_palloc(pool, res->sz * sizeof(freetds_cell_t*));
177 for (i=1; i <= res->sz; ++i) {
178 freetds_cell_t *cell = &res->vars[i-1];
179 cell->type = dbcoltype(sql->proc, i);
180 cell->len = dbcollen(sql->proc, i);
181 cell->data = apr_palloc(pool, cell->len);
182 sql->err = dbbind(sql->proc, i, /*cell->type */ STRINGBIND, cell->len, cell->data);
183 if (sql->err != SUCCEED) {
184 fprintf(stderr, "dbbind error: %d, %d, %d", i, cell->type, cell->len);
186 if ((sql->err != SUCCEED) && (sql->trans != NULL)) {
187 sql->trans->errnum = sql->err;
191 return (sql->err == SUCCEED) ? 0 : 1;
193 static const char *dbd_untaint(apr_pool_t *pool, regex_t *rx, const char *val)
197 /* no untaint expression */
200 if (regexec(rx, val, 1, match, 0) == 0) {
201 return apr_pstrndup(pool, val+match[0].rm_so,
202 match[0].rm_eo - match[0].rm_so);
206 static const char *dbd_statement(apr_pool_t *pool,
207 apr_dbd_prepared_t *stmt,
208 int nargs, const char **args)
218 /* compute upper bound on length (since untaint shrinks) */
219 len = strlen(stmt->fmt) +1;
220 for (i=0; i<nargs; ++i) {
221 len += strlen(args[i]) - 2;
225 p_out = ret = apr_palloc(pool, len);
226 /* FIXME silly bug - this'll catch %%s */
227 while (q = strstr(p_in, "%s"), q != NULL) {
229 strncpy(p_out, p_in, len);
232 var = dbd_untaint(pool, stmt->taint[i], args[i]);
234 strncpy(p_out, var, len);
242 static int dbd_freetds_pselect(apr_pool_t *pool, apr_dbd_t *sql,
243 apr_dbd_results_t **results,
244 apr_dbd_prepared_t *statement,
245 int seek, const char **values)
247 const char *query = dbd_statement(pool, statement,
248 statement->nargs, values);
249 return dbd_freetds_select(pool, sql, results, query, seek);
251 static int dbd_freetds_pvselect(apr_pool_t *pool, apr_dbd_t *sql,
252 apr_dbd_results_t **results,
253 apr_dbd_prepared_t *statement,
254 int seek, va_list args)
259 if (sql->trans && sql->trans->errnum) {
260 return sql->trans->errnum;
263 values = apr_palloc(pool, sizeof(*values) * statement->nargs);
265 for (i = 0; i < statement->nargs; i++) {
266 values[i] = va_arg(args, const char*);
269 return dbd_freetds_pselect(pool, sql, results, statement, seek, values);
271 static int dbd_freetds_query(apr_dbd_t *sql, int *nrows, const char *query);
272 static int dbd_freetds_pquery(apr_pool_t *pool, apr_dbd_t *sql,
273 int *nrows, apr_dbd_prepared_t *statement,
276 const char *query = dbd_statement(pool, statement,
277 statement->nargs, values);
278 return dbd_freetds_query(sql, nrows, query);
280 static int dbd_freetds_pvquery(apr_pool_t *pool, apr_dbd_t *sql, int *nrows,
281 apr_dbd_prepared_t *statement, va_list args)
286 if (sql->trans && sql->trans->errnum) {
287 return sql->trans->errnum;
290 values = apr_palloc(pool, sizeof(*values) * statement->nargs);
292 for (i = 0; i < statement->nargs; i++) {
293 values[i] = va_arg(args, const char*);
295 return dbd_freetds_pquery(pool, sql, nrows, statement, values);
298 static int dbd_freetds_get_row(apr_pool_t *pool, apr_dbd_results_t *res,
299 apr_dbd_row_t **rowp, int rownum)
302 apr_dbd_row_t *row = *rowp;
303 int sequential = ((rownum >= 0) && res->random) ? 0 : 1;
306 row = apr_palloc(pool, sizeof(apr_dbd_row_t));
321 rv = dbnextrow(res->proc);
324 rv = (rownum >= 0) ? dbgetrow(res->proc, rownum) : NO_MORE_ROWS;
327 case SUCCEED: return 0;
328 case REG_ROW: return 0;
330 apr_pool_cleanup_run(res->pool, res->proc, clear_result);
334 case BUF_FULL: return 2; /* FIXME */
341 static const char *dbd_freetds_get_entry(const apr_dbd_row_t *row, int n)
343 /* FIXME: support different data types */
344 /* this fails - bind gets some vars but not others
345 return (const char*)row->res->vars[n].data;
347 DBPROCESS* proc = row->res->proc;
348 BYTE *ptr = dbdata(proc, n+1);
349 int t = dbcoltype(proc, n+1);
350 int l = dbcollen(proc, n+1);
351 if (dbwillconvert(t, SYBCHAR)) {
352 dbconvert(proc, t, ptr, l, SYBCHAR, (BYTE *)row->buf, -1);
353 return (const char*)row->buf;
358 static const char *dbd_freetds_error(apr_dbd_t *sql, int n)
360 /* XXX this doesn't seem to exist in the API ??? */
361 return apr_psprintf(sql->pool, "Error %d", sql->err);
364 static int dbd_freetds_query(apr_dbd_t *sql, int *nrows, const char *query)
366 if (sql->trans && sql->trans->errnum) {
367 return sql->trans->errnum;
370 sql->err = freetds_exec(sql->proc, query, 0, nrows);
372 if (sql->err != SUCCEED) {
374 sql->trans->errnum = sql->err;
381 static const char *dbd_freetds_escape(apr_pool_t *pool, const char *arg,
387 static apr_status_t freetds_regfree(void *rx)
389 regfree((regex_t*)rx);
392 static int recurse_args(apr_pool_t *pool, int n, const char *query,
393 apr_dbd_prepared_t *stmt, int offs)
396 /* we only support %s arguments for now */
399 regmatch_t matches[3];
400 if (regexec(&dbd_freetds_find_arg, query, 3, matches, 0) != 0) {
403 stmt->taint = apr_palloc(pool, n*sizeof(regex_t*));
404 stmt->sz = apr_palloc(pool, n*sizeof(int));
410 int len = matches[1].rm_eo - matches[1].rm_so - 2;
415 ret = recurse_args(pool, n+1, query+matches[0].rm_eo,
416 stmt, offs+matches[0].rm_eo);
418 memmove(stmt->fmt + offs + matches[1].rm_so,
419 stmt->fmt + offs + matches[0].rm_eo-1,
420 strlen(stmt->fmt+offs+matches[0].rm_eo)+2);
422 /* compile untaint to a regex if found */
423 if (matches[1].rm_so == -1) {
424 stmt->taint[n] = NULL;
427 strncpy(arg, query+matches[1].rm_so+1,
428 matches[1].rm_eo - matches[1].rm_so - 2);
429 arg[matches[1].rm_eo - matches[1].rm_so - 2] = '\0';
430 stmt->taint[n] = apr_palloc(pool, sizeof(regex_t));
431 if (regcomp(stmt->taint[n], arg, REG_ICASE|REG_EXTENDED) != 0) {
435 apr_pool_cleanup_register(pool, stmt->taint[n], freetds_regfree,
436 apr_pool_cleanup_null);
440 /* record length if specified */
441 for (i=matches[2].rm_so; i<matches[2].rm_eo; ++i) {
442 sz = 10*sz + (query[i]-'\0');
448 static int dbd_freetds_prepare(apr_pool_t *pool, apr_dbd_t *sql,
449 const char *query, const char *label,
450 int nargs, int nvals, apr_dbd_type_e *types,
451 apr_dbd_prepared_t **statement)
453 apr_dbd_prepared_t *stmt;
456 label = apr_psprintf(pool, "%d", labelnum++);
460 *statement = apr_palloc(pool, sizeof(apr_dbd_prepared_t));
466 stmt->fmt = apr_pstrdup(pool, query);
467 stmt->fmt = recurse_args(pool, 0, query, stmt, stmt->fmt);
469 /* overestimate by a byte or two to simplify */
470 len = strlen("CREATE PROC apr.")
472 + stmt->nargs * strlen(" @arg1 varchar(len1),")
473 + strlen(" AS begin ")
475 + strlen(" end "); /* extra byte for terminator */
477 pquery = apr_pcalloc(pool, len);
478 sprintf(pquery, "CREATE PROC apr.%s", label);
479 for (i=0; i<stmt->nargs; ++i) {
480 sprintf(pquery+strlen(pquery), " @arg%d varchar(%d)", i, stmt->sz[i]);
481 if (i < stmt->nargs-1) {
482 pquery[strlen(pquery)] = ',';
485 strcat(pquery, " AS BEGIN ");
486 strcat(pquery, stmt->fmt);
487 strcat(pquery, " END");
489 return (freetds_exec(sql->proc, pquery, 0, &i) == SUCCEED) ? 0 : 1;
491 stmt->fmt = apr_pstrdup(pool, query);
492 return recurse_args(pool, 0, query, stmt, 0);
497 static int dbd_freetds_start_transaction(apr_pool_t *pool, apr_dbd_t *handle,
498 apr_dbd_transaction_t **trans)
502 /* XXX handle recursive transactions here */
504 handle->err = freetds_exec(handle->proc, "BEGIN TRANSACTION", 0, &dummy);
506 if (dbd_freetds_is_success(handle->err)) {
508 *trans = apr_pcalloc(pool, sizeof(apr_dbd_transaction_t));
510 (*trans)->handle = handle;
511 handle->trans = *trans;
518 static int dbd_freetds_end_transaction(apr_dbd_transaction_t *trans)
522 /* rollback on error or explicit rollback request */
525 trans->handle->err = freetds_exec(trans->handle->proc,
526 "ROLLBACK", 0, &dummy);
529 trans->handle->err = freetds_exec(trans->handle->proc,
530 "COMMIT", 0, &dummy);
532 trans->handle->trans = NULL;
534 return (trans->handle->err == SUCCEED) ? 0 : 1;
537 static DBPROCESS *freetds_open(apr_pool_t *pool, const char *params,
543 static const char *delims = " \r\n\t;|,";
550 char *databaseName = NULL;
552 /* FIXME - this uses malloc */
553 /* FIXME - pass error message back to the caller in case of failure */
558 /* now set login properties */
559 for (ptr = strchr(params, '='); ptr; ptr = strchr(ptr, '=')) {
560 /* don't dereference memory that may not belong to us */
565 for (key = ptr-1; apr_isspace(*key); --key);
567 while (apr_isalpha(*key)) {
572 for (value = ptr+1; apr_isspace(*value); ++value);
574 vlen = strcspn(value, delims);
575 buf = apr_pstrndup(pool, value, vlen); /* NULL-terminated copy */
577 if (!strncasecmp(key, "username", klen)) {
578 DBSETLUSER(login, buf);
580 else if (!strncasecmp(key, "password", klen)) {
581 DBSETLPWD(login, buf);
583 else if (!strncasecmp(key, "appname", klen)) {
584 DBSETLAPP(login, buf);
586 else if (!strncasecmp(key, "dbname", klen)) {
589 else if (!strncasecmp(key, "host", klen)) {
590 DBSETLHOST(login, buf);
592 else if (!strncasecmp(key, "charset", klen)) {
593 DBSETLCHARSET(login, buf);
595 else if (!strncasecmp(key, "lang", klen)) {
596 DBSETLNATLANG(login, buf);
598 else if (!strncasecmp(key, "server", klen)) {
607 process = dbopen(login, server);
609 if (process != NULL && databaseName != NULL)
611 dbuse(process, databaseName);
615 if (process == NULL) {
621 static apr_dbd_t *dbd_freetds_open(apr_pool_t *pool, const char *params,
625 /* FIXME - pass error message back to the caller in case of failure */
626 DBPROCESS *process = freetds_open(pool, params, error);
627 if (process == NULL) {
630 sql = apr_pcalloc(pool, sizeof (apr_dbd_t));
633 sql->params = params;
637 static apr_status_t dbd_freetds_close(apr_dbd_t *handle)
639 dbclose(handle->proc);
643 static apr_status_t dbd_freetds_check_conn(apr_pool_t *pool,
646 if (dbdead(handle->proc)) {
648 dbclose(handle->proc);
649 handle->proc = freetds_open(handle->pool, handle->params, NULL);
650 if (!handle->proc || dbdead(handle->proc)) {
654 /* clear it, in case this is called in error handling */
655 dbcancel(handle->proc);
659 static int dbd_freetds_select_db(apr_pool_t *pool, apr_dbd_t *handle,
662 /* ouch, it's declared int. But we can use APR 0/nonzero */
663 return (dbuse(handle->proc, (char*)name) == SUCCEED) ? APR_SUCCESS : APR_EGENERAL;
666 static void *dbd_freetds_native(apr_dbd_t *handle)
671 static int dbd_freetds_num_cols(apr_dbd_results_t* res)
676 static int dbd_freetds_num_tuples(apr_dbd_results_t* res)
686 static apr_status_t freetds_term(void *dummy)
689 regfree(&dbd_freetds_find_arg);
692 static int freetds_err_handler(DBPROCESS *dbproc, int severity, int dberr,
693 int oserr, char *dberrstr, char *oserrstr)
695 return INT_CANCEL; /* never exit */
697 static void dbd_freetds_init(apr_pool_t *pool)
699 int rv = regcomp(&dbd_freetds_find_arg,
700 "%(\\{[^}]*\\})?([0-9]*)[A-Za-z]", REG_EXTENDED);
703 regerror(rv, &dbd_freetds_find_arg, errmsg, 256);
704 fprintf(stderr, "regcomp failed: %s\n", errmsg);
707 dberrhandle(freetds_err_handler);
708 apr_pool_cleanup_register(pool, NULL, freetds_term, apr_pool_cleanup_null);
712 /* get_name is the only one of these that is implemented */
713 static const char *dbd_freetds_get_name(const apr_dbd_results_t *res, int n)
715 return (const char*) dbcolname(res->proc, n+1); /* numbering starts at 1 */
718 /* These are stubs: transaction modes not implemented here */
719 #define DBD_NOTIMPL APR_ENOTIMPL;
720 static int dbd_freetds_transaction_mode_get(apr_dbd_transaction_t *trans)
722 return trans ? trans->mode : APR_DBD_TRANSACTION_COMMIT;
725 static int dbd_freetds_transaction_mode_set(apr_dbd_transaction_t *trans,
729 trans->mode = mode & TXN_MODE_BITS;
732 return APR_DBD_TRANSACTION_COMMIT;
734 static int dbd_freetds_pvbquery(apr_pool_t *pool, apr_dbd_t *sql, int *nrows,
735 apr_dbd_prepared_t *statement, va_list args)
739 static int dbd_freetds_pbquery(apr_pool_t *pool, apr_dbd_t *sql, int *nrows,
740 apr_dbd_prepared_t * statement,
746 static int dbd_freetds_pvbselect(apr_pool_t *pool, apr_dbd_t *sql,
747 apr_dbd_results_t **results,
748 apr_dbd_prepared_t *statement,
749 int seek, va_list args)
753 static int dbd_freetds_pbselect(apr_pool_t *pool, apr_dbd_t *sql,
754 apr_dbd_results_t **results,
755 apr_dbd_prepared_t *statement,
756 int seek, const void **values)
760 static apr_status_t dbd_freetds_datum_get(const apr_dbd_row_t *row, int n,
761 apr_dbd_type_e type, void *data)
767 APU_MODULE_DECLARE_DATA const apr_dbd_driver_t apr_dbd_freetds_driver = {
772 dbd_freetds_check_conn,
774 dbd_freetds_select_db,
775 dbd_freetds_start_transaction,
776 dbd_freetds_end_transaction,
779 dbd_freetds_num_cols,
780 dbd_freetds_num_tuples,
782 dbd_freetds_get_entry,
787 dbd_freetds_pvselect,
790 /* this is only implemented to support httpd/2.2 standard usage,
791 * as in the original DBD implementation. Everything else is NOTIMPL.
794 dbd_freetds_get_name,
795 dbd_freetds_transaction_mode_get,
796 dbd_freetds_transaction_mode_set,
798 dbd_freetds_pvbquery,
799 dbd_freetds_pvbselect,
801 dbd_freetds_pbselect,
802 dbd_freetds_datum_get