[ Index ] |
|
Code source de CakePHP 1.1.13.4450 |
1 <?php 2 /* SVN FILE: $Id: dbo_sqlite.php 4409 2007-02-02 13:20:59Z phpnut $ */ 3 4 /** 5 * SQLite 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.9.0 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 * Include DBO. 33 */ 34 uses('model'.DS.'datasources'.DS.'dbo_source'); 35 36 /** 37 * DBO implementation for the SQLite DBMS. 38 * 39 * Long description for class 40 * 41 * @package cake 42 * @subpackage cake.cake.libs.model.dbo 43 */ 44 class DboSqlite extends DboSource 45 { 46 /** 47 * Enter description here... 48 * 49 * @var unknown_type 50 */ 51 var $description = "SQLite DBO Driver"; 52 53 /** 54 * Enter description here... 55 * 56 * @var unknown_type 57 */ 58 var $startQuote = '"'; 59 60 /** 61 * Enter description here... 62 * 63 * @var unknown_type 64 */ 65 var $endQuote = '"'; 66 67 /** 68 * Base configuration settings for SQLite driver 69 * 70 * @var array 71 */ 72 var $_baseConfig = array('persistent' => true, 73 'database' => 'cake', 74 'connect' => 'sqlite_popen'); 75 76 /** 77 * SQLite column definition 78 * 79 * @var array 80 */ 81 var $columns = array('primary_key' => array('name' => 'integer primary key'), 82 'string' => array('name' => 'varchar', 'limit' => '255'), 83 'text' => array('name' => 'text'), 84 'integer' => array('name' => 'integer', 'limit' => '11', 'formatter' => 'intval'), 85 'float' => array('name' => 'float', 'formatter' => 'floatval'), 86 'datetime' => array('name' => 'timestamp', 'format' => 'YmdHis', 'formatter' => 'date'), 87 'timestamp' => array('name' => 'timestamp', 'format' => 'YmdHis', 'formatter' => 'date'), 88 'time' => array('name' => 'timestamp', 'format' => 'His', 'formatter' => 'date'), 89 'date' => array('name' => 'date', 'format' => 'Ymd', 'formatter' => 'date'), 90 'binary' => array('name' => 'blob'), 91 'boolean' => array('name' => 'integer', 'limit' => '1')); 92 93 /** 94 * Connects to the database using config['file'] as a filename. 95 * 96 * @param array $config Configuration array for connecting 97 * @return mixed 98 */ 99 function connect() 100 { 101 $config = $this->config; 102 $this->connection = $config['connect']($config['database']); 103 $this->connected = is_resource($this->connection); 104 105 return $this->connected; 106 } 107 108 /** 109 * Disconnects from database. 110 * 111 * @return boolean True if the database could be disconnected, else false 112 */ 113 function disconnect() 114 { 115 $this->connected = !@sqlite_close($this->connection); 116 return !$this->connected; 117 } 118 119 /** 120 * Executes given SQL statement. 121 * 122 * @param string $sql SQL statement 123 * @return resource Result resource identifier 124 */ 125 function _execute($sql) { 126 return sqlite_query($this->connection, $sql); 127 } 128 /** 129 * Returns an array of tables in the database. If there are no tables, an error is raised and the application exits. 130 * 131 * @return array Array of tablenames in the database 132 */ 133 function listSources() { 134 $db = $this->config['database']; 135 $this->config['database'] = basename($this->config['database']); 136 137 $cache = parent::listSources(); 138 if ($cache != null) { 139 return $cache; 140 } 141 142 $result = $this->fetchAll("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"); 143 144 if (!$result || empty($result)) { 145 return array(); 146 } else { 147 $tables = array(); 148 foreach ($result as $table) { 149 $tables[] = $table[0]['name']; 150 } 151 parent::listSources($tables); 152 153 $this->config['database'] = $db; 154 return $tables; 155 } 156 $this->config['database'] = $db; 157 return array(); 158 } 159 160 /** 161 * Returns an array of the fields in given table name. 162 * 163 * @param string $tableName Name of database table to inspect 164 * @return array Fields in table. Keys are name and type 165 */ 166 function describe(&$model) { 167 $cache = parent::describe($model); 168 if ($cache != null) { 169 return $cache; 170 } 171 $fields = false; 172 $cols = sqlite_fetch_column_types($model->tablePrefix.$model->table, $this->connection); 173 174 foreach ($cols as $column => $type) { 175 $fields[] = array('name' => $column, 'type' => $this->column($type), 'null' => true); 176 } 177 178 $this->__cacheDescription($model->tablePrefix.$model->table, $fields); 179 return $fields; 180 } 181 182 /** 183 * Returns a quoted name of $data for use in an SQL statement. 184 * 185 * @param string $data Name (table.field) to be prepared for use in an SQL statement 186 * @return string Quoted for SQLite 187 */ 188 function name ($data) 189 { 190 if ($data == '*') { 191 return '*'; 192 } 193 $pos = strpos($data, '"'); 194 if ($pos === false) { 195 $data = '"'. str_replace('.', '"."', $data) .'"'; 196 } 197 return $data; 198 } 199 200 /** 201 * Returns a quoted and escaped string of $data for use in an SQL statement. 202 * 203 * @param string $data String to be prepared for use in an SQL statement 204 * @return string Quoted and escaped 205 */ 206 function value ($data, $column = null, $safe = false) 207 { 208 $parent = parent::value($data, $column, $safe); 209 210 if ($parent != null) { 211 return $parent; 212 } 213 214 if ($data === null) 215 { 216 return 'NULL'; 217 } 218 219 if($data === '') { 220 return "''"; 221 } 222 223 switch ($column) { 224 case 'boolean': 225 $data = $this->boolean((bool)$data); 226 break; 227 default: 228 $data = sqlite_escape_string($data); 229 break; 230 } 231 return "'" . $data . "'"; 232 } 233 /** 234 * Begin a transaction 235 * 236 * @param unknown_type $model 237 * @return boolean True on success, false on fail 238 * (i.e. if the database/model does not support transactions). 239 */ 240 function begin (&$model) { 241 if (parent::begin($model)) { 242 if ($this->execute('BEGIN')) { 243 $this->__transactionStarted = true; 244 return true; 245 } 246 } 247 return false; 248 } 249 /** 250 * Commit a transaction 251 * 252 * @param unknown_type $model 253 * @return boolean True on success, false on fail 254 * (i.e. if the database/model does not support transactions, 255 * or a transaction has not started). 256 */ 257 function commit (&$model) { 258 if (parent::commit($model)) { 259 $this->__transactionStarted = false; 260 return $this->execute('COMMIT'); 261 } 262 return false; 263 } 264 /** 265 * Rollback a transaction 266 * 267 * @param unknown_type $model 268 * @return boolean True on success, false on fail 269 * (i.e. if the database/model does not support transactions, 270 * or a transaction has not started). 271 */ 272 function rollback (&$model) { 273 if (parent::rollback($model)) { 274 return $this->execute('ROLLBACK'); 275 } 276 return false; 277 } 278 /** 279 * Returns a formatted error message from previous database operation. 280 * 281 * @return string Error message 282 */ 283 function lastError() { 284 $error = sqlite_last_error($this->connection); 285 if ($error) { 286 return $error.': '.sqlite_error_string($error); 287 } 288 return null; 289 } 290 /** 291 * Returns number of affected rows in previous database operation. If no previous operation exists, this returns false. 292 * 293 * @return int Number of affected rows 294 */ 295 function lastAffected() { 296 if ($this->_result) { 297 return sqlite_changes($this->connection); 298 } 299 return false; 300 } 301 /** 302 * Returns number of rows in previous resultset. If no previous resultset exists, 303 * this returns false. 304 * 305 * @return int Number of rows in resultset 306 */ 307 function lastNumRows() { 308 if ($this->_result) { 309 sqlite_num_rows($this->_result); 310 } 311 return false; 312 } 313 /** 314 * Returns the ID generated from the previous INSERT operation. 315 * 316 * @return int 317 */ 318 function lastInsertId() { 319 return sqlite_last_insert_rowid($this->connection); 320 } 321 /** 322 * Converts database-layer column types to basic types 323 * 324 * @param string $real Real database-layer column type (i.e. "varchar(255)") 325 * @return string Abstract column type (i.e. "string") 326 */ 327 function column($real) { 328 if (is_array($real)) { 329 $col = $real['name']; 330 if (isset($real['limit'])) { 331 $col .= '('.$real['limit'].')'; 332 } 333 return $col; 334 } 335 336 $col = low(r(')', '', $real)); 337 $limit = null; 338 @list($col, $limit) = explode('(', $col); 339 340 if (in_array($col, array('text', 'integer', 'float', 'boolean', 'timestamp'))) { 341 return $col; 342 } 343 if (strpos($col, 'varchar') !== false) { 344 return 'string'; 345 } 346 if (in_array($col, array('blob', 'clob'))) { 347 return 'binary'; 348 } 349 if (strpos($col, 'numeric') !== false) { 350 return 'float'; 351 } 352 return 'text'; 353 } 354 355 /** 356 * Enter description here... 357 * 358 * @param unknown_type $results 359 */ 360 function resultSet(&$results) { 361 $this->results =& $results; 362 $this->map = array(); 363 $num_fields = sqlite_num_fields($results); 364 $index = 0; 365 $j = 0; 366 367 while ($j < $num_fields) { 368 $columnName = str_replace('"', '', sqlite_field_name($results, $j)); 369 370 if (strpos($columnName, '.')) { 371 $parts = explode('.', $columnName); 372 $this->map[$index++] = array($parts[0], $parts[1]); 373 } else { 374 $this->map[$index++] = array(0, $columnName); 375 } 376 $j++; 377 } 378 } 379 /** 380 * Fetches the next row from the current result set 381 * 382 * @return unknown 383 */ 384 function fetchResult() { 385 if ($row = sqlite_fetch_array($this->results, SQLITE_ASSOC)) { 386 $resultRow = array(); 387 $i = 0; 388 389 foreach ($row as $index => $field) { 390 if (strpos($index, '.')) { 391 list($table, $column) = explode('.', str_replace('"', '', $index)); 392 $resultRow[$table][$column] = $row[$index]; 393 } else { 394 $resultRow[0][str_replace('"', '', $index)] = $row[$index]; 395 } 396 $i++; 397 } 398 return $resultRow; 399 } else { 400 return false; 401 } 402 } 403 /** 404 * Returns a limit statement in the correct format for the particular database. 405 * 406 * @param int $limit Limit of results returned 407 * @param int $offset Offset from which to start results 408 * @return string SQL limit/offset statement 409 */ 410 function limit ($limit, $offset = null) { 411 if ($limit) { 412 $rt = ''; 413 if (!strpos(strtolower($limit), 'limit') || strpos(strtolower($limit), 'limit') === 0) { 414 $rt = ' LIMIT'; 415 } 416 $rt .= ' ' . $limit; 417 if ($offset) { 418 $rt .= ', ' . $offset; 419 } 420 return $rt; 421 } 422 return null; 423 } 424 } 425 ?>
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 |