Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal-3443488
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Issue forks
drupal-3443488
Commits
94247bfd
Commit
94247bfd
authored
14 years ago
by
Angie Byron
Browse files
Options
Downloads
Patches
Plain Diff
#715108
follow-up by Damien Tournoud, justinrandell: Fixed SQLite Merge queries.
parent
3b2968da
No related branches found
No related tags found
Loading
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
includes/database/sqlite/query.inc
+63
-1
63 additions, 1 deletion
includes/database/sqlite/query.inc
with
63 additions
and
1 deletion
includes/database/sqlite/query.inc
+
63
−
1
View file @
94247bfd
...
...
@@ -63,7 +63,6 @@ public function __toString() {
* UPDATE test SET name = 'newname' WHERE tid = 1 AND name <> 'newname'
*/
class
UpdateQuery_sqlite
extends
UpdateQuery
{
/**
* Helper function that removes the fields that are already in a condition.
*
...
...
@@ -84,6 +83,10 @@ protected function removeFieldsInCondition(&$fields, QueryConditionInterface $co
}
public
function
execute
()
{
if
(
!
empty
(
$this
->
queryOptions
[
'sqlite_return_matched_rows'
]))
{
return
parent
::
execute
();
}
// Get the fields used in the update query, and remove those that are already
// in the condition.
$fields
=
$this
->
expressionFields
+
$this
->
fields
;
...
...
@@ -115,6 +118,65 @@ public function execute() {
}
/**
* SQLite specific implementation of MergeQuery.
*
* SQLite doesn't support row-level locking, but acquire locks on the whole
* database file. We implement MergeQuery using a different strategy:
* - UPDATE xxx WHERE <key condition>
* - if the previous query hasn't matched, INSERT
*
* The first UPDATE query will acquire a RESERVED lock on the database.
*/
class
MergeQuery_sqlite
extends
MergeQuery
{
public
function
execute
()
{
// If validation fails, simply return NULL.
// Note that validation routines in preExecute() may throw exceptions instead.
if
(
!
$this
->
preExecute
())
{
return
NULL
;
}
// Wrap multiple queries in a transaction.
$transaction
=
$this
->
connection
->
startTransaction
();
if
(
$this
->
updateFields
)
{
$update_fields
=
$this
->
updateFields
;
}
else
{
$update_fields
=
$this
->
insertFields
;
// If there are no exclude fields, this is a no-op.
foreach
(
$this
->
excludeFields
as
$exclude_field
)
{
unset
(
$update_fields
[
$exclude_field
]);
}
}
// The update fields are empty, fill them with dummy data.
if
(
!
$update_fields
&&
!
$this
->
expressionFields
)
{
$update_fields
=
array_slice
(
$this
->
keyFields
,
0
,
1
);
}
// Start with an update query, this acquires a RESERVED lock on the database.
// Use the SQLite-specific 'sqlite_return_matched_rows' query option to
// return the number of rows matched by that query, not modified by it.
$update
=
$this
->
connection
->
update
(
$this
->
table
,
array
(
'sqlite_return_matched_rows'
=>
TRUE
)
+
$this
->
queryOptions
)
->
fields
(
$update_fields
);
foreach
(
$this
->
keyFields
as
$field
=>
$value
)
{
$update
->
condition
(
$field
,
$value
);
}
foreach
(
$this
->
expressionFields
as
$field
=>
$expression
)
{
$update
->
expression
(
$field
,
$expression
[
'expression'
],
$expression
[
'arguments'
]);
}
if
(
$update
->
execute
())
{
return
MergeQuery
::
STATUS_UPDATE
;
}
// The UPDATE query failed to match rows, proceed with an INSERT.
$insert_fields
=
$this
->
insertFields
+
$this
->
keyFields
;
$this
->
connection
->
insert
(
$this
->
table
,
$this
->
queryOptions
)
->
fields
(
$insert_fields
)
->
execute
();
return
MergeQuery
::
STATUS_INSERT
;
}
}
/**
* SQLite specific implementation of DeleteQuery.
*
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment