Skip to content

Commit

Permalink
Allow non-int error codes (#365)
Browse files Browse the repository at this point in the history
* fix: work with non-integer error codes
fixes #364

* ci: upgrade php version

* feature: improve error handling of special binding including infiles

* feature: improve error handling of special binding including infiles
closes #364

* test: improve test oop
  • Loading branch information
g105b committed Feb 10, 2024
1 parent 388b6f7 commit 76355b4
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 42 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ 8.1, 8.2 ]
php: [ 8.2, 8.3 ]

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -37,7 +37,7 @@ jobs:
needs: [ composer ]
strategy:
matrix:
php: [ 8.1, 8.2 ]
php: [ 8.2, 8.3 ]

outputs:
coverage: ${{ steps.store-coverage.outputs.coverage_text }}
Expand Down Expand Up @@ -90,7 +90,7 @@ jobs:
needs: [ composer ]
strategy:
matrix:
php: [ 8.1, 8.2 ]
php: [ 8.2, 8.3 ]

steps:
- uses: actions/download-artifact@v3
Expand All @@ -112,7 +112,7 @@ jobs:
needs: [ composer ]
strategy:
matrix:
php: [ 8.1, 8.2 ]
php: [ 8.2, 8.3 ]

steps:
- uses: actions/download-artifact@v3
Expand All @@ -136,7 +136,7 @@ jobs:
needs: [ composer ]
strategy:
matrix:
php: [ 8.1, 8.2 ]
php: [ 8.2, 8.3 ]

steps:
- uses: actions/download-artifact@v3
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
colors="true"
cacheDirectory="test/phpunit/.phpunit.cache"
bootstrap="vendor/autoload.php"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
>
<coverage />

Expand Down
3 changes: 2 additions & 1 deletion src/Connection/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ protected function connect():void {
Connection::ATTR_ERRMODE => Connection::ERRMODE_EXCEPTION,
];

