View source
<?php
class InsertQuery_sqlsrv extends InsertQuery {
public function execute() {
if (!$this
->preExecute()) {
return NULL;
}
$columnInformation = $this->connection
->schema()
->queryColumnInformation($this->table);
foreach ($this->insertFields as $field) {
if (isset($columnInformation['identities'][$field])) {
$this->setIdentity = TRUE;
}
}
if (empty($this->queryOptions['sqlsrv_skip_transactions'])) {
$transaction = $this->connection
->startTransaction();
}
if (!empty($this->fromQuery)) {
$this->insertValues = array();
$stmt = $this->connection
->PDOPrepare($this->connection
->prefixTables((string) $this));
$values = $this->fromQuery
->getArguments();
foreach ($values as $key => $value) {
$stmt
->bindParam($key, $values[$key]);
}
try {
$stmt
->execute();
} catch (Exception $e) {
if (!empty($transaction)) {
$transaction
->rollback();
}
throw $e;
}
return $this->connection
->lastInsertId();
}
if (empty($this->fromQuery) && (empty($this->insertFields) || empty($this->insertValues))) {
$this->insertValues = array();
$stmt = $this->connection
->PDOPrepare($this->connection
->prefixTables('INSERT INTO {' . $this->table . '} DEFAULT VALUES'));
try {
$stmt
->execute();
} catch (Exception $e) {
if (!empty($transaction)) {
$transaction
->rollback();
}
throw $e;
}
return $this->connection
->lastInsertId();
}
$query = (string) $this;
$stmt = $this->connection
->PDOPrepare($this->connection
->prefixTables($query));
$data_values = array();
foreach ($this->insertValues as $insert_index => $insert_values) {
$max_placeholder = 0;
foreach ($this->insertFields as $field_index => $field) {
$placeholder = ':db_insert' . $max_placeholder++;
if (isset($columnInformation['blobs'][$field])) {
$data_values[$placeholder . $insert_index] = fopen('php://memory', 'a');
fwrite($data_values[$placeholder . $insert_index], $insert_values[$field_index]);
rewind($data_values[$placeholder . $insert_index]);
$stmt
->bindParam($placeholder, $data_values[$placeholder . $insert_index], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
}
else {
$data_values[$placeholder . $insert_index] = $insert_values[$field_index];
$stmt
->bindParam($placeholder, $data_values[$placeholder . $insert_index]);
}
}
try {
$stmt
->execute();
} catch (Exception $e) {
if (!empty($transaction)) {
$transaction
->rollback();
}
throw $e;
}
$last_insert_id = $this->connection
->lastInsertId();
}
$this->insertValues = array();
return $last_insert_id;
}
public function __toString() {
$prefix = $this->connection
->makeComment($this->comments);
if (!empty($this->setIdentity)) {
$prefix .= 'SET IDENTITY_INSERT {' . $this->table . '} ON;';
}
if (!empty($this->fromQuery)) {
return $prefix . "INSERT INTO {" . $this->table . '} (' . implode(', ', $this->connection
->quoteIdentifiers($this->insertFields)) . ') ' . $this->fromQuery;
}
$placeholders = array();
for ($i = 0; $i < count($this->insertFields); ++$i) {
$placeholders[] = ':db_insert' . $i;
}
return $prefix . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $this->connection
->quoteIdentifiers($this->insertFields)) . ') VALUES (' . implode(', ', $placeholders) . ')';
}
}
class UpdateQuery_sqlsrv extends UpdateQuery {
protected function excludeNonAffectedRows() {
if (!empty($this->queryOptions['sqlsrv_return_matched_rows'])) {
return;
}
$fields = $this->expressionFields + $this->fields;
$condition = new DatabaseCondition('OR');
foreach ($fields as $field => $data) {
if (is_array($data)) {
$expression = $data['expression'];
$arguments = array();
if (!empty($data['arguments'])) {
foreach ($data['arguments'] as $placeholder => $value) {
$new_placeholder = ':db_exclude_placeholder_' . $this
->nextPlaceholder();
$expression = str_replace($placeholder, $new_placeholder, $expression);
$arguments[$new_placeholder] = $value;
}
}
$condition
->where($field . ' <> ' . $expression, $arguments);
$condition
->isNull($field);
}
elseif (!isset($data)) {
$condition
->isNotNull($field);
}
else {
$condition
->condition($field, $data, '<>');
$condition
->isNull($field);
}
}
if (count($condition)) {
$this->nextPlaceholder = 1000000;
$this
->condition($condition);
}
}
public function execute() {
$this
->excludeNonAffectedRows();
$max_placeholder = 0;
$stmt = $this->connection
->PDOPrepare($this->connection
->prefixTables((string) $this));
$columnInformation = $this->connection
->schema()
->queryColumnInformation($this->table);
$fields = $this->fields;
$expression_fields = array();
foreach ($this->expressionFields as $field => $data) {
if (!empty($data['arguments'])) {
foreach ($data['arguments'] as $placeholder => $argument) {
$stmt
->bindParam($placeholder, $data['arguments'][$placeholder]);
}
}
unset($fields[$field]);
}
$blobs = array();
$blob_count = 0;
foreach ($fields as $field => $value) {
$placeholder = ':db_update_placeholder_' . $max_placeholder++;
if (isset($columnInformation['blobs'][$field])) {
$blobs[$blob_count] = fopen('php://memory', 'a');
fwrite($blobs[$blob_count], $value);
rewind($blobs[$blob_count]);
$stmt
->bindParam($placeholder, $blobs[$blob_count], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
$blob_count++;
}
else {
$stmt
->bindParam($placeholder, $fields[$field]);
}
}
if (count($this->condition)) {
$this->condition
->compile($this->connection, $this);
$arguments = $this->condition
->arguments();
foreach ($arguments as $placeholder => $value) {
$stmt
->bindParam($placeholder, $arguments[$placeholder]);
}
}
$options = $this->queryOptions;
$options['already_prepared'] = TRUE;
$stmt
->execute();
return $stmt
->rowCount();
}
public function __toString() {
$prefix = $this->connection
->makeComment($this->comments);
$fields = $this->fields;
$update_fields = array();
foreach ($this->expressionFields as $field => $data) {
$update_fields[] = $this->connection
->quoteIdentifier($field) . '=' . $data['expression'];
unset($fields[$field]);
}
$max_placeholder = 0;
foreach ($fields as $field => $value) {
$update_fields[] = $this->connection
->quoteIdentifier($field) . '=:db_update_placeholder_' . $max_placeholder++;
}
$query = $prefix . 'UPDATE {' . $this->connection
->escapeTable($this->table) . '} SET ' . implode(', ', $update_fields);
if (count($this->condition)) {
$this->condition
->compile($this->connection, $this);
$query .= "\nWHERE " . $this->condition;
}
return $query;
}
}
class TruncateQuery_sqlsrv extends TruncateQuery {
public function __toString() {
$prefix = $this->connection
->makeComment($this->comments);
return $prefix . 'TRUNCATE TABLE {' . $this->connection
->escapeTable($this->table) . '} ';
}
}
class MergeQuery_sqlsrv extends MergeQuery {
public function execute() {
$this->queryOptions['sqlsrv_skip_transactions'] = TRUE;
return parent::execute();
}
}