From ff5f3e0be7569d311ca1c2249ec1e6419fae3aee Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 3 Oct 2005 15:16:57 +0000 Subject: [PATCH] improve pdo spec documentation git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@197485 c90b9560-bf6c-de11-be94-00142212c4b1 --- internals/pdo/index.xml | 4071 ++++++++++++++++++++------------------- 1 file changed, 2066 insertions(+), 2005 deletions(-) diff --git a/internals/pdo/index.xml b/internals/pdo/index.xml index 30c3cf060f..9ae9f0aaa5 100644 --- a/internals/pdo/index.xml +++ b/internals/pdo/index.xml @@ -1,5 +1,5 @@ - + + + + Bill + Abt + + IBM + + + + Rick + McGuire + + IBM + + + + Wez + Furlong + + OmniTI + + + + 2005 + IBM Corporation + + + 2005 + the PHP Documentation Group + + + + The purpose of this How-To is to provide a basic understanding of the steps + required to write a database driver that interfaces with the PDO layer. + Please note that this is still an evolving API and as such, subject to + change. This document was prepared based on version 0.3 of PDO. + The learning curve is steep; expect to spend a lot of time on the + pre-requisites. + + + + PDO Driver How-To - Purpose - The purpose of this How-To is to provide a basic understanding of the steps required to write a database driver that interfaces with the PDO driver. Please note that this is still an evolving API and as such, subject to change. This document was prepared based on version 0.3 of the PDO driver. The learning curve is steep; expect to spend a lot of time on the pre-requisites. - - - Pre-requisites - The following is list of pre-requisites and assumptions needed for writing a PDO database driver: - - - A working target database, examples, demos, etc. working as per vendor specifications; - + A brief note about this How-To + + The original version of this PDO driver specification was written by + Bill Abt and Rick McGuire of IBM and contributed to the PHP community + to assist in the development of drivers for more databases and to + encourage the emerging standard for database access in PHP. + + + + + Pre-requisites + + The following is list of pre-requisites and assumptions needed for writing + a PDO database driver: + + + + + A working target database, examples, demos, etc. working as per vendor + specifications; + + A working development environment: - + Other Unix: standard development tools supplied by vendor plus the GNU development tool set; @@ -40,58 +94,110 @@ A working PHP environment version 5.0.3 or higher with a working PEAR extension version 1.3.5 or higher; - A working PDO driver environment (can be installed using 'sudo pear install PDO'), including the headers which will be needed to access the PDO type definitions and function declarations; + A working PDO environment (can be installed using 'sudo pear install PDO'), including the headers which will be needed to access the PDO type definitions and function declarations; A good working knowledge of the 'C' programming language; - A good working knowledge of the way to write a PHP extension (I recommend Advanced PHP Programming by George Schlossnagle published by Developer's Library, chapters 21 and 22); + A good working knowledge of the way to write a PHP extension + George Schlossnagle's Advanced PHP Programming (published by + Developer's Library, chapters 21 and 22) is recommended; - Finally, a familiarity with the Zend API that forms the heart of PHP in particular paying attention to the memory management aspects. + + Finally, a familiarity with the Zend API that forms the heart of PHP, in + particular paying attention to the memory management aspects. + - - Preparation and Housekeeping - Source directory layout - The source directory for a PDO is laid out as follows: - ./PDO_<DB>-x.x - config.m4 - config.w32 - CREDITS - pdo_<db>.c - php_pdo_<db>.h - php_pdo_<db>_int.h - <db>_driver.c - <db>_statement.c - where : - - - <DB> is a capitalized short name for the db this driver is for; - - - <db> is a lowercase short name for the db this driver is for and - - - x.x is the version number of the driver. - - - The purpose and contents of these files are defined later in this document. - - Creating a skeleton - The easiest way to get started is to use the ext_skel shell script found in the PHP build tree in the 'ext' directory. This will build a skeleton directory containing a lot of the files listed above. It can be build by executing the following command from within the 'ext' directory: - ./ext_skel --extname=pdo_<db> - This will generate a directory called pdo_<db> containing the skeleton files that you can then modify. This directory should then be moved out of the php extension directory . PDO is a PECL extension and should not be included in the standard extension directory. As long as you have PHP and PDO installed, you should be able to build from any directory. - - - Standard Includes - Build Specific Headers - The header file config.h is generated by the configure process for the platform for the which the driver is being built. If this header is present, the HAVE_CONFIG_H compiler variable is set. This variable should be tested for and if set, the file config.h should be included in the compilation unit. - - PHP Headers + + + Preparation and Housekeeping + + Source directory layout + + + The source directory for a typical PDO driver is laid out as follows, where + SKEL represents a shortened form of the name of the + database that the driver is going to connect to. Even though SKEL is + presented here in uppercase (for clarity), the convention is to use + lowercase characters. + + +pdo_SKEL/ + config.m4 + config.w32 + CREDITS + package.xml + pdo_SKEL.c + php_pdo_SKEL.h + php_pdo_SKEL_int.h + SKEL_dbh.c + SKEL_stmt.c + + + + unix build script + + + win32 build script + + + meta information about the package + + + standard PHP extension glue + + + driver private header + + + contains the implementation of the PDO driver interface + + + contains the implementation of the PDO statement interface + + + The contents of these files are defined later in this document. + + + Creating a skeleton + + + The easiest way to get started is to use the ext_skel + shell script found in the PHP build tree in the ext + directory. This will build a skeleton directory containing a lot of the + files listed above. It can be build by executing the following command from + within the ext directory: + + ./ext_skel --extname=pdo_SKEL + + This will generate a directory called pdo_SKEL containing the + skeleton files that you can then modify. This directory should then be + moved out of the php extension directory . PDO is a PECL extension and + should not be included in the standard extension directory. As long as you + have PHP and PDO installed, you should be able to build from any directory. + + + + Standard Includes + + Build Specific Headers + + + The header file config.h is generated by the configure process for the + platform for the which the driver is being built. If this header is + present, the HAVE_CONFIG_H compiler variable is set. This variable should + be tested for and if set, the file config.h should be included in the + compilation unit. + + + + PHP Headers The following standard public php headers should be included in each source module: - + php.h @@ -102,2067 +208,2022 @@ ext/standard/info.h - PDO Interface Headers + + + PDO Interface Headers + The following standard public pdo header files are also included in each source module: - + + + pdo/php_pdo.h - pdo/php_pdo.h - - - This header file contains definitions of the initialization and shutdown functions in the main driver as well as definitions of global PDO variables.. - - + + This header file contains definitions of the initialization and shutdown + functions in the main driver as well as definitions of global PDO + variables. + + + + pdo/php_pdo_driver.h - pdo/php_pdo_driver.h - - - This header contains the types and API contracts that are used to write a PDO driver. It also contains method signature for calling back into the main PDO driver and registering/unregistering your driver with the main PDO driver. Most importantly, this header file contains the type definitions for PDO database handles and statements. The two main structures a driver has to deal with, pdo_dbh_t and pdo_stmt_t, are described in more detail in Appendix A and B. - - + + This header contains the types and API contracts that are used to write + a PDO driver. It also contains method signature for calling back into + the PDO layer and registering/unregistering your driver with + PDO. Most importantly, this header file contains the type + definitions for PDO database handles and statements. The two main + structures a driver has to deal with, pdo_dbh_t and pdo_stmt_t, are + described in more detail in Appendix A and B. + - - - Driver Specific Headers - The typical pdo driver has two header files that are specific to the database implementation. This does not preclude the use of more depending on the implementation. The following two headers are, by convention, standard: - - - php_pdo_<db>.h - - - This header file is virtually an exact duplicate in functionality and content of the previously defined pdo/php_pdo.h that has been specifically tailored for your database. If your driver requires the use of global variables they should be defined using the ZEND_BEGIN_MODULE_GLOBALS and ZEND_END_MODULE_GLOBALS macros. Macros are then used to access these variables. This macro is usually named PDO_<DB>_G(v) where v is global variable to be accessed. Consult the Zend programmer documentation for more information. - - - - - php_pdo_<db>_int.h - - - This header file typically contains type definitions and function declarations specific to the driver implementation. It also should contain the db specicfic definitions of a pdo_<db>_handle and pdo_<db>_stmt structures. These are the names of the private data structures that are then referenced by the driver_data members of the handle and statement structures. - - - - - - Optional Headers - Depending on the implementation details for a particular driver it may be necessary to include the following header: - - - zend_exceptions.h - - - - Major Structures and Attributes - The major structures, pdo_dbh_t and pdo_stmt_t are defined and explained in Appendix A and B respectively. Database and Statement attributes are defined in Appendix C. Error handling is explained in Appendix D. - Housekeeping unit -- pdo_<db>.c - Entry functions - A structure of type function_entry, called pdo_<db>_functions should be declared and initialized to all NULL. - - Module entry - A structure of type zend_module_entry (typically called pdo_<db>_module_entry should be declared and should include reference to the pdo_<db>_functions table defined previously. - - - Standard PHP Module Extension Functions - PHP_MINIT_FUNCTION - This standard PHP extension function should be used to register your driver with the PDO main driver. This is done by calling the php_pdo_register_driver function passing a pointer to a structure of type pdo_driver_t typically named pdo_<db>_driver. A pdo_driver_t contains a header that is generated using the PDO_DRIVER_HEADER(<db>) macro and pdo_<db>_handle_factory function pointer. The actual function is described during the discussion of the <db>_driver.c unit. - - PHP_MSHUTDOWN_FUNCTION - This standard PHP extension function is used to unregister your driver with the main PDO driver. This is done by calling the php_pdo_unregister_driver function passing the same pdo_<db>_driver structure that was passed in the init function above. - - PHP_MINFO_FUNCTION - This is again a standard PHP extension function. It's purpose is to display information regarding the module. You should include the version of the module and also what version of the db you are dependent on. - Driver unit -- <db>_driver.c - This unit implements all of the database handling methods that support the PDO database handle object. It also contains the error fetching routines. All of these functions will typically need to access the global variable pool. Therefore, it is necessary to use the Zend macro TSRMLS_DC macro at the end of each of these statements. Consult the Zend programmer documentation for more information on this macro. - - static int pdo_<db>_error( - pdo_dbh_t *dbh, - pdo_stmt_t *stmt, - const char *file, - int line - TSRMLS_DC) - The purpose of this function is to be used as a generic error handling function within the driver. It is called by the driver when an error occurs within the driver. If an error occurs that is not related to SQLSTATE, the driver should set either dbh->error_code or stmt->error_code to an SQLSTATE that most closely matches the error or the generic SQLSTATE error “HY000”. The PDO driver file pdo_sqlstate.c contains a table of commonly used SQLSTATE codes that the PDO code explicitly recognizes. This setting of the error code should be done prior to calling this function.; This function should set the global pdo_err variable to the error found in either the dbh or the stmt (if the variable stmt is not NULL). - - - - - - - - dbh - - - Pointer to database handle returned by the handle factory - - - - - stmt - - - Pointer to the most current statement or NULL. If NULL, the error is derived by error code found in the dbh. - - - - - file - - - The source file where the error occurred or NULL if not available. - - - - - line - - - The line number within the source file if available. - - - - - - If the dbh member methods is NULL, this function should call the zend_throw_exception_ex() function otherwise it should return the error code. This function is usually called using a helper macro that customizes the calling sequence for either database handling errors or statement handling errors. - Example macros: - #define pdo_<db>_drv_error(what) pdo_<db>_error(dbh, NULL, what, __FILE__, __LINE__, TSRMLS_CC) - #define pdo_<db>_drv_error(what) pdo_<db>_error(dbh, NULL, what, __FILE__, __LINE__, TSRMLS_CC) - - For more info on error handling, see Appendix C. - - static int pdo_<db>_fetch_error_func( - pdo_dbh_t *dbh, - pdo_stmt_t *stmt, - zval *info - TSRMLS_DC) - The purpose of this function is to translate the database specific error code into an human readable string. - - - - - - - - dbh - - - Pointer to database handle returned by the handle factory - - - - - stmt - - - Pointer to the most current statement or NULL. If NULL, the error translated is derived by error code found in the dbh. - - - - - info - - - A hash table containing error codes and messages. - - - - - - This function's sole purpose is retrieve the current error code and corresponding message and add it to the hash table pointed to by info. - - The error_func should return two pieces of information as successive array elements. The first item is expected to be a numeric error code, the second item is a descriptive string. The best way to set this item is by using add_next_index - - // now add the error information. - // These need to be added in a specific order - add_next_index_long(info, error_code); - add_next_index_string(info, message, 0); - - This function should return 1 if information is available, 0 if the driver does not have additional info. - - static int <db>_handle_closer( + + + + + + Driver Specific Headers - - pdo_dbh_t *dbh + The typical pdo driver has two header files that are specific to the + database implementation. This does not preclude the use of more depending + on the implementation. The following two headers are, by convention, + standard: + + + + php_pdo_SKEL.h + + + This header file is virtually an exact duplicate in functionality + and content of the previously defined pdo/php_pdo.h that has been + specifically tailored for your database. If your driver requires + the use of global variables they should be defined using the + ZEND_BEGIN_MODULE_GLOBALS and ZEND_END_MODULE_GLOBALS macros. + Macros are then used to access these variables. This macro is + usually named PDO_SKEL_G(v) where v is global variable to be + accessed. Consult the Zend programmer documentation for more + information. + + + + + php_pdo_SKEL_int.h + + + This header file typically contains type definitions and function + declarations specific to the driver implementation. It also should + contain the db specicfic definitions of a pdo_SKEL_handle and + pdo_SKEL_stmt structures. These are the names of the private + data structures that are then referenced by the driver_data members + of the handle and statement structures. + + + + + + + Optional Headers + + Depending on the implementation details for a particular driver it may be + necessary to include the following header: - TSRMLS_DC) - This function will be called by the main PDO driver to close an open database. - - - - - - - - dbh - - - Pointer to database handle returned by the handle factory - - - - - - This should do whatever database specific activity that needs to be accomplished to close the open database. The PDO driver ignores the return value from this function. - - static int <db>_handle_preparer( - pdo_dbh_t *dbh, - const char *sql, - long sql_len, - pdo_stmt_t *stmt, - zval *driver_options - TSRMLS_DC) - This function will be called by the main PDO driver to convert raw SQL into a pdo_stmt_t which can then be executed. - - - - - - - - dbh - - - Pointer to database handle returned by the handle factory - - - - - sql - - - Pointer to a character string containing the SQL statement to be prepared. - - - - - sql_len - - - The length of the SQL statement. - - - - - Stmt - - - Pointer to the returned statement or NULL if an error occurs. - - - - - driver_options - - - Any driver specific/defined options. - - - - - - - This function is essentially the constructor for a stmt object. This function is responsible for processing statement options, and setting driver-specific option fields in the pdo_stmt_t control block. - - The PDO driver does not process any statement options on the driver's behalf before calling the preparer function. - - One very important responsibility of this function is the processing of SQL statement parameters. At the time of this call, the PDO driver does not know how the driver supports positional parameters in the SQL statements. Depending on the placeholder option used, the statement string may need to reformatted into the form required by the database driver. The PDO driver implements a special function, pdo_parse_params(), which will reformat the statement string, if necessary, based on the setting of the supports_placeholders field. In particular, if a driver only supports PDO_PLACEHOLDER_NAMED or PDO_PLACEHOLDER_POSITIONAL, pdo_parse_params() should be used to obtain a correctly formatted statement string. This parsing process also will mapped named parameters into positional parameters for the driver. - + +#include <zend_exceptions.h> + + + + + + Fleshing out your skeleton + + + Major Structures and Attributes + + The major structures, pdo_dbh_t and pdo_stmt_t are defined and explained in + Appendix A and B respectively. Database and Statement attributes are + defined in Appendix C. Error handling is explained in Appendix D. + + + + + pdo_SKEL.c: PHP extension glue + function entries + +static function_entry pdo_SKEL_functions[] = { + { NULL, NULL, NULL } +}; + + + This structure is used to register functions into the global php function + namespace. PDO drivers should try to avoid doing this, so it is + recommended that you leave this structure initialized to NULL, as shown in + the synopsis above. + + + + Module entry + = 220050617 +static zend_module_dep pdo_SKEL_deps[] = { + ZEND_MOD_REQUIRED("pdo") + {NULL, NULL, NULL} +}; +#endif +/* }}} */ + +zend_module_entry pdo_SKEL_module_entry = { +#if ZEND_EXTENSION_API_NO >= 220050617 + STANDARD_MODULE_HEADER_EX, NULL, + pdo_SKEL_deps, +#else + STANDARD_MODULE_HEADER, +#endif + "pdo_SKEL", + pdo_SKEL_functions, + PHP_MINIT(pdo_SKEL), + PHP_MSHUTDOWN(pdo_SKEL), + NULL, + NULL, + PHP_MINFO(pdo_SKEL), + PHP_PDO__MODULE_VERSION, + STANDARD_MODULE_PROPERTIES +}; +/* }}} */ + +#ifdef COMPILE_DL_PDO_ +ZEND_GET_MODULE(pdo_db) +#endif]]> + + A structure of type zend_module_entry called +pdo_SKEL_module_entry must be declared and should include reference to +the pdo_SKEL_functions table defined previously. + + + + + Standard PHP Module Extension Functions + + PHP_MINIT_FUNCTION + + + This standard PHP extension function should be used to register your driver + with the PDO layer. This is done by calling the + php_pdo_register_driver function passing a pointer to + a structure of type pdo_driver_t typically named + pdo_SKEL_driver. A pdo_driver_t + contains a header that is generated using the + PDO_DRIVER_HEADER(SKEL) macro and + pdo_SKEL_handle_factory function pointer. The + actual function is described during the discussion of the + SKEL_dbh.c unit. + + + + + PHP_MSHUTDOWN_FUNCTION + + + This standard PHP extension function is used to unregister your driver + from the PDO layer. This is done by calling the + php_pdo_unregister_driver function, passing the same + pdo_SKEL_driver structure that was passed in the + init function above. + + + + PHP_MINFO_FUNCTION + + This is again a standard PHP extension function. Its purpose is to + display information regarding the module when the + phpinfo is called from a script. The convention is + to display the version + of the module and also what version of the db you are dependent on, along + with any other configuration style information that might be relevant. + + + + + + SKEL_driver.c: Driver implementation + + + This unit implements all of the database handling methods that support the + PDO database handle object. It also contains the error fetching routines. + All of these functions will typically need to access the global variable + pool. Therefore, it is necessary to use the Zend macro TSRMLS_DC macro at + the end of each of these statements. Consult the Zend programmer + documentation for more information on this macro. + + + + pdo_SKEL_error + + static int pdo_SKEL_error(pdo_dbh_t *dbh, + pdo_stmt_t *stmt, const char *file, int line TSRMLS_DC) + + + The purpose of this function is to be used as a generic error handling + function within the driver. It is called by the driver when an error occurs + within the driver. If an error occurs that is not related to SQLSTATE, the + driver should set either dbh->error_code or + stmt->error_code to an + SQLSTATE that most closely matches the error or the generic SQLSTATE error + HY000. The file pdo_sqlstate.c in the PDO source contains a table + of commonly used SQLSTATE codes that the PDO code explicitly recognizes. + This setting of the error code should be done prior to calling this + function.; This function should set the global + pdo_err variable to the error found in either the + dbh or the stmt (if the variable stmt is not NULL). + + + + + dbh + + Pointer to the database handle initialized by the handle factory + + + + stmt + + Pointer to the current statement or NULL. If NULL, the error is derived by error code found in the dbh. + + + + file + + The source file where the error occurred or NULL if not available. + + + + line + + The line number within the source file if available. + + + + + + If the dbh member methods is NULL (which implies that the error is being + raised from within the PDO constructor), this function should call the + zend_throw_exception_ex() function otherwise it should return the error + code. This function is usually called using a helper macro that customizes + the calling sequence for either database handling errors or statement + handling errors. + + + + Example macros for invoking pdo_SKEL_error + +#define pdo_SKEL_drv_error(what) \ + pdo_SKEL_error(dbh, NULL, what, __FILE__, __LINE__ TSRMLS_CC) +#define pdo_SKEL_drv_error(what) \ + pdo_SKEL_error(dbh, NULL, what, __FILE__, __LINE__ TSRMLS_CC) + + + + For more info on error handling, see . + + + + Despite being documented here, the PDO driver interface does not specify + that his function be present; it is merely a convenient way to handle + errors, and it just happens to be equally convenient for the majority of + database client library APIs to structure your driver implementation in + this way. + + + + + pdo_SKEL_fetch_error_func + static int pdo_SKEL_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, + zval *info TSRMLS_DC) + + + The purpose of this function is to obtain additional information about the + last error that was triggered. This includes the driver specific error + code and a human readable string. It may also include additional + information if appropriate. This function is called as a result of the PHP + script calling the PDO::errorInfo method. + + + + + dbh + + Pointer to the database handle initialized by the handle factory + + + + stmt + + Pointer to the most current statement or NULL. If NULL, the error translated is derived by error code found in the dbh. + + + + info + + A hash table containing error codes and messages. + + + + + + The error_func should return two pieces of information as successive array + elements. The first item is expected to be a numeric error code, the second + item is a descriptive string. The best way to set this item is by using + add_next_index. Note that the type of the first argument need not be + long; use whichever type most closely matches the error code + returned by the underlying database API. + + + + + + This function should return 1 if information is available, 0 if the driver + does not have additional info. + + + + SKEL_handle_closer + static int SKEL_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) + + + This function will be called by PDO to close an open + database. + + + + + dbh + + Pointer to the database handle initialized by the handle factory + + + + + This should do whatever database specific activity that needs to be + accomplished to close the open database. PDO ignores the return + value from this function. + + + + + SKEL_handle_preparer + static int SKEL_handle_preparer(pdo_dbh_t *dbh, const char *sql, +long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC) + + + This function will be called by PDO in response to + PDO::query and PDO::prepare + calls from the PHP script. The purpose of the function is to prepare + raw SQL for execution, storing whatever state is appropriate into the + stmt that is passed in. + + + + + dbh + + Pointer to the database handle initialized by the handle factory + + + + sql + + Pointer to a character string containing the SQL statement to be prepared. + + + + sql_len + + The length of the SQL statement. + + + + Stmt + + Pointer to the returned statement or NULL if an error occurs. + + + + driver_options + + Any driver specific/defined options. + + + + + This function is essentially the constructor for a stmt object. This + function is responsible for processing statement options, and setting + driver-specific option fields in the pdo_stmt_t structure. + + + PDO does not process any statement options on the driver's + behalf before calling the preparer function. It is your responsibility to + process them before you return, raising an error for any unknown options that + are passed. + + + One very important responsibility of this function is the processing of SQL + statement parameters. At the time of this call, PDO does not if your driver + supports binding parameters into prepared statements, nor does it know if + it supports named or positional parameter naming conventions. + + + Your driver is responsible for setting + stmt->supports_placeholders as appropriate for the + underlying database. This may involved some run-time determination on the + part of your driver, if this setting depends on the version of the database + server to which it is connected. If your driver doesn't directly support + both named and positional parameter conventions, you should use the + pdo_parse_params API to have PDO rewrite the query to + take advantage of the support provided by your database. + + + using pdo_parse_params + supports_placeholders = PDO_PLACEHOLDER_POSITIONAL; + ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len TSRMLS_CC); + + if (ret == 1) { + /* query was re-written */ + sql = nsql; + } else if (ret == -1) { + /* couldn't grok it */ + strcpy(dbh->error_code, stmt->error_code); + return 0; + } + + /* now proceed to prepare the query in "sql" */ +]]> + + + Possible values for supports_placeholders are: + PDO_PLACEHOLDER_NAMED, + PDO_PLACEHOLDER_POSITIONAL and + PDO_PLACEHOLDER_NONE. If the driver doesn't support prepare statements at all, then this function should simply allocate any state that it might need, and then return: + + + implementing preparer for drivers that don't support native prepared statements + driver_data; + pdo_SKEL_stmt *S = ecalloc(1, sizeof(pdo_SKEL_stmt)); + + S->H = H; + stmt->driver_data = S; + stmt->methods = &SKEL_stmt_methods; + stmt->supports_placeholders = PDO_PLACEHOLDER_NONE; + + return 1; +} +]]> + + This function returns 1 on success or 0 on failure. - - static long <db>_handle_doer( - pdo_dbh_t *dbh, - const char *sql, - long sql_len - TSRMLS_DC) - This function will be called by the main PDO driver to execute a raw SQL statement. No pdo_stmt_t is created. - - - - - - - - dbh - - - Pointer to database handle returned by the handle factory - - - - - sql - - + + + SKEL_handle_doer + static long SKEL_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC) + + + This function will be called by PDO to execute a raw SQL + statement. No pdo_stmt_t is created. + + + + + dbh + + Pointer to the database handle initialized by the handle factory + + + + sql + Pointer to a character string containing the SQL statement to be prepared. - - - - - sql_len - - + + + + sql_len + The length of the SQL statement. - - - - - - - This function returns 1 on success or 0 on failure. - - static int <db>_handle_quoter( - pdo_dbh_t *dbh, - const char *unquoted, - int unquoted_len, - char **quoted, - int quoted_len, - enum pdo_param_type param_type - TSRMLS_DC) - This function will be called by the main PDO driver to turn a unquoted string into a quoted string for use in a query. - - - - - - - - dbh - - - Pointer to database handle returned by the handle factory - - - - - unquoted - - + + + + + + This function returns 1 on success or 0 on failure. + + + + + SKEL_handle_quoter + static int SKEL_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, + int unquoted_len, char **quoted, int quoted_len, enum pdo_param_type param_type TSRMLS_DC) + + + This function will be called by PDO to turn an unquoted + string into a quoted string for use in a query. + + + + + dbh + + Pointer to the database handle initialized by the handle factory + + + + unquoted + Pointer to a character string containing the string to be quoted. - - - - - unquoted_len - - + + + + unquoted_len + The length of the string to be quoted. - - - - - quoted - - + + + + quoted + Pointer to the address where a pointer to the newly quoted string will be returned. - - - - - quoted_len - - + + + + quoted_len + The length of the new string. - - - - - param_type - - + + + + param_type + A driver specific hint for driver that have alternate quoting styles - - - - - - - This function is only required if the supports_placeholder value is PDO_PLACEHOLDER_NONE. For database drivers without placeholder support, the PDO code will reformat statement with all parameters replaced by quoted strings directly containing the parameter values. The quoter() function is used to transform a string value into a quoted string appropriate for the driver. If the driver supports parameter placeholders, it is not necessary to implement this function. - - This function returns 1 if the quoting process reformatted the string, and 0 if it was not necessary to change the string. The original string will be used unchanged with a 0 return. - . - static int <db>_handle_begin( - pdo_dbh_t *dbh - TSRMLS_DC) - This function will be called by the main PDO driver to begin a database transaction. - - - - - - - - dbh - - - Pointer to database handle returned by the handle factory - - - - - - This should do whatever database specific activity that needs to be accomplished to begin a transaction. This function returns 1 for success or 0 if an error occurred. - - static int <db>_handle_commit( - pdo_dbh_t *dbh - TSRMLS_DC) - This function will be called by the main PDO driver to end a database transaction. - - - - - - - - dbh - - - Pointer to database handle returned by the handle factory - - - - - - This should do whatever database specific activity that needs to be accomplished to end a transaction. This function returns 1 for success or 0 if an error occurred. - - static int <db>_handle_rollback( - pdo_dbh_t *dbh - TSRMLS_DC) - This function will be called by the main PDO driver to rollback a database transaction. - - - - - - - - dbh - - - Pointer to database handle returned by the handle factory - - - - - + + + + + This function is called in response to a call to + PDO::quote or when the driver has set + supports_placeholder to + PDO_PLACEHOLDER_NONE. The purpose is to quote a + parameter when building SQL statements. + + + If your driver does not support native prepared statements, implementation + of this function is required. + + + This function returns 1 if the quoting process reformatted the string, and + 0 if it was not necessary to change the string. The original string will be + used unchanged with a 0 return. + + + + SKEL_handle_begin + static int SKEL_handle_begin(pdo_dbh_t *dbh TSRMLS_DC) + + + This function will be called by PDO to begin a database transaction. + + + + dbh + + Pointer to the database handle initialized by the handle factory + + + + + This should do whatever database specific activity that needs to be + accomplished to begin a transaction. This function returns 1 for success or + 0 if an error occurred. + + + + SKEL_handle_commit + static int SKEL_handle_commit(pdo_dbh_t *dbh TSRMLS_DC) + + This function will be called by PDO to end a database + transaction. + + + + + dbh + + Pointer to the database handle initialized by the handle factory + + + + + This should do whatever database specific activity that needs to be + accomplished to commit a transaction. This function returns 1 for success or 0 if an error occurred. + + + + SKEL_handle_rollback + static int SKEL_handle_rollback( pdo_dbh_t *dbh TSRMLS_DC) + This function will be called by PDO to rollback a database transaction. + + + dbh + + Pointer to the database handle initialized by the handle factory + + + This should do whatever database specific activity that needs to be accomplished to rollback a transaction. This function returns 1 for success or 0 if an error occurred. - - static int <db>_handle_get_attribute( - pdo_dbh_t *dbh, - long attr, - zval *return_value - TSRMLS_DC) - This function will be called by the main PDO driver to retrieve a database attribute. - - - - - - - - dbh - - - Pointer to database handle returned by the handle factory - - - - - attr - - - Long value of one of the PDO_ATTR_xxxx types. - (see table below for valid attributes) - - - - - return_value - - - The returned value for the attribute. - - - - - - - It is up to the driver to decide which attributes will be supported for a particular implementation. It is not necessary for a driver to supply this function. The PDO driver handles the PDO_ATTR_PERSISTENT, PDO_ATTR_CASE, PDO_ATTR_ORACLE_NULLS, and PDO_ATTR_ERRMODE attributes directly. - + + + SKEL_handle_get_attribute + static int SKEL_handle_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC) + This function will be called by PDO to retrieve a database attribute. + + + + dbh + + Pointer to the database handle initialized by the handle factory + + + + attr + + + long value of one of the PDO_ATTR_xxxx types. See for valid attributes. + + + + + return_value + + The returned value for the attribute. + + + + + It is up to the driver to decide which attributes will be supported for a + particular implementation. It is not necessary for a driver to supply this + function. PDO driver handles the PDO_ATTR_PERSISTENT, PDO_ATTR_CASE, + PDO_ATTR_ORACLE_NULLS, and PDO_ATTR_ERRMODE attributes directly. + This function returns 1 on success or 0 on failure. - - static int <db>_handle_set_attribute( - pdo_dbh_t *dbh, - long attr, - zval *val - TSRMLS_DC) - This function will be called by the main PDO driver to set a database attribute. - - - - - - - - dbh - - - Pointer to database handle returned by the handle factory - - - - - attr - - - Long value of one of the PDO_ATTR_xxxx types. - (see table below for valid attributes) - - - - - val - - - The new value for the attribute. - - - - - - - It is up to the driver to decide which attributes will be supported for a particular implementation. It is not necessary for a driver to provide this function if it does not need to support additional attributes. The PDO driver handles the PDO_ATTR_CASE, PDO_ATTR_ORACLE_NULLS, and PDO_ATTR_ERRMODE attributes directly. - - This function returns 1 on success or 0 on failure. - - static char * <db>_handle_last_id( - pdo_dbh_t *dbh, - const char *name, - unsigned int len - TSRMLS_DC) - This function will be called by the main PDO driver to retrieve the ID of the last inserted row. - - - - - - - - dbh - - - Pointer to database handle returned by the handle factory - - - - - name - - - Pointer to a character string representing the table name if required by the database. - - - - - len - - - The length of the SQL statement. - - - - - - This function returns a character string containing the id of the last inserted row on success or NULL on failure. This is an optional function. - - static int <db>_check_liveness( - pdo_dbh_t *dbh - TSRMLS_DC) - This function will be called by the main PDO driver to test whether or not a persistent connection to a database is alive and ready for use. - - - - - - - - dbh - - - Pointer to database handle returned by the handle factory - - - - - - This function returns 1 if the database connection is alive and ready for use, otherwise it should return 0 to indicate failure or lack of support. This is an optional function. - - static function_entry <db>_get_driver_methods( - pdo_dbh_t *dbh, - int kind - TSRMLS_DC) - This function will be called by the main PDO driver to retrieve a pointer to either the database or statement handling method table. - - - - - - - - dbh - - - Pointer to database handle returned by the handle factory - - - - - kind - - - One of the following: - PDO_DBH_DRIVER_METHOD_KIND_DBH - - - Return a pointer to the database handling method table; - - - PDO_DBH_DRIVER_METHOD_KIND_STMT - - - Return a pointer to the statement handling method table. - - - - - - - - This function returns a pointer to the method handling requested if successful or NULL if the method table cannot be found. - - static int <db>_handle_factory( - pdo_dbh_t *dbh, - zval *driver_options - TSRMLS_DC) - This function will be called by the main PDO driver to create a database handle. For most databases this involves establishing a connection to the database. In some cases, a persistent connection may be requested, in other cases connection pooling may be requested. All of these are database/driver dependent. - - - - - - - - dbh - - - Pointer to database handle returned by the handle factory - - - - - driver_options - - - DB specific options. - - - - - - This function should fill in the passed database handle structure with its' driver specific information on success and return 1, otherwise it should return 0 to indicate failure. - - The PDO driver processes the AUTOCOMMIT and PERSISTENT driver options before calling the handle_factory. It is the handle factory's responsibility to process other options. - - - Driver method table - A static structure of type pdo_dbh_methods named <db>_methods must be declared and initialized to the function pointers for each defined function. If a function is not supported or not implemented the value for that function pointer should be set to NULL. - - - pdo_<db>_driver - A structure of type pdo_driver_t named pdo_<db>_driver should be declared. The PDO_DRIVER_HEADER(<db>) macro should be used to declare the header and the function pointer to the handle factory function should set. - Statement handling unit -- <db>_statement.c - This unit implements all of the database statement handling methods that support the PDO statement object. - - static int <db>_stmt_dtor( - pdo_stmt_t *stmt - TSRMLS_DC) - This function will be called by the main PDO driver to destroy a previously constructed statement object. - - - - - - - - stmt - - - Pointer to statement structure returned by the statement constructor. - - - - - - This should do whatever is necessary to free up any driver specific storage allocated for the statement. The return value from this function is ignored. - - static int <db>_stmt_execute( - pdo_stmt_t *stmt - TSRMLS_DC) - This function will be called by the main PDO driver to execute the prepared SQL statement in the passed statement object. - - - - - - - - stmt - - - Pointer to statement structure returned by the statement constructor. - - - - - - This function returns 1 for success or 0 in the event of failure. - - static int <db>_stmt_fetch( - pdo_stmt_t *stmt, - enum pdo_fetch_orientation ori, - long offset - TSRMLS_DC) - This function will be called by the main PDO driver to fetch a row from a previously executed statement object. - - - - - - - - stmt - - - Pointer to statement structure returned by the statement constructor. - - - - - ori - - - One of PDO_FETCH_ORI_xxx which will determine which row will be fetched. - - - - - offset - - - If ori is set to PDO_FETCH_ORI_ABS or PDO_FETCH_ORI_REL, offset represents the row desired or the row relative to the current position, respectively. Otherwise, this value is ignored. - - - - - - The results of this fetch are driver dependent and the data is usually stored in the driver_data member of pdo_stmt_t object. The ori and offset parameters are only meaningful if the statement represents a scrollable cursor. This function returns 1 for success or 0 in the event of failure. - - static int <db>_stmt_param_hook( - pdo_stmt_t *stmt, - struct pdo_bound_param_data *param, - enum pdo_param_event event_type - TSRMLS_DC) - This function will be called by the main PDO driver for handling of both bound parameters and bound columns. - - - - - - - - - stmt - - - Pointer to statement structure returned by the statement constructor. - - - - - param - - - The structure describing which statement variable will be bound to what variable name. - - - - - event_type - - - The type of event to occur for this parameter, one of the following: - PDO_PARAM_EVT_ALLOC -- allocate the binding; Only called once, when the binding is created. - PDO_PARAM_EVT_FREE -- free the allocated binding; Only called once as part of cleanup. - PDO_PARAM_EXEC_PRE -- bind the variable before execution; - PDO_PARAM_EXEC_POST -- bind the variable after execution; - PDO_PARAM_FETCH_PRE -- bind the variable prior to the fetch; - PDO_PARAM_FETCH_POST -- bind the variable after the fetch. - - - - - - - This hook will be called for each bound parameter and bound column in the statement. For ALLOC and FREE events, a single call will be made for each parameter or column. The param structure contains a driver_data field that the driver can use to store implementation specific information about each of the parameters. - - For all other events, the PDO driver will loop, calling the hook for each of the parameters and colums. - - If this is a bound parameter, the is_param flag in the param structure - - This function returns 1 for success or 0 in the event of failure. - - static int <db>_stmt_describe_col( - pdo_stmt_t *stmt, - int colno - TSRMLS_DC) - This function will be called by the main PDO driver to query information about a particular column. - - - - - - - - stmt - - - Pointer to statement structure returned by the statement constructor. - - - - - colno - - - The column number to be queried. - - - - - - The driver should populate the pdo_stmt_t member columns(colno) with the appropriate information. This function returns 1 for success or 0 in the event of failure. - - static int <db>_stmt_get_col_data( - pdo_stmt_t *stmt, - int colno, - char **ptr, - unsigned long *len, - int *caller_frees - TSRMLS_DC) - This function will be called by the main PDO driver to retrieve data from the specified column. - - - - - - - - stmt - - - Pointer to statement structure returned by the statement constructor. - - - - - colno - - - The column number for which data is to be retrieved. - - - - - ptr - - - Pointer to the retrieved data. - - - - - len - - - The length of the data pointed to by ptr. - - - - - caller_frees - - - If set, ptr should point to emalloc'd memory and the main PDO driver will free it as soon as it is done with it. Otherwise, it will be the responsibility of the driver to free any allocated memory as a result of this call. - - - - - - The driver should return the resultant data and length of that data in the ptr and len variables respectively. It should be noted that the main PDO driver expects the driver to manage the lifetime of the data. This function returns 1 for success or 0 in the event of failure. - - static int <db>_stmt_set_attr( - pdo_stmt_t *stmt, - long attr, - zval *val - TSRMLS_DC) - This function will be called by the main PDO driver to allow the setting of driver specific attributes for a statement object. - - - - - - - - stmt - - - Pointer to statement structure returned by the statement constructor. - - - - - attr - - - The attribute to be set (DRIVER DEFINED AND SPECIFIC). - - - - - val - - - New value for the attribute. - - - - - - This function is driver dependent and allows the driver the capability to set database specific attributes for a statement. This function returns 1 for success or 0 in the event of failure. This is an optional function. If the driver does not support additional settable attributes, it can be NULLed in the method table. The PDO driver does not handle any settable attributes on the database driver's behalf. - - static int <db>_stmt_get_attr( - pdo_stmt_t *stmt, - long attr, - zval *val - TSRMLS_DC) - This function will be called by the main PDO driver to allow the retrieval of driver specific attributes for a statement object. - - - - - - - - stmt - - - Pointer to statement structure returned by the statement constructor. - - - - - attr - - - The attribute to be retrieved, one of: - PDO_ATTR_CURSOR - - - The type of cursor this statement refers to. - - - PDO_ATTR_CURSOR_NAME - - - The name of the cursor this statement refers to. - - - (Others are DRIVER DEFINED AND SPECIFIC). - - - - - val - - - Pointer to the retrieved attribute. - - - - - - This function is driver dependent and allows the driver the capability to retrieve a previously set database specific attribute for a statement. This function returns 1 for success or 0 in the event of failure. This is an optional function. If the driver does not support additional gettable attributes, it can be NULLed in the method table. The PDO driver does not handle any settable attributes on the database driver's behalf. - - THE FOLLOWING FUNCTION IS A ROUGH FIRST CUT AND SUBJECT TO CHANGE. - static int <db>_stmt_get_col_meta( - pdo_stmt_t *stmt, - int colno, - zval *return_value - TSRMLS_DC) - This function will be called by the main PDO driver to retrieve meta-data from the specified column. - - - - - - - - stmt - - - Pointer to statement structure returned by the statement constructor. - - - - - colno - - - The column number for which data is to be retrieved. - - - - - return_value - - - Pointer to the retrieved data. - - - - - - The driver should consult the documentation for this function that can be found in the php_pdo_driver.h header as this will be the most current. This function returns 1 for success or 0 in the event of failure. The database driver does not need to provide this function. - - Statement handling method table - A static structure of type pdo_stmt_methods named <db>_stmt_methods should be declared and initialized to the function pointers for each defined function. If a function is not supported or not implemented the value for that function pointer should be set to NULL. - Building - The build process is designed to work with PEAR (see http://pear.php.net/index.php for more information about PEAR). There are two files that are used to assist in configuring your package for building. The first is config.m4 which is the autoconf configuration file for all platforms except Win32. The second is config.w32 which an autoconf configuration file for use on Win32. Skeleton files for these are built for you when you first set up your project. You then need to customize them to fit the needs of your project. Once you've customized your config files, you can build your driver using the following sequence of commands: - Before first build: - + + + SKEL_handle_set_attribute + static int SKEL_handle_set_attribute(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_DC) + + This function will be called by PDO to set a database attribute, usually in + response to a script calling PDO::setAttribute. + + + + dbh - sudo pear install PDO + Pointer to the database handle initialized by the handle factory - - For each build: - + + + attr - cd pdo_<db> + + long value of one of the PDO_ATTR_xxxx types. See for valid attributes. + + + + val - phpize + The new value for the attribute. + + + + + It is up to the driver to decide which attributes will be supported for a + particular implementation. It is not necessary for a driver to provide this + function if it does not need to support additional attributes. The PDO + driver handles the PDO_ATTR_CASE, PDO_ATTR_ORACLE_NULLS, and + PDO_ATTR_ERRMODE attributes directly. + + + + This function returns 1 on success or 0 on failure. + + + + + SKEL_handle_last_id + static char * SKEL_handle_last_id(pdo_dbh_t *dbh, const char *name, unsigned int len TSRMLS_DC) + + This function will be called by PDO to retrieve the ID of the last inserted + row. + + + + + dbh - ./configure + Pointer to the database handle initialized by the handle factory + + + name - make + + string representing a table or sequence name. + + + + len - sudo make install + the length of the name parameter. - - The process can then be repeated as necessary during the development process. - - - Package format - PDO drivers are released in compress tar format. The following layout should be observed: - ./ - package.xml - PDO_<DB>-xx.xx - … driver build tree - where DB is the capitalized short name for the database and xx.xx is the major and minor version numbers. - Assuming the directory structure above you then create the release package with the following command: - tar cfz pdo_<db>-xx.xx.tar.gz package.xml PDO_<DB>-xx.xx - The resultant tar file is then ready for release. The contents of the package.xml file are described in the PEAR Programmer's documentation (http://pear.php.net/manual/index.php). - - - Releasing - A PDO driver is released using the PHP Extension Community Library (PECL) mechanism. Information about PECL and the coding standards currently in force can be found at http://pecl.php.net/index.php. - - - Testing - To be determined. The testing scheme for PDO is still being developed. - Appendix A -- pdo_dbh_t - - - - - - - - - Variable name - - - Function - - - Responsibility - - - - - ce - - - Required for casting as Zend object. - - - Zend engine. It should be treated as read-only by the driver. - - - - - properties - - - Required for casting as Zend object. - - - Zend engine. It should be treated as read-only by the driver. - - - - - in_get - - - Required for casting as Zend object. - - - Zend engine. It should be treated as read-only by the driver. - - - - - in_set - - - Required for casting as Zend object. - - - Zend engine. It should be treated as read-only by the driver. - - - - - methods - - - Pointer to the methods function pointer for db handling. - - - Driver must set this during the handle factory method. - - - - - driver_data - - - Opaque pointer containing driver specific data. - - - Driver can store driver specific data and structures for database handling. This field should be allocated and set during handle factory method. - - - - - username - - - Database user on behalf access is to be granted. - - - This field is set by the PDO main driver before the factory method is called. It should be treated as read-only by the driver. - - - - - password - - - Password for the database user. - - - This field is set by the PDO main driver before the factory method is called. It should be treated as read-only by the driver. - - - - - is_persistent - - - If true, data stored and pointed to by this handle must be persistently allocated. - - - This field is set by the PDO main driver before the factory method is called. It should be treated as read-only by the driver. - - - - - auto_commit - - - If true, the driver should ensure that a COMMIT is processed at the end of each executed statement. Otherwise, COMMIT will only be done at user request. - - - This field is set by the PDO main driver before the factory method is called. It should be treated as read-only by the driver. - - - - - is_closed - - - If true, this handle is closed and is no longer valid for operations. - - - This field is NOT initialized and does NOT appear to be used at present. - - - - - alloc_own_columns - - - If true, the driver requires that memory be explicitly allocated for all columns returned. Setting this will cause the PDO driver to call the stmt describer() function after the first statement execute and before the first fetch. - - - Driver should set this during the handle factory method. - - - - - in_txn - - - If true, a COMMIT or ROLLBACK is allowed to be called. A transaction is in progress. - - - This field is set and maintained by the PDO main driver and should be treated as read-only by the driver. - - - - - max_escaped_char_length - - - This is the maximum length a single character can become after correct quoting. This is only needed if supports_placeholders is PDO_PLACEHOLDER_NONE. - - - Driver should set this during the handle factory method. - - - - - oracle_nulls - - - If true, convert all empty strings to NULL. - - - Driver should set this during the handle factory method. - - - - - _reserved_flags - - - Reserved for PDO main driver. - - - These are reserved for future use by the main PDO driver. - - - - - data_source - - - A string representing the data source used to open this handle. - - - This field is initially set by the main PDO driver. It can be modified, if necessary, by the driver. For example, a driver may need to pre-pend a directory path to filename passed or may need to add a qualifier as required by a particular database implementation. - - - - - data_source_len - - - The length of the string in the data_source field. - - - This field is initially set by the PDO main driver. If a driver modifies the data_source field such that the length changes, this field can be modified by the driver. - - - - - error_code - - - This is the latest error code. It is the SQLSTATE expressed as 6 character ASCIIZ field. - - - This field is set and maintained by the PDO main driver. Drivers should set the global variable pdo_err whenever an error occurs and the driver will set this field when appropriate. - - - - - error_mode - - - This field contains the mode in which PDO reports errors. This field is set to one of the following: - PDO_ERRMODE_SILENT - - - Fail silently - - - PDO_ERRMODE_WARNING - - - Treat all errors as warnings. - - - PDO_ERRMODE_EXCEPTION - - - Throw exceptions instead of returning error codes. - - - - - - This field is initially set to PDO_ERR_MODE_SILENT by the PDO main driver. It can be subsequently modified by the driver via a call to change the attribute. - - - - - native_case - - - This is set to native case for the database being accessed. It can be one of: - PDO_CASE_NATURAL - - - Mixed case - - - PDO_CASE_UPPER - - - All upper case - - - PDO_CASE_LOWER - - - All lower case - - - - - - This field is set during the handle factory method by the driver to appropriate value. The default value is PDO_CASE_NATURAL. - - - - - desired_case - - - This is set to the case conversion requested by the caller. It can be one of the following: - PDO_CASE_NATURAL - - - Don't do case conversion - - - PDO_CASE_UPPER - - - Convert strings to uppercase - - - PDO_CASE_LOWER - - - Convert strings to lowercase - - - - - - This field is set during the handle factory by the driver based on options passed and can be subsequently modified with a call to change the attribute. The default value for this field is PDO_CASE_NATURAL. - - - - - persistent_id - - - This a hash key that is associated with the handle this structure represents. - - - This field is set by the PDO main driver before the factory method is called. It should be treated as read-only by the driver. - - - - - persistent_id_len - - - This is the length of the hash key represented by the persistent_id field. - - - This field is set by the PDO main driver before the factory method is called. It should be treated as read-only by the driver. - - - - - refcount - - - This field contains the count of how many times this handle is currently referenced. If a handle is persistent, this field is increased when a persistent object is created. It is decremented each time the destructor is called. When the count reaches zero, the handle can be safely destructed. - - - This field is set initially by the main PDO driver to one when the handle is constructed. It should be treated as read-only by the driver. - - - - - cls_methods - - - This field is an array of hashtables containing (at present) driver methods for database handling and statement handling. - - - This field is set and maintained by the PDO main driver and should be treated as read-only by the driver. - - - - - - - - Appendix B -- pdo_stmt_t - - - - - - - - - Variable name - - - Function - - - Responsibility - - - - - ce - - - Required for casting as Zend object. - - - Zend engine. It should be treated as read-only by the driver. - - - - - properties - - - Required for casting as Zend object. - - - Zend engine. It should be treated as read-only by the driver. - - - - - in_get - - - Required for casting as Zend object. - - - Zend engine. It should be treated as read-only by the driver. - - - - - in_set - - - Required for casting as Zend object. - - - Zend engine. It should be treated as read-only by the driver. - - - - - methods - - - Pointer to the methods function pointer for SQL statement handling. - - - Driver should set this during the call to <db>_handle_preparer function. - - - - - driver_data - - - Opaque pointer containing driver specific data. - - - Driver can store driver specific data and structures for database handling. This field is typically allocated and set during the call to <db>_handle_preparer. - - - - - executed - - - If true, the statement represented by this object has been executed at least once, otherwise, it's never been executed. This is set automatically after the first call to the stmt executor() function. - - - This field is set and maintained by the PDO main driver and should be treated as read-only by the driver. - - - - - supports_placeholders - - - This field determines whether the driver support placeholders. It can be any one or combination of the following values: - PDO_PLACEHOLDER_NONE - - - Driver has no placeholder support - - - PDO_PLACEHOLDER_NAMED - - - Driver supports named placeholders - - - PDO_PLACEHOLDER_POSITIONAL - - - Driver supports positional placeholders - - - If both named and positional are supported the field should be set to the OR'd value the two values. - - - The driver should set this field to the appropriate value during the call to <db>_handler_preparer. - - - - - _reserved - - - Reserved for PDO main driver. - - - These are reserved for future use by the main PDO driver. - - - - - column_count - - - This field contains the number of columns in the result set. The contents of this field is not valid until the “executed” field is set to true and the statement has been executed. There may be cases (driver specific) where this value is not valid until the drivers fetch method has been called at least once. - - - This field is maintained by the driver. It is normally set during <db>_stmt_execute but can be reset in <db_stmt_next_rowset as well. - - - - - columns - - - This is a pointer to the column description data. It contains column_count number of elements. A column is represented by the pdo_column_data structure. This structure contains the following members: - - - name -- the name of the column - - - namelen -- the length of name - - - maxlen -- the maximum length of the data this column can handle - - - param_type -- the type of data this column contains - - - precision -- if numeric data, the precision of data contained. - - - - - This field is allocated and freed by the PDO main driver. However, the driver is responsible for populating the contents during the call to <db>_stmt_describe. - - - - - database_object_handle - - - This is the Zend object representation of the database handle. This field is kept so that a reference to the database handle object is always referenced while as statement object exists. - - - This field is set and maintained by the PDO main driver and should be treated as read-only by the driver. There are no known instances where a driver needs to access this variable. - - - - - dbh - - - This is a pointer to pdo_dbh_t representation of the database handle. It is valid so long as the database_object_handle is valid. - - - This field is set and maintained by the PDO main driver and should be treated as read-only by the driver. - - - - - bound_params - - - This is a hashtable of bound input parameters. This is kept because not all drivers support both input and out parameters. - - - This field is set and maintained by the PDO main driver and should be treated as read-only by the driver. There are no known instances where a driver needs to access this variable. - - - - - bound_param_map - - - This hashtable contains a mapping of positions to names when rewriting from named to positional. - - - This field is set and maintained by the PDO main driver and should be treated as read-only by the driver. There are no known instances where a driver needs to access this variable. - - - - - bound_columns - - - This hashtable is used to keep track of PHP variables that are bound to named or positional columns in the result set. - - - This field is set and maintained by the PDO main driver and should be treated as read-only by the driver. There are no known instances where a driver needs to access this variable. - - - - - row_count - - - This field represents the count of rows available in the result set. - - - This field is initialized to zero by the PDO main driver and CAN be set by the driver during the <db>_handle_doer method call. Note: not all drivers have this information available at this time so the reliability of this field is driver specific. - - - - - query_string - - - This character string holds the current raw SQL query associated with this statement. - - - This field is set and maintained by the PDO main driver and should be treated as read-only by the driver. There are no known instances where a driver needs to access this variable. - - - - - query_stringlen - - - This field represents the length of the raw SQL query string. - - - This field is set and maintained by the PDO main driver and should be treated as read-only by the driver. There are no known instances where a driver needs to access this variable. - - - - - error_code - - - This is the latest error code for this statement. It is the SQLSTATE expressed as 6 character field. - - - This field is set and maintained by the PDO main driver. Drivers should set the global variable pdo_err whenever an error occurs and the driver will set this field when appropriate. - - - - - lazy_object_ref - - - This field is used to store the lazy object handle (fetch type is equal to PDO_FETCH_LAZY) that is returned for each lazy fetch. - - - This field is set and maintained by the PDO main driver. It should not be used by drivers. - - - - - refcount - - - This is count of how many lazy fetches have been done. - - - This field is set and maintained by the PDO main driver. It should not be used by drivers. - - - - - default_fetch_type - - - This is the default fetch type. It can be any one of the following: - PDO_FETCH_USE_DEFAULT - - - Use the default fetch mode - - - PDO_FETCH_LAZY - - - Specifies that the fetch method shall return each row as an object with variable names that correspond to the column names returned in the result set. PDO_FETCH_LAZY creates the object variable names as they are accessed. - - - PDO_FETCH_ASSOC - - - Specifies that the fetch method shall return each row as an array indexed by column name as returned in the corresponding result set. - - - PDO_FETCH_NUM - - - Specifies that the fetch method shall return each row as an array indexed by column number as returned in the corresponding result set, starting at column 0. - - - PDO_FETCH_BOTH - - - Specifies that the fetch method shall return each row as an array indexed by both column name and number as returned in the corresponding result set, starting at column 0. - - - PDO_FETCH_OBJ - - - Specifies that the fetch method shall return each row as an object with property names that correspond to the column names returned in the result set. - - - PDO_FETCH_BOUND - - - Specifies that the fetch method shall return TRUE and assign the values of the columns in the result set to the variables to which they were bound. - - - PDO_FETCH_COLUMN - - - Specifies that the fetch method shall return only a single requested column from the next row in the result set. - - - PDO_FETCH_CLASS - - - Specifies that the fetch method shall return a new instance of the requested class, mapping the columns to named properties in the class. - - - PDO_FETCH_INTO - - - Specifies that the fetch method shall update an existing instance of the requested class, mapping the columns to named properties in the class. - - - PDO_FETCH_FUNC - - - Fetch into function and return result of function. - - - - - - This field is set and maintained by the PDO main driver. It should not be used by drivers. The default value for this field is PDO_FETCH_BOTH. - - - - - fetch - - - This union is used to contain fetch information used by the PDO main driver. - - - This field is set and maintained by the PDO main driver. It should not be used by drivers. - - - - - - - - Appendix C - Database and Statement Attributes Table - + + + + + This function returns a character string containing the id of the last + inserted row on success or NULL on failure. This is an optional function. + + + + + SKEL_check_liveness + + static int SKEL_check_liveness(pdo_dbh_t *dbh TSRMLS_DC) + + + This function will be called by PDO to test whether or not a persistent + connection to a database is alive and ready for use. + + + + + dbh + + Pointer to the database handle initialized by the handle factory + + + + + + This function returns 1 if the database connection is alive and ready for use, otherwise it should return 0 to indicate failure or lack of support. + + + + + This is an optional function. + + + + + + + SKEL_get_driver_methods + static function_entry *SKEL_get_driver_methods(pdo_dbh_t *dbh, int kind TSRMLS_DC) + + This function will be called by PDO in response to a call to any method + that is not a part of either the PDO or + PDOStatement classes. It's purpose is to allow the + driver to provide additional driver specific methods to those classes. + + + + + dbh + + Pointer to the database handle initialized by the handle factory + + + + kind + + One of the following: + + + PDO_DBH_DRIVER_METHOD_KIND_DBH + + + Set when the method call was attempted on an instance of the + PDO class. The driver should return a pointer + a function_entry table for any methods it wants to add to that class, + or NULL if there are none. + + + + + PDO_DBH_DRIVER_METHOD_KIND_STMT + + + Set when the method call was attempted on an instance of the + PDOStatement class. The driver should return + a pointer to a function_entry table for any methods it wants to add + to that class, or NULL if there are none. + + + + + + + + + + This function returns a pointer to the function_entry table requested, + or NULL there are no driver specific methods. + + + + + SKEL_handle_factory + static int SKEL_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) + + This function will be called by PDO to create a database handle. For most + databases this involves establishing a connection to the database. In some + cases, a persistent connection may be requested, in other cases connection + pooling may be requested. All of these are database/driver dependent. + + + + + dbh + + Pointer to the database handle initialized by the handle factory + + + + driver_options + + + An array of driver options, keyed by integer option number. See for a list of possible attributes. + + + + + + + + This function should fill in the passed database handle structure with its' + driver specific information on success and return 1, otherwise it should + return 0 to indicate failure. + + + PDO processes the AUTOCOMMIT and PERSISTENT driver options + before calling the handle_factory. It is the handle factory's + responsibility to process other options. + + + + + Driver method table + + A static structure of type pdo_dbh_methods named SKEL_methods must be + declared and initialized to the function pointers for each defined + function. If a function is not supported or not implemented the value for + that function pointer should be set to NULL. + + + + + pdo_SKEL_driver + + A structure of type pdo_driver_t named pdo_SKEL_driver should be declared. + The PDO_DRIVER_HEADER(SKEL) macro should be used to declare the header and + the function pointer to the handle factory function should set. + + + + + SKEL_statement.c: Statement implementation + + This unit implements all of the database statement handling methods that + support the PDO statement object. + + + + SKEL_stmt_dtor + + static int SKEL_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC) + + This function will be called by PDO to destroy a previously constructed statement object. + + + + stmt + + Pointer to the statement structure initialized by SKEL_handle_preparer. + + + + + + This should do whatever is necessary to free up any driver specific storage + allocated for the statement. The return value from this function is + ignored. + + + + + SKEL_stmt_execute + static int SKEL_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC) + + This function will be called by PDO to execute the prepared SQL statement + in the passed statement object. + + + + stmt + + Pointer to the statement structure initialized by SKEL_handle_preparer. + + + + + + This function returns 1 for success or 0 in the event of failure. + + + + SKEL_stmt_fetch + static int SKEL_stmt_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori, + long offset TSRMLS_DC) + + + This function will be called by PDO to fetch a row from a previously + executed statement object. + + + + + stmt + + Pointer to the statement structure initialized by SKEL_handle_preparer. + + + + + ori + + One of PDO_FETCH_ORI_xxx which will determine which row will be fetched. + + + + + offset + + + If ori is set to PDO_FETCH_ORI_ABS or PDO_FETCH_ORI_REL, offset + represents the row desired or the row relative to the current position, + respectively. Otherwise, this value is ignored. + + + + + + + The results of this fetch are driver dependent and the data is usually + stored in the driver_data member of pdo_stmt_t object. The ori and offset + parameters are only meaningful if the statement represents a scrollable + cursor. This function returns 1 for success or 0 in the event of failure. + + + + SKEL_stmt_param_hook + static int SKEL_stmt_param_hook(pdo_stmt_t *stmt, + struct pdo_bound_param_data *param, enum pdo_param_event event_type TSRMLS_DC) + + + This function will be called by PDO for handling of both bound parameters and bound columns. + + + + + stmt + + Pointer to the statement structure initialized by SKEL_handle_preparer. + + + + + param + + + The structure describing either a statement parameter or a bound column. + + + + + + event_type + + The type of event to occur for this parameter, one of the following: + + + + PDO_PARAM_EVT_ALLOC + + Called when PDO allocates the binding. Occurs as part of + PDOStatement::bindParam, + PDOStatement::bindValue or as part of an implicit bind + when calling PDOStatement::execute. This is your + opportunity to take some action at this point; drivers that implement + native prepared statements will typically want to query the parameter + information, reconcile the type with that requested by the script, + allocate an appropriately sized buffer and then bind the parameter to + that buffer. You should not rely on the type or value of the zval at + param->parameter at this point in time. + + + + + PDO_PARAM_EVT_FREE + + Called once per parameter as part of cleanup. You should + release any resources associated with that parameter now. + + + + PDO_PARAM_EXEC_PRE + + Called once for each parameter immediately before calling + SKEL_stmt_execute; take this opportunity to make any final adjustments + ready for execution. In particular, you should note that variables + bound via PDOStatement::bindParam are only legal + to touch now, and not any sooner. + + + + + PDO_PARAM_EXEC_POST + + Called once for each parameter immediately after calling + SKEL_stmt_execute; take this opportunity to make any post-execution + actions that might be required by your driver. + + + + PDO_PARAM_FETCH_PRE + + Called once for each parameter immediately prior to calling + SKEL_stmt_fetch. + + + + PDO_PARAM_FETCH_POST + + Called once for each parameter immediately after calling + SKEL_stmt_fetch. + + + + + + + + + + This hook will be called for each bound parameter and bound column in the + statement. For ALLOC and FREE events, a single call will be made for each + parameter or column. The param structure contains a driver_data field that + the driver can use to store implementation specific information about each + of the parameters. + + + For all other events, PDO may call you multple times as the script issues + PDOStatement::execute and + PDOStatement::fetch calls. + + + If this is a bound parameter, the is_param flag in the param structure, + otherwise, the param structure refers to a bound column. + + + This function returns 1 for success or 0 in the event of failure. + + + + + SKEL_stmt_describe_col + static int SKEL_stmt_describe_col(pdo_stmt_t *stmt, int colno TSRMLS_DC) + + + This function will be called by PDO to query information about a particular + column. + + + + + stmt + + Pointer to the statement structure initialized by SKEL_handle_preparer. + + + + colno + + The column number to be queried. + + + + + The driver should populate the pdo_stmt_t member columns(colno) with the + appropriate information. This function returns 1 for success or 0 in the + event of failure. + + + + + SKEL_stmt_get_col_data + static int SKEL_stmt_get_col_data(pdo_stmt_t *stmt, int colno, + char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC) + + This function will be called by PDO to retrieve data from the specified column. + + + + stmt + + Pointer to the statement structure initialized by SKEL_handle_preparer. + + + + colno + + The column number to be queried. + + + + ptr + + Pointer to the retrieved data. + + + + len + + The length of the data pointed to by ptr. + + + + caller_frees + + If set, ptr should point to emalloc'd memory and the main PDO driver will free it as soon as it is done with it. Otherwise, it will be the responsibility of the driver to free any allocated memory as a result of this call. + + + + + The driver should return the resultant data and length of that data in the + ptr and len variables respectively. It should be noted that the main PDO + driver expects the driver to manage the lifetime of the data. This function + returns 1 for success or 0 in the event of failure. + + + + SKEL_stmt_set_attr + static int SKEL_stmt_set_attr(pdo_stmt_t *stmt, long attr, zval *val TSRMLS_DC) + + + This function will be called by PDO to allow the setting of driver specific + attributes for a statement object. + + + + + stmt + + Pointer to the statement structure initialized by SKEL_handle_preparer. + + + + attr + + + long value of one of the PDO_ATTR_xxxx types. See for valid attributes. + + + + + val + + The new value for the attribute. + + + + + + + This function is driver dependent and allows the driver the capability to + set database specific attributes for a statement. This function returns 1 + for success or 0 in the event of failure. This is an optional function. If + the driver does not support additional settable attributes, it can be + NULLed in the method table. The PDO driver does not handle any settable + attributes on the database driver's behalf. + + + + SKEL_stmt_get_attr + static int SKEL_stmt_get_attr(pdo_stmt_t *stmt, long attr, zval + *return_value TSRMLS_DC) + + This function will be called by PDO to allow the retrieval of driver + specific attributes for a statement object. + + + + + stmt + + Pointer to the statement structure initialized by SKEL_handle_preparer. + + + + attr + + + long value of one of the PDO_ATTR_xxxx types. See for valid attributes. + + + + + return_value + + The returned value for the attribute. + + + + + + This function is driver dependent and allows the driver the capability to + retrieve a previously set database specific attribute for a statement. This + function returns 1 for success or 0 in the event of failure. This is an + optional function. If the driver does not support additional gettable + attributes, it can be NULLed in the method table. The PDO driver does not + handle any settable attributes on the database driver's behalf. + + + + SKEL_stmt_get_col_meta + static int SKEL_stmt_get_col_meta(pdo_stmt_t *stmt, int colno, + zval *return_value TSRMLS_DC) + + + This function is not well defined and is subject to change. + + + + + This function will be called by PDO to retrieve meta-data from the + specified column. + + + + stmt + + Pointer to the statement structure initialized by SKEL_handle_preparer. + + + + colno + + The column number for which data is to be retrieved. + + + + return_value + + Holds the returned meta data. + + + + + + The driver author should consult the documentation for this function that can be + found in the php_pdo_driver.h header as this will be the most current. This + function returns 1 for success or 0 in the event of failure. The database + driver does not need to provide this function. + + + + + Statement handling method table + + A static structure of type pdo_stmt_methods named SKEL_stmt_methods should + be declared and initialized to the function pointers for each defined + function. If a function is not supported or not implemented the value for + that function pointer should be set to NULL. + + + + + + + Building + + The build process is designed to work with PEAR (see http://pear.php.net/index.php for more information about + PEAR). There are two files that are used to assist in configuring your + package for building. The first is config.m4 which is the autoconf + configuration file for all platforms except Win32. The second is config.w32 + which a build configuration file for use on Win32. Skeleton files for + these are built for you when you first set up your project. You then need + to customize them to fit the needs of your project. Once you've customized + your config files, you can build your driver using the following sequence + of commands: + + + + Before first build: + + + +$ sudo pear install PDO + + + + For each build: + + + +$ cd pdo_SKEL +$ phpize +$ ./configure +$ make +$ sudo make install + + + + The process can then be repeated as necessary during the development + process. + + + + + Testing + + PDO has a set of "core" tests that all drivers should pass before being + released. They're designed to run from the PHP source distribution, so + running the tests for your driver requires moving things around a bit. + The suggested procedure is to obtain the latest PHP 5.1 snapshot and + perform the following step: + + + +$ cp -r pdo_SKEL /path/to/php-5.1/ext + + + This will allow the test harness to run your tests. The next thing you + need to do is create a test that will redirect into the PDO common core tests. + The convention is to name this file common.phpt; it + should be placed in the tests subdirectory that was created by + ext_skel when you created your extension skeleton. + The content of this file should look something like the following: + + +--REDIRECTTEST-- +if (false !== getenv('PDO_SKEL_TEST_DSN')) { +# user set them from their shell + $config['ENV']['PDOTEST_DSN'] = getenv('PDO_SKEL_TEST_DSN'); + $config['ENV']['PDOTEST_USER'] = getenv('PDO_SKEL_TEST_USER'); + $config['ENV']['PDOTEST_PASS'] = getenv('PDO_SKEL_TEST_PASS'); + if (false !== getenv('PDO_SKEL_TEST_ATTR')) { + $config['ENV']['PDOTEST_ATTR'] = getenv('PDO_SKEL_TEST_ATTR'); + } + return $config; +} +return array( + 'ENV' => array( + 'PDOTEST_DSN' => 'SKEL:dsn', + 'PDOTEST_USER' => 'username', + 'PDOTEST_PASS' => 'password' + ), + 'TESTS' => 'ext/pdo/tests' + ); +]]> + + + This will cause the common core tests to be run, passing the values of + PDOTEST_DSN, PDOTEST_USER and + PDOTEST_PASS to the PDO constructor as the + dsn, username and + password parameters. It will first check the environment, so + that appropriate values can be passed in when the test harness is run, + rather than hard-coding the database credentials into the test file. + + + + The test harness can be invoked as follows: + + + +$ cd /path/to/php-5.1 +$ make TESTS=ext/pdo_SKEL/tests PDO_SKEL_TEST_DSN="skel:dsn" \ + PDO_SKEL_TEST_USER=user PDO_SKEL_TEST_PASS=pass test + + + + + + Packaging and distribution + + Creating a package + + PDO drivers are released via PECL; all the usual rules for PECL extensions + apply. Packaging is accomplished by creating a valid + package.xml file and then running: + + +$ pear package + + + This will create a tarball named PDO_SKEL-X.Y.Z.tgz. + + + Before releasing the package, you should test that it builds correctly; if + you've made a mistake in your config.m4 or + package.xml files, the package may not function + correctly. You can test the build, without installing anything, using the + following invocation: + + +$ pear build package.xml + + + Once this is proven to work, you can test installation: + + +$ pear package +$ sudo pear install PDO_SKEL-X.Y.X.tgz + + + + Full details about package.xml can be found in the PEAR + Programmer's documentation (http://pear.php.net/manual/index.php). + + + + Releasing the package + + A PDO driver is released via the PHP Extension Community Library (PECL). + Information about PECL can be found at http://pecl.php.net/index.php. + + + + + + pdo_dbh_t definition + + All fields should be treated as read-only by the driver, unless explicitly + stated otherwise. + +
+ pdo_dbh_t + +/* represents a connection to a database */ +struct _pdo_dbh_t { + /* driver specific methods */ + struct pdo_dbh_methods *methods; + /* driver specific data */ + void *driver_data; + + /* credentials */ + char *username, *password; + + /* if true, then data stored and pointed at by this handle must all be + * persistently allocated */ + unsigned is_persistent:1; + + /* if true, driver should act as though a COMMIT were executed between + * each executed statement; otherwise, COMMIT must be carried out manually + * */ + unsigned auto_commit:1; + + /* if true, the driver requires that memory be allocated explicitly for + * the columns that are returned */ + unsigned alloc_own_columns:1; + + /* if true, commit or rollBack is allowed to be called */ + unsigned in_txn:1; + + /* max length a single character can become after correct quoting */ + unsigned max_escaped_char_length:3; + + /* data source string used to open this handle */ + const char *data_source; + unsigned long data_source_len; + + /* the global error code. */ + pdo_error_type error_code; + + enum pdo_case_conversion native_case, desired_case; +}; + +
+ + + + The driver must set this during + SKEL_handle_factory. + + + + + This item is for use by the driver; the intended usage is to store a + pointer (during SKEL_handle_factory) + to whatever instance data is required to maintain a connection to + the database. + + + + + The username and password that were passed into the PDO constructor. + The driver should use these values when it initiates a connection to the + database. + + + + + If this is set to 1, then any data that is referenced by the + dbh, including whatever structure your driver allocates, + MUST be allocated persistently. This is easy to + achieve; rather than using the usual emalloc simply + use pemalloc and pass the value of this flag as the + last parameter. Failure to use the appropriate kind of memory can lead + to serious memory faults, resulting (in the best case) a hard crash, and + in the worst case, an exploitable memory problem. + + + If, for whatever reason, your driver is not suitable to run persistently, + you MUST check this flag in your + SKEL_handle_factory and raise an appropriate error. + + + + + You should check this value in your SKEL_handle_doer + and SKEL_stmt_execute functions; if it evaluates to + true, you must attempt to commit the query now. Most database + implementations offer an auto-commit mode that handles this automatically. + + + + + If your database client library API operates by fetching data into a + caller-supplied buffer, you should set this flag to 1 during your + SKEL_handle_factory. When set, PDO will call your + SKEL_stmt_describer earlier that it would + otherwise. This early call allows you to determine those buffer sizes + and issue appropriate calls to the database client library. + + + If your database client library API simply returns pointers to its own + internal buffers for you to copy after each fetch call, you should leave + this value set to 0. + + + + + If your driver doesn't support native prepared statements + (supports_placeholders is set to + PDO_PLACEHOLDER_NONE), you must set + this value to the maximum length that can be taken up by a single + character when it is quoted by your + SKEL_handle_quoter function. This value is used to + calculate the amount of buffer space required when PDO it executes the + statement. + + + + + This holds the value of the DSN that was passed into the PDO + constructor. If your driver implementation needed to modify the DSN for + whatever reason, it should update this member during + SKEL_handle_factory. Modifying this member should + be avoided. If you do change it, you must ensure that + data_source_len is also correct. + + + + + Whenever an error occurs during a call to one of your driver methods, + you should set this member to the SQLSTATE code that best describes the + error and return an error. In this HOW-TO, the suggested practice is to + call SKEL_handle_error when an error is detected, + and have it set the error code. + + + + + Your driver should set this during + SKEL_handle_factory; the value should reflect how + the database returns the names of the columns in result sets. If the + name matches the case that was used in the query, set it to + PDO_CASE_NATURAL (this is actually the default). + If the column names are always returned in upper case, set it to + PDO_CASE_UPPER. If the column names are always + return in lower case, set it to PDO_CASE_LOWER. + The value you set it used to determine if PDO should perform case + folding when the user sets the PDO_ATTR_CASE + attribute. + + + +
+ + pdo_stmt_t definition + + All fields should be treated as read-only unless explicitly stated + otherwise. + +
+ pdo_stmt_t + +/* represents a prepared statement */ +struct _pdo_stmt_t { + /* driver specifics */ + struct pdo_stmt_methods *methods; + void *driver_data; + + /* if true, we've already successfully executed this statement at least + * once */ + unsigned executed:1; + /* if true, the statement supports placeholders and can implement + * bindParam() for its prepared statements, if false, PDO should + * emulate prepare and bind on its behalf */ + unsigned supports_placeholders:2; + + /* the number of columns in the result set; not valid until after + * the statement has been executed at least once. In some cases, might + * not be valid until fetch (at the driver level) has been called at least once. + * */ + int column_count; + struct pdo_column_data *columns; + + /* points at the dbh that this statement was prepared on */ + pdo_dbh_t *dbh; + + /* keep track of bound input parameters. Some drivers support + * input/output parameters, but you can't rely on that working */ + HashTable *bound_params; + /* When rewriting from named to positional, this maps positions to names */ + HashTable *bound_param_map; + /* keep track of PHP variables bound to named (or positional) columns + * in the result set */ + HashTable *bound_columns; + + /* not always meaningful */ + long row_count; + + /* used to hold the statement's current query */ + char *query_string; + int query_stringlen; + + /* the copy of the query with expanded binds ONLY for emulated-prepare drivers */ + char *active_query_string; + int active_query_stringlen; + + /* the cursor specific error code. */ + pdo_error_type error_code; + + /* used by the query parser for driver specific + * parameter naming (see pgsql driver for example) */ + const char *named_rewrite_template; +}; + +
+ + + + The driver must set this during + SKEL_handle_preparer. + + + + + This item is for use by the driver; the intended usage is to store a + pointer (during SKEL_handle_factory) + to whatever instance data is required to maintain a connection to + the database. + + + + + This is set by PDO after the statement has been executed for the first + time. Your driver can inspect this value to determine if it can skip + one-time actions as an optimization. + + + + + Discussed in more detail in + + + + + Your driver is responsible for setting this field to the number of + columns available in a result set. This is usually set during + SKEL_stmt_execute but with some database + implementations, the column count may not be available until + SKEL_stmt_fetch has been called at least once. + Drivers that SKEL_stmt_next_rowset should update the + column count when a new rowset is available. + + + + + PDO will allocate this field based on the value that you set for the + column count. You are responsible for populating each column during + SKEL_stmt_describe. You must set the + precision, maxlen, + name, namelen and + param_type members for each column. + The name is expected to be allocated using + emalloc; PDO will efree at the + appropriate time. + + + +
+ + + Constants + + Database and Statement Attributes Table - + Attribute - + Valid value(s) - + PDO_ATTR_AUTOCOMMIT - - ZVAL_BOOL + + BOOL TRUE if autocommit is set, FALSE otherwise. - Dbh->auto_commit contains value. Processed by the PDO driver directly. + dbh->auto_commit contains value. Processed by PDO directly. - + PDO_ATTR_PREFETCH - - ZVAL_LONG + + LONG Value of the prefetch size in drivers that support it. - + PDO_ATTR_TIMEOUT - - ZVAL_LONG + + LONG How long to wait for a db operation before timing out. - + PDO_ATTR_ERRMODE - - ZVAL_LONG - PDO_ERRMODE_SILENT - - - Fail silently - - - PDO_ERRMODE_WARNING - - - Treat all errors as warnings. - - - PDO_ERRMODE_EXCEPTION - - - Throw exceptions instead of returning error codes. - - - + + LONG + Processed and handled by PDO - + PDO_ATTR_SERVER_VERSION - - ZVAL_STRING - The “human-readable” string representing the Server/Version this driver supports. + + STRING + The human-readable string representing the + Server/Version this driver is currently connected to. - + PDO_ATTR_CLIENT_VERSION - - ZVAL_STRING - The “human-readable” string representing the Client/Version this driver supports. + + STRING + The human-readable string representing the Client/Version this driver supports. - + PDO_ATTR_SERVER_INFO - - ZVAL_STRING - The “human-readable” description of the Server. + + STRING + The human-readable description of the Server. - + PDO_ATTR_CONNECTION_STATUS - - ZVAL_LONG + + LONG Values not yet defined - + PDO_ATTR_CASE - - ZVAL_LONG - PDO_CASE_NATURAL - - - Don't do case conversion - - - PDO_CASE_UPPER - - - Convert strings to uppercase - - - PDO_CASE_LOWER - - - Convert strings to lowercase - - - - - + + LONG + Processed and handled by PDO. - + PDO_ATTR_CURSOR_NAME - - ZVAL_STRING - String representing the name for a database cursor for use in “where current in <name>” SQL statements + + STRING + + String representing the name for a database cursor for use in + where current in <name> SQL statements. + - + PDO_ATTR_CURSOR - - ZVAL_LONG - PDO_CURSOR_FWDONLY - - + + LONG + + + PDO_CURSOR_FWDONLY + Forward only cursor - - PDO_CURSOR_SCROLL - + + + PDO_CURSOR_SCROLL Scrollable cursor - - - - - - - PDO_ATTR_ORACLE_NULLS - - - ZVAL_LONG - (For Oracle Driver) - dbh->oracle_nulls is set the result of Z_LVAL_P(value) ? 1 : 0 - - - - - PDO_ATTR_PERSISTENT - - - ZVAL_BOOL - True if persistent connection, false if not. Processed directly by the PDO driver. - - - - - PDO_ATTR_STATEMENT_CLASS - - - ZVAL_LONG - Values not yet defined. + + - - - The values for the attributes above are all defined in terms of the Zend API. The Zend API contains macros that can be used to convert a *zval to a value. These macros are defined in the Zend header file, zend_API.h in the Zend directory of your PHP build directory. Some of these attributes can be used with the statement attribute handlers such as the PDO_ATTR_CURSOR and PDO_ATTR_CURSOR_NAME. See the statement attribute handling functions for more information. - - Appendix D -- Error handling - - Error handling is implemented using a hand-shaking protocol between the main PDO driver and the database driver code. The database driver code signals the PDO driver than an error has occurred via a failure (“0”) return from any of the interface functions. If a zero is returned, the field error_code in the control block appropriate to the context (either the pdo_dbh_t or pdo_stmt_t block). In practice, it is probably a good idea to set the field in both blocks to the same value to ensure the correct one is getting used. - - The error_mode field is a six-field field containing a 5 character ASCIIZ SQLSTATE identifier code. This code drives the error message process. The SQLSTATE code is used to look up an error message in the internal PDO error message table (see pdo_sqlstate.c for a list of error codes and their messages). If the code is not known to the PDO driver, a default “Unknown Message” value will be used. - - In addition to the SQLSTATE code and error message, the PDO driver will call the driver-specific fetch-err() routine to obtain supplemental data for the particular error condition. This routine is passed an array into which the driver may place additional information. This array has slot positions assigned to particular types of supplemental info: - - +
+ + The values for the attributes above are all defined in terms of the Zend + API. The Zend API contains macros that can be used to convert a *zval to a + value. These macros are defined in the Zend header file, zend_API.h in the + Zend directory of your PHP build directory. Some of these attributes can be + used with the statement attribute handlers such as the PDO_ATTR_CURSOR and + PDO_ATTR_CURSOR_NAME. See the statement attribute handling functions for + more information. + +
+ + Error handling + + Error handling is implemented using a hand-shaking protocol between + PDO and the database driver code. The database driver code + signals PDO than an error has occurred via a failure + (0) return from any of the interface functions. If a zero + is returned, the field error_code in the control block appropriate to the + context (either the pdo_dbh_t or pdo_stmt_t block). In practice, it is + probably a good idea to set the field in both blocks to the same value to + ensure the correct one is getting used. + + + + The error_mode field is a six-byte field containing a 5 character ASCIIZ + SQLSTATE identifier code. This code drives the error message process. The + SQLSTATE code is used to look up an error message in the internal PDO error + message table (see pdo_sqlstate.c for a list of error codes and their + messages). If the code is not known to PDO, a default + Unknown Message value will be used. + + + + In addition to the SQLSTATE code and error message, PDO will + call the driver-specific fetch_err() routine to obtain supplemental data + for the particular error condition. This routine is passed an array into + which the driver may place additional information. This array has slot + positions assigned to particular types of supplemental info: + + + - A native error code. This will frequently be an error code obtained from the database API. + + A native error code. This will frequently be an error code obtained + from the database API. + - A descriptive string. This string can contain anything additional information related to the failure. Database drivers typically include information such as an error message, code location of the failure, and any additional descriptive information the driver developer feels worthy of inclusion. It is generally a good idea to include all diagnostic information obtainable from the database interface at the time of the failure. For driver-detected errors (such as memory allocation problems), the driver developer can define whatever error information that seems appropriate. + + A descriptive string. This string can contain anything additional + information related to the failure. Database drivers typically include + information such as an error message, code location of the failure, and + any additional descriptive information the driver developer feels + worthy of inclusion. It is generally a good idea to include all + diagnostic information obtainable + from the database interface at the time of the failure. For + driver-detected errors (such as memory allocation problems), the driver + developer can define whatever error information that seems appropriate. + - - -