/** * @return string */ protected function _renderColumnsClauseToString() { $columns = array(); foreach ($this->_parts[self::COLUMNS] as $columnEntry) { list($correlationName, $column, $alias) = $columnEntry; if ($column instanceof Zend_Db_Expr) { $columns[] = $this->_adapter->quoteColumnAs($column, $alias, true); } else { if ($column == '*') { $column = new Zend_Db_Expr('*'); $alias = null; } if (empty($correlationName)) { $columns[] = $this->_adapter->quoteColumnAs($column, $alias, true); } else { $columns[] = $this->_adapter->quoteColumnAs(array($correlationName, $column), $alias, true); } } } return implode(', ', $columns); }
/** * Render DISTINCT clause * * @param string $sql SQL query * @return string */ protected function _renderColumns($sql) { if (!count($this->_parts[self::COLUMNS])) { return null; } $columns = array(); foreach ($this->_parts[self::COLUMNS] as $columnEntry) { list($correlationName, $column, $alias) = $columnEntry; if ($column instanceof Zend_Db_Expr) { $columns[] = $this->_adapter->quoteColumnAs($column, $alias, true); } else { if ($column == self::SQL_WILDCARD) { $column = new Zend_Db_Expr(self::SQL_WILDCARD); $alias = null; } if (empty($correlationName)) { $columns[] = $this->_adapter->quoteColumnAs($column, $alias, true); } else { $columns[] = $this->_adapter->quoteColumnAs(array($correlationName, $column), $alias, true); } } } return $sql .= ' ' . implode(', ', $columns); }
/** * Quote a column identifier and alias. * * @param string|array|Zend_Db_Expr $ident The identifier or expression. * @param string $alias An alias for the column. * @param boolean $auto (optional) If true, heed the AUTO_QUOTE_IDENTIFIERS config option. * @return string The quoted identifier and alias. */ public function quoteColumnAs($ident, $alias, $auto = false) { return $this->_adapter->quoteColumnAs($ident, $alias, $auto); }
/** * Converts this object to an SQL SELECT string. * * @todo use $this->_adapter->quoteColumns() for non-PDO adapters * @todo use $this->_adapter->quoteTableNames() for non-PDO adapters * @todo use prepared queries for PDO adapters instead of constructing all the SQL ourselves * like in Adapter/Abstract.php.html:query() * @return string This object as a SELECT string. */ public function __toString() { // initial SELECT [DISTINCT] [FOR UPDATE] $sql = "SELECT"; if ($this->_parts[self::DISTINCT]) { $sql .= " DISTINCT"; } if ($this->_parts[self::FOR_UPDATE]) { $sql .= " FOR UPDATE"; } $sql .= "\n\t"; // add columns if ($this->_parts[self::COLUMNS]) { $columns = array(); foreach ($this->_parts[self::COLUMNS] as $correlationName => $columnList) { foreach ($columnList as $alias => $column) { if (!is_string($alias)) { $alias = null; } if ($column instanceof Zend_Db_Expr) { $columns[] = $this->_adapter->quoteColumnAs($column, $alias); } else { if ($column == '*') { $column = new Zend_Db_Expr('*'); $alias = null; } if (empty($correlationName)) { $columns[] = $this->_adapter->quoteColumnAs($column, $alias); } else { $columns[] = $this->_adapter->quoteColumnAs(array($correlationName, $column), $alias); } } } } $sql .= implode(",\n\t", $columns); } // from these joined tables if ($this->_parts[self::FROM]) { $from = array(); // array_pop() foreach ($this->_parts[self::FROM] as $correlationName => $table) { $tmp = ''; if (empty($from)) { // First table is named alone ignoring join information $tmp .= $this->_adapter->quoteTableAs($table['tableName'], $correlationName); } else { // Subsequent tables may have joins if (!empty($table['joinType'])) { $tmp .= ' ' . strtoupper($table['joinType']) . ' '; } $tmp .= $this->_adapter->quoteTableAs($table['tableName'], $correlationName); if (!empty($table['joinCondition'])) { $tmp .= ' ON ' . $table['joinCondition']; } } // add the table name and condition // add to the list $from[] = $tmp; } // add the list of all joins if (!empty($from)) { $sql .= "\nFROM " . implode("\n", $from); } // with these where conditions if ($this->_parts[self::WHERE]) { $sql .= "\nWHERE\n\t"; $sql .= implode("\n\t", $this->_parts[self::WHERE]); } // grouped by these columns if ($this->_parts[self::GROUP]) { $sql .= "\nGROUP BY\n\t"; $l = array(); foreach ($this->_parts[self::GROUP] as $term) { $l[] = $this->_adapter->quoteIdentifier($term); } $sql .= implode(",\n\t", $l); } // having these conditions if ($this->_parts[self::HAVING]) { $sql .= "\nHAVING\n\t"; $sql .= implode("\n\t", $this->_parts[self::HAVING]); } } // ordered by these columns if ($this->_parts[self::ORDER]) { $sql .= "\nORDER BY\n\t"; $l = array(); foreach ($this->_parts[self::ORDER] as $term) { if (is_array($term)) { $l[] = $this->_adapter->quoteIdentifier($term[0]) . ' ' . $term[1]; } else { $l[] = $this->_adapter->quoteIdentifier($term); } } $sql .= implode(",\n\t", $l); } // determine offset $count = 0; $offset = 0; if (!empty($this->_parts[self::LIMIT_OFFSET])) { $offset = (int) $this->_parts[self::LIMIT_OFFSET]; // this should be reduced to the max integer PHP can support $count = intval(9223372036854775807); } // determine count if (!empty($this->_parts[self::LIMIT_COUNT])) { $count = (int) $this->_parts[self::LIMIT_COUNT]; } // add limits clause if ($count > 0) { $sql .= "\n"; $sql = trim($this->_adapter->limit($sql, $count, $offset)); } return $sql; }