query.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. /**
  3. * SQL Server-specific implementation of INSERT.
  4. *
  5. * SQL Server doesn't supports multi-insert queries, and needs special handling for
  6. * binary columns.
  7. */
  8. class InsertQuery_sqlsrv extends InsertQuery {
  9. public function execute() {
  10. if (!$this->preExecute()) {
  11. return NULL;
  12. }
  13. // Fetch the list of blobs and sequences used on that table.
  14. $columnInformation = $this->connection->schema()->queryColumnInformation($this->table);
  15. // Find out if there is an identity field set in this insert.
  16. $this->setIdentity = !empty($columnInformation['identity']) && in_array($columnInformation['identity'], $this->insertFields);
  17. $identity = !empty($columnInformation['identity']) ? $columnInformation['identity'] : NULL;
  18. // Retrieve query options.
  19. $options = $this->queryOptions;
  20. #region Select Based Insert
  21. if (!empty($this->fromQuery)) {
  22. // Re-initialize the values array so that we can re-use this query.
  23. $this->insertValues = array();
  24. $stmt = $this->connection->prepareQuery((string) $this);
  25. // Handle the case of SELECT-based INSERT queries first.
  26. $arguments = $this->fromQuery->getArguments();
  27. DatabaseUtils::BindArguments($stmt, $arguments);
  28. // Run the query
  29. $this->connection->query($stmt, array(), $options);
  30. // We can only have 1 identity column per table (or none, where fetchColumn will fail)
  31. try {
  32. return $stmt->fetchColumn(0);
  33. }
  34. catch(\PDOException $e) {
  35. return NULL;
  36. }
  37. }
  38. #endregion
  39. #region Inserts with no values (full defaults)
  40. // Handle the case of full-default queries.
  41. if (empty($this->fromQuery) && (empty($this->insertFields) || empty($this->insertValues))) {
  42. // Re-initialize the values array so that we can re-use this query.
  43. $this->insertValues = array();
  44. $stmt = $this->connection->prepareQuery((string) $this);
  45. // Run the query
  46. $this->connection->query($stmt, array(), $options);
  47. // We can only have 1 identity column per table (or none, where fetchColumn will fail)
  48. try {
  49. return $stmt->fetchColumn(0);
  50. }
  51. catch(\PDOException $e) {
  52. return NULL;
  53. }
  54. }
  55. #endregion
  56. #region Regular Inserts
  57. // Each insert happens in its own query. However, we wrap it in a transaction
  58. // so that it is atomic where possible.
  59. $transaction = NULL;
  60. $batch_size = 200;
  61. // At most we can process in batches of 250 elements.
  62. $batch = array_splice($this->insertValues, 0, $batch_size);
  63. // If we are going to need more than one batch for this... start a transaction.
  64. if (empty($this->queryOptions['sqlsrv_skip_transactions']) && !empty($this->insertValues)) {
  65. $transaction = $this->connection->startTransaction('', DatabaseTransactionSettings::GetBetterDefaults());
  66. }
  67. while (!empty($batch)) {
  68. // Give me a query with the amount of batch inserts.
  69. $query = (string) $this->__toString2(count($batch));
  70. // Prepare the query.
  71. $stmt = $this->connection->prepareQuery($query);
  72. // We use this array to store references to the blob handles.
  73. // This is necessary because the PDO will otherwise messes up with references.
  74. $blobs = array();
  75. $max_placeholder = 0;
  76. foreach ($batch as $insert_index => $insert_values) {
  77. $values = array_combine($this->insertFields, $insert_values);
  78. DatabaseUtils::BindValues($stmt, $values, $blobs, ':db_insert', $columnInformation, $max_placeholder, $insert_index);
  79. }
  80. // Run the query
  81. $this->connection->query($stmt, array(), array_merge($options, array('fetch' => PDO::FETCH_ASSOC)));
  82. // We can only have 1 identity column per table (or none, where fetchColumn will fail)
  83. // When the column does not have an identity column, no results are thrown back.
  84. foreach($stmt as $insert) {
  85. try {
  86. $this->inserted_keys[] = $insert[$identity];
  87. }
  88. catch(\Exception $e) {
  89. $this->inserted_keys[] = NULL;
  90. }
  91. }
  92. // Fetch the next batch.
  93. $batch = array_splice($this->insertValues, 0, $batch_size);
  94. }
  95. // If we started a transaction, commit it.
  96. if ($transaction) {
  97. $transaction->commit();
  98. }
  99. // Re-initialize the values array so that we can re-use this query.
  100. $this->insertValues = array();
  101. // Return the last inserted key.
  102. return empty($this->inserted_keys) ? NULL : end($this->inserted_keys);
  103. #endregion
  104. }
  105. // Because we can handle multiple inserts, give
  106. // an option to retrieve all keys.
  107. public $inserted_keys = array();
  108. public function __toString() {
  109. return $this->__toString2(1);
  110. }
  111. /**
  112. * The aspect of the query depends on the batch size...
  113. *
  114. * @param mixed $batch_size
  115. * @throws Exception
  116. * @return string
  117. */
  118. private function __toString2($batch_size) {
  119. // Make sure we don't go crazy with this numbers.
  120. if ($batch_size > 250) {
  121. throw new Exception("MSSQL Native Batch Insert limited to 250.");
  122. }
  123. // Fetch the list of blobs and sequences used on that table.
  124. $columnInformation = $this->connection->schema()->queryColumnInformation($this->table);
  125. // Create a sanitized comment string to prepend to the query.
  126. $prefix = $this->connection->makeComment($this->comments);
  127. $output = NULL;
  128. // Enable direct insertion to identity columns if necessary.
  129. if (!empty($this->setIdentity)) {
  130. $prefix .= 'SET IDENTITY_INSERT {' . $this->table . '} ON;';
  131. }
  132. // Using PDO->lastInsertId() is not reliable on highly concurrent scenarios.
  133. // It is much better to use the OUTPUT option of SQL Server.
  134. if (isset($columnInformation['identities']) && !empty($columnInformation['identities'])) {
  135. $identities = array_keys($columnInformation['identities']);
  136. $identity = reset($identities);
  137. $output = "OUTPUT (Inserted.{$identity})";
  138. }
  139. else {
  140. $output = "OUTPUT (1)";
  141. }
  142. // If we're selecting from a SelectQuery, finish building the query and
  143. // pass it back, as any remaining options are irrelevant.
  144. if (!empty($this->fromQuery)) {
  145. if (empty($this->insertFields)) {
  146. return $prefix . "INSERT INTO {{$this->table}} {$output}" . $this->fromQuery;
  147. }
  148. else {
  149. $fields_csv = implode(', ', $this->connection->quoteIdentifiers($this->insertFields));
  150. return $prefix . "INSERT INTO {{$this->table}} ({$fields_csv}) {$output} " . $this->fromQuery;
  151. }
  152. }
  153. // Full default insert
  154. if (empty($this->insertFields)) {
  155. return $prefix . "INSERT INTO {{$this->table}} {$output} DEFAULT VALUES";
  156. }
  157. // Build the list of placeholders, a set of placeholders
  158. // for each element in the batch.
  159. $placeholders = array();
  160. $field_count = count($this->insertFields);
  161. for($j = 0; $j < $batch_size; $j++) {
  162. $batch_placeholders = array();
  163. for ($i = 0; $i < $field_count; ++$i) {
  164. $batch_placeholders[] = ':db_insert' . (($field_count * $j) + $i);
  165. }
  166. $placeholders[] = '(' . implode(', ', $batch_placeholders) . ')';
  167. }
  168. $sql = $prefix . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $this->connection->quoteIdentifiers($this->insertFields)) . ') ' . $output . ' VALUES ' . PHP_EOL;
  169. $sql .= implode(', ', $placeholders) . PHP_EOL;
  170. return $sql;
  171. }
  172. }
  173. /**
  174. * SQL Server-specific implementation of UPDATE.
  175. *
  176. * The specific parts are:
  177. * - SQL Server returns the number of matched rows to an UPDATE, and Drupal
  178. * requires the number of affected rows to be returned.
  179. * - SQL Server requires a special handling for the blobs.
  180. */
  181. class UpdateQuery_sqlsrv extends UpdateQuery {
  182. public function execute() {
  183. // Retrieve query options.
  184. $options = $this->queryOptions;
  185. // Fetch the list of blobs and sequences used on that table.
  186. $columnInformation = $this->connection->schema()->queryColumnInformation($this->table);
  187. // MySQL is a pretty slut that swallows everything thrown at it,
  188. // like trying to update an identity field...
  189. if (isset($columnInformation['identity']) && isset($this->fields[$columnInformation['identity']])) {
  190. unset($this->fields[$columnInformation['identity']]);
  191. }
  192. // Because we filter $fields the same way here and in __toString(), the
  193. // placeholders will all match up properly.
  194. $stmt = $this->connection->prepareQuery((string)$this);
  195. // Expressions take priority over literal fields, so we process those first
  196. // and remove any literal fields that conflict.
  197. $fields = $this->fields;
  198. DatabaseUtils::BindExpressions($stmt, $this->expressionFields, $fields);
  199. // We use this array to store references to the blob handles.
  200. // This is necessary because the PDO will otherwise messes up with references.
  201. $blobs = array();
  202. DatabaseUtils::BindValues($stmt, $fields, $blobs, ':db_update_placeholder_', $columnInformation);
  203. // Add conditions.
  204. if (count($this->condition)) {
  205. $this->condition->compile($this->connection, $this);
  206. $arguments = $this->condition->arguments();
  207. DatabaseUtils::BindArguments($stmt, $arguments);
  208. }
  209. $options = $this->queryOptions;
  210. $options['already_prepared'] = TRUE;
  211. // Run the statement.
  212. $this->connection->query($stmt, array(), $options);
  213. return $stmt->rowCount();
  214. }
  215. public function __toString() {
  216. // Create a sanitized comment string to prepend to the query.
  217. $prefix = $this->connection->makeComment($this->comments);
  218. // Expressions take priority over literal fields, so we process those first
  219. // and remove any literal fields that conflict.
  220. $fields = $this->fields;
  221. $update_fields = array();
  222. foreach ($this->expressionFields as $field => $data) {
  223. $update_fields[] = $this->connection->quoteIdentifier($field) . '=' . $data['expression'];
  224. unset($fields[$field]);
  225. }
  226. $max_placeholder = 0;
  227. foreach ($fields as $field => $value) {
  228. $update_fields[] = $this->connection->quoteIdentifier($field) . '=:db_update_placeholder_' . ($max_placeholder++);
  229. }
  230. $query = $prefix . 'UPDATE {' . $this->connection->escapeTable($this->table) . '} SET ' . implode(', ', $update_fields);
  231. if (count($this->condition)) {
  232. $this->condition->compile($this->connection, $this);
  233. // There is an implicit string cast on $this->condition.
  234. $query .= "\nWHERE " . $this->condition;
  235. }
  236. return $query;
  237. }
  238. }
  239. /**
  240. * SQL Server-specific implementation of TRUNCATE.
  241. */
  242. class TruncateQuery_sqlsrv extends TruncateQuery {
  243. public function __toString() {
  244. // Create a sanitized comment string to prepend to the query.
  245. $prefix = $this->connection->makeComment($this->comments);
  246. return $prefix . 'TRUNCATE TABLE {' . $this->connection->escapeTable($this->table) . '} ';
  247. }
  248. }
  249. /**
  250. * SQL Server-specific implementation of the MERGE operation.
  251. *
  252. * Tested to be at least 50% faster than parent's implementation.
  253. */
  254. class MergeQuery_sqlsrv extends MergeQuery {
  255. public function execute() {
  256. if (!count($this->condition)) {
  257. throw new InvalidMergeQueryException(t('Invalid merge query: no conditions'));
  258. }
  259. // Retrieve query options.
  260. $options = $this->queryOptions;
  261. // Keep a reference to the blobs.
  262. $blobs = array();
  263. // Fetch the list of blobs and sequences used on that table.
  264. $columnInformation = $this->connection->schema()->queryColumnInformation($this->table);
  265. // Find out if there is an identity field set in this insert.
  266. $this->setIdentity = !empty($columnInformation['identity']) && in_array($columnInformation['identity'], array_keys($this->insertFields));
  267. // Initialize placeholder count.
  268. $max_placeholder = 0;
  269. // Build the query.
  270. $stmt = $this->connection->prepareQuery((string)$this);
  271. // Build the arguments: 1. condition.
  272. $arguments = $this->condition->arguments();
  273. DatabaseUtils::BindArguments($stmt, $arguments);
  274. // 2. When matched part.
  275. $fields = $this->updateFields;
  276. DatabaseUtils::BindExpressions($stmt, $this->expressionFields, $fields);
  277. DatabaseUtils::BindValues($stmt, $fields, $blobs, ':db_merge_placeholder_', $columnInformation, $max_placeholder);
  278. // 3. When not matched part.
  279. DatabaseUtils::BindValues($stmt, $this->insertFields, $blobs, ':db_merge_placeholder_', $columnInformation, $max_placeholder);
  280. // 4. Run the query, this will return UPDATE or INSERT
  281. $this->connection->query($stmt, array(), $options);
  282. $result = NULL;
  283. foreach ($stmt as $value) {
  284. $result = $value->{'$action'};
  285. }
  286. switch($result) {
  287. case 'UPDATE':
  288. return static::STATUS_UPDATE;
  289. case 'INSERT':
  290. return static::STATUS_INSERT;
  291. }
  292. }
  293. public function __toString() {
  294. // Initialize placeholder count.
  295. $max_placeholder = 0;
  296. $max_placeholder_conditions = 0;
  297. $query = array();
  298. // Enable direct insertion to identity columns if necessary.
  299. if (!empty($this->setIdentity)) {
  300. $query[] = 'SET IDENTITY_INSERT {' . $this->table . '} ON;';
  301. }
  302. $query[] = 'MERGE INTO {' . $this->table . '} _target';
  303. // 1. Condition part.
  304. $this->condition->compile($this->connection, $this);
  305. $key_conditions = array();
  306. $template_item = array();
  307. $conditions = $this->conditions();
  308. unset($conditions['#conjunction']);
  309. foreach ($conditions as $condition) {
  310. $key_conditions[] = '_target.' . $this->connection->escapeField($condition['field']) . ' = ' . '_source.' . $this->connection->escapeField($condition['field']);
  311. $template_item[] = ':db_condition_placeholder_' . $max_placeholder_conditions++ . ' AS ' . $this->connection->escapeField($condition['field']);
  312. }
  313. $query[] = 'USING (SELECT ' . implode(', ', $template_item) . ') _source ' . PHP_EOL . 'ON ' . implode(' AND ', $key_conditions);
  314. // 2. "When matched" part.
  315. // Expressions take priority over literal fields, so we process those first
  316. // and remove any literal fields that conflict.
  317. $fields = $this->updateFields;
  318. $update_fields = array();
  319. foreach ($this->expressionFields as $field => $data) {
  320. $update_fields[] = $field . '=' . $data['expression'];
  321. unset($fields[$field]);
  322. }
  323. foreach ($fields as $field => $value) {
  324. $update_fields[] = $field . '=:db_merge_placeholder_' . ($max_placeholder++);
  325. }
  326. if (!empty($update_fields)) {
  327. $query[] = 'WHEN MATCHED THEN UPDATE SET ' . implode(', ', $update_fields);
  328. }
  329. // 3. "When not matched" part.
  330. if ($this->insertFields) {
  331. // Build the list of placeholders.
  332. $placeholders = array();
  333. for ($i = 0; $i < count($this->insertFields); ++$i) {
  334. $placeholders[] = ':db_merge_placeholder_' . ($max_placeholder++);
  335. }
  336. $query[] = 'WHEN NOT MATCHED THEN INSERT (' . implode(', ', $this->connection->quoteIdentifiers(array_keys($this->insertFields))) . ') VALUES (' . implode(', ', $placeholders) . ')';
  337. }
  338. else {
  339. $query[] = 'WHEN NOT MATCHED THEN INSERT DEFAULT VALUES';
  340. }
  341. // Return information about the query.
  342. $query[] = 'OUTPUT $action;';
  343. return implode(PHP_EOL, $query);
  344. }
  345. }