From ad180ce883c3e64b89d433ab32035841c73d89cc Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 18 Sep 2005 01:13:06 +0000 Subject: [PATCH] IBM has contributed an howto for creating PDO drivers. They supplied the text to me as a word doc, and I've converted that to Open Office and then used OOo2sDbk (http://www.chez.com/ebellot/ooo2sdbk/) to arrive at what you see here. This text is based on an older version of the driver API, and I will be updating this in the near future. I'd like to ask a phpdoc'er to volunteer to patch up the formatting to make it acceptable for inclusion in the manual before I do this; I'm really short on time right now, and I'm not up to speed with the latest phpdoc rules. Feel free to split this document out into multiple files as you see fit. Many thanks! git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@196270 c90b9560-bf6c-de11-be94-00142212c4b1 --- internals/pdo/index.xml | 2191 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 2191 insertions(+) create mode 100644 internals/pdo/index.xml diff --git a/internals/pdo/index.xml b/internals/pdo/index.xml new file mode 100644 index 0000000000..30c3cf060f --- /dev/null +++ b/internals/pdo/index.xml @@ -0,0 +1,2191 @@ + + + + + + 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 working development environment: + + + Other Unix: standard development tools supplied by vendor plus the GNU development tool set; + + + Win32: Visual Studio compiler suite; + + + Linux: standard development tools, gcc, ld, make, autoconf, automake, etc., versions dependent on distribution; + + + + + 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 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); + + + 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 + The following standard public php headers should be included in each source module: + + + php.h + + + php_ini.h + + + ext/standard/info.h + + + PDO Interface Headers + The following standard public pdo header files are also included in each source module: + + + 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.. + + + + + 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. + + + + + + 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( + + + pdo_dbh_t *dbh + + 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. + + 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 + + + Pointer to a character string containing the SQL statement to be prepared. + + + + + 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 + + + Pointer to a character string containing the string to be quoted. + + + + + unquoted_len + + + The length of the string to be quoted. + + + + + quoted + + + Pointer to the address where a pointer to the newly quoted string will be returned. + + + + + quoted_len + + + The length of the new string. + + + + + 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 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. + + 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: + + + sudo pear install PDO + + + For each build: + + + cd pdo_<db> + + + phpize + + + ./configure + + + make + + + sudo make install + + + 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 + + + + + + + + Attribute + + + Valid value(s) + + + + + PDO_ATTR_AUTOCOMMIT + + + ZVAL_BOOL + TRUE if autocommit is set, FALSE otherwise. + Dbh->auto_commit contains value. Processed by the PDO driver directly. + + + + + PDO_ATTR_PREFETCH + + + ZVAL_LONG + Value of the prefetch size in drivers that support it. + + + + + PDO_ATTR_TIMEOUT + + + ZVAL_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. + + + + + + + + PDO_ATTR_SERVER_VERSION + + + ZVAL_STRING + The “human-readable” string representing the Server/Version this driver supports. + + + + + PDO_ATTR_CLIENT_VERSION + + + ZVAL_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. + + + + + PDO_ATTR_CONNECTION_STATUS + + + ZVAL_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 + + + + + + + + + + PDO_ATTR_CURSOR_NAME + + + ZVAL_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 + + + Forward only cursor + + + 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: + + + + 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. + + + + + + + + + + + +