1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
| <?php
/**
* AdoDB layer for DBO.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package datasources
* @subpackage datasources.models.datasources.dbo
* @since CakePHP Datasources v 0.1
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
//App::import('Datasource','DboSource');
App::uses('DboSource', 'Model/Datasource');
/**
* Include AdoDB files.
*/
App::import('Vendor', 'NewADOConnection', array('file' => 'adodb' . DS . 'adodb.inc.php'));
/**
* AdoDB DBO implementation.
*
* Database abstraction implementation for the AdoDB library.
*
* @package cake
* @subpackage cake.cake.libs.model.datasources.dbo
*/
class DboAdodb extends DboSource {
/**
* Enter description here...
*
* @var string
*/
var $description = "ADOdb DBO Driver";
/**
* ADOConnection object with which we connect.
*
* @var ADOConnection The connection object.
* @access private
*/
var $_adodb = null;
/**
* Array translating ADOdb column MetaTypes to cake-supported metatypes
*
* @var array
* @access private
*/
var $_adodbColumnTypes = array(
'string' => 'C',
'text' => 'X',
'date' => 'D',
'timestamp' => 'T',
'time' => 'T',
'datetime' => 'T',
'boolean' => 'L',
'float' => 'N',
'integer' => 'I',
'binary' => 'R',
);
/**
* ADOdb column definition
*
* @var array
*/
var $columns = array(
'primary_key' => array('name' => 'R', 'limit' => 11),
'string' => array('name' => 'C', 'limit' => '255'),
'text' => array('name' => 'X'),
'integer' => array('name' => 'I', 'limit' => '11', 'formatter' => 'intval'),
'float' => array('name' => 'N', 'formatter' => 'floatval'),
'timestamp' => array('name' => 'T', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
'time' => array('name' => 'T', 'format' => 'H:i:s', 'formatter' => 'date'),
'datetime' => array('name' => 'T', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
'date' => array('name' => 'D', 'format' => 'Y-m-d', 'formatter' => 'date'),
'binary' => array('name' => 'B'),
'boolean' => array('name' => 'L', 'limit' => '1')
);
/**
* Connects to the database using options in the given configuration array.
*
* @param array $config Configuration array for connecting
*/
function connect() {
$config = $this->config;
$persistent = strrpos($config['connect'], '|p');
if ($persistent === false) {
$adodb_driver = $config['connect'];
$connect = 'Connect';
} else {
$adodb_driver = substr($config['connect'], 0, $persistent);
$connect = 'PConnect';
}
if (!$this->enabled()) {
return false;
}
$this->_adodb = NewADOConnection($adodb_driver);
$this->_adodbDataDict = NewDataDictionary($this->_adodb, $adodb_driver);
$this->startQuote = $this->_adodb->nameQuote;
$this->endQuote = $this->_adodb->nameQuote;
$this->connected = $this->_adodb->$connect($config['host'], $config['login'], $config['password'], $config['database']);
$this->_adodbMetatyper = &$this->_adodb->execute('Select 1');
return $this->connected;
}
/**
* Check that AdoDB is available.
*
* @return boolean
*/
function enabled() {
return function_exists('NewADOConnection');
}
/**
* Disconnects from database.
*
* @return boolean True if the database could be disconnected, else false
*/
function disconnect() {
return $this->_adodb->Close();
}
/**
* Executes given SQL statement.
*
* @param string $sql SQL statement
* @return resource Result resource identifier
*/
function _execute($sql, $params = array(), $prepareOptions = array()) {
global $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
return $this->_adodb->execute($sql, $params, $prepareOptions);
}
/**
* Returns a row from current resultset as an array .
*
* @return array The fetched row as an array
*/
function fetchRow($sql = null) {
if (!empty($sql) && is_string($sql) && strlen($sql) > 5) {
if (!$this->execute($sql)) {
return null;
}
}
if (!$this->hasResult()) {
return null;
} else {
$resultRow = $this->_result->FetchRow();
$this->resultSet($resultRow);
return $this->fetchResult();
}
}
/**
* Begin a transaction
*
* @param unknown_type $model
* @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions).
*/
function begin() {
if (parent::begin()) {
if ($this->_adodb->BeginTrans()) {
$this->_transactionStarted = true;
return true;
}
}
return false;
}
/**
* Commit a transaction
*
* @param unknown_type $model
* @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions,
* or a transaction has not started).
*/
function commit() {
if (parent::commit()) {
$this->_transactionStarted = false;
return $this->_adodb->CommitTrans();
}
return false;
}
/**
* Rollback a transaction
*
* @param unknown_type $model
* @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions,
* or a transaction has not started).
*/
function rollback() {
if (parent::rollback()) {
return $this->_adodb->RollbackTrans();
}
return false;
}
/**
* Returns an array of tables in the database. If there are no tables, an error is raised and the application exits.
*
* @return array Array of tablenames in the database
*/
function listSources($data = null) {
$tables = $this->_adodb->MetaTables('TABLES');
if (!count($tables) > 0) {
trigger_error(ERROR_NO_TABLE_LIST, E_USER_NOTICE);
exit;
}
return $tables;
}
/**
* Returns an array of the fields in the table used by the given model.
*
* @param AppModel $model Model object
* @return array Fields in table. Keys are name and type
*/
function describe($model) {
$cache = parent::describe($model);
if ($cache != null) {
return $cache;
}
$fields = false;
$cols = $this->_adodb->MetaColumns($this->fullTableName($model, false));
foreach ($cols as $column) {
$fields[$column->name] = array(
'type' => $this->column($column->type),
'null' => !$column->not_null,
'length' => $column->max_length,
);
if ($column->has_default) {
$fields[$column->name]['default'] = $column->default_value;
}
if ($column->primary_key == 1) {
$fields[$column->name]['key'] = 'primary';
}
}
$this->__cacheDescription($this->fullTableName($model, false), $fields);
return $fields;
}
/**
* Returns a formatted error message from previous database operation.
*
* @return string Error message
*/
function lastError(PDOStatement $query = null) {
return $this->_adodb->ErrorMsg();
}
/**
* Returns number of affected rows in previous database operation, or false if no previous operation exists.
*
* @return integer Number of affected rows
*/
function lastAffected($source = null) {
return $this->_adodb->Affected_Rows();
}
/**
* Returns number of rows in previous resultset, or false if no previous resultset exists.
*
* @return integer Number of rows in resultset
*/
function lastNumRows($source = null) {
return $this->_result ? $this->_result->RecordCount() : false;
}
/**
* Returns the ID generated from the previous INSERT operation.
*
* @return int
*
* @Returns the last autonumbering ID inserted. Returns false if function not supported.
*/
function lastInsertId($source = null) {
return $this->_adodb->Insert_ID();
}
/**
* Returns a LIMIT statement in the correct format for the particular database.
*
* @param integer $limit Limit of results returned
* @param integer $offset Offset from which to start results
* @return string SQL limit/offset statement
* @todo Please change output string to whatever select your database accepts. adodb doesn't allow us to get the correct limit string out of it.
*/
function limit($limit, $offset = null) {
if ($limit) {
$rt = '';
if (!strpos(strtolower($limit), 'limit') || strpos(strtolower($limit), 'limit') === 0) {
$rt = ' LIMIT';
}
if ($offset) {
$rt .= ' ' . $offset . ',';
}
$rt .= ' ' . $limit;
return $rt;
}
return null;
// please change to whatever select your database accepts
// adodb doesn't allow us to get the correct limit string out of it
}
/**
* Converts database-layer column types to basic types
*
* @param string $real Real database-layer column type (i.e. "varchar(255)")
* @return string Abstract column type (i.e. "string")
*/
function column($real) {
$metaTypes = array_flip($this->_adodbColumnTypes);
$interpreted_type = $this->_adodbMetatyper->MetaType($real);
if (!isset($metaTypes[$interpreted_type])) {
return 'text';
}
return $metaTypes[$interpreted_type];
}
/**
* Returns a quoted and escaped string of $data for use in an SQL statement.
*
* @param string $data String to be prepared for use in an SQL statement
* @param string $column_type The type of the column into which this data will be inserted
* @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided
* @return string Quoted and escaped data
*/
function value($data, $column = null, $safe = false) {
$parent = parent::value($data, $column, $safe);
if ($parent != null) {
return $parent;
}
if ($data === null || (is_array($data) && empty($data))) {
return 'NULL';
}
if ($data === '') {
return "''";
}
return $this->_adodb->qstr($data);
}
/**
* Generates the fields list of an SQL query.
*
* @param Model $model
* @param string $alias Alias tablename
* @param mixed $fields
* @return array
*/
function fields(Model $model, $alias = null, $fields = array(), $quote = true) {
if (empty($alias)) {
$alias = $model->alias;
}
$fields = parent::fields($model, $alias, $fields, false);
if (!$quote) {
return $fields;
}
$count = count($fields);
if ($count >= 1 && $fields[0] != '*' && strpos($fields[0], 'COUNT(*)') === false) {
for ($i = 0; $i < $count; $i++) {
if (!preg_match('/^.+\\(.*\\)/', $fields[$i]) && !preg_match('/\s+AS\s+/', $fields[$i])) {
$prepend = '';
if (strpos($fields[$i], 'DISTINCT') !== false) {
$prepend = 'DISTINCT ';
$fields[$i] = trim(str_replace('DISTINCT', '', $fields[$i]));
}
if (strrpos($fields[$i], '.') === false) {
$fields[$i] = $prepend . $this->name($alias) . '.' . $this->name($fields[$i]) . ' AS ' . $this->name($alias . '__' . $fields[$i]);
} else {
$build = explode('.', $fields[$i]);
$fields[$i] = $prepend . $this->name($build[0]) . '.' . $this->name($build[1]) . ' AS ' . $this->name($build[0] . '__' . $build[1]);
}
}
}
}
return $fields;
}
/**
* Build ResultSets and map data
*
* @param array $results
*/
function resultSet(&$results) {
$num_fields = count($results);
$fields = array_keys($results);
$this->results =& $results;
$this->map = array();
$index = 0;
$j = 0;
while ($j < $num_fields) {
$columnName = $fields[$j];
if (strpos($columnName, '__')) {
$parts = explode('__', $columnName);
$this->map[$index++] = array($parts[0], $parts[1]);
} else {
$this->map[$index++] = array(0, $columnName);
}
$j++;
}
}
/**
* Fetches the next row from the current result set
*
* @return unknown
*/
function fetchResult() {
if (!empty($this->results)) {
$row = $this->results;
$this->results = null;
} else {
$row = $this->_result->FetchRow();
}
if (empty($row)) {
return false;
}
$resultRow = array();
$fields = array_keys($row);
$count = count($fields);
$i = 0;
for ($i = 0; $i < $count; $i++) { //$row as $index => $field) {
list($table, $column) = $this->map[$i];
$resultRow[$table][$column] = $row[$fields[$i]];
}
return $resultRow;
}
/**
* Generate a database-native column schema string
*
* @param array $column An array structured like the following: array('name'=>'value', 'type'=>'value'[, options]),
* where options can be 'default', 'length', or 'key'.
* @return string
*/
function buildColumn($column) {
$name = $type = null;
extract(array_merge(array('null' => true), $column));
if (empty($name) || empty($type)) {
trigger_error('Column name or type not defined in schema', E_USER_WARNING);
return null;
}
//$metaTypes = array_flip($this->_adodbColumnTypes);
if (!isset($this->_adodbColumnTypes[$type])) {
trigger_error("Column type {$type} does not exist", E_USER_WARNING);
return null;
}
$metaType = $this->_adodbColumnTypes[$type];
$concreteType = $this->_adodbDataDict->ActualType($metaType);
$real = $this->columns[$type];
//UUIDs are broken so fix them.
if ($type == 'string' && isset($real['length']) && $real['length'] == 36) {
$concreteType = 'CHAR';
}
$out = $this->name($name) . ' ' . $concreteType;
if (isset($real['limit']) || isset($real['length']) || isset($column['limit']) || isset($column['length'])) {
if (isset($column['length'])) {
$length = $column['length'];
} elseif (isset($column['limit'])) {
$length = $column['limit'];
} elseif (isset($real['length'])) {
$length = $real['length'];
} else {
$length = $real['limit'];
}
$out .= '(' . $length . ')';
}
$_notNull = $_default = $_autoInc = $_constraint = $_unsigned = false;
if (isset($column['key']) && $column['key'] == 'primary' && $type == 'integer') {
$_constraint = '';
$_autoInc = true;
} elseif (isset($column['key']) && $column['key'] == 'primary') {
$_notNull = '';
} elseif (isset($column['default']) && isset($column['null']) && $column['null'] == false) {
$_notNull = true;
$_default = $column['default'];
} elseif ( isset($column['null']) && $column['null'] == true) {
$_notNull = false;
$_default = 'NULL';
}
if (isset($column['default']) && $_default == false) {
$_default = $this->value($column['default']);
}
if (isset($column['null']) && $column['null'] == false) {
$_notNull = true;
}
//use concrete instance of DataDict to make the suffixes for us.
$out .= $this->_adodbDataDict->_CreateSuffix($out, $metaType, $_notNull, $_default, $_autoInc, $_constraint, $_unsigned);
return $out;
}
/**
* Checks if the result is valid
*
* @return boolean True if the result is valid, else false
*/
function hasResult() {
return is_object($this->_result) && !$this->_result->EOF;
}
} |
Partager