/usr/share/php/propel/adapter/DBAdapter.php is in php-propel-runtime 1.6.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 | <?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
/**
* DBAdapter</code> defines the interface for a Propel database adapter.
*
* <p>Support for new databases is added by subclassing
* <code>DBAdapter</code> and implementing its abstract interface, and by
* registering the new database adapter and corresponding Propel
* driver in the private adapters map (array) in this class.</p>
*
* <p>The Propel database adapters exist to present a uniform
* interface to database access across all available databases. Once
* the necessary adapters have been written and configured,
* transparent swapping of databases is theoretically supported with
* <i>zero code change</i> and minimal configuration file
* modifications.</p>
*
* @author Hans Lellelid <hans@xmpl.org> (Propel)
* @author Jon S. Stevens <jon@latchkey.com> (Torque)
* @author Brett McLaughlin <bmclaugh@algx.net> (Torque)
* @author Daniel Rall <dlr@finemaltcoding.com> (Torque)
* @version $Revision$
* @package propel.runtime.adapter
*/
abstract class DBAdapter
{
const ID_METHOD_NONE = 0;
const ID_METHOD_AUTOINCREMENT = 1;
const ID_METHOD_SEQUENCE = 2;
/**
* Propel driver to Propel adapter map.
* @var array
*/
private static $adapters = array(
'mysql' => 'DBMySQL',
'mysqli' => 'DBMySQLi',
'mssql' => 'DBMSSQL',
'sqlsrv' => 'DBSQLSRV',
'oracle' => 'DBOracle',
'oci' => 'DBOracle',
'pgsql' => 'DBPostgres',
'sqlite' => 'DBSQLite',
'' => 'DBNone',
);
/**
* Creates a new instance of the database adapter associated
* with the specified Propel driver.
*
* @param string $driver The name of the Propel driver to create a new adapter instance
* for or a shorter form adapter key.
*
* @throws PropelException If the adapter could not be instantiated.
* @return DBAdapter An instance of a Propel database adapter.
*/
public static function factory($driver)
{
$adapterClass = isset(self::$adapters[$driver]) ? self::$adapters[$driver] : null;
if ($adapterClass !== null) {
$a = new $adapterClass();
return $a;
} else {
throw new PropelException("Unsupported Propel driver: " . $driver . ": Check your configuration file");
}
}
/**
* Prepare connection parameters.
*
* @param array $params
* @return array
*/
public function prepareParams($settings)
{
return $settings;
}
/**
* This method is called after a connection was created to run necessary
* post-initialization queries or code.
*
* If a charset was specified, this will be set before any other queries
* are executed.
*
* This base method runs queries specified using the "query" setting.
*
* @see setCharset()
*
* @param PDO $con A PDO connection instance.
* @param array $settings An array of settings.
*/
public function initConnection(PDO $con, array $settings)
{
if (isset($settings['charset']['value'])) {
$this->setCharset($con, $settings['charset']['value']);
}
if (isset($settings['queries']) && is_array($settings['queries'])) {
foreach ($settings['queries'] as $queries) {
foreach ((array) $queries as $query) {
$con->exec($query);
}
}
}
}
/**
* Sets the character encoding using SQL standard SET NAMES statement.
*
* This method is invoked from the default initConnection() method and must
* be overridden for an RDMBS which does _not_ support this SQL standard.
*
* @see initConnection()
*
* @param PDO $con A $PDO PDO connection instance.
* @param string $charset The $string charset encoding.
*/
public function setCharset(PDO $con, $charset)
{
$con->exec("SET NAMES '" . $charset . "'");
}
/**
* This method is used to ignore case.
*
* @param string $in The string to transform to upper case.
* @return string The upper case string.
*/
abstract public function toUpperCase($in);
/**
* Returns the character used to indicate the beginning and end of
* a piece of text used in a SQL statement (generally a single
* quote).
*
* @return string The text delimeter.
*/
public function getStringDelimiter()
{
return '\'';
}
/**
* This method is used to ignore case.
*
* @param string $in The string whose case to ignore.
* @return string The string in a case that can be ignored.
*/
abstract public function ignoreCase($in);
/**
* This method is used to ignore case in an ORDER BY clause.
* Usually it is the same as ignoreCase, but some databases
* (Interbase for example) does not use the same SQL in ORDER BY
* and other clauses.
*
* @param string $in The string whose case to ignore.
* @return string The string in a case that can be ignored.
*/
public function ignoreCaseInOrderBy($in)
{
return $this->ignoreCase($in);
}
/**
* Returns SQL which concatenates the second string to the first.
*
* @param string $s1 String to concatenate.
* @param string $s2 String to append.
*
* @return string
*/
abstract public function concatString($s1, $s2);
/**
* Returns SQL which extracts a substring.
*
* @param string $s String to extract from.
* @param integer $pos Offset to start from.
* @param integer $len Number of characters to extract.
*
* @return string
*/
abstract public function subString($s, $pos, $len);
/**
* Returns SQL which calculates the length (in chars) of a string.
*
* @param string $s String to calculate length of.
* @return string
*/
abstract public function strLength($s);
/**
* Quotes database objec identifiers (table names, col names, sequences, etc.).
* @param string $text The identifier to quote.
* @return string The quoted identifier.
*/
public function quoteIdentifier($text)
{
return '"' . $text . '"';
}
/**
* Quotes a database table which could have space seperating it from an alias, both should be identified seperately
* This doesn't take care of dots which separate schema names from table names. Adapters for RDBMs which support
* schemas have to implement that in the platform-specific way.
*
* @param string $table The table name to quo
* @return string The quoted table name
**/
public function quoteIdentifierTable($table)
{
return implode(" ", array_map(array($this, "quoteIdentifier"), explode(" ", $table) ) );
}
/**
* Returns the native ID method for this RDBMS.
*
* @return integer One of DBAdapter:ID_METHOD_SEQUENCE, DBAdapter::ID_METHOD_AUTOINCREMENT.
*/
protected function getIdMethod()
{
return DBAdapter::ID_METHOD_AUTOINCREMENT;
}
/**
* Whether this adapter uses an ID generation system that requires getting ID _before_ performing INSERT.
*
* @return boolean
*/
public function isGetIdBeforeInsert()
{
return ($this->getIdMethod() === DBAdapter::ID_METHOD_SEQUENCE);
}
/**
* Whether this adapter uses an ID generation system that requires getting ID _before_ performing INSERT.
*
* @return boolean
*/
public function isGetIdAfterInsert()
{
return ($this->getIdMethod() === DBAdapter::ID_METHOD_AUTOINCREMENT);
}
/**
* Gets the generated ID (either last ID for autoincrement or next sequence ID).
* Warning: duplicates logic from DefaultPlatform::getIdentifierPhp().
* Any code modification here must be ported there.
*
* @param PDO $con
* @param string $name
*
* @return mixed
*/
public function getId(PDO $con, $name = null)
{
return $con->lastInsertId($name);
}
/**
* Formats a temporal value brefore binding, given a ColumnMap object.
*
* @param mixed $value The temporal value
* @param mixed $type PropelColumnTypes constant, or ColumnMap object
*
* @return string The formatted temporal value
*/
public function formatTemporalValue($value, $type)
{
/** @var $dt PropelDateTime */
if ($dt = PropelDateTime::newInstance($value)) {
if ($type instanceof ColumnMap) {
$type = $type->getType();
}
switch ($type) {
case PropelColumnTypes::TIMESTAMP:
case PropelColumnTypes::BU_TIMESTAMP:
$value = $dt->format($this->getTimestampFormatter());
break;
case PropelColumnTypes::DATE:
case PropelColumnTypes::BU_DATE:
$value = $dt->format($this->getDateFormatter());
break;
case PropelColumnTypes::TIME:
$value = $dt->format($this->getTimeFormatter());
break;
}
}
return $value;
}
/**
* Returns timestamp formatter string for use in date() function.
*
* @return string
*/
public function getTimestampFormatter()
{
return 'Y-m-d H:i:s';
}
/**
* Returns date formatter string for use in date() function.
*
* @return string
*/
public function getDateFormatter()
{
return "Y-m-d";
}
/**
* Returns time formatter string for use in date() function.
*
* @return string
*/
public function getTimeFormatter()
{
return "H:i:s";
}
/**
* Should Column-Names get identifiers for inserts or updates.
* By default false is returned -> backwards compability.
*
* it`s a workaround...!!!
*
* @todo should be abstract
* @deprecated
*
* @return boolean
*/
public function useQuoteIdentifier()
{
return false;
}
/**
* Allows manipulation of the query string before PDOStatement is instantiated.
*
* @param string $sql The sql statement
* @param array $params array('column' => ..., 'table' => ..., 'value' => ...)
* @param Criteria $values
* @param DatabaseMap $dbMap
*/
public function cleanupSQL(&$sql, array &$params, Criteria $values, DatabaseMap $dbMap)
{
}
/**
* Modifies the passed-in SQL to add LIMIT and/or OFFSET.
*
* @param string $sql
* @param integer $offset
* @param integer $limit
*/
abstract public function applyLimit(&$sql, $offset, $limit);
/**
* Gets the SQL string that this adapter uses for getting a random number.
*
* @param mixed $seed (optional) seed value for databases that support this
*/
abstract public function random($seed = null);
/**
* Returns the "DELETE FROM <table> [AS <alias>]" part of DELETE query.
*
* @param Criteria $criteria
* @param string $tableName
*
* @return string
*/
public function getDeleteFromClause($criteria, $tableName)
{
$sql = 'DELETE ';
if ($queryComment = $criteria->getComment()) {
$sql .= '/* ' . $queryComment . ' */ ';
}
if ($realTableName = $criteria->getTableForAlias($tableName)) {
if ($this->useQuoteIdentifier()) {
$realTableName = $this->quoteIdentifierTable($realTableName);
}
$sql .= $tableName . ' FROM ' . $realTableName . ' AS ' . $tableName;
} else {
if ($this->useQuoteIdentifier()) {
$tableName = $this->quoteIdentifierTable($tableName);
}
$sql .= 'FROM ' . $tableName;
}
return $sql;
}
/**
* Builds the SELECT part of a SQL statement based on a Criteria
* taking into account select columns and 'as' columns (i.e. columns aliases)
* Move from BasePeer to DBAdapter and turn from static to non static
*
* @param Criteria $criteria
* @param array $fromClause
* @param boolean $aliasAll
*
* @return string
*/
public function createSelectSqlPart(Criteria $criteria, &$fromClause, $aliasAll = false)
{
$selectClause = array();
if ($aliasAll) {
$this->turnSelectColumnsToAliases($criteria);
// no select columns after that, they are all aliases
} else {
foreach ($criteria->getSelectColumns() as $columnName) {
// expect every column to be of "table.column" formation
// it could be a function: e.g. MAX(books.price)
$tableName = null;
$selectClause[] = $columnName; // the full column name: e.g. MAX(books.price)
$parenPos = strrpos($columnName, '(');
$dotPos = strrpos($columnName, '.', ($parenPos !== false ? $parenPos : 0));
if ($dotPos !== false) {
if ($parenPos === false) { // table.column
$tableName = substr($columnName, 0, $dotPos);
} else { // FUNC(table.column)
// functions may contain qualifiers so only take the last
// word as the table name.
// COUNT(DISTINCT books.price)
$tableName = substr($columnName, $parenPos + 1, $dotPos - ($parenPos + 1));
$lastSpace = strrpos($tableName, ' ');
if ($lastSpace !== false) { // COUNT(DISTINCT books.price)
$tableName = substr($tableName, $lastSpace + 1);
}
}
// is it a table alias?
$tableName2 = $criteria->getTableForAlias($tableName);
if ($tableName2 !== null) {
$fromClause[] = $tableName2 . ' ' . $tableName;
} else {
$fromClause[] = $tableName;
}
} // if $dotPost !== false
}
}
// set the aliases
foreach ($criteria->getAsColumns() as $alias => $col) {
$selectClause[] = $col . ' AS ' . $alias;
}
$selectModifiers = $criteria->getSelectModifiers();
$queryComment = $criteria->getComment();
// Build the SQL from the arrays we compiled
$sql = "SELECT "
. ($queryComment ? '/* ' . $queryComment . ' */ ' : '')
. ($selectModifiers ? (implode(' ', $selectModifiers) . ' ') : '')
. implode(", ", $selectClause);
return $sql;
}
/**
* Ensures uniqueness of select column names by turning them all into aliases
* This is necessary for queries on more than one table when the tables share a column name
* Moved from BasePeer to DBAdapter and turned from static to non static
*
* @see http://propel.phpdb.org/trac/ticket/795
*
* @param Criteria $criteria
* @return Criteria The input, with Select columns replaced by aliases
*/
public function turnSelectColumnsToAliases(Criteria $criteria)
{
$selectColumns = $criteria->getSelectColumns();
// clearSelectColumns also clears the aliases, so get them too
$asColumns = $criteria->getAsColumns();
$criteria->clearSelectColumns();
$columnAliases = $asColumns;
// add the select columns back
foreach ($selectColumns as $clause) {
// Generate a unique alias
$baseAlias = preg_replace('/\W/', '_', $clause);
$alias = $baseAlias;
// If it already exists, add a unique suffix
$i = 0;
while (isset($columnAliases[$alias])) {
$i++;
$alias = $baseAlias . '_' . $i;
}
// Add it as an alias
$criteria->addAsColumn($alias, $clause);
$columnAliases[$alias] = $clause;
}
// Add the aliases back, don't modify them
foreach ($asColumns as $name => $clause) {
$criteria->addAsColumn($name, $clause);
}
return $criteria;
}
/**
* Binds values in a prepared statement.
*
* This method is designed to work with the BasePeer::createSelectSql() method, which creates
* both the SELECT SQL statement and populates a passed-in array of parameter
* values that should be substituted.
*
* <code>
* $db = Propel::getDB($criteria->getDbName());
* $sql = BasePeer::createSelectSql($criteria, $params);
* $stmt = $con->prepare($sql);
* $params = array();
* $db->populateStmtValues($stmt, $params, Propel::getDatabaseMap($critera->getDbName()));
* $stmt->execute();
* </code>
*
* @param PDOStatement $stmt
* @param array $params array('column' => ..., 'table' => ..., 'value' => ...)
* @param DatabaseMap $dbMap
*/
public function bindValues(PDOStatement $stmt, array $params, DatabaseMap $dbMap)
{
$position = 0;
foreach ($params as $param) {
$position++;
$parameter = ':p' . $position;
$value = $param['value'];
if (null === $value) {
$stmt->bindValue($parameter, null, PDO::PARAM_NULL);
continue;
}
$tableName = $param['table'];
if (null === $tableName) {
$type = isset($param['type']) ? $param['type'] : PDO::PARAM_STR;
$stmt->bindValue($parameter, $value, $type);
continue;
}
$cMap = $dbMap->getTable($tableName)->getColumn($param['column']);
$this->bindValue($stmt, $parameter, $value, $cMap, $position);
}
}
/**
* Binds a value to a positioned parameted in a statement,
* given a ColumnMap object to infer the binding type.
* Warning: duplicates logic from DefaultPlatform::getColumnBindingPHP().
* Any code modification here must be ported there.
*
* @param PDOStatement $stmt The statement to bind
* @param string $parameter Parameter identifier
* @param mixed $value The value to bind
* @param ColumnMap $cMap The ColumnMap of the column to bind
* @param null|integer $position The position of the parameter to bind
*
* @return boolean
*/
public function bindValue(PDOStatement $stmt, $parameter, $value, ColumnMap $cMap, $position = null)
{
if ($cMap->isTemporal()) {
$value = $this->formatTemporalValue($value, $cMap);
} elseif (is_resource($value) && $cMap->isLob()) {
// we always need to make sure that the stream is rewound, otherwise nothing will
// get written to database.
rewind($value);
}
return $stmt->bindValue($parameter, $value, $cMap->getPdoType());
}
/**
* Do Explain Plan for query object or query string
*
* @param PropelPDO $con propel connection
* @param ModelCriteria|string $query query the criteria or the query string
* @throws PropelException if explain plan is not implemented for adapter
* @return PDOStatement A PDO statement executed using the connection, ready to be fetched
*/
public function doExplainPlan(PropelPDO $con, $query)
{
throw new PropelException("Explain plan is not implemented for this adapter");
}
}
|