internals2.pdo.implementing.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 1,696 行 · 第 1/4 页
HTML
1,696 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Fleshing out your skeleton</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="internals2.pdo.preparation.html">Preparation and Housekeeping</a></div> <div class="next" style="text-align: right; float: right;"><a href="internals2.pdo.building.html">Building</a></div> <div class="up"><a href="internals2.pdo.html">PDO Driver How-To</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="internals2.pdo.implementing" class="sect1"> <h2 class="title">Fleshing out your skeleton</h2> <div id="internals2.pdo.implementing.structures" class="sect2"> <h3 class="title">Major Structures and Attributes</h3> <p class="para"> 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. </p> </div> <div id="internals2.pdo.implementing.skel" class="sect2"> <h3 class="title">pdo_SKEL.c: PHP extension glue</h3> <div id="internals2.pdo.implementing.skel.entries" class="sect3"> <h4 class="title">function entries</h4> <pre class="synopsis"><div class="cdata"><pre>static function_entry pdo_SKEL_functions[] = { { NULL, NULL, NULL }};</pre></div></pre> <p class="para"> 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. </p> </div> <div id="internals2.pdo.implementing.skel.module" class="sect3"> <h4 class="title">Module entry</h4> <pre class="synopsis"><div class="cdata"><pre>/* {{{ pdo_SKEL_module_entry */#if ZEND_EXTENSION_API_NO >= 220050617static 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_<DB>_MODULE_VERSION, STANDARD_MODULE_PROPERTIES};/* }}} */#ifdef COMPILE_DL_PDO_<DB>ZEND_GET_MODULE(pdo_db)#endif</pre></div></pre> <p class="para"> 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. </p> </div> <div id="internals2.pdo.implementing.skel.functions" class="sect3"> <h4 class="title">Standard PHP Module Extension Functions</h4> <div id="internals2.pdo.implementing.skel.functions.minit" class="sect4"> <h5 class="title">PHP_MINIT_FUNCTION</h5> <pre class="synopsis"><div class="cdata"><pre>/* {{{ PHP_MINIT_FUNCTION */PHP_MINIT_FUNCTION(pdo_SKEL){ return php_pdo_register_driver(&pdo_SKEL_driver);}/* }}} */</pre></div></pre> <p class="para"> This standard PHP extension function should be used to register your driver with the PDO layer. This is done by calling the <b>php_pdo_register_driver()</b> function passing a pointer to a structure of type <span class="type pdo_driver_t">pdo_driver_t</span> typically named <i>pdo_SKEL_driver</i>. A <span class="type pdo_driver_t">pdo_driver_t</span> contains a header that is generated using the <i>PDO_DRIVER_HEADER(SKEL)</i> macro and <b>pdo_SKEL_handle_factory()</b> function pointer. The actual function is described during the discussion of the <var class="filename">SKEL_dbh.c</var> unit. </p> </div> <div id="internals2.pdo.implementing.skel.functions.mshutdown" class="sect4"> <h5 class="title">PHP_MSHUTDOWN_FUNCTION</h5> <pre class="synopsis"><div class="cdata"><pre>/* {{{ PHP_MSHUTDOWN_FUNCTION */PHP_MSHUTDOWN_FUNCTION(pdo_SKEL){ php_pdo_unregister_driver(&pdo_SKEL_driver); return SUCCESS;}/* }}} */</pre></div></pre> <p class="para"> This standard PHP extension function is used to unregister your driver from the PDO layer. This is done by calling the <b>php_pdo_unregister_driver()</b> function, passing the same <i>pdo_SKEL_driver</i> structure that was passed in the init function above. </p> </div> <div id="internals2.pdo.implementing.skel.functions.minfo" class="sect4"> <h5 class="title">PHP_MINFO_FUNCTION</h5> <p class="para"> This is again a standard PHP extension function. Its purpose is to display information regarding the module when the <a href="function.phpinfo.html" class="function">phpinfo()</a> 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. </p> </div> </div> </div> <div id="internals2.pdo.implementing.driver" class="sect2"> <h3 class="title">SKEL_driver.c: Driver implementation</h3> <p class="para"> 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. </p> <div id="internals2.pdo.implementing.driver.error" class="sect3"> <h4 class="title">pdo_SKEL_error</h4> <pre class="synopsis"><div class="cdata"><pre>static int pdo_SKEL_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line TSRMLS_DC)</pre></div></pre> <p class="para"> 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 <i>dbh->error_code</i> or <i>stmt->error_code</i> to an SQLSTATE that most closely matches the error or the generic SQLSTATE error "<span class="quote">HY000</span>". 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 <i><tt class="parameter">pdo_err</tt></i> variable to the error found in either the dbh or the stmt (if the variable stmt is not NULL). </p> <dl> <dt> <span class="term">dbh</span> <dd> <p class="para">Pointer to the database handle initialized by the handle factory</p> </dd> </dt> <dt> <span class="term">stmt</span> <dd> <p class="para">Pointer to the current statement or NULL. If NULL, the error is derived by error code found in the dbh.</p> </dd> </dt> <dt> <span class="term">file</span> <dd> <p class="para">The source file where the error occurred or NULL if not available.</p> </dd> </dt> <dt> <span class="term">line</span> <dd> <p class="para">The line number within the source file if available.</p> </dd> </dt> </dl> <p class="para"> 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. </p> <div class="example" id="internals2.pdo.implementing.driver.error.ex-macros" name="internals2.pdo.implementing.driver.error.ex-macros"> <p><b>Example #1 Example macros for invoking pdo_SKEL_error</b></p> <div class="example-contents"><div class="cdata"><pre>#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)</pre></div> </div> </div> <p class="para"> For more info on error handling, see <a href="internals2.pdo.error-handling.html" class="xref">Error handling</a>. </p> <blockquote><p><b class="note">Note</b>: Despite being documented here, the PDO driver interface does not specify that this 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. <br /> </p></blockquote> </div> <div id="internals2.pdo.implementing.driver.fetch-err" class="sect3"> <h4 class="title">pdo_SKEL_fetch_error_func</h4> <pre class="synopsis"><div class="cdata"><pre>static int pdo_SKEL_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC)</pre></div></pre> <p class="para"> 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 <a href="pdo.errorinfo.html" class="function">PDO::errorInfo()</a> method. </p> <dl> <dt> <span class="term">dbh</span> <dd> <p class="para">Pointer to the database handle initialized by the handle factory</p> </dd> </dt> <dt> <span class="term">stmt</span> <dd> <p class="para"> Pointer to the most current statement or NULL. If NULL, the error translated is derived by error code found in the dbh. </p> </dd> </dt> <dt> <span class="term">info</span> <dd> <p class="para">A hash table containing error codes and messages.</p> </dd> </dt> </dl> <p class="para"> 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 <span class="type long">long</span>; use whichever type most closely matches the error code returned by the underlying database API. </p> <div class="example-contents"><div class="cdata"><pre>/* now add the error information. *//* These need to be added in a specific order */add_next_index_long(info, error_code); /* driver specific error code */add_next_index_string(info, message, 0); /* readable error message */</pre></div></div> <p class="para"> This function should return 1 if information is available, 0 if the driver does not have additional info. </p> </div> <div id="internals2.pdo.implementing.driver.handle-closer" class="sect3"> <h4 class="title">SKEL_handle_closer</h4> <pre class="synopsis"><div class="cdata"><pre>static int SKEL_handle_closer(pdo_dbh_t *dbh TSRMLS_DC)</pre></div></pre> <p class="para"> This function will be called by PDO to close an open database. </p> <dl> <dt> <span class="term">dbh</span> <dd> <p class="para">Pointer to the database handle initialized by the handle factory</p> </dd> </dt> </dl> <p class="para"> 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. </p> </div> <div id="internals2.pdo.preparer" class="sect3"> <h4 class="title">SKEL_handle_preparer</h4> <pre class="synopsis"><div class="cdata"><pre>static int SKEL_handle_preparer(pdo_dbh_t *dbh, const char *sql,long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC)</pre></div></pre> <p class="para"> This function will be called by PDO in response to <a href="pdo.query.html" class="function">PDO::query()</a> and <a href="pdo.prepare.html" class="function">PDO::prepare()</a> calls from the PHP script. The purpose of the function is to prepare raw SQL for execution, storing whatever state is appropriate into the <i><tt class="parameter">stmt</tt></i> that is passed in. </p> <dl> <dt> <span class="term">dbh</span> <dd> <p class="para">Pointer to the database handle initialized by the handle factory</p> </dd> </dt> <dt> <span class="term">sql</span> <dd> <p class="para">Pointer to a character string containing the SQL statement to be prepared.</p> </dd> </dt> <dt> <span class="term">sql_len</span> <dd> <p class="para">The length of the SQL statement.</p> </dd> </dt> <dt> <span class="term">Stmt</span> <dd> <p class="para">Pointer to the returned statement or NULL if an error occurs.</p> </dd> </dt> <dt> <span class="term">driver_options</span> <dd> <p class="para">Any driver specific/defined options.</p> </dd>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?