public Merge::execute()
Runs the query against the database.
Return value
\Drupal\Core\Database\StatementInterface|null A prepared statement, or NULL if the query is not valid.
Overrides Query::execute
File
- core/lib/Drupal/Core/Database/Query/Merge.php, line 352
Class
- Merge
- General class for an abstracted MERGE query operation.
Namespace
Drupal\Core\Database\Query
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | public function execute() { // Default options for merge queries. $this ->queryOptions += array ( 'throw_exception' => TRUE, ); try { if (! count ( $this ->condition)) { throw new InvalidMergeQueryException(t( 'Invalid merge query: no conditions' )); } $select = $this ->connection->select( $this ->conditionTable) ->condition( $this ->condition); $select ->addExpression( '1' ); if (! $select ->execute()->fetchField()) { try { $insert = $this ->connection->insert( $this ->table)->fields( $this ->insertFields); if ( $this ->defaultFields) { $insert ->useDefaults( $this ->defaultFields); } $insert ->execute(); return self::STATUS_INSERT; } catch (IntegrityConstraintViolationException $e ) { // The insert query failed, maybe it's because a racing insert query // beat us in inserting the same row. Retry the select query, if it // returns a row, ignore the error and continue with the update // query below. if (! $select ->execute()->fetchField()) { throw $e ; } } } if ( $this ->needsUpdate) { $update = $this ->connection->update( $this ->table) ->fields( $this ->updateFields) ->condition( $this ->condition); if ( $this ->expressionFields) { foreach ( $this ->expressionFields as $field => $data ) { $update ->expression( $field , $data [ 'expression' ], $data [ 'arguments' ]); } } $update ->execute(); return self::STATUS_UPDATE; } } catch (\Exception $e ) { if ( $this ->queryOptions[ 'throw_exception' ]) { throw $e ; } else { return NULL; } } } |
Please login to continue.