| [ Index ] |
|
Code source de CakePHP 1.1.13.4450 |
1 <?php 2 /* SVN FILE: $Id: dbo_pear.php 4409 2007-02-02 13:20:59Z phpnut $ */ 3 4 /** 5 * {@link http://pear.php.net/package/DB PEAR::DB} layer for DBO. 6 * 7 * Long description for file 8 * 9 * PHP versions 4 and 5 10 * 11 * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> 12 * Copyright 2005-2007, Cake Software Foundation, Inc. 13 * 1785 E. Sahara Avenue, Suite 490-204 14 * Las Vegas, Nevada 89104 15 * 16 * Licensed under The MIT License 17 * Redistributions of files must retain the above copyright notice. 18 * 19 * @filesource 20 * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. 21 * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project 22 * @package cake 23 * @subpackage cake.cake.libs.model.dbo 24 * @since CakePHP(tm) v 0.2.9 25 * @version $Revision: 4409 $ 26 * @modifiedby $LastChangedBy: phpnut $ 27 * @lastmodified $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $ 28 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 29 */ 30 31 /** 32 * Create an include path required PEAR libraries. 33 */ 34 uses ('model' . DS . 'datasources' . DS . 'dbo_source'); 35 36 ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . PEAR); 37 vendor ('Pear/DB'); 38 39 /** 40 * {@link http://pear.php.net/package/DB PEAR::DB} layer for DBO. 41 * 42 * Long description for class 43 * 44 * @package cake 45 * @subpackage cake.cake.libs.model.dbo 46 */ 47 class DboPear extends DboSource{ 48 49 /** 50 * PEAR::DB object with which we connect. 51 * 52 * @var DB The connection object. 53 * @access private 54 */ 55 var $_pear = null; 56 57 /** 58 * Connects to the database using options in the given configuration array. 59 * 60 * @param array $config Configuration array for connecting 61 * @return boolean True if the database could be connected, else false 62 */ 63 function connect($config) { 64 $this->config =$config; 65 $dsn =$config['driver'] . '://' . $config['login'] . ':' . $config['password'] . '@' 66 . $config['host'] . '/' . $config['database']; 67 $options=array('debug' => Configure::read() - 1, 68 'portability' => DB_PORTABILITY_ALL,); 69 70 $this->_pear =&DB::connect($dsn, $options); 71 $this->connected=$this->_pear ? true : false; 72 return !(PEAR::isError($this->_pear)); 73 } 74 75 /** 76 * Disconnects from database. 77 * 78 * @return boolean True if the database could be disconnected, else false 79 */ 80 function disconnect() { 81 die ('Please implement DBO::disconnect() first.'); 82 } 83 84 /** 85 * Executes given SQL statement. 86 * 87 * @param string $sql SQL statement 88 * @return resource Result resource identifier 89 */ 90 function execute($sql) { 91 return $this->_pear->query($sql); 92 } 93 94 /** 95 * Returns a row from given resultset as an array . 96 * 97 * @return array The fetched row as an array 98 */ 99 function fetchRow() { 100 return $this->_result->fetchRow(DB_FETCHMODE_ASSOC); 101 } 102 103 /** 104 * Returns an array of tables in the database. If there are no tables, an error is raised and the application exits. 105 * :WARNING: :TODO: POSTGRESQL & MYSQL ONLY! PEAR::DB doesn't support universal table listing. 106 * 107 * @return array Array of tablenames in the database 108 */ 109 function tablesList() { 110 $driver=$this->config['driver']; 111 $tables=array(); 112 113 if ('postgres' == $driver) { 114 $sql ="SELECT a.relname AS name 115 FROM pg_class a, pg_user b 116 WHERE ( relkind = 'r') and relname !~ '^pg_' AND relname !~ '^sql_' 117 AND relname !~ '^xin[vx][0-9]+' AND b.usesysid = a.relowner 118 AND NOT (EXISTS (SELECT viewname FROM pg_views WHERE viewname=a.relname));"; 119 120 $result=$this->all($sql); 121 122 foreach($result as $item) { 123 $tables[] = $item['name']; 124 } 125 } elseif('mysql' == $driver) { 126 $result=array(); 127 $result=mysql_list_tables($this->config['database']); 128 129 while($item = mysql_fetch_array($result)) { 130 $tables[] = $item[0]; 131 } 132 } else { 133 die ('Please implement DBO_Pear::tablesList() for your database driver.'); 134 } 135 136 if (!$result) { 137 trigger_error(ERROR_NO_TABLE_LIST, E_USER_ERROR); 138 exit; 139 } else { 140 return $tables; 141 } 142 } 143 144 /** 145 * Returns an array of the fields in given table name. 146 * 147 * @param string $tableName Name of database table to inspect 148 * @return array Fields in table. Keys are name and type 149 */ 150 function fields($tableName) { 151 $data =$this->_pear->tableInfo($tableName); 152 $fields=false; 153 154 foreach($data as $item) { 155 $fields[] = array('name' => $item['name'], 156 'type' => $item['type']); 157 } 158 159 return $fields; 160 } 161 162 /** 163 * Returns a quoted and escaped string of $data for use in an SQL statement. 164 * 165 * @param string $data String to be prepared for use in an SQL statement 166 * @return string Quoted and escaped 167 */ 168 function prepareValue($data) { 169 return $this->_pear->quoteSmart($data); 170 } 171 172 /** 173 * Returns a formatted error message from previous database operation. 174 * 175 * @return string Error message 176 */ 177 function lastError() { 178 return PEAR::isError($this->_result) ? $this->_result->getMessage() : null; 179 } 180 181 /** 182 * Returns number of affected rows in previous database operation. If no previous operation exists, this returns false. 183 * 184 * @return int Number of affected rows 185 */ 186 function lastAffected() { 187 return $this->_pear->affectedRows(); 188 } 189 190 /** 191 * Returns number of rows in previous resultset. If no previous resultset exists, 192 * this returns false. 193 * 194 * @return int Number of rows in resultset 195 */ 196 function lastNumRows() { 197 if (method_exists($this->_result, 'numRows')) { 198 return $this->_result->numRows(); 199 } else { 200 return false; 201 } 202 } 203 204 /** 205 * Returns the ID generated from the previous INSERT operation. 206 * 207 * @param string $table Name of the database table 208 * @return int 209 */ 210 function lastInsertId($table) { 211 return $this->field('id', "SELECT MAX(id) FROM {$table}"); 212 } 213 214 /** 215 * Returns a limit statement in the correct format for the particular database. 216 * 217 * @param int $limit Limit of results returned 218 * @param int $offset Offset from which to start results 219 * @return string SQL limit/offset statement 220 */ 221 function selectLimit($limit, $offset = '0') { 222 return ' ' . $this->_pear->modifyLimitQuery('', $offset, $limit); 223 } 224 } 225 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 19:27:47 2007 | par Balluche grâce à PHPXref 0.7 |