PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - sqlite - pdo_sqlite2.c
Test: PHP Code Coverage
Date: 2009-11-23 Instrumented lines: 283
Code covered: 67.1 % Executed lines: 190
Legend: not executed executed

       1                 : /*
       2                 :   +----------------------------------------------------------------------+
       3                 :   | PHP Version 6                                                        |
       4                 :   +----------------------------------------------------------------------+
       5                 :   | Copyright (c) 1997-2009 The PHP Group                                |
       6                 :   +----------------------------------------------------------------------+
       7                 :   | This source file is subject to version 3.01 of the PHP license,      |
       8                 :   | that is bundled with this package in the file LICENSE, and is        |
       9                 :   | available through the world-wide-web at the following url:           |
      10                 :   | http://www.php.net/license/3_01.txt                                  |
      11                 :   | If you did not receive a copy of the PHP license and are unable to   |
      12                 :   | obtain it through the world-wide-web, please send a note to          |
      13                 :   | license@php.net so we can mail you a copy immediately.               |
      14                 :   +----------------------------------------------------------------------+
      15                 :   | Author: Wez Furlong <wez@php.net>                                    |
      16                 :   +----------------------------------------------------------------------+
      17                 : */
      18                 : 
      19                 : /* $Id: pdo_sqlite2.c 276986 2009-03-10 23:40:06Z helly $ */
      20                 : #ifdef HAVE_CONFIG_H
      21                 : #include "config.h"
      22                 : #endif
      23                 : #include "php.h"
      24                 : 
      25                 : #ifdef PHP_SQLITE2_HAVE_PDO
      26                 : #include "sqlite.h"
      27                 : #include "pdo/php_pdo.h"
      28                 : #include "pdo/php_pdo_driver.h"
      29                 : #include "zend_exceptions.h"
      30                 : 
      31                 : #define php_sqlite_encode_binary(in, n, out) sqlite_encode_binary((const unsigned char *)in, n, (unsigned char *)out)
      32                 : #define php_sqlite_decode_binary(in, out)    sqlite_decode_binary((const unsigned char *)in, (unsigned char *)out)
      33                 : 
      34                 : 
      35                 : typedef struct {
      36                 :         const char *file;
      37                 :         int line;
      38                 :         unsigned int errcode;
      39                 :         char *errmsg;
      40                 : } pdo_sqlite2_error_info;
      41                 : 
      42                 : typedef struct {
      43                 :         sqlite *db;
      44                 :         pdo_sqlite2_error_info einfo;
      45                 : } pdo_sqlite2_db_handle;
      46                 : 
      47                 : typedef struct {
      48                 :         pdo_sqlite2_db_handle   *H;
      49                 :         sqlite_vm *vm;
      50                 :         const char **rowdata, **colnames;
      51                 :         int ncols;
      52                 :         unsigned pre_fetched:1;
      53                 :         unsigned done:1;
      54                 :         pdo_sqlite2_error_info einfo;
      55                 : } pdo_sqlite2_stmt;
      56                 : 
      57                 : extern int _pdo_sqlite2_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, char *errmsg, const char *file, int line TSRMLS_DC);
      58                 : #define pdo_sqlite2_error(msg, s) _pdo_sqlite2_error(s, NULL, msg, __FILE__, __LINE__ TSRMLS_CC)
      59                 : #define pdo_sqlite2_error_stmt(msg, s) _pdo_sqlite2_error(stmt->dbh, stmt, msg, __FILE__, __LINE__ TSRMLS_CC)
      60                 : 
      61                 : extern struct pdo_stmt_methods sqlite2_stmt_methods;
      62                 : 
      63                 : static int pdo_sqlite2_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC)
      64              85 : {
      65              85 :         pdo_sqlite2_stmt *S = (pdo_sqlite2_stmt*)stmt->driver_data;
      66                 : 
      67              85 :         if (S->vm) {
      68              72 :                 char *errmsg = NULL;
      69              72 :                 sqlite_finalize(S->vm, &errmsg);
      70              72 :                 if (errmsg) {
      71               0 :                         sqlite_freemem(errmsg);
      72                 :                 }
      73              72 :                 S->vm = NULL;
      74                 :         }
      75              85 :         if (S->einfo.errmsg) {
      76               5 :                 pefree(S->einfo.errmsg, stmt->dbh->is_persistent);
      77                 :         }
      78              85 :         efree(S);
      79              85 :         return 1;
      80                 : }
      81                 : 
      82                 : static int pdo_sqlite2_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
      83             143 : {
      84             143 :         pdo_sqlite2_stmt *S = (pdo_sqlite2_stmt*)stmt->driver_data;
      85             143 :         char *errmsg = NULL;
      86                 :         const char *tail;
      87                 : 
      88             143 :         if (stmt->executed && !S->done) {
      89               6 :                 sqlite_finalize(S->vm, &errmsg);
      90               6 :                 pdo_sqlite2_error_stmt(errmsg, stmt);
      91               6 :                 errmsg = NULL;
      92               6 :                 S->vm = NULL;
      93                 :         }
      94                 : 
      95             143 :         S->einfo.errcode = sqlite_compile(S->H->db, stmt->active_query_string, &tail, &S->vm, &errmsg);
      96             143 :         if (S->einfo.errcode != SQLITE_OK) {
      97               1 :                 pdo_sqlite2_error_stmt(errmsg, stmt);
      98               1 :                 return 0;
      99                 :         }
     100                 : 
     101             142 :         S->done = 0;
     102             142 :         S->einfo.errcode = sqlite_step(S->vm, &S->ncols, &S->rowdata, &S->colnames);
     103             142 :         switch (S->einfo.errcode) {
     104                 :                 case SQLITE_ROW:
     105             104 :                         S->pre_fetched = 1;
     106             104 :                         stmt->column_count = S->ncols;
     107             104 :                         return 1;
     108                 : 
     109                 :                 case SQLITE_DONE:
     110              37 :                         stmt->column_count = S->ncols;
     111              37 :                         stmt->row_count = sqlite_changes(S->H->db);
     112              37 :                         S->einfo.errcode = sqlite_reset(S->vm, &errmsg);
     113              37 :                         if (S->einfo.errcode != SQLITE_OK) {
     114               0 :                                 pdo_sqlite2_error_stmt(errmsg, stmt);
     115                 :                         }
     116              37 :                         S->done = 1;
     117              37 :                         return 1;
     118                 : 
     119                 :                 case SQLITE_ERROR:
     120                 :                 case SQLITE_MISUSE:
     121                 :                 case SQLITE_BUSY:
     122                 :                 default:
     123               1 :                         pdo_sqlite2_error_stmt(errmsg, stmt);
     124               1 :                         return 0;
     125                 :         }
     126                 : }
     127                 : 
     128                 : static int pdo_sqlite2_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
     129                 :                 enum pdo_param_event event_type TSRMLS_DC)
     130             704 : {
     131             704 :         return 1;
     132                 : }
     133                 : 
     134                 : static int pdo_sqlite2_stmt_fetch(pdo_stmt_t *stmt,
     135                 :         enum pdo_fetch_orientation ori, long offset TSRMLS_DC)
     136             274 : {
     137             274 :         pdo_sqlite2_stmt *S = (pdo_sqlite2_stmt*)stmt->driver_data;
     138             274 :         char *errmsg = NULL;
     139                 : 
     140             274 :         if (!S->vm) {
     141               0 :                 return 0;       
     142                 :         }
     143             274 :         if (S->pre_fetched) {
     144             101 :                 S->pre_fetched = 0;
     145             101 :                 return 1;
     146                 :         }
     147             173 :         if (S->done) {
     148               3 :                 return 0;
     149                 :         }
     150                 : 
     151             170 :         S->einfo.errcode = sqlite_step(S->vm, &S->ncols, &S->rowdata, &S->colnames);
     152             170 :         switch (S->einfo.errcode) {
     153                 :                 case SQLITE_ROW:
     154              92 :                         return 1;
     155                 : 
     156                 :                 case SQLITE_DONE:
     157              78 :                         S->done = 1;
     158              78 :                         S->einfo.errcode = sqlite_reset(S->vm, &errmsg);
     159              78 :                         if (S->einfo.errcode != SQLITE_OK) {
     160               0 :                                 pdo_sqlite2_error_stmt(errmsg, stmt);
     161               0 :                                 errmsg = NULL;
     162                 :                         }
     163              78 :                         return 0;
     164                 : 
     165                 :                 default:
     166               0 :                         pdo_sqlite2_error_stmt(errmsg, stmt);
     167               0 :                         return 0;
     168                 :         }
     169                 : }
     170                 : 
     171                 : static int pdo_sqlite2_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
     172             107 : {
     173             107 :         pdo_sqlite2_stmt *S = (pdo_sqlite2_stmt*)stmt->driver_data;
     174                 : 
     175             107 :         if(colno >= S->ncols) {
     176                 :                 /* error invalid column */
     177               0 :                 pdo_sqlite2_error_stmt(NULL, stmt);
     178               0 :                 return 0;
     179                 :         }
     180                 : 
     181             107 :         stmt->columns[colno].name = estrdup(S->colnames[colno]);
     182             107 :         stmt->columns[colno].namelen = strlen(stmt->columns[colno].name);
     183             107 :         stmt->columns[colno].maxlen = 0xffffffff;
     184             107 :         stmt->columns[colno].precision = 0;
     185             107 :         stmt->columns[colno].param_type = PDO_PARAM_STR;
     186                 : 
     187             107 :         return 1;
     188                 : }
     189                 : 
     190                 : static int pdo_sqlite2_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC)
     191             367 : {
     192             367 :         pdo_sqlite2_stmt *S = (pdo_sqlite2_stmt*)stmt->driver_data;
     193             367 :         if (!S->vm) {
     194               0 :                 return 0;
     195                 :         }
     196             367 :         if(colno >= S->ncols) {
     197                 :                 /* error invalid column */
     198               0 :                 pdo_sqlite2_error_stmt(NULL, stmt);
     199               0 :                 return 0;
     200                 :         }
     201             367 :         if (S->rowdata[colno]) {
     202             350 :                 if (S->rowdata[colno][0] == '\x01') {
     203                 :                         /* encoded */
     204               0 :                         *caller_frees = 1;
     205               0 :                         *ptr = emalloc(strlen(S->rowdata[colno]));
     206               0 :                         *len = php_sqlite_decode_binary(S->rowdata[colno]+1, *ptr);
     207               0 :                         (*(char**)ptr)[*len] = '\0';
     208                 :                 } else {
     209             350 :                         *ptr = (char*)S->rowdata[colno];
     210             350 :                         *len = strlen(*ptr);
     211                 :                 }
     212                 :         } else {
     213              17 :                 *ptr = NULL;
     214              17 :                 *len = 0;
     215                 :         }
     216             367 :         return 1;
     217                 : }
     218                 : 
     219                 : struct pdo_stmt_methods sqlite2_stmt_methods = {
     220                 :         pdo_sqlite2_stmt_dtor,
     221                 :         pdo_sqlite2_stmt_execute,
     222                 :         pdo_sqlite2_stmt_fetch,
     223                 :         pdo_sqlite2_stmt_describe,
     224                 :         pdo_sqlite2_stmt_get_col,
     225                 :         pdo_sqlite2_stmt_param_hook,
     226                 :         NULL, /* set_attr */
     227                 :         NULL, /* get_attr */
     228                 :         NULL
     229                 : };
     230                 : 
     231                 : 
     232                 : int _pdo_sqlite2_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, char *errmsg, const char *file, int line TSRMLS_DC) /* {{{ */
     233             273 : {
     234             273 :         pdo_sqlite2_db_handle *H = (pdo_sqlite2_db_handle *)dbh->driver_data;
     235             273 :         pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code;
     236             273 :         pdo_sqlite2_error_info *einfo = &H->einfo;
     237                 :         pdo_sqlite2_stmt *S;
     238                 : 
     239             273 :         if (stmt) {
     240               8 :                 S = stmt->driver_data;
     241               8 :                 einfo = &S->einfo;
     242                 :         }
     243                 : 
     244             273 :         einfo->file = file;
     245             273 :         einfo->line = line;
     246                 : 
     247             273 :         if (einfo->errmsg) {
     248             180 :                 pefree(einfo->errmsg, dbh->is_persistent);
     249             180 :                 einfo->errmsg = NULL;
     250                 :         }
     251                 : 
     252             273 :         if (einfo->errcode != SQLITE_OK) {
     253             273 :                 if (errmsg) {
     254             266 :                         einfo->errmsg = pestrdup(errmsg, dbh->is_persistent);
     255             266 :                         sqlite_freemem(errmsg);
     256                 :                 } else {
     257               7 :                         einfo->errmsg = pestrdup(sqlite_error_string(einfo->errcode), dbh->is_persistent);
     258                 :                 }
     259                 :         } else { /* no error */
     260               0 :                 strcpy(*pdo_err, PDO_ERR_NONE);
     261               0 :                 return 0;
     262                 :         }
     263             273 :         switch (einfo->errcode) {
     264                 :                 case SQLITE_NOTFOUND:
     265               0 :                         strcpy(*pdo_err, "42S02");
     266               0 :                         break;  
     267                 : 
     268                 :                 case SQLITE_INTERRUPT:
     269               0 :                         strcpy(*pdo_err, "01002");
     270               0 :                         break;
     271                 : 
     272                 :                 case SQLITE_NOLFS:
     273               0 :                         strcpy(*pdo_err, "HYC00");
     274               0 :                         break;
     275                 : 
     276                 :                 case SQLITE_TOOBIG:
     277               0 :                         strcpy(*pdo_err, "22001");
     278               0 :                         break;
     279                 : 
     280                 :                 case SQLITE_CONSTRAINT:
     281               0 :                         strcpy(*pdo_err, "23000");
     282               0 :                         break;
     283                 : 
     284                 :                 case SQLITE_ERROR:
     285                 :                 default:
     286             273 :                         strcpy(*pdo_err, "HY000");
     287                 :                         break;
     288                 :         }
     289                 : 
     290             273 :         if (!dbh->methods) {
     291                 : #if PHP_VERSION_ID > 50200
     292               0 :                 zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
     293                 :                                 *pdo_err, einfo->errcode, einfo->errmsg);
     294                 : #else
     295                 :                 zend_throw_exception_ex(php_pdo_get_exception(TSRMLS_C), 0 TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
     296                 :                                 *pdo_err, einfo->errcode, einfo->errmsg);
     297                 : #endif
     298                 :         }
     299                 : 
     300             273 :         return einfo->errcode;
     301                 : }
     302                 : /* }}} */
     303                 : 
     304                 : static int pdo_sqlite2_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC)
     305              17 : {
     306              17 :         pdo_sqlite2_db_handle *H = (pdo_sqlite2_db_handle *)dbh->driver_data;
     307              17 :         pdo_sqlite2_error_info *einfo = &H->einfo;
     308                 :         pdo_sqlite2_stmt *S;
     309                 : 
     310              17 :         if (stmt) {
     311              12 :                 S = stmt->driver_data;
     312              12 :                 einfo = &S->einfo;
     313                 :         }
     314                 : 
     315              17 :         if (einfo->errcode) {
     316               8 :                 add_next_index_long(info, einfo->errcode);
     317               8 :                 if (einfo->errmsg) {
     318               4 :                         add_next_index_string(info, einfo->errmsg, 1);
     319                 :                 }
     320                 :         }
     321                 : 
     322              17 :         return 1;
     323                 : }
     324                 : 
     325                 : static int sqlite2_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
     326              88 : {
     327              88 :         pdo_sqlite2_db_handle *H = (pdo_sqlite2_db_handle *)dbh->driver_data;
     328                 :         
     329              88 :         if (H) {
     330              88 :                 if (H->db) {
     331              88 :                         sqlite_close(H->db);
     332              88 :                         H->db = NULL;
     333                 :                 }
     334              88 :                 if (H->einfo.errmsg) {
     335              87 :                         pefree(H->einfo.errmsg, dbh->is_persistent);
     336              87 :                         H->einfo.errmsg = NULL;
     337                 :                 }
     338              88 :                 pefree(H, dbh->is_persistent);
     339              88 :                 dbh->driver_data = NULL;
     340                 :         }
     341              88 :         return 0;
     342                 : }
     343                 : /* }}} */
     344                 : 
     345                 : static int sqlite2_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC)
     346              86 : {
     347              86 :         pdo_sqlite2_db_handle *H = (pdo_sqlite2_db_handle *)dbh->driver_data;
     348              86 :         pdo_sqlite2_stmt *S = ecalloc(1, sizeof(pdo_sqlite2_stmt));
     349                 : 
     350              86 :         S->H = H;
     351              86 :         stmt->driver_data = S;
     352              86 :         stmt->methods = &sqlite2_stmt_methods;
     353              86 :         stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
     354                 : 
     355              86 :         if (PDO_CURSOR_FWDONLY != pdo_attr_lval(driver_options, PDO_ATTR_CURSOR, PDO_CURSOR_FWDONLY TSRMLS_CC)) {
     356               0 :                 H->einfo.errcode = SQLITE_ERROR;
     357               0 :                 pdo_sqlite2_error(NULL, dbh);
     358               0 :                 return 0;
     359                 :         }
     360                 : 
     361              86 :         return 1;
     362                 : }
     363                 : 
     364                 : static long sqlite2_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC)
     365             389 : {
     366             389 :         pdo_sqlite2_db_handle *H = (pdo_sqlite2_db_handle *)dbh->driver_data;
     367             389 :         char *errmsg = NULL;
     368                 : 
     369             389 :         if ((H->einfo.errcode = sqlite_exec(H->db, sql, NULL, NULL, &errmsg)) != SQLITE_OK) {
     370             265 :                 pdo_sqlite2_error(errmsg, dbh);
     371             265 :                 return -1;
     372                 :         } else {
     373             124 :                 return sqlite_changes(H->db);
     374                 :         }
     375                 : }
     376                 : 
     377                 : static char *pdo_sqlite2_last_insert_id(pdo_dbh_t *dbh, const char *name, int *len TSRMLS_DC)
     378               0 : {
     379               0 :         pdo_sqlite2_db_handle *H = (pdo_sqlite2_db_handle *)dbh->driver_data;
     380                 :         char *id;
     381                 :         
     382               0 :         id = php_pdo_int64_to_str(sqlite_last_insert_rowid(H->db) TSRMLS_CC);
     383               0 :         *len = strlen(id);
     384               0 :         return id;
     385                 : }
     386                 : 
     387                 : static int sqlite2_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, char **quoted, int *quotedlen, enum pdo_param_type paramtype  TSRMLS_DC)
     388              98 : {
     389                 :         char *ret;
     390                 : 
     391              98 :         if (unquotedlen && (unquoted[0] == '\x01' || memchr(unquoted, '\0', unquotedlen) != NULL)) {
     392                 :                 /* binary string */
     393                 :                 int len;
     394               0 :                 ret = safe_emalloc(1 + unquotedlen / 254, 257, 5);
     395               0 :                 ret[0] = '\'';
     396               0 :                 ret[1] = '\x01';
     397               0 :                 len = php_sqlite_encode_binary(unquoted, unquotedlen, ret+2);
     398               0 :                 ret[len + 2] = '\'';
     399               0 :                 ret[len + 3] = '\0';
     400               0 :                 *quoted = ret;
     401               0 :                 *quotedlen = len + 3;
     402                 :                 /* fprintf(stderr, "Quoting:%d:%.*s:\n", *quotedlen, *quotedlen, *quoted); */
     403               0 :                 return 1;
     404              98 :         } else if (unquotedlen) {
     405              97 :                 ret = sqlite_mprintf("'%q'", unquoted);
     406              97 :                 if (ret) {
     407              97 :                         *quoted = estrdup(ret);
     408              97 :                         *quotedlen = strlen(ret);
     409              97 :                         sqlite_freemem(ret);
     410              97 :                         return 1;
     411                 :                 }
     412               0 :                 return 0;
     413                 :         } else {
     414               1 :                 *quoted = estrdup("''");
     415               1 :                 *quotedlen = 2;
     416               1 :                 return 1;
     417                 :         }
     418                 : }
     419                 : 
     420                 : static int sqlite2_handle_begin(pdo_dbh_t *dbh TSRMLS_DC)
     421               3 : {
     422               3 :         pdo_sqlite2_db_handle *H = (pdo_sqlite2_db_handle *)dbh->driver_data;
     423               3 :         char *errmsg = NULL;
     424                 : 
     425               3 :         if (sqlite_exec(H->db, "BEGIN", NULL, NULL, &errmsg) != SQLITE_OK) {
     426               0 :                 pdo_sqlite2_error(errmsg, dbh);
     427               0 :                 return 0;
     428                 :         }
     429               3 :         return 1;
     430                 : }
     431                 : 
     432                 : static int sqlite2_handle_commit(pdo_dbh_t *dbh TSRMLS_DC)
     433               1 : {
     434               1 :         pdo_sqlite2_db_handle *H = (pdo_sqlite2_db_handle *)dbh->driver_data;
     435               1 :         char *errmsg = NULL;
     436                 : 
     437               1 :         if (sqlite_exec(H->db, "COMMIT", NULL, NULL, &errmsg) != SQLITE_OK) {
     438               0 :                 pdo_sqlite2_error(errmsg, dbh);
     439               0 :                 return 0;
     440                 :         }
     441               1 :         return 1;
     442                 : }
     443                 : 
     444                 : static int sqlite2_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC)
     445               2 : {
     446               2 :         pdo_sqlite2_db_handle *H = (pdo_sqlite2_db_handle *)dbh->driver_data;
     447               2 :         char *errmsg = NULL;
     448                 : 
     449               2 :         if (sqlite_exec(H->db, "ROLLBACK", NULL, NULL, &errmsg) != SQLITE_OK) {
     450               0 :                 pdo_sqlite2_error(errmsg, dbh);
     451               0 :                 return 0;
     452                 :         }
     453               2 :         return 1;
     454                 : }
     455                 : 
     456                 : static int pdo_sqlite2_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC)
     457               0 : {
     458               0 :         switch (attr) {
     459                 :                 case PDO_ATTR_CLIENT_VERSION:
     460                 :                 case PDO_ATTR_SERVER_VERSION:
     461               0 :                         ZVAL_STRING(return_value, (char *)sqlite_libversion(), 1);
     462                 :                         break;
     463                 :                 
     464                 :                 default:
     465               0 :                         return 0;       
     466                 :         }
     467                 : 
     468               0 :         return 1;
     469                 : }
     470                 : 
     471                 : static int pdo_sqlite2_set_attr(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_DC)
     472               0 : {
     473               0 :         pdo_sqlite2_db_handle *H = (pdo_sqlite2_db_handle *)dbh->driver_data;
     474                 : 
     475               0 :         switch (attr) {
     476                 :                 case PDO_ATTR_TIMEOUT:
     477               0 :                         convert_to_long(val);
     478               0 :                         sqlite_busy_timeout(H->db, Z_LVAL_P(val) * 1000);
     479               0 :                         return 1;
     480                 :         }
     481               0 :         return 0;
     482                 : }
     483                 : 
     484                 : static PHP_FUNCTION(sqlite2_create_function)
     485               0 : {
     486                 :         /* TODO: implement this stuff */
     487               0 : }
     488                 : 
     489                 : static const zend_function_entry dbh_methods[] = {
     490                 :         PHP_FE(sqlite2_create_function, NULL)
     491                 :         {NULL, NULL, NULL}
     492                 : };
     493                 : 
     494                 : static const zend_function_entry *get_driver_methods(pdo_dbh_t *dbh, int kind TSRMLS_DC)
     495               0 : {
     496               0 :         switch (kind) {
     497                 :                 case PDO_DBH_DRIVER_METHOD_KIND_DBH:
     498               0 :                         return dbh_methods;
     499                 : 
     500                 :                 default:
     501               0 :                         return NULL;
     502                 :         }
     503                 : }
     504                 : 
     505                 : static struct pdo_dbh_methods sqlite2_methods = {
     506                 :         sqlite2_handle_closer,
     507                 :         sqlite2_handle_preparer,
     508                 :         sqlite2_handle_doer,
     509                 :         sqlite2_handle_quoter,
     510                 :         sqlite2_handle_begin,
     511                 :         sqlite2_handle_commit,
     512                 :         sqlite2_handle_rollback,
     513                 :         pdo_sqlite2_set_attr,
     514                 :         pdo_sqlite2_last_insert_id,
     515                 :         pdo_sqlite2_fetch_error_func,
     516                 :         pdo_sqlite2_get_attribute,
     517                 :         NULL,   /* check_liveness: not needed */
     518                 :         get_driver_methods,
     519                 :         NULL
     520                 : };
     521                 : 
     522                 : static char *make_filename_safe(const char *filename TSRMLS_DC)
     523              89 : {
     524              89 :         if (strncmp(filename, ":memory:", sizeof(":memory:")-1)) {
     525               0 :                 char *fullpath = expand_filepath(filename, NULL TSRMLS_CC);
     526                 : 
     527               0 :                 if (!fullpath) {
     528               0 :                         return NULL;
     529                 :                 }
     530                 : 
     531               0 :                 if (php_check_open_basedir(fullpath TSRMLS_CC)) {
     532               0 :                         efree(fullpath);
     533               0 :                         return NULL;
     534                 :                 }
     535               0 :                 return fullpath;
     536                 :         }
     537              89 :         return estrdup(filename);
     538                 : }
     539                 : 
     540                 : static int authorizer(void *autharg, int access_type, const char *arg3, const char *arg4,
     541                 :                 const char *arg5, const char *arg6)
     542             635 : {
     543                 :         char *filename;
     544             635 :         switch (access_type) {
     545                 :                 case SQLITE_COPY: {
     546                 :                         TSRMLS_FETCH();
     547               0 :                         filename = make_filename_safe(arg4 TSRMLS_CC);
     548               0 :                         if (!filename) {
     549               0 :                                 return SQLITE_DENY;
     550                 :                         }
     551               0 :                         efree(filename);
     552               0 :                         return SQLITE_OK;
     553                 :                 }
     554                 : 
     555                 :                 case SQLITE_ATTACH: {
     556                 :                         TSRMLS_FETCH();
     557               0 :                         filename = make_filename_safe(arg3 TSRMLS_CC);
     558               0 :                         if (!filename) {
     559               0 :                                 return SQLITE_DENY;
     560                 :                         }
     561               0 :                         efree(filename);
     562               0 :                         return SQLITE_OK;
     563                 :                 }
     564                 : 
     565                 :                 default:
     566                 :                         /* access allowed */
     567             635 :                         return SQLITE_OK;
     568                 :         }
     569                 : }
     570                 : 
     571                 : static int pdo_sqlite2_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */
     572              89 : {
     573                 :         pdo_sqlite2_db_handle *H;
     574              89 :         int ret = 0;
     575              89 :         long timeout = 60;
     576                 :         char *filename;
     577              89 :         char *errmsg = NULL;
     578                 : 
     579              89 :         H = pecalloc(1, sizeof(pdo_sqlite2_db_handle), dbh->is_persistent);
     580                 : 
     581              89 :         H->einfo.errcode = 0;
     582              89 :         H->einfo.errmsg = NULL;
     583              89 :         dbh->driver_data = H;
     584                 : 
     585              89 :         filename = make_filename_safe(dbh->data_source TSRMLS_CC);
     586                 : 
     587              89 :         if (!filename) {
     588                 : #if PHP_VERSION_ID > 50200
     589               0 :                 zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC,
     590                 :                                 "open_basedir prohibits opening %s",
     591                 :                                 dbh->data_source);
     592                 : #else
     593                 :                 zend_throw_exception_ex(php_pdo_get_exception(TSRMLS_C), 0 TSRMLS_CC,
     594                 :                                 "open_basedir prohibits opening %s",
     595                 :                                 dbh->data_source);
     596                 : #endif
     597               0 :                 goto cleanup;
     598                 :         }
     599                 : 
     600              89 :         H->db = sqlite_open(filename, 0666, &errmsg);
     601              89 :         efree(filename);
     602                 : 
     603              89 :         if (!H->db) {
     604               0 :                 pdo_sqlite2_error(errmsg, dbh);
     605               0 :                 goto cleanup;
     606                 :         }
     607                 : 
     608              89 :         sqlite_set_authorizer(H->db, authorizer, NULL);
     609                 : 
     610              89 :         if (driver_options) {
     611               0 :                 timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, timeout TSRMLS_CC);
     612                 :         }
     613              89 :         sqlite_busy_timeout(H->db, timeout * 1000);
     614                 : 
     615              89 :         dbh->alloc_own_columns = 1;
     616              89 :         dbh->max_escaped_char_length = 2;
     617                 : 
     618              89 :         ret = 1;
     619                 : 
     620              89 : cleanup:
     621              89 :         dbh->methods = &sqlite2_methods;
     622                 : 
     623              89 :         return ret;
     624                 : }
     625                 : /* }}} */
     626                 : 
     627                 : pdo_driver_t pdo_sqlite2_driver = {
     628                 :         PDO_DRIVER_HEADER(sqlite2),
     629                 :         pdo_sqlite2_handle_factory
     630                 : };
     631                 : 
     632                 : 
     633                 : 
     634                 : #endif
     635                 : 
     636                 : 
     637                 : /*
     638                 :  * Local variables:
     639                 :  * tab-width: 4
     640                 :  * c-basic-offset: 4
     641                 :  * End:
     642                 :  * vim600: noet sw=4 ts=4 fdm=marker
     643                 :  * vim<600: noet sw=4 ts=4
     644                 :  */

Generated by: LTP GCOV extension version 1.5

Generated at Mon, 23 Nov 2009 17:39:39 +0000 (34 hours ago)

Copyright © 2005-2009 The PHP Group
All rights reserved.