Overview

Namespaces

  • Alchemy
    • core
      • query
      • schema
    • dialect
    • engine
    • orm
    • tests
    • util
      • promise
  • PHP

Classes

  • ColumnRef
  • DDLQuery
  • Expression
  • Insert
  • Join
  • Predicate
  • Query
  • Scalar
  • TableRef

Interfaces

  • IQuery
  • IQueryFragment
  • IQueryValue
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace Alchemy\core\query;
  4: use Alchemy\core\Element;
  5: use Alchemy\util\Monad;
  6: 
  7: 
  8: /**
  9:  * Represents a generalized SQL query
 10:  */
 11: class Query extends Element implements IQuery {
 12:     protected $columns = array();
 13:     protected $joins = array();
 14:     protected $where;
 15:     protected $limit;
 16:     protected $offset;
 17:     protected $table;
 18: 
 19: 
 20:     public function __construct($type, TableRef $table) {
 21:         parent::__construct($type);
 22:         $this->table = $table;
 23:     }
 24: 
 25: 
 26:     public function __set($name, $value) {
 27:         if (!($value instanceof Element) || !$value->getTag('expr.value')) {
 28:             $schema = $this->table->schema();
 29:             $value = $schema->hasColumn($name)
 30:                 ? $schema->getColumn($name)->encode($value)
 31:                 : new Scalar($value);
 32:         }
 33: 
 34:         $this->columns[$name] = $value;
 35:     }
 36: 
 37: 
 38:     /**
 39:      * Add multiple columns to the query by providing
 40:      * multiple arguments. See {@link Query::column()}
 41:      * Pass, ColumnRefs, ["Name", Value] pairs, or a
 42:      * single array of either as necessary.
 43:      *
 44:      * @param  array|ColumnRef
 45:      *         ...
 46:      * @return array           current column list if no arguments
 47:      */
 48:     public function columns() {
 49:         if (func_num_args() == 0) {
 50:             return $this->columns;
 51:         }
 52: 
 53:         $columns = func_get_args();
 54:         $columns = (count($columns) == 1 && is_array($columns[0])) ? $columns[0] : $columns;
 55: 
 56:         foreach ($columns as $name => $column) {
 57:             if (is_integer($name) && is_array($column)) {
 58:                 $name   = $column[0];
 59:                 $column = $column[1];
 60:             }
 61: 
 62:             $name = is_string($name) ? $name : $column->name();
 63:             $this->__set($name, $column);
 64:         }
 65: 
 66:         return $this;
 67:     }
 68: 
 69: 
 70:     /**
 71:      * Recursively get all scalar parameters used by this expression
 72:      *
 73:      * @return array array(Scalar, Scalar, ...)
 74:      */
 75:     public function parameters() {
 76:         $params = $this->where ? $this->where->parameters() : array();
 77: 
 78:         if ($this->limit) {
 79:             $params[] = $this->limit;
 80:         }
 81: 
 82:         if ($this->offset) {
 83:             $params[] = $this->offset;
 84:         }
 85: 
 86:         foreach ($this->columns as $column) {
 87:             if ($column instanceof Scalar) {
 88:                 $params[] = $column;
 89:             }
 90:         }
 91: 
 92:         foreach ($this->joins as $join) {
 93:             $params = array_merge($params, $join->parameters());
 94:         }
 95: 
 96:         return $params;
 97:     }
 98: 
 99: 
100:     /**
101:      * Add a join to the query
102:      *
103:      * @param Table $table
104:      * @param Predicate $on
105:      * @param $direction Optional join direction
106:      * @param $type Optional join type
107:      */
108:     public function join($table, Predicate $on = null, $direction = null, $type = null) {
109:         $direction = $direction ?: Join::LEFT;
110:         $type = $type ?: Join::INNER;
111: 
112:         $this->joins[] = new Join($direction, $type, $table, $on);
113:         return $this;
114:     }
115: 
116: 
117:     public function joins() {
118:         return $this->joins;
119:     }
120: 
121: 
122:     /**
123:      * Shortcut for doing an OUTER JOIN
124:      *
125:      * @param Table $table
126:      * @param Predicate $on
127:      * @param $direction Optional join direction
128:      */
129:     public function outerJoin($table, Predicate $on, $direction = null) {
130:         return $this->join($table, $on, $direction, Join::OUTER);
131:     }
132: 
133: 
134:     /**
135:      * Return the table this query applies to
136:      *
137:      * @return TableRef $table
138:      */
139:     public function table() {
140:         return $this->table;
141:     }
142: 
143: 
144:     /**
145:      * Set the Query's WHERE expression. Calling this
146:      * multiple times will overwrite the previous expressions.
147:      * You should instead call this once with a CompoundExpression.
148:      *
149:      * @param Expression $expr
150:      */
151:     public function where($expr = false) {
152:         if ($expr === false) {
153:             return $this->where;
154:         }
155: 
156:         $this->where = is_null($expr) ? null : Predicate::ALL(func_get_args());
157:         return $this;
158:     }
159: 
160: 
161:     /**
162:      * Limit number of rows affected by query.
163:      *
164:      * @param integer $limit Query limit.
165:      */
166:     public function limit($limit = false) {
167:         if ($limit === false) {
168:             return $this->limit;
169:         }
170: 
171:         $this->limit = is_null($limit) ? null : new Scalar($limit);
172:         return $this;
173:     }
174: 
175: 
176:     /**
177:      * Offset start of rows affected by query.
178:      *
179:      * @param integer $offset Query offset.
180:      */
181:     public function offset($offset = false) {
182:         if ($offset === false) {
183:             return $this->offset;
184:         }
185: 
186:         $this->offset = is_null($offset) ? null : new Scalar($offset);
187:         return $this;
188:     }
189: }
190: 
API documentation generated by ApiGen 2.8.0