Back to index

php5  5.3.10
dblib_driver.c
Go to the documentation of this file.
00001 /*
00002   +----------------------------------------------------------------------+
00003   | PHP Version 5                                                        |
00004   +----------------------------------------------------------------------+
00005   | Copyright (c) 1997-2012 The PHP Group                                |
00006   +----------------------------------------------------------------------+
00007   | This source file is subject to version 3.01 of the PHP license,      |
00008   | that is bundled with this package in the file LICENSE, and is        |
00009   | available through the world-wide-web at the following url:           |
00010   | http://www.php.net/license/3_01.txt                                  |
00011   | If you did not receive a copy of the PHP license and are unable to   |
00012   | obtain it through the world-wide-web, please send a note to          |
00013   | license@php.net so we can mail you a copy immediately.               |
00014   +----------------------------------------------------------------------+
00015   | Author: Wez Furlong <wez@php.net>                                    |
00016   |         Frank M. Kromann <frank@kromann.info>                        |
00017   +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: dblib_driver.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 # include "config.h"
00024 #endif
00025 
00026 #include "php.h"
00027 #include "php_ini.h"
00028 #include "ext/standard/info.h"
00029 #include "pdo/php_pdo.h"
00030 #include "pdo/php_pdo_driver.h"
00031 #include "php_pdo_dblib.h"
00032 #include "php_pdo_dblib_int.h"
00033 #include "zend_exceptions.h"
00034 
00035 static int dblib_fetch_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC)
00036 {
00037        pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
00038        pdo_dblib_err *einfo = &H->err;
00039        pdo_dblib_stmt *S = NULL;
00040        char *message;
00041        char *msg;
00042 
00043        if (stmt) {
00044               S = (pdo_dblib_stmt*)stmt->driver_data;
00045               einfo = &S->err;
00046        }
00047 
00048        if (einfo->dberr == SYBESMSG && einfo->lastmsg) {
00049               msg = einfo->lastmsg;
00050        } else if (einfo->dberr == SYBESMSG && DBLIB_G(err).lastmsg) {
00051               msg = DBLIB_G(err).lastmsg;
00052               DBLIB_G(err).lastmsg = NULL;
00053        } else {
00054               msg = einfo->dberrstr;
00055        }
00056 
00057        spprintf(&message, 0, "%s [%d] (severity %d) [%s]",
00058               msg, einfo->dberr, einfo->severity, stmt ? stmt->active_query_string : "");
00059 
00060        add_next_index_long(info, einfo->dberr);
00061        add_next_index_string(info, message, 0);
00062        add_next_index_long(info, einfo->oserr);
00063        add_next_index_long(info, einfo->severity);
00064        if (einfo->oserrstr) {
00065               add_next_index_string(info, einfo->oserrstr, 1);
00066        }
00067 
00068        return 1;
00069 }
00070 
00071 
00072 static int dblib_handle_closer(pdo_dbh_t *dbh TSRMLS_DC)
00073 {
00074        pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
00075 
00076        if (H) {
00077               if (H->link) {
00078                      dbclose(H->link);
00079                      H->link = NULL;
00080               }
00081               if (H->login) {
00082                      dbfreelogin(H->login);
00083                      H->login = NULL;
00084               }
00085               pefree(H, dbh->is_persistent);
00086               dbh->driver_data = NULL;
00087        }
00088        return 0;
00089 }
00090 
00091 static int dblib_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC)
00092 {
00093        pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
00094        pdo_dblib_stmt *S = ecalloc(1, sizeof(*S));
00095        
00096        S->H = H;
00097        stmt->driver_data = S;
00098        stmt->methods = &dblib_stmt_methods;
00099        stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
00100        S->err.sqlstate = stmt->error_code;
00101 
00102        return 1;
00103 }
00104 
00105 static long dblib_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC)
00106 {
00107        pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
00108        RETCODE ret, resret;
00109 
00110        dbsetuserdata(H->link, (BYTE*)&H->err);
00111 
00112        if (FAIL == dbcmd(H->link, sql)) {
00113               return -1;
00114        }
00115 
00116        if (FAIL == dbsqlexec(H->link)) {
00117               return -1;
00118        }
00119        
00120        resret = dbresults(H->link);
00121 
00122        if (resret == FAIL) {
00123               return -1;
00124        }
00125 
00126        ret = dbnextrow(H->link);
00127        if (ret == FAIL) {
00128               return -1;
00129        }
00130 
00131        if (dbnumcols(H->link) <= 0) {
00132               return DBCOUNT(H->link);
00133        }
00134 
00135        /* throw away any rows it might have returned */
00136        dbcanquery(H->link);
00137 
00138        return DBCOUNT(H->link);
00139 }
00140 
00141 static int dblib_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, char **quoted, int *quotedlen, enum pdo_param_type paramtype TSRMLS_DC)
00142 {
00143        pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
00144        char *q;
00145        int l = 1;
00146 
00147        *quoted = q = safe_emalloc(2, unquotedlen, 3);
00148        *q++ = '\'';
00149 
00150        while (unquotedlen--) {
00151               if (*unquoted == '\'') {
00152                      *q++ = '\'';
00153                      *q++ = '\'';
00154                      l += 2;
00155               } else {
00156                      *q++ = *unquoted;
00157                      ++l;
00158               }
00159               unquoted++;
00160        }
00161 
00162        *q++ = '\'';
00163        *q++ = '\0';
00164        *quotedlen = l+1;
00165        
00166        return 1;
00167 }
00168 
00169 static struct pdo_dbh_methods dblib_methods = {
00170        dblib_handle_closer,
00171        dblib_handle_preparer,
00172        dblib_handle_doer,
00173        dblib_handle_quoter,
00174        NULL,
00175        NULL,
00176        NULL,
00177        NULL,
00178        NULL, /* last insert */
00179        dblib_fetch_error, /* fetch error */
00180        NULL, /* get attr */
00181        NULL, /* check liveness */
00182 };
00183 
00184 static int pdo_dblib_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC)
00185 {
00186        pdo_dblib_db_handle *H;
00187        int i, ret = 0;
00188        struct pdo_data_src_parser vars[] = {
00189               { "charset",  NULL,  0 },
00190               { "appname",  "PHP " PDO_DBLIB_FLAVOUR,   0 },
00191               { "host",            "127.0.0.1", 0 },
00192               { "dbname",          NULL,  0 },
00193               { "secure",          NULL,  0 }, /* DBSETLSECURE */
00194               /* TODO: DBSETLVERSION ? */
00195        };
00196 
00197        php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 5);
00198 
00199        H = pecalloc(1, sizeof(*H), dbh->is_persistent);
00200        H->login = dblogin();
00201        H->err.sqlstate = dbh->error_code;
00202 
00203        if (!H->login) {
00204               goto cleanup;
00205        }
00206 
00207        if (dbh->username) {
00208               DBSETLUSER(H->login, dbh->username);
00209        }
00210        if (dbh->password) {
00211               DBSETLPWD(H->login, dbh->password);
00212        }
00213        
00214 #if !PHP_DBLIB_IS_MSSQL
00215        if (vars[0].optval) {
00216               DBSETLCHARSET(H->login, vars[0].optval);
00217        }
00218 #endif
00219 
00220        DBSETLAPP(H->login, vars[1].optval);
00221 
00222 #if PHP_DBLIB_IS_MSSQL
00223        dbprocerrhandle(H->login, (EHANDLEFUNC) error_handler);
00224        dbprocmsghandle(H->login, (MHANDLEFUNC) msg_handler);
00225 #endif
00226 
00227        H->link = dbopen(H->login, vars[2].optval);
00228 
00229        if (H->link == NULL) {
00230               goto cleanup;
00231        }
00232 
00233        /* dblib do not return more than this length from text/image */
00234        DBSETOPT(H->link, DBTEXTLIMIT, "2147483647");
00235        
00236        /* limit text/image from network */
00237        DBSETOPT(H->link, DBTEXTSIZE, "2147483647");
00238 
00239        if (vars[3].optval && FAIL == dbuse(H->link, vars[3].optval)) {
00240               goto cleanup;
00241        }
00242 
00243        ret = 1;
00244        dbh->max_escaped_char_length = 2;
00245        dbh->alloc_own_columns = 1;
00246 
00247 cleanup:
00248        for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
00249               if (vars[i].freeme) {
00250                      efree(vars[i].optval);
00251               }
00252        }
00253 
00254        dbh->methods = &dblib_methods;
00255        dbh->driver_data = H;
00256 
00257        if (!ret) {
00258               zend_throw_exception_ex(php_pdo_get_exception(), DBLIB_G(err).dberr TSRMLS_CC,
00259                      "SQLSTATE[%s] %s (severity %d)",
00260                      DBLIB_G(err).sqlstate,
00261                      DBLIB_G(err).dberrstr,
00262                      DBLIB_G(err).severity);
00263        }
00264 
00265        return ret;
00266 }
00267 
00268 pdo_driver_t pdo_dblib_driver = {
00269 #if PDO_DBLIB_IS_MSSQL
00270        PDO_DRIVER_HEADER(mssql),
00271 #elif defined(PHP_WIN32)
00272        PDO_DRIVER_HEADER(sybase),
00273 #else
00274        PDO_DRIVER_HEADER(dblib),
00275 #endif
00276        pdo_dblib_handle_factory
00277 };
00278