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-2005 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.86 2007/01/22 01:17:48 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.  * Identifiers for the placeholders used in prepared statements.
  191.  * @see DB_common::prepare()
  192.  */
  193.  
  194. /**
  195.  * Indicates a scalar (<kbd>?</kbd>) placeholder was used
  196.  *
  197.  * Quote and escape the value as necessary.
  198.  */
  199. define('DB_PARAM_SCALAR'1);
  200.  
  201. /**
  202.  * Indicates an opaque (<kbd>&</kbd>) placeholder was used
  203.  *
  204.  * The value presented is a file name.  Extract the contents of that file
  205.  * and place them in this column.
  206.  */
  207. define('DB_PARAM_OPAQUE'2);
  208.  
  209. /**
  210.  * Indicates a misc (<kbd>!</kbd>) placeholder was used
  211.  *
  212.  * The value should not be quoted or escaped.
  213.  */
  214. define('DB_PARAM_MISC',   3);
  215. /**#@-*/
  216.  
  217.  
  218. // }}}
  219. // {{{ binary data-related
  220.  
  221.  
  222.  * The different ways of returning binary data from queries.
  223.  */
  224.  
  225. /**
  226.  * Sends the fetched data straight through to output
  227.  */
  228. define('DB_BINMODE_PASSTHRU'1);
  229.  
  230. /**
  231.  * Lets you return data as usual
  232.  */
  233. define('DB_BINMODE_RETURN'2);
  234.  
  235. /**
  236.  * Converts the data to hex format before returning it
  237.  *
  238.  * For example the string "123" would become "313233".
  239.  */
  240. define('DB_BINMODE_CONVERT'3);
  241. /**#@-*/
  242.  
  243.  
  244. // }}}
  245. // {{{ fetch modes
  246.  
  247.  
  248.  * Fetch Modes.
  249.  * @see DB_common::setFetchMode()
  250.  */
  251.  
  252. /**
  253.  * Indicates the current default fetch mode should be used
  254.  * @see DB_common::$fetchmode
  255.  */
  256. define('DB_FETCHMODE_DEFAULT'0);
  257.  
  258. /**
  259.  * Column data indexed by numbers, ordered from 0 and up
  260.  */
  261. define('DB_FETCHMODE_ORDERED'1);
  262.  
  263. /**
  264.  * Column data indexed by column names
  265.  */
  266. define('DB_FETCHMODE_ASSOC'2);
  267.  
  268. /**
  269.  * Column data as object properties
  270.  */
  271. define('DB_FETCHMODE_OBJECT'3);
  272.  
  273. /**
  274.  * For multi-dimensional results, make the column name the first level
  275.  * of the array and put the row number in the second level of the array
  276.  *
  277.  * This is flipped from the normal behavior, which puts the row numbers
  278.  * in the first level of the array and the column names in the second level.
  279.  */
  280. define('DB_FETCHMODE_FLIPPED'4);
  281. /**#@-*/
  282.  
  283.  * Old fetch modes.  Left here for compatibility.
  284.  */
  285. define('DB_GETMODE_ORDERED'DB_FETCHMODE_ORDERED);
  286. define('DB_GETMODE_ASSOC',   DB_FETCHMODE_ASSOC);
  287. define('DB_GETMODE_FLIPPED'DB_FETCHMODE_FLIPPED);
  288. /**#@-*/
  289.  
  290.  
  291. // }}}
  292. // {{{ tableInfo() && autoPrepare()-related
  293.  
  294.  
  295.  * The type of information to return from the tableInfo() method.
  296.  *
  297.  * Bitwised constants, so they can be combined using <kbd>|</kbd>
  298.  * and removed using <kbd>^</kbd>.
  299.  *
  300.  * @see DB_common::tableInfo()
  301.  *
  302.  *  {@internal Since the TABLEINFO constants are bitwised, if more of them are
  303.  *  added in the future, make sure to adjust DB_TABLEINFO_FULL accordingly.}}
  304.  */
  305. define('DB_TABLEINFO_ORDER'1);
  306. define('DB_TABLEINFO_ORDERTABLE'2);
  307. define('DB_TABLEINFO_FULL'3);
  308. /**#@-*/
  309.  
  310.  
  311.  * The type of query to create with the automatic query building methods.
  312.  * @see DB_common::autoPrepare(), DB_common::autoExecute()
  313.  */
  314. define('DB_AUTOQUERY_INSERT'1);
  315. define('DB_AUTOQUERY_UPDATE'2);
  316. /**#@-*/
  317.  
  318.  
  319. // }}}
  320. // {{{ portability modes
  321.  
  322.  
  323.  * Portability Modes.
  324.  *
  325.  * Bitwised constants, so they can be combined using <kbd>|</kbd>
  326.  * and removed using <kbd>^</kbd>.
  327.  *
  328.  * @see DB_common::setOption()
  329.  *
  330.  *  {@internal Since the PORTABILITY constants are bitwised, if more of them are
  331.  *  added in the future, make sure to adjust DB_PORTABILITY_ALL accordingly.}}
  332.  */
  333.  
  334. /**
  335.  * Turn off all portability features
  336.  */
  337. define('DB_PORTABILITY_NONE'0);
  338.  
  339. /**
  340.  * Convert names of tables and fields to lower case
  341.  * when using the get*(), fetch*() and tableInfo() methods
  342.  */
  343. define('DB_PORTABILITY_LOWERCASE'1);
  344.  
  345. /**
  346.  * Right trim the data output by get*() and fetch*()
  347.  */
  348. define('DB_PORTABILITY_RTRIM'2);
  349.  
  350. /**
  351.  * Force reporting the number of rows deleted
  352.  */
  353. define('DB_PORTABILITY_DELETE_COUNT'4);
  354.  
  355. /**
  356.  * Enable hack that makes numRows() work in Oracle
  357.  */
  358. define('DB_PORTABILITY_NUMROWS'8);
  359.  
  360. /**
  361.  * Makes certain error messages in certain drivers compatible
  362.  * with those from other DBMS's
  363.  *
  364.  * + mysql, mysqli:  change unique/primary key constraints
  365.  *   DB_ERROR_ALREADY_EXISTS -> DB_ERROR_CONSTRAINT
  366.  *
  367.  * + odbc(access):  MS's ODBC driver reports 'no such field' as code
  368.  *   07001, which means 'too few parameters.'  When this option is on
  369.  *   that code gets mapped to DB_ERROR_NOSUCHFIELD.
  370.  */
  371. define('DB_PORTABILITY_ERRORS'16);
  372.  
  373. /**
  374.  * Convert null values to empty strings in data output by
  375.  * get*() and fetch*()
  376.  */
  377. define('DB_PORTABILITY_NULL_TO_EMPTY'32);
  378.  
  379. /**
  380.  * Turn on all portability features
  381.  */
  382. define('DB_PORTABILITY_ALL'63);
  383. /**#@-*/
  384.  
  385. // }}}
  386.  
  387.  
  388. // }}}
  389. // {{{ class DB
  390.  
  391.  * Database independent query interface
  392.  *
  393.  * The main "DB" class is simply a container class with some static
  394.  * methods for creating DB objects as well as some utility functions
  395.  * common to all parts of DB.
  396.  *
  397.  * The object model of DB is as follows (indentation means inheritance):
  398.  * <pre>
  399.  * DB           The main DB class.  This is simply a utility class
  400.  *              with some "static" methods for creating DB objects as
  401.  *              well as common utility functions for other DB classes.
  402.  *
  403.  * DB_common    The base for each DB implementation.  Provides default
  404.  * |            implementations (in OO lingo virtual methods) for
  405.  * |            the actual DB implementations as well as a bunch of
  406.  * |            query utility functions.
  407.  * |
  408.  * +-DB_mysql   The DB implementation for MySQL.  Inherits DB_common.
  409.  *              When calling DB::factory or DB::connect for MySQL
  410.  *              connections, the object returned is an instance of this
  411.  *              class.
  412.  * </pre>
  413.  *
  414.  * @category   Database
  415.  * @package    DB
  416.  * @author     Stig Bakken <ssb@php.net>
  417.  * @author     Tomas V.V.Cox <cox@idecnet.com>
  418.  * @author     Daniel Convissor <danielc@php.net>
  419.  * @copyright  1997-2005 The PHP Group
  420.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  421.  * @version    Release: 1.7.10
  422.  * @link       http://pear.php.net/package/DB
  423.  */
  424. class DB
  425. {
  426.     // {{{ &factory()
  427.  
  428.     
  429.     /**
  430.      * Create a new DB object for the specified database type but don't
  431.      * connect to the database
  432.      *
  433.      * @param string $type     the database type (eg "mysql")
  434.      * @param array  $options  an associative array of option names and values
  435.      *
  436.      * @return object  new DB object.  A DB_Error object on failure.
  437.      *
  438.      * @see DB_common::setOption()
  439.      */
  440.     function &factory($type$options = false)
  441.     {
  442.         if (!is_array($options)) {
  443.             $options = array('persistent' => $options);
  444.         }
  445.  
  446.         if (isset($options['debug']&& $options['debug'>= 2{
  447.             // expose php errors with sufficient debug level
  448.             include_once "DB/{$type}.php";
  449.         else {
  450.             @include_once "DB/{$type}.php";
  451.         }
  452.  
  453.         $classname = "DB_${type}";
  454.  
  455.         if (!class_exists($classname)) {
  456.             $tmp = PEAR::raiseError(nullDB_ERROR_NOT_FOUNDnullnull,
  457.                                     "Unable to include the DB/{$type}.php"
  458.                                     . " file for '$dsn'",
  459.                                     'DB_Error'true);
  460.             return $tmp;
  461.         }
  462.  
  463.         @$obj =new $classname;
  464.  
  465.         foreach ($options as $option => $value{
  466.             $test $obj->setOption($option$value);
  467.             if (DB::isError($test)) {
  468.                 return $test;
  469.             }
  470.         }
  471.  
  472.         return $obj;
  473.     }
  474.  
  475.     // }}}
  476.     // {{{ &connect()
  477.  
  478.     
  479.     /**
  480.      * Create a new DB object including a connection to the specified database
  481.      *
  482.      * Example 1.
  483.      * <code>
  484.      * require_once 'DB.php';
  485.      *
  486.      * $dsn = 'pgsql://user:password@host/database';
  487.      * $options = array(
  488.      *     'debug'       => 2,
  489.      *     'portability' => DB_PORTABILITY_ALL,
  490.      * );
  491.      *
  492.      * $db =& DB::connect($dsn, $options);
  493.      * if (PEAR::isError($db)) {
  494.      *     die($db->getMessage());
  495.      * }
  496.      * </code>
  497.      *
  498.      * @param mixed $dsn      the string "data source name" or array in the
  499.      *                          format returned by DB::parseDSN()
  500.      * @param array $options  an associative array of option names and values
  501.      *
  502.      * @return object  new DB object.  A DB_Error object on failure.
  503.      *
  504.      * @uses DB_dbase::connect(), DB_fbsql::connect(), DB_ibase::connect(),
  505.      *        DB_ifx::connect(), DB_msql::connect(), DB_mssql::connect(),
  506.      *        DB_mysql::connect(), DB_mysqli::connect(), DB_oci8::connect(),
  507.      *        DB_odbc::connect(), DB_pgsql::connect(), DB_sqlite::connect(),
  508.      *        DB_sybase::connect()
  509.      *
  510.      * @uses DB::parseDSN(), DB_common::setOption(), PEAR::isError()
  511.      */
  512.     function &connect($dsn$options = array())
  513.     {
  514.         $dsninfo DB::parseDSN($dsn);
  515.         $type $dsninfo['phptype'];
  516.  
  517.         if (!is_array($options)) {
  518.             /*
  519.              * For backwards compatibility.  $options used to be boolean,
  520.              * indicating whether the connection should be persistent.
  521.              */
  522.             $options = array('persistent' => $options);
  523.         }
  524.  
  525.         if (isset($options['debug']&& $options['debug'>= 2{
  526.             // expose php errors with sufficient debug level
  527.             include_once "DB/${type}.php";
  528.         else {
  529.             @include_once "DB/${type}.php";
  530.         }
  531.  
  532.         $classname = "DB_${type}";
  533.         if (!class_exists($classname)) {
  534.             $tmp = PEAR::raiseError(nullDB_ERROR_NOT_FOUNDnullnull,
  535.                                     "Unable to include the DB/{$type}.php"
  536.                                     . " file for '$dsn'",
  537.                                     'DB_Error'true);
  538.             return $tmp;
  539.         }
  540.  
  541.         @$obj =new $classname;
  542.  
  543.         foreach ($options as $option => $value{
  544.             $test $obj->setOption($option$value);
  545.             if (DB::isError($test)) {
  546.                 return $test;
  547.             }
  548.         }
  549.  
  550.         $err $obj->connect($dsninfo$obj->getOption('persistent'));
  551.         if (DB::isError($err)) {
  552.             if (is_array($dsn)) {
  553.                 $err->addUserInfo(DB::getDSNString($dsntrue));
  554.             else {
  555.                 $err->addUserInfo($dsn);
  556.             }
  557.             return $err;
  558.         }
  559.  
  560.         return $obj;
  561.     }
  562.  
  563.     // }}}
  564.     // {{{ apiVersion()
  565.  
  566.     
  567.     /**
  568.      * Return the DB API version
  569.      *
  570.      * @return string  the DB API version number
  571.      */
  572.     function apiVersion()
  573.     {
  574.         return '1.7.10';
  575.     }
  576.  
  577.     // }}}
  578.     // {{{ isError()
  579.  
  580.     
  581.     /**
  582.      * Determines if a variable is a DB_Error object
  583.      *
  584.      * @param mixed $value  the variable to check
  585.      *
  586.      * @return bool  whether $value is DB_Error object
  587.      */
  588.     function isError($value)
  589.     {
  590.         return is_a($value'DB_Error');
  591.     }
  592.  
  593.     // }}}
  594.     // {{{ isConnection()
  595.  
  596.     
  597.     /**
  598.      * Determines if a value is a DB_<driver> object
  599.      *
  600.      * @param mixed $value  the value to test
  601.      *
  602.      * @return bool  whether $value is a DB_<driver> object
  603.      */
  604.     function isConnection($value)
  605.     {
  606.         return (is_object($value&&
  607.                 is_subclass_of($value'db_common'&&
  608.                 method_exists($value'simpleQuery'));
  609.     }
  610.  
  611.     // }}}
  612.     // {{{ isManip()
  613.  
  614.     
  615.     /**
  616.      * Tell whether a query is a data manipulation or data definition query
  617.      *
  618.      * Examples of data manipulation queries are INSERT, UPDATE and DELETE.
  619.      * Examples of data definition queries are CREATE, DROP, ALTER, GRANT,
  620.      * REVOKE.
  621.      *
  622.      * @param string $query  the query
  623.      *
  624.      * @return boolean  whether $query is a data manipulation query
  625.      */
  626.     function isManip($query)
  627.     {
  628.         $manips 'INSERT|UPDATE|DELETE|REPLACE|'
  629.                 . 'CREATE|DROP|'
  630.                 . 'LOAD DATA|SELECT .* INTO .* FROM|COPY|'
  631.                 . 'ALTER|GRANT|REVOKE|'
  632.                 . 'LOCK|UNLOCK';
  633.         if (preg_match('/^\s*"?(' $manips ')\s+/i'$query)) {
  634.             return true;
  635.         }
  636.         return false;
  637.     }
  638.  
  639.     // }}}
  640.     // {{{ errorMessage()
  641.  
  642.     
  643.     /**
  644.      * Return a textual error message for a DB error code
  645.      *
  646.      * @param integer $value  the DB error code
  647.      *
  648.      * @return string  the error message or false if the error code was
  649.      *                   not recognized
  650.      */
  651.     function errorMessage($value)
  652.     {
  653.         static $errorMessages;
  654.         if (!isset($errorMessages)) {
  655.             $errorMessages = array(
  656.                 DB_ERROR                    => 'unknown error',
  657.                 DB_ERROR_ACCESS_VIOLATION   => 'insufficient permissions',
  658.                 DB_ERROR_ALREADY_EXISTS     => 'already exists',
  659.                 DB_ERROR_CANNOT_CREATE      => 'can not create',
  660.                 DB_ERROR_CANNOT_DROP        => 'can not drop',
  661.                 DB_ERROR_CONNECT_FAILED     => 'connect failed',
  662.                 DB_ERROR_CONSTRAINT         => 'constraint violation',
  663.                 DB_ERROR_CONSTRAINT_NOT_NULL=> 'null value violates not-null constraint',
  664.                 DB_ERROR_DIVZERO            => 'division by zero',
  665.                 DB_ERROR_EXTENSION_NOT_FOUND=> 'extension not found',
  666.                 DB_ERROR_INVALID            => 'invalid',
  667.                 DB_ERROR_INVALID_DATE       => 'invalid date or time',
  668.                 DB_ERROR_INVALID_DSN        => 'invalid DSN',
  669.                 DB_ERROR_INVALID_NUMBER     => 'invalid number',
  670.                 DB_ERROR_MISMATCH           => 'mismatch',
  671.                 DB_ERROR_NEED_MORE_DATA     => 'insufficient data supplied',
  672.                 DB_ERROR_NODBSELECTED       => 'no database selected',
  673.                 DB_ERROR_NOSUCHDB           => 'no such database',
  674.                 DB_ERROR_NOSUCHFIELD        => 'no such field',
  675.                 DB_ERROR_NOSUCHTABLE        => 'no such table',
  676.                 DB_ERROR_NOT_CAPABLE        => 'DB backend not capable',
  677.                 DB_ERROR_NOT_FOUND          => 'not found',
  678.                 DB_ERROR_NOT_LOCKED         => 'not locked',
  679.                 DB_ERROR_SYNTAX             => 'syntax error',
  680.                 DB_ERROR_UNSUPPORTED        => 'not supported',
  681.                 DB_ERROR_TRUNCATED          => 'truncated',
  682.                 DB_ERROR_VALUE_COUNT_ON_ROW => 'value count on row',
  683.                 DB_OK                       => 'no error',
  684.             );
  685.         }
  686.  
  687.         if (DB::isError($value)) {
  688.             $value $value->getCode();
  689.         }
  690.  
  691.         return isset($errorMessages[$value]$errorMessages[$value]
  692.                      : $errorMessages[DB_ERROR];
  693.     }
  694.  
  695.     // }}}
  696.     // {{{ parseDSN()
  697.  
  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 (