public function queryToString(SimDAL_Query $query) { $wheres = array(); foreach ($query->getWheres() as $where) { $wheres[] = $this->processWhere($where); } $limit = $this->processQueryLimit($query->getLimit()->getLimit(), $query->getLimit()->getOffset(), $query); $columns = null; if ($query->getType() == SimDAL_Query::TYPE_SELECT) { $columns = $this->processQueryPrimaryTableColumns($query); } $joins = $this->processQueryJoins($query, $columns); $from = $query->getFrom(); $schema = $query->getSchema(); if (!is_null($schema) && !empty($schema)) { $from = $query->getSchema() . '.' . $from; } switch ($query->getType()) { case SimDAL_Query::TYPE_SELECT: $sql = 'SELECT ' . implode(', ', $columns) . ' '; $sql .= 'FROM ' . $from; $sql .= $joins; break; case SimDAL_Query::TYPE_DELETE: $sql = 'DELETE '; $sql .= 'FROM ' . $from; $sql .= $joins; break; case SimDAL_Query::TYPE_UPDATE: $sql = 'UPDATE ' . $from; $sql .= $joins; $sets = $this->processWhereSets($query); $sql .= ' SET ' . implode(', ', $sets); } if (count($wheres) > 0) { $sql .= ' WHERE ' . implode(' AND ', $wheres); } if ($query->getOrderBy() !== null) { $sql .= ' ' . $this->processOrderBy($query->getOrderBy(), $query); } $sql .= ' ' . $limit; return $sql; }
protected function _countLoadedEntities(SimDAL_Query $query) { $count = 0; foreach ($this->_data as $loaded) { /* @var $where SimDAL_Query_Where */ foreach ($query->getWheres() as $where) { if (!$where instanceof SimDAL_Query_Where_Column) { throw new Exception('Only column where are implemented for counting'); } $column = $where->getLeftValue(); $getter = 'get' . ucfirst($column->getProperty()); $value = $where->getRightValue(); if ($loaded->{$getter}() != $value) { continue 2; } } $count++; } return $count; }
protected function _queryToString(SimDAL_Query $query) { $sql = 'SELECT * FROM ' . $query->getFrom(); foreach ($query->getJoins() as $join) { $sql .= $join->getJoinType() . ' ' . $join->getTable() . ' ON '; foreach ($join->getWheres() as $where) { $method = '_process' . $where->getProcessMethod(); $sql = $this->{$method}($where); } } $wheres = array(); foreach ($query->getWheres() as $where) { $method = '_process' . $where->getProcessMethod(); $wheres[] = $this->{$method}($where); } if (count($wheres) > 0) { $sql .= ' WHERE ' . implode(' AND ', $wheres); } return $sql; }