Skip to content
Permalink
Browse files

Refactoring for code reuse

  • Loading branch information...
lorenzo committed Jan 10, 2013
1 parent 2db5305 commit e0ac0e7560730fa440d01335c8c9df901454546b
Showing with 23 additions and 44 deletions.
  1. +23 −44 lib/Cake/Model/Datasource/Database/Query.php
@@ -292,60 +292,20 @@ public function join($tables = null, $types = [], $overwrite = false) {
}
public function where($conditions = null, $types = [], $overwrite = false) {
$where = $this->_parts['where'] ?: $this->newExpr();
if ($conditions === null) {
return $where;
}
if (is_callable($conditions)) {
$this->_parts['where'] = $conditions($where, $this);
return $this;
}
if ($overwrite) {
$this->_parts['where'] = $this->newExpr()->add($conditions, $types);
} else {
$where->add($conditions, $types);
$this->_parts['where'] = $this->newExpr();
}
$this->_parts['where'] = $where;
$this->_dirty = true;
$this->_conjugate('where', $conditions, 'AND', $types);
return $this;
}
public function andWhere($conditions, $types = []) {
$where = $this->_parts['where'] ?: $this->newExpr();
if (is_callable($conditions)) {
$conditions = $conditions($this->newExpr(), $this);
}
if ($where->type() === 'AND') {
$where->add($conditions, $types);
} else {
$where = $this->newExpr()->add([$conditions, $where], $types);
}
$this->_parts['where'] = $where;
$this->_dirty = true;
$this->_conjugate('where', $conditions, 'AND', $types);
return $this;
}
public function orWhere($conditions, $types = []) {
$where = $this->_parts['where'] ?: $this->newExpr()->type('OR');
if (is_callable($conditions)) {
$conditions = $conditions($this->newExpr(), $this);
}
if ($where->type() === 'OR') {
$where->add($conditions, $types);
} else {
$where = $this->newExpr()->type('OR')->add([$conditions, $where], $types);
}
$this->_parts['where'] = $where;
$this->_dirty = true;
$this->_conjugate('where', $conditions, 'OR', $types);
return $this;
}
@@ -437,6 +397,25 @@ public function getIterator() {
return $this->_iterator;
}
protected function _conjugate($part, $append, $conjunction, $types) {
$expression = $this->_parts[$part] ?: $this->newExpr();
if (is_callable($append)) {
$append = $append($this->newExpr(), $this);
}
if ($expression->type() === $conjunction) {
$expression->add($append, $types);
} else {
$expression = $this->newExpr()
->type($conjunction)
->add([$append, $expression], $types);
}
$this->_parts[$part] = $expression;
$this->_dirty = true;
}
/**
* Returns string representation of this query (complete SQL statement)
*

0 comments on commit e0ac0e7

Please sign in to comment.
You can’t perform that action at this time.