DB
[ class tree: DB ] [ index: DB ] [ all elements ]

Source for file DB.php

Documentation is available at DB.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * Database independent query interface
  7.  *
  8.  * PHP versions 4 and 5
  9.  *
  10.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  11.  * that is available through the world-wide-web at the following URI:
  12.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  13.  * the PHP License and are unable to obtain it through the web, please
  14.  * send a note to license@php.net so we can mail you a copy immediately.
  15.  *
  16.  * @category   Database
  17.  * @package    DB
  18.  * @author     Stig Bakken <ssb@php.net>
  19.  * @author     Tomas V.V.Cox <cox@idecnet.com>
  20.  * @author     Daniel Convissor <danielc@php.net>
  21.  * @copyright  1997-2007 The PHP Group
  22.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  23.  * @version    CVS: $Id: DB.php,v 1.88 2007/08/12 05:27:25 aharvey Exp $
  24.  * @link       http://pear.php.net/package/DB
  25.  */
  26.  
  27. /**
  28.  * Obtain the PEAR class so it can be extended from
  29.  */
  30. require_once 'PEAR.php';
  31.  
  32.  
  33. // {{{ constants
  34. // {{{ error codes
  35.  
  36. /**#@+
  37.  * One of PEAR DB's portable error codes.
  38.  * @see DB_common::errorCode(), DB::errorMessage()
  39.  *
  40.  * {@internal If you add an error code here, make sure you also add a textual
  41.  * version of it in DB::errorMessage().}}
  42.  */
  43.  
  44. /**
  45.  * The code returned by many methods upon success
  46.  */
  47. define('DB_OK'1);
  48.  
  49. /**
  50.  * Unkown error
  51.  */
  52. define('DB_ERROR'-1);
  53.  
  54. /**
  55.  * Syntax error
  56.  */
  57. define('DB_ERROR_SYNTAX'-2);
  58.  
  59. /**
  60.  * Tried to insert a duplicate value into a primary or unique index
  61.  */
  62. define('DB_ERROR_CONSTRAINT'-3);
  63.  
  64. /**
  65.  * An identifier in the query refers to a non-existant object
  66.  */
  67. define('DB_ERROR_NOT_FOUND'-4);
  68.  
  69. /**
  70.  * Tried to create a duplicate object
  71.  */
  72. define('DB_ERROR_ALREADY_EXISTS'-5);
  73.  
  74. /**
  75.  * The current driver does not support the action you attempted
  76.  */
  77. define('DB_ERROR_UNSUPPORTED'-6);
  78.  
  79. /**
  80.  * The number of parameters does not match the number of placeholders
  81.  */
  82. define('DB_ERROR_MISMATCH'-7);
  83.  
  84. /**
  85.  * A literal submitted did not match the data type expected
  86.  */
  87. define('DB_ERROR_INVALID'-8);
  88.  
  89. /**
  90.  * The current DBMS does not support the action you attempted
  91.  */
  92. define('DB_ERROR_NOT_CAPABLE'-9);
  93.  
  94. /**
  95.  * A literal submitted was too long so the end of it was removed
  96.  */
  97. define('DB_ERROR_TRUNCATED'-10);
  98.  
  99. /**
  100.  * A literal number submitted did not match the data type expected
  101.  */
  102. define('DB_ERROR_INVALID_NUMBER'-11);
  103.  
  104. /**
  105.  * A literal date submitted did not match the data type expected
  106.  */
  107. define('DB_ERROR_INVALID_DATE'-12);
  108.  
  109. /**
  110.  * Attempt to divide something by zero
  111.  */
  112. define('DB_ERROR_DIVZERO'-13);
  113.  
  114. /**
  115.  * A database needs to be selected
  116.  */
  117. define('DB_ERROR_NODBSELECTED'-14);
  118.  
  119. /**
  120.  * Could not create the object requested
  121.  */
  122. define('DB_ERROR_CANNOT_CREATE'-15);
  123.  
  124. /**
  125.  * Could not drop the database requested because it does not exist
  126.  */
  127. define('DB_ERROR_CANNOT_DROP'-17);
  128.  
  129. /**
  130.  * An identifier in the query refers to a non-existant table
  131.  */
  132. define('DB_ERROR_NOSUCHTABLE'-18);
  133.  
  134. /**
  135.  * An identifier in the query refers to a non-existant column
  136.  */
  137. define('DB_ERROR_NOSUCHFIELD'-19);
  138.  
  139. /**
  140.  * The data submitted to the method was inappropriate
  141.  */
  142. define('DB_ERROR_NEED_MORE_DATA'-20);
  143.  
  144. /**
  145.  * The attempt to lock the table failed
  146.  */
  147. define('DB_ERROR_NOT_LOCKED'-21);
  148.  
  149. /**
  150.  * The number of columns doesn't match the number of values
  151.  */
  152. define('DB_ERROR_VALUE_COUNT_ON_ROW'-22);
  153.  
  154. /**
  155.  * The DSN submitted has problems
  156.  */
  157. define('DB_ERROR_INVALID_DSN'-23);
  158.  
  159. /**
  160.  * Could not connect to the database
  161.  */
  162. define('DB_ERROR_CONNECT_FAILED'-24);
  163.  
  164. /**
  165.  * The PHP extension needed for this DBMS could not be found
  166.  */
  167. define('DB_ERROR_EXTENSION_NOT_FOUND',-25);
  168.  
  169. /**
  170.  * The present user has inadequate permissions to perform the task requestd
  171.  */
  172. define('DB_ERROR_ACCESS_VIOLATION'-26);
  173.  
  174. /**
  175.  * The database requested does not exist
  176.  */
  177. define('DB_ERROR_NOSUCHDB'-27);
  178.  
  179. /**
  180.  * Tried to insert a null value into a column that doesn't allow nulls
  181.  */
  182. define('DB_ERROR_CONSTRAINT_NOT_NULL',-29);
  183. /**#@-*/
  184.  
  185.  
  186. // }}}
  187. // {{{ prepared statement-related
  188.  
  189.  
  190. /**#@+
  191.  * Identifiers for the placeholders used in prepared statements.
  192.  * @see DB_common::prepare()
  193.  */
  194.  
  195. /**
  196.  * Indicates a scalar (<kbd>?</kbd>) placeholder was used
  197.  *
  198.  * Quote and escape the value as necessary.
  199.  */
  200. define('DB_PARAM_SCALAR'1);
  201.  
  202. /**
  203.  * Indicates an opaque (<kbd>&</kbd>) placeholder was used
  204.  *
  205.  * The value presented is a file name.  Extract the contents of that file
  206.  * and place them in this column.
  207.  */
  208. define('DB_PARAM_OPAQUE'2);
  209.  
  210. /**
  211.  * Indicates a misc (<kbd>!</kbd>) placeholder was used
  212.  *
  213.  * The value should not be quoted or escaped.
  214.  */
  215. define('DB_PARAM_MISC',   3);
  216. /**#@-*/
  217.  
  218.  
  219. // }}}
  220. // {{{ binary data-related
  221.  
  222.  
  223. /**#@+
  224.  * The different ways of returning binary data from queries.
  225.  */
  226.  
  227. /**
  228.  * Sends the fetched data straight through to output
  229.  */
  230. define('DB_BINMODE_PASSTHRU'1);
  231.  
  232. /**
  233.  * Lets you return data as usual
  234.  */
  235. define('DB_BINMODE_RETURN'2);
  236.  
  237. /**
  238.  * Converts the data to hex format before returning it
  239.  *
  240.  * For example the string "123" would become "313233".
  241.  */
  242. define('DB_BINMODE_CONVERT'3);
  243. /**#@-*/
  244.  
  245.  
  246. // }}}
  247. // {{{ fetch modes
  248.  
  249.  
  250. /**#@+
  251.  * Fetch Modes.
  252.  * @see DB_common::setFetchMode()
  253.  */
  254.  
  255. /**
  256.  * Indicates the current default fetch mode should be used
  257.  * @see DB_common::$fetchmode
  258.  */
  259. define('DB_FETCHMODE_DEFAULT'0);
  260.  
  261. /**
  262.  * Column data indexed by numbers, ordered from 0 and up
  263.  */
  264. define('DB_FETCHMODE_ORDERED'1);
  265.  
  266. /**
  267.  * Column data indexed by column names
  268.  */
  269. define('DB_FETCHMODE_ASSOC'2);
  270.  
  271. /**
  272.  * Column data as object properties
  273.  */
  274. define('DB_FETCHMODE_OBJECT'3);
  275.  
  276. /**
  277.  * For multi-dimensional results, make the column name the first level
  278.  * of the array and put the row number in the second level of the array
  279.  *
  280.  * This is flipped from the normal behavior, which puts the row numbers
  281.  * in the first level of the array and the column names in the second level.
  282.  */
  283. define('DB_FETCHMODE_FLIPPED'4);
  284. /**#@-*/
  285.  
  286. /**#@+
  287.  * Old fetch modes.  Left here for compatibility.
  288.  */
  289. define('DB_GETMODE_ORDERED'DB_FETCHMODE_ORDERED);
  290. define('DB_GETMODE_ASSOC',   DB_FETCHMODE_ASSOC);
  291. define('DB_GETMODE_FLIPPED'DB_FETCHMODE_FLIPPED);
  292. /**#@-*/
  293.  
  294.  
  295. // }}}
  296. // {{{ tableInfo() && autoPrepare()-related
  297.  
  298.  
  299. /**#@+
  300.  * The type of information to return from the tableInfo() method.
  301.  *
  302.  * Bitwised constants, so they can be combined using <kbd>|</kbd>
  303.  * and removed using <kbd>^</kbd>.
  304.  *
  305.  * @see DB_common::tableInfo()
  306.  *
  307.  * {@internal Since the TABLEINFO constants are bitwised, if more of them are
  308.  * added in the future, make sure to adjust DB_TABLEINFO_FULL accordingly.}}
  309.  */
  310. define('DB_TABLEINFO_ORDER'1);
  311. define('DB_TABLEINFO_ORDERTABLE'2);
  312. define('DB_TABLEINFO_FULL'3);
  313. /**#@-*/
  314.  
  315.  
  316. /**#@+
  317.  * The type of query to create with the automatic query building methods.
  318.  * @see DB_common::autoPrepare(), DB_common::autoExecute()
  319.  */
  320. define('DB_AUTOQUERY_INSERT'1);
  321. define('DB_AUTOQUERY_UPDATE'2);
  322. /**#@-*/
  323.  
  324.  
  325. // }}}
  326. // {{{ portability modes
  327.  
  328.  
  329. /**#@+
  330.  * Portability Modes.
  331.  *
  332.  * Bitwised constants, so they can be combined using <kbd>|</kbd>
  333.  * and removed using <kbd>^</kbd>.
  334.  *
  335.  * @see DB_common::setOption()
  336.  *
  337.  * {@internal Since the PORTABILITY constants are bitwised, if more of them are
  338.  * added in the future, make sure to adjust DB_PORTABILITY_ALL accordingly.}}
  339.  */
  340.  
  341. /**
  342.  * Turn off all portability features
  343.  */
  344. define('DB_PORTABILITY_NONE'0);
  345.  
  346. /**
  347.  * Convert names of tables and fields to lower case
  348.  * when using the get*(), fetch*() and tableInfo() methods
  349.  */
  350. define('DB_PORTABILITY_LOWERCASE'1);
  351.  
  352. /**
  353.  * Right trim the data output by get*() and fetch*()
  354.  */
  355. define('DB_PORTABILITY_RTRIM'2);
  356.  
  357. /**
  358.  * Force reporting the number of rows deleted
  359.  */
  360. define('DB_PORTABILITY_DELETE_COUNT'4);
  361.  
  362. /**
  363.  * Enable hack that makes numRows() work in Oracle
  364.  */
  365. define('DB_PORTABILITY_NUMROWS'8);
  366.  
  367. /**
  368.  * Makes certain error messages in certain drivers compatible
  369.  * with those from other DBMS's
  370.  *
  371.  * + mysql, mysqli:  change unique/primary key constraints
  372.  *   DB_ERROR_ALREADY_EXISTS -> DB_ERROR_CONSTRAINT
  373.  *
  374.  * + odbc(access):  MS's ODBC driver reports 'no such field' as code
  375.  *   07001, which means 'too few parameters.'  When this option is on
  376.  *   that code gets mapped to DB_ERROR_NOSUCHFIELD.
  377.  */
  378. define('DB_PORTABILITY_ERRORS'16);
  379.  
  380. /**
  381.  * Convert null values to empty strings in data output by
  382.  * get*() and fetch*()
  383.  */
  384. define('DB_PORTABILITY_NULL_TO_EMPTY'32);
  385.  
  386. /**
  387.  * Turn on all portability features
  388.  */
  389. define('DB_PORTABILITY_ALL'63);
  390. /**#@-*/
  391.  
  392. // }}}
  393.  
  394.  
  395. // }}}
  396. // {{{ class DB
  397.  
  398. /**
  399.  * Database independent query interface
  400.  *
  401.  * The main "DB" class is simply a container class with some static
  402.  * methods for creating DB objects as well as some utility functions
  403.  * common to all parts of DB.
  404.  *
  405.  * The object model of DB is as follows (indentation means inheritance):
  406.  * <pre>
  407.  * DB           The main DB class.  This is simply a utility class
  408.  *              with some "static" methods for creating DB objects as
  409.  *              well as common utility functions for other DB classes.
  410.  *
  411.  * DB_common    The base for each DB implementation.  Provides default
  412.  * |            implementations (in OO lingo virtual methods) for
  413.  * |            the actual DB implementations as well as a bunch of
  414.  * |            query utility functions.
  415.  * |
  416.  * +-DB_mysql   The DB implementation for MySQL.  Inherits DB_common.
  417.  *              When calling DB::factory or DB::connect for MySQL
  418.  *              connections, the object returned is an instance of this
  419.  *              class.
  420.  * </pre>
  421.  *
  422.  * @category   Database
  423.  * @package    DB
  424.  * @author     Stig Bakken <ssb@php.net>
  425.  * @author     Tomas V.V.Cox <cox@idecnet.com>
  426.  * @author     Daniel Convissor <danielc@php.net>
  427.  * @copyright  1997-2007 The PHP Group
  428.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  429.  * @version    Release: 1.7.14RC1
  430.  * @link       http://pear.php.net/package/DB
  431.  */
  432. class DB
  433. {
  434.     // {{{ &factory()
  435.  
  436.     /**
  437.      * Create a new DB object for the specified database type but don't
  438.      * connect to the database
  439.      *
  440.      * @param string $type     the database type (eg "mysql")
  441.      * @param array  $options  an associative array of option names and values
  442.      *
  443.      * @return object  new DB object.  A DB_Error object on failure.
  444.      *
  445.      * @see DB_common::setOption()
  446.      */
  447.     function &factory($type$options = false)
  448.     {
  449.         if (!is_array($options)) {
  450.             $options = array('persistent' => $options);
  451.         }
  452.  
  453.         if (isset($options['debug']&& $options['debug'>= 2{
  454.             // expose php errors with sufficient debug level
  455.             include_once "DB/{$type}.php";
  456.         else {
  457.             @include_once "DB/{$type}.php";
  458.         }
  459.  
  460.         $classname = "DB_${type}";
  461.  
  462.         if (!class_exists($classname)) {
  463.             $tmp = PEAR::raiseError(nullDB_ERROR_NOT_FOUNDnullnull,
  464.                                     "Unable to include the DB/{$type}.php"
  465.                                     . " file for '$dsn'",
  466.                                     'DB_Error'true);
  467.             return $tmp;
  468.         }
  469.  
  470.         @$obj = new $classname;
  471.  
  472.         foreach ($options as $option => $value{
  473.             $test $obj->setOption($option$value);
  474.             if (DB::isError($test)) {
  475.                 return $test;
  476.             }
  477.         }
  478.  
  479.         return $obj;
  480.     }
  481.  
  482.     // }}}
  483.     // {{{ &connect()
  484.  
  485.     /**
  486.      * Create a new DB object including a connection to the specified database
  487.      *
  488.      * Example 1.
  489.      * <code>
  490.      * require_once 'DB.php';
  491.      *
  492.      * $dsn = 'pgsql://user:password@host/database';
  493.      * $options = array(
  494.      *     'debug'       => 2,
  495.      *     'portability' => DB_PORTABILITY_ALL,
  496.      * );
  497.      *
  498.      * $db =& DB::connect($dsn, $options);
  499.      * if (PEAR::isError($db)) {
  500.      *     die($db->getMessage());
  501.      * }
  502.      * </code>
  503.      *
  504.      * @param mixed $dsn      the string "data source name" or array in the
  505.      *                          format returned by DB::parseDSN()
  506.      * @param array $options  an associative array of option names and values
  507.      *
  508.      * @return object  new DB object.  A DB_Error object on failure.
  509.      *
  510.      * @uses DB_dbase::connect(), DB_fbsql::connect(), DB_ibase::connect(),
  511.      *        DB_ifx::connect(), DB_msql::connect(), DB_mssql::connect(),
  512.      *        DB_mysql::connect(), DB_mysqli::connect(), DB_oci8::connect(),
  513.      *        DB_odbc::connect(), DB_pgsql::connect(), DB_sqlite::connect(),
  514.      *        DB_sybase::connect()
  515.      *
  516.      * @uses DB::parseDSN(), DB_common::setOption(), PEAR::isError()
  517.      */
  518.     function &connect($dsn$options = array())
  519.     {
  520.         $dsninfo DB::parseDSN($dsn);
  521.         $type $dsninfo['phptype'];
  522.  
  523.         if (!is_array($options)) {
  524.             /*
  525.              * For backwards compatibility.  $options used to be boolean,
  526.              * indicating whether the connection should be persistent.
  527.              */
  528.             $options = array('persistent' => $options);
  529.         }
  530.  
  531.         if (isset($options['debug']&& $options['debug'>= 2{
  532.             // expose php errors with sufficient debug level
  533.             include_once "DB/${type}.php";
  534.         else {
  535.             @include_once "DB/${type}.php";
  536.         }
  537.  
  538.         $classname = "DB_${type}";
  539.         if (!class_exists($classname)) {
  540.             $tmp = PEAR::raiseError(nullDB_ERROR_NOT_FOUNDnullnull,
  541.                                     "Unable to include the DB/{$type}.php"
  542.                                     . " file for '$dsn'",
  543.                                     'DB_Error'true);
  544.             return $tmp;
  545.         }
  546.  
  547.         @$obj = new $classname;
  548.  
  549.         foreach ($options as $option => $value{
  550.             $test $obj->setOption($option$value);
  551.             if (DB::isError($test)) {
  552.                 return $test;
  553.             }
  554.         }
  555.  
  556.         $err $obj->connect($dsninfo$obj->getOption('persistent'));
  557.         if (DB::isError($err)) {
  558.             if (is_array($dsn)) {
  559.                 $err->addUserInfo(DB::getDSNString($dsntrue));
  560.             else {
  561.                 $err->addUserInfo($dsn);
  562.             }
  563.             return $err;
  564.         }
  565.  
  566.         return $obj;
  567.     }
  568.  
  569.     // }}}
  570.     // {{{ apiVersion()
  571.  
  572.     /**
  573.      * Return the DB API version
  574.      *
  575.      * @return string  the DB API version number
  576.      */
  577.     function apiVersion()
  578.     {
  579.         return '1.7.14RC1';
  580.     }
  581.  
  582.     // }}}
  583.     // {{{ isError()
  584.  
  585.     /**
  586.      * Determines if a variable is a DB_Error object
  587.      *
  588.      * @param mixed $value  the variable to check
  589.      *
  590.      * @return bool  whether $value is DB_Error object
  591.      */
  592.     function isError($value)
  593.     {
  594.         return is_a($value'DB_Error');
  595.     }
  596.  
  597.     // }}}
  598.     // {{{ isConnection()
  599.  
  600.     /**
  601.      * Determines if a value is a DB_<driver> object
  602.      *
  603.      * @param mixed $value  the value to test
  604.      *
  605.      * @return bool  whether $value is a DB_<driver> object
  606.      */
  607.     function isConnection($value)
  608.     {
  609.         return (is_object($value&&
  610.                 is_subclass_of($value'db_common'&&
  611.                 method_exists($value'simpleQuery'));
  612.     }
  613.  
  614.     // }}}
  615.     // {{{ isManip()
  616.  
  617.     /**
  618.      * Tell whether a query is a data manipulation or data definition query
  619.      *
  620.      * Examples of data manipulation queries are INSERT, UPDATE and DELETE.
  621.      * Examples of data definition queries are CREATE, DROP, ALTER, GRANT,
  622.      * REVOKE.
  623.      *
  624.      * @param string $query  the query
  625.      *
  626.      * @return boolean  whether $query is a data manipulation query
  627.      */
  628.     function isManip($query)
  629.     {
  630.         $manips 'INSERT|UPDATE|DELETE|REPLACE|'
  631.                 . 'CREATE|DROP|'
  632.                 . 'LOAD DATA|SELECT .* INTO .* FROM|COPY|'
  633.                 . 'ALTER|GRANT|REVOKE|'
  634.                 . 'LOCK|UNLOCK';
  635.         if (preg_match('/^\s*"?(' $manips ')\s+/i'$query)) {
  636.             return true;
  637.         }
  638.         return false;
  639.     }
  640.  
  641.     // }}}
  642.     // {{{ errorMessage()
  643.  
  644.     /**
  645.      * Return a textual error message for a DB error code
  646.      *
  647.      * @param integer $value  the DB error code
  648.      *
  649.      * @return string  the error message or false if the error code was
  650.      *                   not recognized
  651.      */
  652.     function errorMessage($value)
  653.     {
  654.         static $errorMessages;
  655.         if (!isset($errorMessages)) {
  656.             $errorMessages = array(
  657.                 DB_ERROR                    => 'unknown error',
  658.                 DB_ERROR_ACCESS_VIOLATION   => 'insufficient permissions',
  659.                 DB_ERROR_ALREADY_EXISTS     => 'already exists',
  660.                 DB_ERROR_CANNOT_CREATE      => 'can not create',
  661.                 DB_ERROR_CANNOT_DROP        => 'can not drop',
  662.                 DB_ERROR_CONNECT_FAILED     => 'connect failed',
  663.                 DB_ERROR_CONSTRAINT         => 'constraint violation',
  664.                 DB_ERROR_CONSTRAINT_NOT_NULL=> 'null value violates not-null constraint',
  665.                 DB_ERROR_DIVZERO            => 'division by zero',
  666.                 DB_ERROR_EXTENSION_NOT_FOUND=> 'extension not found',
  667.                 DB_ERROR_INVALID            => 'invalid',
  668.                 DB_ERROR_INVALID_DATE       => 'invalid date or time',
  669.                 DB_ERROR_INVALID_DSN        => 'invalid DSN',
  670.                 DB_ERROR_INVALID_NUMBER     => 'invalid number',
  671.                 DB_ERROR_MISMATCH           => 'mismatch',
  672.                 DB_ERROR_NEED_MORE_DATA     => 'insufficient data supplied',
  673.                 DB_ERROR_NODBSELECTED       => 'no database selected',
  674.                 DB_ERROR_NOSUCHDB           => 'no such database',
  675.                 DB_ERROR_NOSUCHFIELD        => 'no such field',
  676.                 DB_ERROR_NOSUCHTABLE        => 'no such table',
  677.                 DB_ERROR_NOT_CAPABLE        => 'DB backend not capable',
  678.                 DB_ERROR_NOT_FOUND          => 'not found',
  679.                 DB_ERROR_NOT_LOCKED         => 'not locked',
  680.                 DB_ERROR_SYNTAX             => 'syntax error',
  681.                 DB_ERROR_UNSUPPORTED        => 'not supported',
  682.                 DB_ERROR_TRUNCATED          => 'truncated',
  683.                 DB_ERROR_VALUE_COUNT_ON_ROW => 'value count on row',
  684.                 DB_OK                       => 'no error',
  685.             );
  686.         }
  687.  
  688.         if (DB::isError($value)) {
  689.             $value $value->getCode();
  690.         }
  691.  
  692.         return isset($errorMessages[$value]$errorMessages[$value]
  693.                      : $errorMessages[DB_ERROR];
  694.     }
  695.  
  696.     // }}}
  697.     // {{{ parseDSN()
  698.  
  699.     /**
  700.      * Parse a data source name
  701.      *
  702.      * Additional keys can be added by appending a URI query string to the
  703.      * end of the DSN.
  704.      *
  705.      * The format of the supplied DSN is in its fullest form:
  706.      * <code>
  707.      *  phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true
  708.      * </code>
  709.      *
  710.      * Most variations are allowed:
  711.      * <code>
  712.      *  phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644
  713.      *  phptype://username:password@hostspec/database_name
  714.      *  phptype://username:password@hostspec
  715.      *  phptype://username@hostspec
  716.      *  phptype://hostspec/database
  717.      *  phptype://hostspec
  718.      *  phptype(dbsyntax)
  719.      *  phptype
  720.      * </code>
  721.      *
  722.      * @param string $dsn Data Source Name to be parsed
  723.      *
  724.      * @return array an associative array with the following keys:
  725.      *   + phptype:  Database backend used in PHP (mysql, odbc etc.)
  726.      *   + dbsyntax: Database used with regards to SQL syntax etc.
  727.      *   + protocol: Communication protocol to use (tcp, unix etc.)
  728.      *   + hostspec: Host specification (hostname[:port])
  729.      *   + database: Database to use on the DBMS server
  730.      *   + username: User name for login
  731.      *   + password: Password for login
  732.      */
  733.     function parseDSN($dsn)
  734.     {
  735.         $parsed = array(
  736.             'phptype'  => false,
  737.             'dbsyntax' => false,
  738.             'username' => false,
  739.             'password' => false,
  740.             'protocol' => false,
  741.             'hostspec' => false,
  742.             'port'     => false,
  743.             'socket'   => false,
  744.             'database' => false,
  745.         );
  746.  
  747.         if (is_array($dsn)) {
  748.             $dsn array_merge($parsed$dsn);
  749.             if (!$dsn['dbsyntax']{
  750.                 $dsn['dbsyntax'$dsn['phptype'];
  751.             }
  752.             return $dsn;
  753.         }
  754.  
  755.         // Find phptype and dbsyntax
  756.         if (($pos strpos($dsn'://')) !== false{
  757.             $str substr($dsn0$pos);
  758.             $dsn substr($dsn$pos + 3);
  759.         else {
  760.             $str $dsn;
  761.             $dsn = null;
  762.         }
  763.  
  764.         // Get phptype and dbsyntax
  765.         // $str => phptype(dbsyntax)
  766.         if (preg_match('|^(.+?)\((.*?)\)$|'$str$arr)) {
  767.             $parsed['phptype']  $arr[1];
  768.             $parsed['dbsyntax'!$arr[2$arr[1$arr[2];
  769.         else {
  770.             $parsed['phptype']  $str;
  771.