protected function visitSelect(Select $select) { $this->selectCount++; $columns = array(); foreach ($select->getColumns() as $c) { $columns[] = $this->process($c); } $froms = array(); foreach ($select->getFroms() as $f) { $froms[] = $this->process($f); } $sql = 'SELECT ' . ($select->distinct ? 'DISTINCT ' : '') . implode(', ', $columns) . ' FROM ' . implode(', ', $froms); $where = $select->whereClause; $orderBy = $select->orderByClause; $limit = $this->getLimitClause($select->offset, $select->limit); if ($where !== null) { $sql .= ' WHERE ' . $this->process($where); } if ($orderBy !== null) { $sql .= ' ORDER BY ' . $this->process($orderBy); } if (!empty($limit)) { $sql .= $limit; } if ($this->isSubquery()) { $sql = '(' . $sql . ')'; } $this->selectCount--; return $sql; }
public function read(Query $query) { $metaclass = $query->getMetaclass(); $table = $this->getTable($metaclass->getStorageName()); $select = new Select(array($table)); $conditions = array(); foreach ($query->getConditions() as $condition) { $column = $this->getColumnFromProperty($table, $condition->subject); $newCondition = $column->op($condition->operator, $condition->value); if ($condition->negated) { $newCondition->negate(); } $conditions[] = $newCondition; } if (count($conditions) !== 0) { call_user_func_array(array($select, 'where'), $conditions); } $sorts = array(); foreach ($query->getSorts() as $sort) { $column = $this->getColumnFromProperty($table, $sort->subject); $newSort = $sort->ascending ? $column->asc() : $column->desc(); $sorts[] = $newSort; } if (count($sorts) !== 0) { call_user_func_array(array($select, 'orderBy'), $sorts); } $limit = $query->getLimit(); if ($limit) { $select->limit($limit); } $offset = $query->getOffset(); if ($offset) { $select->offset($offset); } return $this->connection->execute($select); }