if($this->settings->getSchema() === Settings::DRIVER_MYSQL) {
if($this->settings->getDriver() === Settings::DRIVER_MYSQL) {
$options[Connection::MYSQL_ATTR_INIT_COMMAND]
= "SET SESSION collation_connection='"
. $this->settings->getCollation()
. "'";
$options[Connection::MYSQL_ATTR_LOCAL_INFILE] = true;
}

$this->connection = new Connection(
Expand Down
54 changes: 33 additions & 21 deletions src/Query/SqlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
/** @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */
class SqlQuery extends Query {
const SPECIAL_BINDINGS = [
"limit",
"offset",
"groupBy",
"orderBy",
"field" => ["groupBy", "orderBy"],
"int" => ["limit", "offset"],
"string" => ["infileName"],
];

/** @param array<string, mixed>|array<mixed> $bindings */
Expand Down Expand Up @@ -72,8 +71,8 @@ public function execute(array $bindings = []):ResultSet {
}
catch(PDOException $exception) {
throw new PreparedStatementException(
$exception->getMessage(),
$exception->getCode(),
$exception->getMessage() . " (" . $exception->getCode(),
0,
$exception
);
}
Expand Down Expand Up @@ -108,24 +107,37 @@ public function injectSpecialBindings(
string $sql,
array $bindings
):string {
foreach(self::SPECIAL_BINDINGS as $special) {
$specialPlaceholder = ":" . $special;
foreach(self::SPECIAL_BINDINGS as $type => $specialList) {
foreach($specialList as $special) {
$specialPlaceholder = ":" . $special;

if(!array_key_exists($special, $bindings)) {
continue;
}
if(!array_key_exists($special, $bindings)) {
continue;
}

$replacement = $this->escapeSpecialBinding(
$bindings[$special],
$special
);
if($type !== "string") {
$replacement = $this->escapeSpecialBinding(
$bindings[$special],
$special
);
}

$sql = str_replace(
$specialPlaceholder,
$replacement,
$sql
);
unset($bindings[$special]);
if($type === "field") {
$words = explode(" ", $bindings[$special]);
$words[0] = "`" . $words[0] . "`";
$replacement = implode(" ", $words);
}
elseif($type === "string") {
$replacement = "'" . $bindings[$special] . "'";
}

$sql = str_replace(
$specialPlaceholder,
$replacement,
$sql
);
unset($bindings[$special]);
}
}

foreach($bindings as $key => $value) {
Expand Down
4 changes: 2 additions & 2 deletions test/phpunit/Connection/DefaultSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public function testGetDefaultCharset() {
self::assertEquals(DefaultSettings::DEFAULT_COLLATION, $settings->getCollation());
}

public function getDrivers():array {
public static function getDrivers():array {
return [
[Settings::DRIVER_MYSQL, 3306],
[Settings::DRIVER_POSTGRES, 5432],
[Settings::DRIVER_SQLSERVER, 1433],
[Settings::DRIVER_SQLITE, 0],
];
}
}
}
4 changes: 2 additions & 2 deletions test/phpunit/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private static function queryPathProvider(bool $exists, $extension = "sql") {
return $data;
}

public function queryPathNestedProvider() {
public static function queryPathNestedProvider() {
$data = [];

$n = rand(2, 6);
Expand Down Expand Up @@ -163,4 +163,4 @@ private static function queryCollectionPathProvider(

return $data;
}
}
}
16 changes: 8 additions & 8 deletions test/phpunit/Migration/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function testCheckFileListOrder(array $fileList) {

/** @dataProvider dataMigrationFileListMissing */
public function testCheckFileListOrderMissing(array $fileList) {
$path = $this->getMigrationDirectory();
$path = self::getMigrationDirectory();
$this->createFiles($fileList, $path);

$settings = $this->createSettings($path);
Expand Down Expand Up @@ -542,15 +542,15 @@ public function testMigrationErrorOutputToStream(array $fileList) {
}
}

public function dataMigrationFileList():array {
$fileList = $this->generateFileList();
public static function dataMigrationFileList():array {
$fileList = self::generateFileList();
return [
[$fileList]
];
}

public function dataMigrationFileListMissing():array {
$fileList = $this->generateFileList(
public static function dataMigrationFileListMissing():array {
$fileList = self::generateFileList(
true,
false
);
Expand All @@ -559,8 +559,8 @@ public function dataMigrationFileListMissing():array {
];
}

public function dataMigrationFileListDuplicate():array {
$fileList = $this->generateFileList(
public static function dataMigrationFileListDuplicate():array {
$fileList = self::generateFileList(
false,
true
);
Expand Down Expand Up @@ -638,7 +638,7 @@ protected function hashMigrationToDb(
}
}

private function generateFileList($missingFiles = false, $duplicateFiles = false) {
private static function generateFileList($missingFiles = false, $duplicateFiles = false) {
$fileList = [];

$migLength = rand(10, 30);
Expand Down
4 changes: 2 additions & 2 deletions test/phpunit/Query/SqlQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public function testSpecialBindingsNoAscDesc(
self::assertStringNotContainsString(":limit", $injectedSql);
self::assertStringNotContainsString(":offset", $injectedSql);

self::assertStringContainsString("order by sortColumn", $injectedSql);
self::assertStringContainsString("order by `sortColumn`", $injectedSql);
self::assertStringContainsString("limit 100", $injectedSql);
self::assertStringContainsString("offset 25", $injectedSql);
}
Expand All @@ -260,7 +260,7 @@ public function testSpecialBindingsAscDesc(
"offset" => 25,
]);

self::assertStringContainsString("order by sortColumn desc", $injectedSql);
self::assertStringContainsString("order by `sortColumn` desc", $injectedSql);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/phpunit/Result/RowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testGetIntNullable() {
self::assertNull($row->getInt("does_not_exist"));
}

public function data_getTestRow():array {
public static function data_getTestRow():array {
$data = [];

$columns = ["id", "name", "example", "exampleFloat", "exampleDateTime", "exampleBool"];
Expand Down

0 comments on commit 76355b4

Please sign in to comment.