/usr/share/php/propel/query/Criterion.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 | <?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
*/
/**
* This is an "inner" class that describes an object in the criteria.
*
* In Torque this is an inner class of the Criteria class.
*
* @author Hans Lellelid <hans@xmpl.org> (Propel)
* @version $Revision$
* @package propel.runtime.query
*/
class Criterion
{
const UND = " AND ";
const ODER = " OR ";
/** Value of the CO. */
protected $value;
/** Comparison value.
* @var SqlEnum
*/
protected $comparison;
/** Table name. */
protected $table;
/** Real table name */
protected $realtable;
/** Column name. */
protected $column;
/**
* Binding type to be used for Criteria::RAW comparison
* @var string any of the PDO::PARAM_ constant values
*/
protected $type;
/** flag to ignore case in comparison */
protected $ignoreStringCase = false;
/**
* The DBAdaptor which might be used to get db specific
* variations of sql.
*/
protected $db;
/**
* other connected criteria and their conjunctions.
*/
protected $clauses = array();
protected $conjunctions = array();
/** "Parent" Criteria class */
protected $parent;
/**
* Create a new instance.
*
* @param Criteria $outer The outer class (this is an "inner" class).
* @param string $column TABLE.COLUMN format.
* @param mixed $value
* @param string $comparison
* @param string $type
*/
public function __construct(Criteria $outer, $column, $value, $comparison = null, $type = null)
{
$this->value = $value;
$dotPos = strrpos($column, '.');
if ($dotPos === false || $comparison == Criteria::RAW) {
// no dot => aliased column
$this->table = null;
$this->column = $column;
} else {
$this->table = substr($column, 0, $dotPos);
$this->column = substr($column, $dotPos + 1);
}
$this->comparison = ($comparison === null) ? Criteria::EQUAL : $comparison;
$this->type = $type;
$this->init($outer);
}
/**
* Init some properties with the help of outer class
* @param Criteria $criteria The outer class
*/
public function init(Criteria $criteria)
{
// init $this->db
try {
$db = Propel::getDB($criteria->getDbName());
$this->setDB($db);
} catch (Exception $e) {
// we are only doing this to allow easier debugging, so
// no need to throw up the exception, just make note of it.
Propel::log("Could not get a DBAdapter, sql may be wrong", Propel::LOG_ERR);
}
// init $this->realtable
$realtable = $criteria->getTableForAlias($this->table);
$this->realtable = $realtable ? $realtable : $this->table;
}
/**
* Get the column name.
*
* @return string A String with the column name.
*/
public function getColumn()
{
return $this->column;
}
/**
* Set the table name.
*
* @param string $name A String with the table name.
* @return void
*/
public function setTable($name)
{
$this->table = $name;
}
/**
* Get the table name.
*
* @return string A String with the table name.
*/
public function getTable()
{
return $this->table;
}
/**
* Get the comparison.
*
* @return string A String with the comparison.
*/
public function getComparison()
{
return $this->comparison;
}
/**
* Get the value.
*
* @return mixed An Object with the value.
*/
public function getValue()
{
return $this->value;
}
/**
* Get the value of db.
* The DBAdapter which might be used to get db specific
* variations of sql.
* @return DBAdapter value of db.
*/
public function getDB()
{
return $this->db;
}
/**
* Set the value of db.
* The DBAdapter might be used to get db specific variations of sql.
* @param DBAdapter $v Value to assign to db.
* @return void
*/
public function setDB(DBAdapter $v)
{
$this->db = $v;
foreach ($this->clauses as $clause) {
$clause->setDB($v);
}
}
/**
* Sets ignore case.
*
* @param boolean $b True if case should be ignored.
* @return Criterion A modified Criterion object.
*/
public function setIgnoreCase($b)
{
$this->ignoreStringCase = (boolean) $b;
return $this;
}
/**
* Is ignore case on or off?
*
* @return boolean True if case is ignored.
*/
public function isIgnoreCase()
{
return $this->ignoreStringCase;
}
/**
* Get the list of clauses in this Criterion.
* @return array
*/
private function getClauses()
{
return $this->clauses;
}
/**
* Get the list of conjunctions in this Criterion
* @return array
*/
public function getConjunctions()
{
return $this->conjunctions;
}
/**
* Append an AND Criterion onto this Criterion's list.
*/
public function addAnd(Criterion $criterion)
{
$this->clauses[] = $criterion;
$this->conjunctions[] = self::UND;
return $this;
}
/**
* Append an OR Criterion onto this Criterion's list.
*
* @param Criterion $criterion
* @return Criterion
*/
public function addOr(Criterion $criterion)
{
$this->clauses[] = $criterion;
$this->conjunctions[] = self::ODER;
return $this;
}
/**
* Appends a Prepared Statement representation of the Criterion
* onto the buffer.
*
* @param string &$sb The string that will receive the Prepared Statement
* @param array $params A list to which Prepared Statement parameters will be appended
* @return void
* @throws PropelException - if the expression builder cannot figure out how to turn a specified
* expression into proper SQL.
*/
public function appendPsTo(&$sb, array &$params)
{
$sb .= str_repeat ( '(', count($this->clauses) );
$this->dispatchPsHandling($sb, $params);
foreach ($this->clauses as $key => $clause) {
$sb .= $this->conjunctions[$key];
$clause->appendPsTo($sb, $params);
$sb .= ')';
}
}
/**
* Figure out which Criterion method to use
* to build the prepared statement and parameters using to the Criterion comparison
* and call it to append the prepared statement and the parameters of the current clause
*
* @param string &$sb The string that will receive the Prepared Statement
* @param array $params A list to which Prepared Statement parameters will be appended
*/
protected function dispatchPsHandling(&$sb, array &$params)
{
switch ($this->comparison) {
case Criteria::CUSTOM:
// custom expression with no parameter binding
$this->appendCustomToPs($sb, $params);
break;
case Criteria::RAW:
// custom expression with a typed parameter binding
$this->appendRawToPs($sb, $params);
break;
case Criteria::IN:
case Criteria::NOT_IN:
// table.column IN (?, ?) or table.column NOT IN (?, ?)
$this->appendInToPs($sb, $params);
break;
case Criteria::LIKE:
case Criteria::NOT_LIKE:
case Criteria::ILIKE:
case Criteria::NOT_ILIKE:
// table.column LIKE ? or table.column NOT LIKE ? (or ILIKE for Postgres)
$this->appendLikeToPs($sb, $params);
break;
default:
// table.column = ? or table.column >= ? etc. (traditional expressions, the default)
$this->appendBasicToPs($sb, $params);
}
}
/**
* Appends a Prepared Statement representation of the Criterion onto the buffer
* For custom expressions with no binding, e.g. 'NOW() = 1'
*
* @param string &$sb The string that will receive the Prepared Statement
* @param array $params A list to which Prepared Statement parameters will be appended
*/
protected function appendCustomToPs(&$sb, array &$params)
{
if ($this->value !== "") {
$sb .= (string) $this->value;
}
}
/**
* Appends a Prepared Statement representation of the Criterion onto the buffer
* For custom expressions with a typed binding, e.g. 'foobar = ?'
*
* @param string &$sb The string that will receive the Prepared Statement
* @param array $params A list to which Prepared Statement parameters will be appended
*
* @throws PropelException
*/
protected function appendRawToPs(&$sb, array &$params)
{
if (substr_count($this->column, '?') != 1) {
throw new PropelException(sprintf('Could not build SQL for expression "%s" because Criteria::RAW works only with a clause containing a single question mark placeholder', $this->column));
}
$params[] = array('table' => null, 'type' => $this->type, 'value' => $this->value);
$sb .= str_replace('?', ':p' . count($params), $this->column);
}
/**
* Appends a Prepared Statement representation of the Criterion onto the buffer
* For IN expressions, e.g. table.column IN (?, ?) or table.column NOT IN (?, ?)
*
* @param string &$sb The string that will receive the Prepared Statement
* @param array $params A list to which Prepared Statement parameters will be appended
*/
protected function appendInToPs(&$sb, array &$params)
{
if ($this->value !== "") {
$bindParams = array();
$index = count($params); // to avoid counting the number of parameters for each element in the array
foreach ((array) $this->value as $value) {
$params[] = array('table' => $this->realtable, 'column' => $this->column, 'value' => $value);
$index++; // increment this first to correct for wanting bind params to start with :p1
$bindParams[] = ':p' . $index;
}
if (count($bindParams)) {
$field = ($this->table === null) ? $this->column : $this->table . '.' . $this->column;
$sb .= $field . $this->comparison . '(' . implode(',', $bindParams) . ')';
} else {
$sb .= ($this->comparison === Criteria::IN) ? "1<>1" : "1=1";
}
}
}
/**
* Appends a Prepared Statement representation of the Criterion onto the buffer
* For LIKE expressions, e.g. table.column LIKE ? or table.column NOT LIKE ? (or ILIKE for Postgres)
*
* @param string &$sb The string that will receive the Prepared Statement
* @param array $params A list to which Prepared Statement parameters will be appended
*/
protected function appendLikeToPs(&$sb, array &$params)
{
$field = ($this->table === null) ? $this->column : $this->table . '.' . $this->column;
$db = $this->getDb();
// If selection is case insensitive use ILIKE for PostgreSQL or SQL
// UPPER() function on column name for other databases.
if ($this->ignoreStringCase) {
if ($db instanceof DBPostgres) {
if ($this->comparison === Criteria::LIKE) {
$this->comparison = Criteria::ILIKE;
} elseif ($this->comparison === Criteria::NOT_LIKE) {
$this->comparison = Criteria::NOT_ILIKE;
}
} else {
$field = $db->ignoreCase($field);
}
}
$params[] = array('table' => $this->realtable, 'column' => $this->column, 'value' => $this->value);
$sb .= $field . $this->comparison;
// If selection is case insensitive use SQL UPPER() function
// on criteria or, if Postgres we are using ILIKE, so not necessary.
if ($this->ignoreStringCase && !($db instanceof DBPostgres)) {
$sb .= $db->ignoreCase(':p'.count($params));
} else {
$sb .= ':p'.count($params);
}
}
/**
* Appends a Prepared Statement representation of the Criterion onto the buffer
* For traditional expressions, e.g. table.column = ? or table.column >= ? etc.
*
* @param string &$sb The string that will receive the Prepared Statement
* @param array $params A list to which Prepared Statement parameters will be appended
*
* @throws PropelException
*/
protected function appendBasicToPs(&$sb, array &$params)
{
$field = ($this->table === null) ? $this->column : $this->table . '.' . $this->column;
// NULL VALUES need special treatment because the SQL syntax is different
// i.e. table.column IS NULL rather than table.column = null
if ($this->value !== null) {
// ANSI SQL functions get inserted right into SQL (not escaped, etc.)
if ($this->value === Criteria::CURRENT_DATE || $this->value === Criteria::CURRENT_TIME || $this->value === Criteria::CURRENT_TIMESTAMP) {
$sb .= $field . $this->comparison . $this->value;
} else {
$params[] = array('table' => $this->realtable, 'column' => $this->column, 'value' => $this->value);
// default case, it is a normal col = value expression; value
// will be replaced w/ '?' and will be inserted later using PDO bindValue()
if ($this->ignoreStringCase) {
$sb .= $this->getDb()->ignoreCase($field) . $this->comparison . $this->getDb()->ignoreCase(':p'.count($params));
} else {
$sb .= $field . $this->comparison . ':p'.count($params);
}
}
} else {
// value is null, which means it was either not specified or specifically
// set to null.
if ($this->comparison === Criteria::EQUAL || $this->comparison === Criteria::ISNULL) {
$sb .= $field . Criteria::ISNULL;
} elseif ($this->comparison === Criteria::NOT_EQUAL || $this->comparison === Criteria::ISNOTNULL) {
$sb .= $field . Criteria::ISNOTNULL;
} else {
// for now throw an exception, because not sure how to interpret this
throw new PropelException("Could not build SQL for expression: $field " . $this->comparison . " NULL");
}
}
}
/**
* This method checks another Criteria to see if they contain
* the same attributes and hashtable entries.
*
* @param Criterion|null $obj
* @return boolean
*/
public function equals($obj)
{
// TODO: optimize me with early outs
if ($this === $obj) {
return true;
}
if (($obj === null) || !($obj instanceof Criterion)) {
return false;
}
$crit = $obj;
$isEquiv = ( ( ($this->table === null && $crit->getTable() === null)
|| ( $this->table !== null && $this->table === $crit->getTable() )
)
&& $this->column === $crit->getColumn()
&& $this->comparison === $crit->getComparison());
// check chained criterion
$clausesLength = count($this->clauses);
$isEquiv &= (count($crit->getClauses()) == $clausesLength);
$critConjunctions = $crit->getConjunctions();
$critClauses = $crit->getClauses();
for ($i=0; $i < $clausesLength && $isEquiv; $i++) {
$isEquiv &= ($this->conjunctions[$i] === $critConjunctions[$i]);
$isEquiv &= ($this->clauses[$i] === $critClauses[$i]);
}
if ($isEquiv) {
$isEquiv &= $this->value === $crit->getValue();
}
return $isEquiv;
}
/**
* Returns a hash code value for the object.
*/
public function hashCode()
{
$h = crc32(serialize($this->value)) ^ crc32($this->comparison);
if ($this->table !== null) {
$h ^= crc32($this->table);
}
if ($this->column !== null) {
$h ^= crc32($this->column);
}
foreach ($this->clauses as $clause) {
// TODO: i KNOW there is a php incompatibility with the following line
// but i dont remember what it is, someone care to look it up and
// replace it if it doesnt bother us?
// $clause->appendPsTo($sb='',$params=array());
$sb = '';
$params = array();
$clause->appendPsTo($sb,$params);
$h ^= crc32(serialize(array($sb,$params)));
unset ( $sb, $params );
}
return $h;
}
/**
* Get all tables from nested criterion objects
* @return array
*/
public function getAllTables()
{
$tables = array();
$this->addCriterionTable($this, $tables);
return $tables;
}
/**
* method supporting recursion through all criterions to give
* us a string array of tables from each criterion
*
* @param Criterion $c
* @param array &$s
* @return void
*/
private function addCriterionTable(Criterion $c, array &$s)
{
$s[] = $c->getTable();
foreach ( $c->getClauses() as $clause ) {
$this->addCriterionTable($clause, $s);
}
}
/**
* get an array of all criterion attached to this
* recursing through all sub criterion
* @return array Criterion[]
*/
public function getAttachedCriterion()
{
$criterions = array($this);
foreach ($this->getClauses() as $criterion) {
$criterions = array_merge($criterions, $criterion->getAttachedCriterion());
}
return $criterions;
}
/**
* Ensures deep cloning of attached objects
*/
public function __clone()
{
foreach ($this->clauses as $key => $criterion) {
$this->clauses[$key] = clone $criterion;
}
}
}
|