[Openfirst-cvscommit] base/includes ODBCDataBase.php,NONE,1.1 MSSQLDataBase.php,1.1,1.2 MySQLDataBas
Brought to you by:
xtimg
From: Jamie <ast...@us...> - 2005-10-15 19:21:11
|
Update of /cvsroot/openfirst/base/includes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30346/includes Modified Files: MSSQLDataBase.php MySQLDataBase.php dbase.php globals.php Added Files: ODBCDataBase.php Log Message: True-blue OOP for DataBase - Completed move to seperate implementations - Some more functions added (eg, DataBase::factory()) - Of course default behavior/compatibility issues. Index: MSSQLDataBase.php =================================================================== RCS file: /cvsroot/openfirst/base/includes/MSSQLDataBase.php,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MSSQLDataBase.php 7 Oct 2005 18:15:34 -0000 1.1 --- MSSQLDataBase.php 15 Oct 2005 19:21:01 -0000 1.2 *************** *** 29,33 **** /*private*/ var $type, $connection, $lastquery, $db; // Wrapper for database selection. ! function MSSQLDataBase($type = dbMSSQL, $server = false, $username = false, $password = false, $newlink = false, $intclientflags = false) { $this->type = dbMSSQL; global $sqlTablePrefix; --- 29,33 ---- /*private*/ var $type, $connection, $lastquery, $db; // Wrapper for database selection. ! function MSSQLDataBase($type = dbMSSQL, $server = false, $username = false, $password = false, $newlink = false, $flags = false) { $this->type = dbMSSQL; global $sqlTablePrefix; *************** *** 37,42 **** } ! function selectDB($databasename) { $this->db = $databasename; $this->checkForFunction('mssql_select_db'); return mssql_select_db($databasename, $this->connection); --- 37,54 ---- } ! /*static*/ function &factory($server = false, $username = false, $password = false, $newlink = false, $flags = false) { ! return new MSSQLDataBase(dbMSSQL, $server, $username, $password); ! } ! ! /*public*/ function getTypeName() { ! return 'Microsoft SQL'; ! } ! ! function selectDB($databasename, $prefix=false) { $this->db = $databasename; + if ($prefix !== false) { + $this->prefix = $prefix; + } + $this->checkForFunction('mssql_select_db'); return mssql_select_db($databasename, $this->connection); *************** *** 108,112 **** function freeResult($resource) { $this->checkForFunction('mssql_free_result'); ! if (!is_resource($resource)) return false; return mssql_free_result($resource); } --- 120,124 ---- function freeResult($resource) { $this->checkForFunction('mssql_free_result'); ! if (!is_resource($resource)) return true; return mssql_free_result($resource); } --- NEW FILE: ODBCDataBase.php --- <?php /* * openFIRST.base - includes/ODBCDataBase.php * * Copyright (C) 2003, * openFIRST Project * Original Author: Jamie Bliss <ja...@op...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ // Purpose: Provide implementation of DataBase for ODBC require_once('dbase.php'); class ODBCDataBase extends DataBase { /*private*/ var $type, $connection, $lastquery, $db, $prefix; // Wrapper for database selection. function ODBCDataBase($type = dbMYSQL, $server = '', $username = '', $password = '', $newlink = false, $flags = null) { $this->type = $type; global $sqlTablePrefix; $this->prefix = $sqlTablePrefix; $this->checkForFunction('odbc_connect'); if($newlink !== false) { $this->connection = odbc_connect($server, $username, $password, $newlink); } else { $this->connection = odbc_connect($server, $username, $password); } } /*static*/ function &factory($server = false, $username = false, $password = false, $newlink = false, $flags = false) { return new MySQLDataBase(dbODBC, $server, $username, $password, $newlink); } /*public*/ function getTypeName() { return 'ODBC'; } function selectDB($databasename, $prefix=false) { $this->db = $databasename; if ($prefix !== false) { $this->prefix = $prefix; } //ODBC does not require slecting a DB } function errorNumber() { $this->checkForFunction('odbc_error'); return odbc_error($this->connection); } function errorString() { $this->checkForFunction('odbc_errormsg'); return odbc_errormsg($this->connection); } function query($string, $batchsize = false) { global $ogLastQuery; $ogLastQuery = $this->lastquery = "\$string = \"$string\", \$linkidentifier = \"{$this->connection}\", \$batchsize = \"$batchsize\""; $this->checkForFunction('odbc_exec'); return odbc_exec($this->connection, $string); } function fetchObject($resource, $rownumber = false) { $this->checkForFunction('odbc_fetch_object'); if($rownumber !== false) { return odbc_fetch_object($resource, $rownumber); } else { return odbc_fetch_object($resource); } } function numberOfRows($resource) { $this->checkForFunction('odbc_num_rows'); if (!is_resource($resource)) return -1; return odbc_num_rows($resource); } function freeResult($resource) { $this->checkForFunction('odbc_free_result'); if (!is_resource($resource)) return true; return odbc_free_result($resource); } # Check if there the connection is valid function check() { #TODO: Write me! } #Does ODBC support backticks???? /* function quoteName($name, $delimiter = ',') { if (is_array($name)) { $value = ''; foreach ($name as $text) { $value .= $this->quoteName($text).$delimiter; } $value = substr($value, 0, -strlen($delimiter)); return $value; } else { return '`'.$this->escape($name).'`'; } }*/ # Does ODBC support REPLACE???? /* function replace( $table, $unique, $values ) { $sql = "REPLACE INTO ".$this->quoteTable($table)." SET ".$this->quoteFDPairs($values); return $this->query( $sql ); }*/ } ?> Index: dbase.php =================================================================== RCS file: /cvsroot/openfirst/base/includes/dbase.php,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** dbase.php 7 Oct 2005 18:15:34 -0000 1.8 --- dbase.php 15 Oct 2005 19:21:01 -0000 1.9 *************** *** 29,90 **** define('dbMSSQL', 'MSSQL'); define('dbODBC', 'ODBC'); $ogLastQuery = ''; - if (!isset($DBaseType)) { $DBaseType = dbMYSQL; } class DataBase { /*private*/ var $type, $connection, $lastquery, $db, $prefix; // Wrapper for database selection. ! function DataBase($type = dbMYSQL, $server = '', $username = '', $password = '', $newlink = '', $intclientflags = '') { $this->type = $type; global $sqlTablePrefix; $this->prefix = $sqlTablePrefix; - switch ($this->type) { - case dbMYSQL: - $this->checkForFunction('mysql_connect'); - if($intclientflags != '') { - $this->connection = mysql_connect($server, $username, $password, $newlink, $intclientflags); - } elseif($newlink != '') { - $this->connection = mysql_connect($server, $username, $password, $newlink); - } else { - $this->connection = mysql_connect($server, $username, $password); - } - break; - - case dbMSSQL: - $this->checkForFunction('mssql_connect'); - $this->connection = mssql_connect($server, $username, $password); - break; - - case dbODBC: - $this->checkForFunction('odbc_connect'); - if($newlink != '') { - $this->connection = odbc_connect($server, $username, $password, $newlink); - } else { - $this->connection = odbc_connect($server, $username, $password); - } - break; - - default: - $this->badDBType(); - } } /*public*/ function getTypeName() { ! $typetext = $this->type; ! switch ($this->type) { ! case dbMYSQL: ! $typetext = 'MySQL'; ! break; ! ! case dbMSSQL: ! $typetext = 'Microsoft SQL'; ! break; ! ! case dbODBC: ! $typetext = 'ODBC'; ! break; ! } ! return $typetext; } --- 29,67 ---- define('dbMSSQL', 'MSSQL'); define('dbODBC', 'ODBC'); + define('dbUNKNOWN', '?'); $ogLastQuery = ''; + function &ofCreateDataBase($type, $server, $password, $newlink = false, $flags = false) { + $class = "{$type}DataBase"; + if (!class_exists($class)) { + // This assumption is only for DataBase implementations + require_once("{$class}.php"); + } + //$obj =& $class::factory($server, $password, $newlink, $flags); + $obj =& call_user_func(array($class, 'factory'), $server, $password, $newlink, $flags); + return $obj; + } + + /** Base class for actual implementation. + * Performs default actions and provides the interface definition. + */ class DataBase { /*private*/ var $type, $connection, $lastquery, $db, $prefix; // Wrapper for database selection. ! function DataBase($type = dbUNKNOWN, $server = '', $username = '', $password = '', $newlink = '', $flags = '') { $this->type = $type; global $sqlTablePrefix; $this->prefix = $sqlTablePrefix; } + /*static*/ function &factory($server = false, $username = false, $password = false, $newlink = false, $flags = false) { + # Override + } + + /** Returns the display name for this type of database. + */ /*public*/ function getTypeName() { ! return $this->type; } *************** *** 101,166 **** } ! function selectDB($databasename) { $this->db = $databasename; ! switch ($this->type) { ! case dbMYSQL: ! $this->checkForFunction('mysql_select_db'); ! return mysql_select_db($databasename, $this->connection); ! break; ! ! case dbMSSQL: ! $this->checkForFunction('mssql_select_db'); ! return mssql_select_db($databasename, $this->connection); ! break; ! ! case dbODBC: ! //ODBC does not require slecting a DB ! break; ! ! default: ! $this->badDBType(); } } function errorNumber() { ! switch ($this->type) { ! case dbMYSQL: ! $this->checkForFunction('mysql_errno'); ! return mysql_errno($this->connection); ! break; ! ! case dbMSSQL: ! // MSSQL doesn't have an equivelent function ! return 0; ! break; ! ! case dbODBC: ! $this->checkForFunction('odbc_error'); ! return odbc_error($this->connection); ! break; ! ! default: ! $this->badDBType(); ! } } function errorString() { ! switch ($this->type) { ! case dbMYSQL: ! $this->checkForFunction('mysql_error'); ! return mysql_error($this->connection); ! break; ! ! case dbMSSQL: ! return ''; ! break; ! ! case dbODBC: ! $this->checkForFunction('odbc_errormsg'); ! return odbc_errormsg($this->connection); ! ! default: ! $this->badDBType(); ! } } --- 78,95 ---- } ! function selectDB($databasename, $prefix=false) { $this->db = $databasename; ! if ($prefix !== false) { ! $this->prefix = $prefix; } + # Override } function errorNumber() { ! # Override } function errorString() { ! # Override } *************** *** 170,348 **** $ogLastQuery = $this->lastquery = "\$string = \"$string\", \$linkidentifier = \"{$this->connection}\", \$batchsize = \"$batchsize\""; ! switch ($this->type) { ! case dbMYSQL: ! $this->checkForFunction('mysql_query'); ! return mysql_query($string, $this->connection); ! break; ! ! case dbMSSQL: ! $this->checkForFunction('mssql_query'); ! if($batchsize !== false) { ! return mssql_query($string, $this->connection, $batchsize); ! } else { ! return mssql_query($string, $this->connection); ! } ! break; ! ! case dbODBC: ! $this->checkForFunction('odbc_exec'); ! // Note: this may be misleading, the variable names are not reflective of their content in this particular line, as the odbc function uses an order different from the other databases. ! return odbc_exec($string, $this->connection); ! ! default: ! $this->badDBType(); ! } } function fetchObject($resource, $rownumber = false) { ! switch ($this->type) { ! case dbMYSQL: ! $this->checkForFunction('mysql_fetch_object'); ! return mysql_fetch_object($resource); ! break; ! ! case dbMSSQL: ! $this->checkForFunction('mssql_fetch_object'); ! return mssql_fetch_object($resource); ! break; ! ! case dbODBC: ! $this->checkForFunction('odbc_fetch_object'); ! // Note that odbc_fetch_object() is not documented ! if($rownumber !== false) { ! return odbc_fetch_object($resource, $rownumber); ! } else { ! return odbc_fetch_object($resource); ! } ! break; ! ! default: ! $this->badDBType(); ! } } function numberOfRows($resource) { ! switch ($this->type) { ! case dbMYSQL: ! $this->checkForFunction('mysql_num_rows'); ! if (!is_resource($resource)) return -1; ! return mysql_num_rows($resource); ! break; ! ! case dbMSSQL: ! $this->checkForFunction('mssql_num_rows'); ! if (!is_resource($resource)) return -1; ! return mssql_num_rows($resource); ! break; ! ! case dbODBC: ! $this->checkForFunction('odbc_num_rows'); ! if($resource != "") { ! if (!is_resource($resource)) return -1; ! return(odbc_num_rows($resource)); ! } else { ! return(odbc_num_rows()); ! } ! break; ! ! default: ! $this->badDBType(); ! } } function freeResult($resource) { ! switch ($this->type) { ! case dbMYSQL: ! $this->checkForFunction('mysql_free_result'); ! if (!is_resource($resource)) return false; ! return mysql_free_result($resource); ! break; ! ! case dbMSSQL: ! $this->checkForFunction('mssql_free_result'); ! if (!is_resource($resource)) return false; ! return mssql_free_result($resource); ! ! case dbODBC: ! $this->checkForFunction('odbc_free_result'); ! if (!is_resource($resource)) return false; ! return odbc_free_result($resource); ! ! default: ! $this->badDBType(); ! } } function getVersion() { ! switch ($this->type) { ! case dbMYSQL: ! $res = $this->query('SELECT VERSION() AS mysql_version'); ! $v = $this->fetchObject($res); ! $this->freeResult($res); ! return $this->getTypeName().' '.$v->mysql_version; ! break; ! ! case dbMSSQL: ! $res = $this->query('SELECT @@VERSION'); ! $v = $this->fetchObject($res); ! $this->freeResult($res); ! ob_start(); ! print_r($v); ! $ver = ob_get_contents(); ! ob_end_clean(); ! return $this->getTypeName().' '.$ver.' <br /><strong>Warning:</strong> unconfirmed.'; ! break; ! ! case dbODBC: ! return $this->getTypeName(); ! break; ! ! default: ! return 'Unknown DB type: '.$this->getTypeName(); ! } } function getSize() { ! $sqlDatabase = $this->db; ! switch ($this->type) { ! case dbMYSQL: ! $dbsize = 0; ! $dq = $this->query('SHOW TABLE STATUS FROM '.$this->quoteDatabase($sqlDatabase)); ! while($d = $this->fetchObject($dq)) { ! $dbsize += $d->Data_length; ! $dbsize += $d->Index_length; ! $dbsize += $d->Data_free; //Overhead ! } ! $this->freeResult($dq); ! return ofFormatSize($dbsize); ! break; ! ! case dbMSSQL: ! $res = $this->query('SELECT ((SUM(size) * 8.0) * 1024.0) as dbsize FROM sysfiles'); ! $s = $this->fetchObject($res); ! $this->freeResult($res); ! return ofFormatSize($s->dbsize); ! ! default: ! return 'Size not supported'; ! } } # Check if there the connection is valid function check() { ! #TODO: Write me! ! switch ($this->type) { ! case dbMYSQL: ! break; ! ! case dbMSSQL: ! break; ! ! case dbODBC: ! break; ! ! default: ! $this->badDBType(); ! } } --- 99,128 ---- $ogLastQuery = $this->lastquery = "\$string = \"$string\", \$linkidentifier = \"{$this->connection}\", \$batchsize = \"$batchsize\""; ! # Override } function fetchObject($resource, $rownumber = false) { ! # Override } function numberOfRows($resource) { ! # Override } function freeResult($resource) { ! # Override } function getVersion() { ! return 'Unknown DB type: '.$this->getTypeName(); } function getSize() { ! return 'Size not supported'; } # Check if there the connection is valid function check() { ! # Override } *************** *** 371,383 **** function escape($text) { ! switch ($this->type) { ! case dbMYSQL: ! return mysql_real_escape_string($text); ! break; ! ! # MSSQL and ODBC don't have specific escaping functions. ! default: ! return addslashes($text); ! } } --- 151,156 ---- function escape($text) { ! # Override, if able ! return addslashes($text); } *************** *** 389,392 **** --- 162,166 ---- #Used for quoting field and DB names + # Should we use something other than backticks? function quoteName($name, $delimiter = ',') { if (is_array($name)) { *************** *** 507,510 **** --- 281,286 ---- * Copied from MediaWiki (but removed the fancy stuff). * + * Should this be changed to an alternate form by default? + * * @param string $table The table to perform it on. * @param array $unique An array of unique values that distinquish a row. *************** *** 516,518 **** } } ! ?> --- 292,294 ---- } } ! ?> \ No newline at end of file Index: globals.php =================================================================== RCS file: /cvsroot/openfirst/base/includes/globals.php,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** globals.php 24 Aug 2005 02:57:44 -0000 1.14 --- globals.php 15 Oct 2005 19:21:01 -0000 1.15 *************** *** 65,70 **** require_once('compatibility.php'); - if (!defined('OPENFIRST_NO_INSTALLATION')) - require_once('dbase.php'); set_include_path( get_include_path().PATH_SEPARATOR."$configdir/".PATH_SEPARATOR."."); --- 65,68 ---- *************** *** 72,77 **** require_once('functions.php'); ! if (!defined('OPENFIRST_NO_INSTALLATION')) require_once('auth.php'); if (!defined('OPENFIRST_INSTALLATION_SCRIPT')) { require_once('Module.php'); --- 70,77 ---- require_once('functions.php'); ! if (!defined('OPENFIRST_NO_INSTALLATION')) { ! require_once('dbase.php'); require_once('auth.php'); + } if (!defined('OPENFIRST_INSTALLATION_SCRIPT')) { require_once('Module.php'); *************** *** 83,86 **** --- 83,88 ---- $sqlTablePrefix = 'ofirst_'; + $DBaseType = dbMYSQL; + $sqlServer = $sqlUser = $sqlPassword = false; if (!defined('OPENFIRST_INSTALLATION_SCRIPT')) *************** *** 88,93 **** if (!defined('OPENFIRST_NO_INSTALLATION')) { ! $ogDB = new DataBase($DBaseType, $sqlServer, $sqlUser, $sqlPassword); ! $ogDB->selectDB($sqlDatabase); } --- 90,95 ---- if (!defined('OPENFIRST_NO_INSTALLATION')) { ! $ogDB = ofCreateDataBase($DBaseType, $sqlServer, $sqlUser, $sqlPassword); ! $ogDB->selectDB($sqlDatabase, $sqlTablePrefix); } Index: MySQLDataBase.php =================================================================== RCS file: /cvsroot/openfirst/base/includes/MySQLDataBase.php,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** MySQLDataBase.php 7 Oct 2005 18:15:34 -0000 1.1 --- MySQLDataBase.php 15 Oct 2005 19:21:01 -0000 1.2 *************** *** 29,39 **** /*private*/ var $type, $connection, $lastquery, $db, $prefix; // Wrapper for database selection. ! function MySQLDataBase($type = dbMYSQL, $server = false, $username = false, $password = false, $newlink = false, $intclientflags = false) { $this->type = dbMYSQL; global $sqlTablePrefix; $this->prefix = $sqlTablePrefix; $this->checkForFunction('mysql_connect'); ! if($intclientflags !== false) { ! $this->connection = mysql_connect($server, $username, $password, $newlink, $intclientflags); } elseif($newlink !== false) { $this->connection = mysql_connect($server, $username, $password, $newlink); --- 29,39 ---- /*private*/ var $type, $connection, $lastquery, $db, $prefix; // Wrapper for database selection. ! function MySQLDataBase($type = dbMYSQL, $server = false, $username = false, $password = false, $newlink = false, $flags = false) { $this->type = dbMYSQL; global $sqlTablePrefix; $this->prefix = $sqlTablePrefix; $this->checkForFunction('mysql_connect'); ! if($flags !== false) { ! $this->connection = mysql_connect($server, $username, $password, $newlink, $flags); } elseif($newlink !== false) { $this->connection = mysql_connect($server, $username, $password, $newlink); *************** *** 43,52 **** } ! /*static*/ function &factory($server = false, $username = false, $password = false, $newlink = false, $intclientflags = false) { ! return new MySQLDataBase(dbMYSQL, $server, $username, $password, $intclientflags); } ! function selectDB($databasename) { $this->db = $databasename; $this->checkForFunction('mysql_select_db'); return mysql_select_db($databasename, $this->connection); --- 43,60 ---- } ! /*static*/ function &factory($server = false, $username = false, $password = false, $newlink = false, $flags = false) { ! return new MySQLDataBase(dbMYSQL, $server, $username, $password, $newlink, $flags); } ! /*public*/ function getTypeName() { ! return 'MySQL'; ! } ! ! function selectDB($databasename, $prefix=false) { $this->db = $databasename; + if ($prefix !== false) { + $this->prefix = $prefix; + } + $this->checkForFunction('mysql_select_db'); return mysql_select_db($databasename, $this->connection); *************** *** 85,89 **** function freeResult($resource) { $this->checkForFunction('mysql_free_result'); ! if (!is_resource($resource)) return false; return mysql_free_result($resource); } --- 93,97 ---- function freeResult($resource) { $this->checkForFunction('mysql_free_result'); ! if (!is_resource($resource)) return true; return mysql_free_result($resource); } |