Skip to content

Commit

Permalink
support queries which acquire read locks (#179)
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Staab <m.staab@complex-it.de>
  • Loading branch information
staabm and clxmstaab authored Jan 22, 2022
1 parent 47d8cce commit 76fea3a
Show file tree
Hide file tree
Showing 5 changed files with 363 additions and 3 deletions.
164 changes: 164 additions & 0 deletions .phpstan-dba.cache

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

143 changes: 143 additions & 0 deletions .phpunit-phpstan-dba.cache

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions src/QueryReflection/QuerySimulation.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,23 @@ public static function simulate(string $queryString): ?string

private static function stripTraillingLimit(string $queryString): ?string
{
// XXX someday we will use a proper SQL parser,
$queryString = rtrim($queryString, ';');

// strip trailling FOR UPDATE/FOR SHARE
$queryString = preg_replace('/(.*)FOR (UPDATE|SHARE)\s*$/i', '$1', $queryString);

if (null === $queryString) {
throw new ShouldNotHappenException('Could not strip trailling FOR UPDATE/SHARE from query');
}

// strip trailling OFFSET
$queryString = preg_replace('/(.*)OFFSET\s+["\']?\d+["\']?\s*$/i', '$1', $queryString);

if (null === $queryString) {
throw new ShouldNotHappenException('Could not strip trailing offset from query');
throw new ShouldNotHappenException('Could not strip trailing OFFSET from query');
}

// XXX someday we will use a proper SQL parser,
// which would also allow us to support even more complex expressions like SELECT .. LIMIT X, Y FOR UPDATE
return preg_replace('/\s*LIMIT\s+["\']?\d+["\']?\s*(,\s*["\']?\d*["\']?)?\s*$/i', '', $queryString);
}
}
Loading

0 comments on commit 76fea3a

Please sign in to comment.