Merge.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. namespace Drupal\Core\Database\Query;
  3. use Drupal\Core\Database\Database;
  4. use Drupal\Core\Database\Connection;
  5. use Drupal\Core\Database\IntegrityConstraintViolationException;
  6. /**
  7. * General class for an abstracted MERGE query operation.
  8. *
  9. * An ANSI SQL:2003 compatible database would run the following query:
  10. *
  11. * @code
  12. * MERGE INTO table_name_1 USING table_name_2 ON (condition)
  13. * WHEN MATCHED THEN
  14. * UPDATE SET column1 = value1 [, column2 = value2 ...]
  15. * WHEN NOT MATCHED THEN
  16. * INSERT (column1 [, column2 ...]) VALUES (value1 [, value2 ...
  17. * @endcode
  18. *
  19. * Other databases (most notably MySQL, PostgreSQL and SQLite) will emulate
  20. * this statement by running a SELECT and then INSERT or UPDATE.
  21. *
  22. * By default, the two table names are identical and they are passed into the
  23. * the constructor. table_name_2 can be specified by the
  24. * MergeQuery::conditionTable() method. It can be either a string or a
  25. * subquery.
  26. *
  27. * The condition is built exactly like SelectQuery or UpdateQuery conditions,
  28. * the UPDATE query part is built similarly like an UpdateQuery and finally the
  29. * INSERT query part is built similarly like an InsertQuery. However, both
  30. * UpdateQuery and InsertQuery has a fields method so
  31. * MergeQuery::updateFields() and MergeQuery::insertFields() needs to be called
  32. * instead. MergeQuery::fields() can also be called which calls both of these
  33. * methods as the common case is to use the same column-value pairs for both
  34. * INSERT and UPDATE. However, this is not mandatory. Another convenient
  35. * wrapper is MergeQuery::key() which adds the same column-value pairs to the
  36. * condition and the INSERT query part.
  37. *
  38. * Several methods (key(), fields(), insertFields()) can be called to set a
  39. * key-value pair for the INSERT query part. Subsequent calls for the same
  40. * fields override the earlier ones. The same is true for UPDATE and key(),
  41. * fields() and updateFields().
  42. */
  43. class Merge extends Query implements ConditionInterface {
  44. use QueryConditionTrait;
  45. /**
  46. * Returned by execute() if an INSERT query has been executed.
  47. */
  48. const STATUS_INSERT = 1;
  49. /**
  50. * Returned by execute() if an UPDATE query has been executed.
  51. */
  52. const STATUS_UPDATE = 2;
  53. /**
  54. * The table to be used for INSERT and UPDATE.
  55. *
  56. * @var string
  57. */
  58. protected $table;
  59. /**
  60. * The table or subquery to be used for the condition.
  61. */
  62. protected $conditionTable;
  63. /**
  64. * An array of fields on which to insert.
  65. *
  66. * @var array
  67. */
  68. protected $insertFields = [];
  69. /**
  70. * An array of fields which should be set to their database-defined defaults.
  71. *
  72. * Used on INSERT.
  73. *
  74. * @var array
  75. */
  76. protected $defaultFields = [];
  77. /**
  78. * An array of values to be inserted.
  79. *
  80. * @var string
  81. */
  82. protected $insertValues = [];
  83. /**
  84. * An array of fields that will be updated.
  85. *
  86. * @var array
  87. */
  88. protected $updateFields = [];
  89. /**
  90. * Array of fields to update to an expression in case of a duplicate record.
  91. *
  92. * This variable is a nested array in the following format:
  93. * @code
  94. * <some field> => array(
  95. * 'condition' => <condition to execute, as a string>,
  96. * 'arguments' => <array of arguments for condition, or NULL for none>,
  97. * );
  98. * @endcode
  99. *
  100. * @var array
  101. */
  102. protected $expressionFields = [];
  103. /**
  104. * Flag indicating whether an UPDATE is necessary.
  105. *
  106. * @var bool
  107. */
  108. protected $needsUpdate = FALSE;
  109. /**
  110. * Constructs a Merge object.
  111. *
  112. * @param \Drupal\Core\Database\Connection $connection
  113. * A Connection object.
  114. * @param string $table
  115. * Name of the table to associate with this query.
  116. * @param array $options
  117. * Array of database options.
  118. */
  119. public function __construct(Connection $connection, $table, array $options = []) {
  120. $options['return'] = Database::RETURN_AFFECTED;
  121. parent::__construct($connection, $options);
  122. $this->table = $table;
  123. $this->conditionTable = $table;
  124. $this->condition = new Condition('AND');
  125. }
  126. /**
  127. * Sets the table or subquery to be used for the condition.
  128. *
  129. * @param $table
  130. * The table name or the subquery to be used. Use a Select query object to
  131. * pass in a subquery.
  132. *
  133. * @return \Drupal\Core\Database\Query\Merge
  134. * The called object.
  135. */
  136. protected function conditionTable($table) {
  137. $this->conditionTable = $table;
  138. return $this;
  139. }
  140. /**
  141. * Adds a set of field->value pairs to be updated.
  142. *
  143. * @param $fields
  144. * An associative array of fields to write into the database. The array keys
  145. * are the field names and the values are the values to which to set them.
  146. *
  147. * @return \Drupal\Core\Database\Query\Merge
  148. * The called object.
  149. */
  150. public function updateFields(array $fields) {
  151. $this->updateFields = $fields;
  152. $this->needsUpdate = TRUE;
  153. return $this;
  154. }
  155. /**
  156. * Specifies fields to be updated as an expression.
  157. *
  158. * Expression fields are cases such as counter = counter + 1. This method
  159. * takes precedence over MergeQuery::updateFields() and its wrappers,
  160. * MergeQuery::key() and MergeQuery::fields().
  161. *
  162. * @param $field
  163. * The field to set.
  164. * @param $expression
  165. * The field will be set to the value of this expression. This parameter
  166. * may include named placeholders.
  167. * @param $arguments
  168. * If specified, this is an array of key/value pairs for named placeholders
  169. * corresponding to the expression.
  170. *
  171. * @return \Drupal\Core\Database\Query\Merge
  172. * The called object.
  173. */
  174. public function expression($field, $expression, array $arguments = NULL) {
  175. $this->expressionFields[$field] = [
  176. 'expression' => $expression,
  177. 'arguments' => $arguments,
  178. ];
  179. $this->needsUpdate = TRUE;
  180. return $this;
  181. }
  182. /**
  183. * Adds a set of field->value pairs to be inserted.
  184. *
  185. * @param $fields
  186. * An array of fields on which to insert. This array may be indexed or
  187. * associative. If indexed, the array is taken to be the list of fields.
  188. * If associative, the keys of the array are taken to be the fields and
  189. * the values are taken to be corresponding values to insert. If a
  190. * $values argument is provided, $fields must be indexed.
  191. * @param $values
  192. * An array of fields to insert into the database. The values must be
  193. * specified in the same order as the $fields array.
  194. *
  195. * @return \Drupal\Core\Database\Query\Merge
  196. * The called object.
  197. */
  198. public function insertFields(array $fields, array $values = []) {
  199. if ($values) {
  200. $fields = array_combine($fields, $values);
  201. }
  202. $this->insertFields = $fields;
  203. return $this;
  204. }
  205. /**
  206. * Specifies fields for which the database-defaults should be used.
  207. *
  208. * If you want to force a given field to use the database-defined default,
  209. * not NULL or undefined, use this method to instruct the database to use
  210. * default values explicitly. In most cases this will not be necessary
  211. * unless you are inserting a row that is all default values, as you cannot
  212. * specify no values in an INSERT query.
  213. *
  214. * Specifying a field both in fields() and in useDefaults() is an error
  215. * and will not execute.
  216. *
  217. * @param $fields
  218. * An array of values for which to use the default values
  219. * specified in the table definition.
  220. *
  221. * @return \Drupal\Core\Database\Query\Merge
  222. * The called object.
  223. */
  224. public function useDefaults(array $fields) {
  225. $this->defaultFields = $fields;
  226. return $this;
  227. }
  228. /**
  229. * Sets common field-value pairs in the INSERT and UPDATE query parts.
  230. *
  231. * This method should only be called once. It may be called either
  232. * with a single associative array or two indexed arrays. If called
  233. * with an associative array, the keys are taken to be the fields
  234. * and the values are taken to be the corresponding values to set.
  235. * If called with two arrays, the first array is taken as the fields
  236. * and the second array is taken as the corresponding values.
  237. *
  238. * @param $fields
  239. * An array of fields to insert, or an associative array of fields and
  240. * values. The keys of the array are taken to be the fields and the values
  241. * are taken to be corresponding values to insert.
  242. * @param $values
  243. * An array of values to set into the database. The values must be
  244. * specified in the same order as the $fields array.
  245. *
  246. * @return \Drupal\Core\Database\Query\Merge
  247. * The called object.
  248. */
  249. public function fields(array $fields, array $values = []) {
  250. if ($values) {
  251. $fields = array_combine($fields, $values);
  252. }
  253. foreach ($fields as $key => $value) {
  254. $this->insertFields[$key] = $value;
  255. $this->updateFields[$key] = $value;
  256. }
  257. $this->needsUpdate = TRUE;
  258. return $this;
  259. }
  260. /**
  261. * Sets the key fields to be used as conditions for this query.
  262. *
  263. * This method should only be called once. It may be called either
  264. * with a single associative array or two indexed arrays. If called
  265. * with an associative array, the keys are taken to be the fields
  266. * and the values are taken to be the corresponding values to set.
  267. * If called with two arrays, the first array is taken as the fields
  268. * and the second array is taken as the corresponding values.
  269. *
  270. * The fields are copied to the condition of the query and the INSERT part.
  271. * If no other method is called, the UPDATE will become a no-op.
  272. *
  273. * @param $fields
  274. * An array of fields to set, or an associative array of fields and values.
  275. * @param $values
  276. * An array of values to set into the database. The values must be
  277. * specified in the same order as the $fields array.
  278. *
  279. * @return $this
  280. */
  281. public function keys(array $fields, array $values = []) {
  282. if ($values) {
  283. $fields = array_combine($fields, $values);
  284. }
  285. foreach ($fields as $key => $value) {
  286. $this->insertFields[$key] = $value;
  287. $this->condition($key, $value);
  288. }
  289. return $this;
  290. }
  291. /**
  292. * Sets a single key field to be used as condition for this query.
  293. *
  294. * Same as \Drupal\Core\Database\Query\Merge::keys() but offering a signature
  295. * that is more natural for the case of a single key.
  296. *
  297. * @param string $field
  298. * The name of the field to set.
  299. * @param mixed $value
  300. * The value to set into the database.
  301. *
  302. * @return $this
  303. *
  304. * @see \Drupal\Core\Database\Query\Merge::keys()
  305. */
  306. public function key($field, $value = NULL) {
  307. // @todo D9: Remove this backwards-compatibility shim.
  308. if (is_array($field)) {
  309. $this->keys($field, isset($value) ? $value : []);
  310. }
  311. else {
  312. $this->keys([$field => $value]);
  313. }
  314. return $this;
  315. }
  316. /**
  317. * Implements PHP magic __toString method to convert the query to a string.
  318. *
  319. * In the degenerate case, there is no string-able query as this operation
  320. * is potentially two queries.
  321. *
  322. * @return string
  323. * The prepared query statement.
  324. */
  325. public function __toString() {
  326. }
  327. public function execute() {
  328. // Default options for merge queries.
  329. $this->queryOptions += [
  330. 'throw_exception' => TRUE,
  331. ];
  332. try {
  333. if (!count($this->condition)) {
  334. throw new InvalidMergeQueryException(t('Invalid merge query: no conditions'));
  335. }
  336. $select = $this->connection->select($this->conditionTable)
  337. ->condition($this->condition);
  338. $select->addExpression('1');
  339. if (!$select->execute()->fetchField()) {
  340. try {
  341. $insert = $this->connection->insert($this->table)->fields($this->insertFields);
  342. if ($this->defaultFields) {
  343. $insert->useDefaults($this->defaultFields);
  344. }
  345. $insert->execute();
  346. return self::STATUS_INSERT;
  347. }
  348. catch (IntegrityConstraintViolationException $e) {
  349. // The insert query failed, maybe it's because a racing insert query
  350. // beat us in inserting the same row. Retry the select query, if it
  351. // returns a row, ignore the error and continue with the update
  352. // query below.
  353. if (!$select->execute()->fetchField()) {
  354. throw $e;
  355. }
  356. }
  357. }
  358. if ($this->needsUpdate) {
  359. $update = $this->connection->update($this->table)
  360. ->fields($this->updateFields)
  361. ->condition($this->condition);
  362. if ($this->expressionFields) {
  363. foreach ($this->expressionFields as $field => $data) {
  364. $update->expression($field, $data['expression'], $data['arguments']);
  365. }
  366. }
  367. $update->execute();
  368. return self::STATUS_UPDATE;
  369. }
  370. }
  371. catch (\Exception $e) {
  372. if ($this->queryOptions['throw_exception']) {
  373. throw $e;
  374. }
  375. else {
  376. return NULL;
  377. }
  378. }
  379. }
  380. }