cvs: /php4/pear .cvsignore /php4/pear/DB common.php mysql.php /php4/pear DB.php /php4/pear/DB odbc.php

From: Date: Mon, 29 Nov 1999 15:14:30 +0000
Subject: cvs: /php4/pear .cvsignore /php4/pear/DB common.php mysql.php /php4/pear DB.php /php4/pear/DB odbc.php
Groups: php.version4 
Request: Send a blank email to php-version4+get-7045@lists.php.net to get a copy of this message
ssb Mon Nov 29 10:14:30 1999 EDT Added files: /php4/pear .cvsignore /php4/pear/DB common.php mysql.php Modified files: /php4/pear DB.php /php4/pear/DB odbc.php Log: - DB classes now inherit DB_common - Added DB_ERROR_NODBSELECTED - Added DB::connect($dsn[, $persistent] which creates a DB object and connects $dsn is a "data source name" in a format specific to DB: phptype(dbsyntax)://username:password@protocol+hostspec/database Don't worry, such a dsn can be as short as "mysql" - Added mysql support Index: php4/pear/DB.php diff -u php4/pear/DB.php:1.1 php4/pear/DB.php:1.2 --- php4/pear/DB.php:1.1 Mon Nov 22 06:00:53 1999 +++ php4/pear/DB.php Mon Nov 29 10:13:55 1999 @@ -36,6 +36,7 @@ define("DB_ERROR_INVALID_NUMBER", -11); define("DB_ERROR_INVALID_DATE", -12); define("DB_ERROR_DIVZERO", -13); +define("DB_ERROR_NODBSELECTED", -14); // }}} // {{{ Prepare/execute parameter types @@ -84,6 +85,27 @@ return $obj; } + function connect($dsn, $persistent = false) { + global $USED_PACKAGES; + + $dsninfo = DB::parseDSN($dsn); + $type = $dsninfo['phptype']; + // "include" should be replaced with "use" once PHP gets it + $pkgname = 'DB/' . $type; + if (!is_array($USED_PACKAGES) || !$USED_PACKAGES[$pkgname]) { + print "${pkgname}.php\n"; + if (!@include($pkgname . '.php')) { + return DB_ERROR_NOT_FOUND; + } else { + $USED_PACKAGES[$pkgname] = true; + } + } + $classname = 'DB_' . $type; + $obj = new $classname; + $obj->connect(&$dsninfo, $persistent); + return $obj; // XXX ADDREF + } + /** * Return the DB API version. * @return int the DB API version number @@ -121,11 +143,97 @@ DB_ERROR_NOT_CAPABLE => "DB implementation not capable", DB_ERROR_INVALID_NUMBER => "invalid number", DB_ERROR_INVALID_DATE => "invalid date or time", - DB_ERROR_DIVZERO => "division by zero" + DB_ERROR_DIVZERO => "division by zero", + DB_ERROR_NODBSELECTED => "no database selected" ); } return $errorMessages[$code]; } + + /** + * Parse a data source name and return an associative array with + * the following keys: + * phptype Database backend used in PHP (mysql, odbc etc.) + * dbsyntax Database used with regards to SQL syntax etc. + * protocol Communication protocol to use (tcp, unix etc.) + * hostspec Host specification (hostname[:port]) + * database Database to use on the DBMS server + * username User name for login + * password Password for login + * + * The format of the supplied DSN is in its fullest form: + * phptype(dbsyntax)://username:password@protocol+hostspec/database + * Most variations are allowed: + * phptype://username:password@protocol+hostspec/database + * phptype://username:password@hostspec/database + * phptype://username:password@hostspec + * phptype://hostspec/database + * phptype://hostspec + * phptype(dbsyntax) + * phptype + * + * FALSE is returned on error. + * + * @param $dsn Data Source Name to be parsed + */ + function parseDSN($dsn) { + $parsed = array( + 'phptype' => false, + 'dbsyntax' => false, + 'protocol' => false, + 'hostspec' => false, + 'database' => false, + 'username' => false, + 'password' => false + ); + if (ereg('^([^:]+)://', $dsn, &$arr)) { + $dbtype = $arr[1]; + $dsn = ereg_replace('^[^:]+://', '', $dsn); + // match "phptype(dbsyntax)" + if (ereg('^([^\(]+)\((.+)\)$', $dbtype, &$arr)) { + $parsed['phptype'] = $arr[1]; + $parsed['dbsyntax'] = $arr[2]; + } else { + $parsed['phptype'] = $dbtype; + } + } else { + // match "phptype(dbsyntax)" + if (ereg('^([^\(]+)\((.+)\)$', $dsn, &$arr)) { + $parsed['phptype'] = $arr[1]; + $parsed['dbsyntax'] = $arr[2]; + } else { + $parsed['phptype'] = $dsn; + } + return $parsed; // XXX ADDREF + } + + if (ereg('^(.*)/([^/]+)$', $dsn, &$arr)) { + $parsed['database'] = $arr[2]; + $dsn = $arr[1]; + } + + if (ereg('^([^:]+):([^@]+)@?(.*)$', $dsn, &$arr)) { + $parsed['username'] = $arr[1]; + $parsed['password'] = $arr[2]; + $dsn = $arr[3]; + } elseif (ereg('^([^:]+)@(.*)$', $dsn, &$arr)) { + $parsed['username'] = $arr[1]; + $dsn = $arr[3]; + } + + if (ereg('^([^\+]+)\+(.*)$', $dsn, &$arr)) { + $parsed['protocol'] = $arr[1]; + $dsn = $arr[2]; + } + + $parsed['hostspec'] = $dsn; + + if (!$parsed['dbsyntax']) { + $parsed['dbsyntax'] = $parsed['phptype']; + } + + return $parsed; // XXX ADDREF + } } // }}} @@ -145,8 +253,8 @@ * @param $dbh DB object reference * @param $result result resource id */ - function DB_result($dbh, $result) { - $this->dbh = $dbh; + function DB_result(&$dbh, $result) { + $this->dbh = &$dbh; $this->result = $result; } @@ -163,7 +271,7 @@ * @param $arr reference to data array * @return int error code */ - function fetchInto(const $arr) { + function fetchInto(&$arr) { return $this->dbh->fetchInto($this->result, &$arr); } Index: php4/pear/DB/odbc.php diff -u php4/pear/DB/odbc.php:1.1 php4/pear/DB/odbc.php:1.2 --- php4/pear/DB/odbc.php:1.1 Mon Nov 22 06:00:54 1999 +++ php4/pear/DB/odbc.php Mon Nov 29 10:13:57 1999 @@ -31,12 +31,17 @@ // a reference. // -class DB_odbc { +if ($GLOBALS['USED_PACKAGES']['DB/odbc']) return; +$GLOBALS['USED_PACKAGES']['DB/odbc'] = true; +if (!$GLOBALS['USED_PACKAGES']['DB/common']) { + include("DB/common.php"); +} + +class DB_odbc extends DB_common { // {{{ properties var $connection; var $type; - var $features; // }}} @@ -110,13 +115,6 @@ function quoteString($string) { return str_replace("'", "''", $string); - } - - // }}} - // {{{ capableOf() - - function capableOf($feature) { - return $this->features[$feature]; } // }}}

« previous php.version4 (#7045) next »