dbo_pear.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: dbo__pear_8php-source.html 675 2008-12-26 00:27:14Z gwoo $ */
00003 
00004 /**
00005  * {@link http://pear.php.net/package/DB PEAR::DB} layer for DBO.
00006  *
00007  * Long description for file
00008  *
00009  * PHP versions 4 and 5
00010  *
00011  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00012  * Copyright 2005-2008, Cake Software Foundation, Inc.
00013  *                              1785 E. Sahara Avenue, Suite 490-204
00014  *                              Las Vegas, Nevada 89104
00015  *
00016  * Licensed under The MIT License
00017  * Redistributions of files must retain the above copyright notice.
00018  *
00019  * @filesource
00020  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00021  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00022  * @package         cake
00023  * @subpackage      cake.cake.libs.model.dbo
00024  * @since           CakePHP(tm) v 0.2.9
00025  * @version         $Revision: 675 $
00026  * @modifiedby      $LastChangedBy: gwoo $
00027  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 2008) $
00028  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00029  */
00030 
00031 /**
00032  * Create an include path required PEAR libraries.
00033  */
00034 ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . PEAR);
00035 vendor ('Pear/DB');
00036 
00037 /**
00038  * {@link http://pear.php.net/package/DB PEAR::DB} layer for DBO.
00039  *
00040  * Long description for class
00041  *
00042  * @package     cake
00043  * @subpackage  cake.cake.libs.model.dbo
00044  */
00045 class DboPear extends DboSource{
00046 
00047 /**
00048  * PEAR::DB object with which we connect.
00049  *
00050  * @var DB The connection object.
00051  * @access private
00052  */
00053      var $_pear = null;
00054 
00055 /**
00056  * Connects to the database using options in the given configuration array.
00057  *
00058  * @param array $config Configuration array for connecting
00059  * @return boolean True if the database could be connected, else false
00060  */
00061      function connect($config) {
00062           $this->config   =$config;
00063           $dsn            =$config['driver'] . '://' . $config['login'] . ':' . $config['password'] . '@'
00064               . $config['host'] . '/' . $config['database'];
00065           $options=array('debug' => Configure::read() - 1,
00066                       'portability' => DB_PORTABILITY_ALL,);
00067 
00068           $this->_pear    =&DB::connect($dsn, $options);
00069           $this->connected=$this->_pear ? true : false;
00070           return !(PEAR::isError($this->_pear));
00071      }
00072 
00073 /**
00074  * Disconnects from database.
00075  *
00076  * @return boolean True if the database could be disconnected, else false
00077  */
00078      function disconnect() {
00079           die (__('Please implement DBO::disconnect() first.'));
00080      }
00081 
00082 /**
00083  * Executes given SQL statement.
00084  *
00085  * @param string $sql SQL statement
00086  * @return resource Result resource identifier
00087  */
00088      function execute($sql) {
00089           return $this->_pear->query($sql);
00090      }
00091 
00092 /**
00093  * Returns a row from given resultset as an array .
00094  *
00095  * @return array The fetched row as an array
00096  */
00097     function fetchRow($sql = null) {
00098         if (!empty($sql) && is_string($sql) && strlen($sql) > 5) {
00099             if (!$this->execute($sql)) {
00100                 return null;
00101             }
00102         }
00103         return $this->_result->fetchRow(DB_FETCHMODE_ASSOC);
00104     }
00105 
00106 /**
00107  * Returns an array of tables in the database. If there are no tables, an error is raised and the application exits.
00108  * :WARNING: :TODO: POSTGRESQL & MYSQL ONLY! PEAR::DB doesn't support universal table listing.
00109  *
00110  * @return array Array of tablenames in the database
00111  */
00112      function tablesList() {
00113           $driver=$this->config['driver'];
00114           $tables=array();
00115 
00116           if ('postgres' == $driver) {
00117                 $sql   ="SELECT a.relname AS name
00118                         FROM pg_class a, pg_user b
00119                         WHERE ( relkind = 'r') and relname !~ '^pg_' AND relname !~ '^sql_'
00120                         AND relname !~ '^xin[vx][0-9]+' AND b.usesysid = a.relowner
00121                         AND NOT (EXISTS (SELECT viewname FROM pg_views WHERE viewname=a.relname));";
00122 
00123                 $result=$this->all($sql);
00124 
00125                 foreach ($result as $item) {
00126                      $tables[] = $item['name'];
00127                 }
00128           } elseif ('mysql' == $driver) {
00129                 $result=array();
00130                 $result=mysql_list_tables($this->config['database']);
00131 
00132                 while ($item = mysql_fetch_array($result)) {
00133                      $tables[] = $item[0];
00134                 }
00135           } else {
00136                 die (__('Please implement DBO_Pear::tablesList() for your database driver.'));
00137           }
00138 
00139           if (!$result) {
00140                 trigger_error(ERROR_NO_TABLE_LIST, E_USER_ERROR);
00141                 exit;
00142           } else {
00143                 return $tables;
00144           }
00145      }
00146 
00147 /**
00148  * Returns an array of the fields in given table name.
00149  *
00150  * @param string $tableName Name of database table to inspect
00151  * @return array Fields in table. Keys are name and type
00152  */
00153      function fields($tableName) {
00154           $data  =$this->_pear->tableInfo($tableName);
00155           $fields=false;
00156 
00157           foreach ($data as $item) {
00158                 $fields[] = array('name' => $item['name'],
00159                             'type' => $item['type']);
00160           }
00161 
00162           return $fields;
00163      }
00164 
00165 /**
00166  * Returns a quoted and escaped string of $data for use in an SQL statement.
00167  *
00168  * @param string $data String to be prepared for use in an SQL statement
00169  * @return string Quoted and escaped
00170  */
00171      function prepareValue($data) {
00172           return $this->_pear->quoteSmart($data);
00173      }
00174 
00175 /**
00176  * Returns a formatted error message from previous database operation.
00177  *
00178  * @return string Error message
00179  */
00180      function lastError() {
00181           return PEAR::isError($this->_result) ? $this->_result->getMessage() : null;
00182      }
00183 
00184 /**
00185  * Returns number of affected rows in previous database operation. If no previous operation exists, this returns false.
00186  *
00187  * @return int Number of affected rows
00188  */
00189      function lastAffected() {
00190           return $this->_pear->affectedRows();
00191      }
00192 
00193 /**
00194  * Returns number of rows in previous resultset. If no previous resultset exists,
00195  * this returns false.
00196  *
00197  * @return int Number of rows in resultset
00198  */
00199      function lastNumRows() {
00200           if (method_exists($this->_result, 'numRows')) {
00201                 return $this->_result->numRows();
00202           } else {
00203                 return false;
00204           }
00205      }
00206 
00207 /**
00208  * Returns the ID generated from the previous INSERT operation.
00209  *
00210  * @param string $table Name of the database table
00211  * @return int
00212  */
00213      function lastInsertId($table) {
00214           return $this->field('id', "SELECT MAX(id) FROM {$table}");
00215      }
00216 
00217 /**
00218  * Returns a limit statement in the correct format for the particular database.
00219  *
00220  * @param int $limit Limit of results returned
00221  * @param int $offset Offset from which to start results
00222  * @return string SQL limit/offset statement
00223  */
00224      function selectLimit($limit, $offset = '0') {
00225           return ' ' . $this->_pear->modifyLimitQuery('', $offset, $limit);
00226      }
00227 /**
00228  * Inserts multiple values into a join table
00229  *
00230  * @param string $table
00231  * @param string $fields
00232  * @param array $values
00233  */
00234     function insertMulti($table, $fields, $values) {
00235         $count = count($values);
00236         for ($x = 0; $x < $count; $x++) {
00237             $this->query("INSERT INTO {$table} ({$fields}) VALUES {$values[$x]}");
00238         }
00239     }
00240 }
00241 ?>