| Versionen | |
|---|---|
| drupal7 | public SelectQuery::__toString() |
includes/
<?php
public function __toString() {
// SELECT
$query = 'SELECT ';
if ($this->distinct) {
$query .= 'DISTINCT ';
}
// FIELDS and EXPRESSIONS
$fields = array();
foreach ($this->fields as $alias => $field) {
// Always use the AS keyword for field aliases, as some
// databases require it (e.g., PostgreSQL).
$fields[] = (isset($field['table']) ? $field['table'] . '.' : '') . $field['field'] . ' AS ' . $field['alias'];
}
foreach ($this->expressions as $alias => $expression) {
$fields[] = $expression['expression'] . ' AS ' . $expression['alias'];
}
foreach ($this->tables as $alias => $table) {
if (!empty($table['all_fields'])) {
$fields[] = $alias . '.*';
}
}
$query .= implode(', ', $fields);
// FROM - We presume all queries have a FROM, as any query that doesn't won't need the query builder anyway.
$query .= "\nFROM ";
foreach ($this->tables as $alias => $table) {
$query .= "\n";
if (isset($table['join type'])) {
$query .= $table['join type'] . ' JOIN ';
}
// If the table is a subquery, compile it and integrate it into this query.
if ($table['table'] instanceof SelectQueryInterface) {
$table_string = '(' . (string) $table['table'] . ')';
}
else {
$table_string = '{' . $this->connection->escapeTable($table['table']) . '}';
}
// Don't use the AS keyword for table aliases, as some
// databases don't support it (e.g., Oracle).
$query .= $table_string . ' ' . $table['alias'];
if (!empty($table['condition'])) {
$query .= ' ON ' . $table['condition'];
}
}
// WHERE
if (count($this->where)) {
$this->where->compile($this->connection);
// There is an implicit string cast on $this->condition.
$query .= "\nWHERE " . $this->where;
}
// GROUP BY
if ($this->group) {
$query .= "\nGROUP BY " . implode(', ', $this->group);
}
// HAVING
if (count($this->having)) {
$this->having->compile($this->connection);
// There is an implicit string cast on $this->having.
$query .= "\nHAVING " . $this->having;
}
// ORDER BY
if ($this->order) {
$query .= "\nORDER BY ";
$fields = array();
foreach ($this->order as $field => $direction) {
$fields[] = $field . ' ' . $direction;
}
$query .= implode(', ', $fields);
}
// RANGE is database specific, so we can't do it here.
return $query;
}
?>
Kommentare
Kommentar hinzufügen