Skip to content

Commit 7d0fb3b

Browse files
authored
transform write query into read query (#497)
1 parent af71a9e commit 7d0fb3b

21 files changed

+7414
-7144
lines changed

.phpstan-dba-mysqli.cache

+151-203
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.phpstan-dba-pdo-mysql.cache

+151-203
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ $config = new RuntimeConfiguration();
5757
// $config->debugMode(true);
5858
// $config->stringifyTypes(true);
5959
// $config->analyzeQueryPlans(true);
60-
// $config->analyzeWriteQueries(true); // requires transaction support in db schema and db driver
6160

6261
// TODO: Put your database credentials here
6362
$mysqli = new mysqli('hostname', 'username', 'password', 'database');

docs/configuration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ If not configured otherwise, the following defaults are used:
88
- when analyzing a php8+ codebase, [`PDO::ERRMODE_EXCEPTION` error handling](https://www.php.net/manual/en/pdo.error-handling.php) is assumed.
99
- when analyzing a php8.1+ codebase, [`mysqli_report(\MYSQLI_REPORT_ERROR | \MYSQLI_REPORT_STRICT);` error handling](https://www.php.net/mysqli_report) is assumed.
1010
- the fetch mode defaults to `QueryReflector::FETCH_TYPE_BOTH`, but can be configured using the [`defaultFetchMode`](https://github.com/staabm/phpstan-dba/tree/main/src/QueryReflection/RuntimeConfiguration.php) option.
11-
- only readable queries are analyzed per default. In case your database schema and database driver support transactions, you may consider enabled writable queries using the [`analyzeWriteQueries`](https://github.com/staabm/phpstan-dba/tree/main/src/QueryReflection/RuntimeConfiguration.php) option.
11+
- read and write queries are analyzed per default. you may consider disable write query analysis using the [`analyzeWriteQueries`](https://github.com/staabm/phpstan-dba/tree/main/src/QueryReflection/RuntimeConfiguration.php) option.

src/Analyzer/QueryPlanAnalyzerMysql.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function analyze(string $query): QueryPlanResult
6262
$this->connection->rollBack();
6363
}
6464
} else {
65-
$this->connection->begin_transaction();
65+
$this->connection->begin_transaction(\MYSQLI_TRANS_START_READ_ONLY);
6666

6767
try {
6868
$result = $this->connection->query($simulatedQuery);

src/DbSchema/SchemaHasherMysql.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function hashDb(): string
7272
$this->connection->rollBack();
7373
}
7474
} else {
75-
$this->connection->begin_transaction();
75+
$this->connection->begin_transaction(\MYSQLI_TRANS_START_READ_ONLY);
7676

7777
try {
7878
$result = $this->connection->query($query);

src/QueryReflection/QueryReflection.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,22 @@ public static function setupReflector(QueryReflector $reflector, RuntimeConfigur
5959

6060
public function validateQueryString(string $queryString): ?Error
6161
{
62+
$queryType = self::getQueryType($queryString);
63+
6264
if (self::getRuntimeConfiguration()->isAnalyzingWriteQueries()) {
63-
if (!\in_array(self::getQueryType($queryString), [
64-
'SELECT',
65+
if (\in_array($queryType, [
6566
'INSERT',
6667
'DELETE',
6768
'UPDATE',
6869
'REPLACE',
6970
], true)) {
71+
// turn write queries into explain, so we don't need to execute a query which might modify data
72+
$queryString = 'EXPLAIN '.$queryString;
73+
} elseif ('SELECT' !== $queryType) {
7074
return null;
7175
}
7276
} else {
73-
if ('SELECT' !== self::getQueryType($queryString)) {
77+
if ('SELECT' !== $queryType) {
7478
return null;
7579
}
7680
}

src/QueryReflection/RuntimeConfiguration.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class RuntimeConfiguration
4646
/**
4747
* @var bool
4848
*/
49-
private $writableQueries = false;
49+
private $writableQueries = true;
5050
/**
5151
* @var bool|0|positive-int
5252
*/

0 commit comments

Comments
 (0)