View source
<?php
class SelectQuery_sqlsrv extends SelectQuery {
public function preExecute(SelectQueryInterface $query = NULL) {
if (!isset($query)) {
$query = $this;
}
if ($this
->isPrepared()) {
return TRUE;
}
parent::preExecute($query);
if ($this->distinct || $this->group) {
$columns = $this->order + array_flip($this->group);
$counter = 0;
foreach ($columns as $field => $dummy) {
if (!isset($this->fields[$field]) && !isset($this->expressions[$field])) {
$alias = '_field_' . $counter++;
$this
->addExpression($field, $alias);
$this->queryOptions['sqlsrv_drop_columns'][] = $alias;
}
}
foreach ($this->group as $key => &$group_field) {
if (isset($this->fields[$group_field])) {
$field = $this->fields[$group_field];
$group_field = (isset($field['table']) ? $this->connection
->escapeTable($field['table']) . '.' : '') . $this->connection
->escapeField($field['field']);
}
else {
if (isset($this->expressions[$group_field])) {
$expression = $this->expressions[$group_field];
$group_field = $expression['expression'];
if (is_array($expression['arguments'])) {
$this->queryOptions['insecure'] = TRUE;
}
}
}
}
}
return $this->prepared;
}
public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder) {
$this->inSubQuery = $queryPlaceholder != $this;
return parent::compile($connection, $queryPlaceholder);
}
public function __toString() {
if (!$this
->compiled()) {
$this
->compile($this->connection, $this);
}
$comments = $this->connection
->makeComment($this->comments);
$query = $comments . 'SELECT ';
if ($this->distinct) {
$query .= 'DISTINCT ';
}
$fields = array();
foreach ($this->tables as $alias => $table) {
if (!empty($table['all_fields'])) {
$fields[] = $this->connection
->escapeTable($alias) . '.*';
}
}
foreach ($this->fields as $alias => $field) {
$fields[] = (isset($field['table']) ? $this->connection
->escapeTable($field['table']) . '.' : '') . $this->connection
->escapeField($field['field']) . ' AS ' . $this->connection
->escapeField($field['alias']);
}
foreach ($this->expressions as $alias => $expression) {
$fields[] = $expression['expression'] . ' AS ' . $expression['alias'];
}
$query .= implode(', ', $fields);
$query .= "\nFROM ";
foreach ($this->tables as $alias => $table) {
$query .= "\n";
if (isset($table['join type'])) {
$query .= $table['join type'] . ' JOIN ';
}
if ($table['table'] instanceof SelectQueryInterface) {
$subquery = $table['table'];
$subquery
->preExecute();
$table_string = '(' . (string) $subquery . ')';
}
else {
$table_string = '{' . $this->connection
->escapeTable($table['table']) . '}';
}
$query .= $table_string . ' ' . $this->connection
->escapeTable($table['alias']);
if (!empty($table['condition'])) {
$query .= ' ON ' . $table['condition'];
}
}
if (count($this->where)) {
$query .= "\nWHERE ( " . $this->where . " )";
}
if ($this->group) {
$query .= "\nGROUP BY " . implode(', ', $this->group);
}
if (count($this->having)) {
$query .= "\nHAVING " . $this->having;
}
if ($this->order && (empty($this->inSubQuery) || !empty($this->range))) {
$query .= "\nORDER BY ";
$fields = array();
foreach ($this->order as $field => $direction) {
$fields[] = $field . ' ' . $direction;
}
$query .= implode(', ', $fields);
}
if (!empty($this->range)) {
$query = $this->connection
->addRangeToQuery($query, $this->range['start'], $this->range['length']);
}
if ($this->union) {
foreach ($this->union as $union) {
$query .= ' ' . $union['type'] . ' ' . (string) $union['query'];
}
}
return $query;
}
public function orderRandom() {
$alias = $this
->addExpression('NEWID()', 'random_field');
$this
->orderBy($alias);
return $this;
}
}