Back to index

php5  5.3.10
pdo_dblib.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: pdo_dblib.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 ZEND_DECLARE_MODULE_GLOBALS(dblib)
00036 static PHP_GINIT_FUNCTION(dblib);
00037 
00038 const zend_function_entry pdo_dblib_functions[] = {
00039        PHP_FE_END
00040 };
00041 
00042 #if ZEND_MODULE_API_NO >= 20050922
00043 static const zend_module_dep pdo_dblib_deps[] = {
00044        ZEND_MOD_REQUIRED("pdo")
00045        ZEND_MOD_END
00046 };
00047 #endif
00048 
00049 #if PDO_DBLIB_IS_MSSQL
00050 zend_module_entry pdo_mssql_module_entry = {
00051 #else
00052 zend_module_entry pdo_dblib_module_entry = {
00053 #endif
00054 #if ZEND_MODULE_API_NO >= 20050922
00055        STANDARD_MODULE_HEADER_EX, NULL,
00056        pdo_dblib_deps,
00057 #else
00058        STANDARD_MODULE_HEADER,
00059 #endif
00060 #if PDO_DBLIB_IS_MSSQL
00061        "pdo_mssql",
00062 #elif defined(PHP_WIN32)
00063        "pdo_sybase",
00064 #else
00065        "pdo_dblib",
00066 #endif
00067        pdo_dblib_functions,
00068        PHP_MINIT(pdo_dblib),
00069        PHP_MSHUTDOWN(pdo_dblib),
00070        NULL,
00071        PHP_RSHUTDOWN(pdo_dblib),
00072        PHP_MINFO(pdo_dblib),
00073        "1.0.1",
00074        PHP_MODULE_GLOBALS(dblib),
00075        PHP_GINIT(dblib),
00076        NULL,
00077        NULL,
00078        STANDARD_MODULE_PROPERTIES_EX
00079 };
00080 
00081 #if defined(COMPILE_DL_PDO_DBLIB) || defined(COMPILE_DL_PDO_MSSQL)
00082 #if PDO_DBLIB_IS_MSSQL
00083 ZEND_GET_MODULE(pdo_mssql)
00084 #else
00085 ZEND_GET_MODULE(pdo_dblib)
00086 #endif
00087 #endif
00088 
00089 int error_handler(DBPROCESS *dbproc, int severity, int dberr,
00090        int oserr, char *dberrstr, char *oserrstr)
00091 {
00092        pdo_dblib_err *einfo;
00093        char *state = "HY000";
00094        TSRMLS_FETCH();
00095 
00096        einfo = (pdo_dblib_err*)dbgetuserdata(dbproc);
00097        if (!einfo) einfo = &DBLIB_G(err);
00098 
00099        einfo->severity = severity;
00100        einfo->oserr = oserr;
00101        einfo->dberr = dberr;
00102        if (einfo->oserrstr) {
00103               efree(einfo->oserrstr);
00104        }
00105        if (einfo->dberrstr) {
00106               efree(einfo->dberrstr);
00107        }
00108        if (oserrstr) {
00109               einfo->oserrstr = estrdup(oserrstr);
00110        } else {
00111               einfo->oserrstr = NULL;
00112        }
00113        if (dberrstr) {
00114               einfo->dberrstr = estrdup(dberrstr);
00115        } else {
00116               einfo->dberrstr = NULL;
00117        }
00118 
00119        switch (dberr) {
00120               case SYBESEOF:
00121               case SYBEFCON:       state = "01002"; break;
00122               case SYBEMEM: state = "HY001"; break;
00123               case SYBEPWD: state = "28000"; break;
00124        }
00125        strcpy(einfo->sqlstate, state);
00126 
00127 #if 0
00128        php_error_docref(NULL TSRMLS_CC, E_WARNING,
00129               "dblib error: %d %s (severity %d)",
00130               dberr, dberrstr, severity); 
00131 #endif
00132 
00133        return INT_CANCEL;
00134 }
00135 
00136 int msg_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate,
00137        int severity, char *msgtext, char *srvname, char *procname, DBUSMALLINT line)
00138 {
00139        pdo_dblib_err *einfo;
00140        TSRMLS_FETCH();
00141 
00142        if (severity) {
00143               einfo = (pdo_dblib_err*)dbgetuserdata(dbproc);
00144               if (!einfo) {
00145                      einfo = &DBLIB_G(err);
00146               }
00147 
00148               if (einfo->lastmsg) {
00149                      efree(einfo->lastmsg);
00150               }
00151 
00152               einfo->lastmsg = estrdup(msgtext);
00153        }
00154 
00155 #if 0
00156        php_error_docref(NULL TSRMLS_CC, E_WARNING, "dblib message: %s (severity %d)", msgtext, severity);
00157 #endif
00158 
00159        return 0;
00160 }
00161 
00162 static PHP_GINIT_FUNCTION(dblib)
00163 {
00164        memset(dblib_globals, 0, sizeof(*dblib_globals));
00165        dblib_globals->err.sqlstate = dblib_globals->sqlstate;
00166 }
00167 
00168 PHP_RSHUTDOWN_FUNCTION(pdo_dblib)
00169 {
00170        if (DBLIB_G(err).oserrstr) {
00171               efree(DBLIB_G(err).oserrstr);
00172               DBLIB_G(err).oserrstr = NULL;
00173        }
00174        if (DBLIB_G(err).dberrstr) {
00175               efree(DBLIB_G(err).dberrstr);
00176               DBLIB_G(err).dberrstr = NULL;
00177        }
00178        if (DBLIB_G(err).lastmsg) {
00179               efree(DBLIB_G(err).lastmsg);
00180               DBLIB_G(err).lastmsg = NULL;
00181        }
00182        return SUCCESS;
00183 }
00184 
00185 PHP_MINIT_FUNCTION(pdo_dblib)
00186 {
00187        if (FAIL == dbinit()) {
00188               return FAILURE;
00189        }
00190        
00191        if (FAILURE == php_pdo_register_driver(&pdo_dblib_driver)) {
00192               return FAILURE;
00193        }
00194        
00195        /* TODO: 
00196        
00197        dbsetifile()
00198        dbsetmaxprocs()
00199        dbsetlogintime()
00200        dbsettime()
00201        
00202         */
00203 
00204 #if !PHP_DBLIB_IS_MSSQL
00205        dberrhandle(error_handler);
00206        dbmsghandle(msg_handler);
00207 #endif
00208 
00209        return SUCCESS;
00210 }
00211 
00212 PHP_MSHUTDOWN_FUNCTION(pdo_dblib)
00213 {
00214        php_pdo_unregister_driver(&pdo_dblib_driver);
00215        dbexit();
00216        return SUCCESS;
00217 }
00218 
00219 PHP_MINFO_FUNCTION(pdo_dblib)
00220 {
00221        php_info_print_table_start();
00222        php_info_print_table_header(2, "PDO Driver for "
00223 #if PDO_DBLIB_IS_MSSQL
00224               "MSSQL"
00225 #elif defined(PHP_WIN32)
00226               "FreeTDS/Sybase/MSSQL"
00227 #else
00228               "FreeTDS/Sybase"
00229 #endif
00230               " DB-lib", "enabled");
00231        php_info_print_table_row(2, "Flavour", PDO_DBLIB_FLAVOUR);
00232        php_info_print_table_end();
00233 }
00234