public function Select::preExecute in Drupal driver for SQL Server and SQL Azure 8.2
Same name and namespace in other branches
- 8 drivers/lib/Drupal/Driver/Database/sqlsrv/Select.php \Drupal\Driver\Database\sqlsrv\Select::preExecute()
- 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Select.php \Drupal\Driver\Database\sqlsrv\Select::preExecute()
Override for SelectQuery::preExecute().
Ensure that all the fields in ORDER BY and GROUP BY are part of the main query.
Overrides Select::preExecute
File
- drivers/
lib/ Drupal/ Driver/ Database/ sqlsrv/ Select.php, line 56 - Definition of Drupal\Driver\Database\sqlsrv\Select
Class
Namespace
Drupal\Driver\Database\sqlsrvCode
public function preExecute(DatabaseSelectInterface $query = null) {
// If no query object is passed in, use $this.
if (!isset($query)) {
$query = $this;
}
// Only execute this once.
if ($this
->isPrepared()) {
return true;
}
// Execute standard pre-execution first.
parent::preExecute($query);
if ($this->distinct || $this->group) {
// When the query is DISTINCT or contains GROUP BY fields, all the fields
// in the GROUP BY and ORDER BY clauses must appear in the returned
// columns.
$columns = $this->order + array_flip($this->group);
$counter = 0;
foreach ($columns as $field => $dummy) {
$found = false;
foreach ($this->fields as $f) {
if (!isset($f['table']) || !isset($f['field'])) {
continue;
}
$alias = "{$f['table']}.{$f['field']}";
if ($alias == $field) {
$found = true;
break;
}
}
if (!isset($this->fields[$field]) && !isset($this->expressions[$field]) && !$found) {
$alias = '_field_' . $counter++;
$this
->addExpression($field, $alias, [], false, false);
$this->queryOptions['sqlsrv_drop_columns'][] = $alias;
}
}
// The other way round is also true, if using aggregates, all the fields in the SELECT
// must be present in the GROUP BY.
if (!empty($this->group)) {
foreach ($this->fields as $field) {
$spec = $field['table'] . '.' . $field['field'];
$alias = $field['alias'];
if (!isset($this->group[$spec]) && !isset($this->group[$alias])) {
$this->group[$spec] = $spec;
}
}
}
// More over, GROUP BY columns cannot use aliases, so expand them to
// their full expressions.
foreach ($this->group as $key => &$group_field) {
// Expand an alias on a 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']);
}
elseif (isset($this->expressions[$group_field])) {
$expression = $this->expressions[$group_field];
$group_field = $expression['expression'];
// If the expression has arguments, we now
// have duplicate placeholders. Run as insecure.
if (is_array($expression['arguments'])) {
$this->queryOptions['insecure'] = true;
}
}
}
}
// Verify type bindings in the conditions, and throw the Exception
// now to prevent a bug in MSSQLPDO where transactions are f**** UP
// when the driver throws a PDOException().
// @see https://github.com/Azure/msphpsql/issues/50
// TODO: Remove when the issue is fixed in the PDO driver.
//if (DatabaseUtils::GetConfigBoolean('MSSQL_VERIFY_NUMERIC_BINDINGS')) {
// foreach($this->where->conditions() as $condition) {
// if (!isset($condition['field']) || !is_string($condition['field'])) {
// continue;
// }
// // Make sure we have a valid $table.$field format.
// $parts = explode('.', $condition['field']);
// if (count($parts) !== 2) {
// continue;
// }
// list($table, $field_alias) = $parts;
// // Fin the real field name if this was an alias.
// $fields = $this->getFields();
// $field = $field_alias;
// if (isset($fields[$field_alias])) {
// $field = $fields[$field_alias]['field'];
// }
// // Get the real table name.
// $tables = $this->getTables();
// if (!isset($tables[$table])) {
// continue;
// }
// $real_table = $tables[$table]['table'];
// /** @var DatabaseSchema_sqlsrv **/
// $schema = $this->connection->schema();
// $spec = $schema->queryColumnInformation($real_table);
// $col_spec = $spec['columns'][$field];
// $values = $condition['value'];
// if (!is_array($values)) {
// $values = array($values);
// }
// foreach($values as $value) {
// if (!is_numeric($value) && !is_bool($value)) {
// if (in_array($col_spec['type'], array('int', 'bigint'))) {
// // This is anyways going to throw an exception when running the query against the PDO driver.
// throw new \PDOException('Invalid type binding!');
// }
// }
// }
// }
//}
return $this->prepared;
}