Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

"require-dev": {
"ext-sqlite3": "*",
"phpstan/phpstan": "^v1.10",
"phpstan/phpstan": "^v1.10 || ^2.0",
"phpunit/phpunit": "^10.1 || ^12.0",
"phpmd/phpmd": "^2.13",
"squizlabs/php_codesniffer": "^3.7 || ^4.0"
Expand Down
23 changes: 17 additions & 6 deletions composer.lock

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

2 changes: 1 addition & 1 deletion src/Connection/DefaultSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function getConnectionName():string {
return $this->connectionName;
}

/** @return array<string, string> */
/** @return array<string, string|int|array<int, bool|int>> */
public function getConnectionSettings():array {
// NOTE: It's not possible to test the 'port' values returned by this method
// because the DefaultSettings can only ever return the DEFAULT_DRIVER port
Expand Down
15 changes: 10 additions & 5 deletions src/Migration/AbstractMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use GT\Database\Connection\Settings;
use GT\Database\Database;
use LogicException;
use SplFileInfo;
use SplFileObject;

Expand All @@ -15,7 +16,7 @@ abstract class AbstractMigrator {
protected ?SplFileObject $streamOut = null;

protected string $driver;
protected Database $dbClient;
protected ?Database $dbClient;
protected string $path;
protected string $tableName;
protected Settings $settings;
Expand All @@ -39,6 +40,10 @@ public function __construct(
$this->dbClient = new Database($settings);
}

protected function getDatabase():Database {
return $this->dbClient ?? throw new LogicException("Migration database is disconnected");
}

abstract protected function getDefaultTableName():string;

public function setOutput(
Expand Down Expand Up @@ -104,7 +109,7 @@ public function extractNumberFromFilename(string $pathName):int {
protected function executeSqlFile(string $file):string {
$md5 = md5_file($file);
foreach($this->splitSqlFile($file) as $sql) {
$this->dbClient->executeSql($sql);
$this->getDatabase()->executeSql($sql);
}

return $md5;
Expand All @@ -123,7 +128,7 @@ protected function countSqlStatements(string $file):int {
protected function tableHasColumn(string $columnName):bool {
switch($this->driver) {
case Settings::DRIVER_SQLITE:
$result = $this->dbClient->executeSql(
$result = $this->getDatabase()->executeSql(
"PRAGMA table_info(`{$this->tableName}`)"
);
foreach($result->fetchAll() as $row) {
Expand All @@ -135,7 +140,7 @@ protected function tableHasColumn(string $columnName):bool {

default:
// @codeCoverageIgnoreStart
$result = $this->dbClient->executeSql(
$result = $this->getDatabase()->executeSql(
"show columns from `{$this->tableName}` like ?",
[$columnName]
);
Expand All @@ -152,7 +157,7 @@ protected function ensureColumnExists(
return;
}

$this->dbClient->executeSql(
$this->getDatabase()->executeSql(
"alter table `{$this->tableName}` add `$columnName` $definition"
);
}
Expand Down
28 changes: 14 additions & 14 deletions src/Migration/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use GT\Database\DatabaseException;

/**
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings("PHPMD.ExcessiveClassComplexity")
*/
class Migrator extends AbstractMigrator {
const COLUMN_QUERY_NUMBER = "queryNumber";
Expand Down Expand Up @@ -36,7 +36,7 @@ protected function getDefaultTableName():string {
public function checkMigrationTableExists():bool {
switch($this->driver) {
case Settings::DRIVER_SQLITE:
$result = $this->dbClient->executeSql(
$result = $this->getDatabase()->executeSql(
"select name from sqlite_master "
. "where type=? "
. "and name like ?", [
Expand All @@ -48,7 +48,7 @@ public function checkMigrationTableExists():bool {

default:
// @codeCoverageIgnoreStart
$result = $this->dbClient->executeSql(
$result = $this->getDatabase()->executeSql(
"show tables like ?",
[
$this->tableName
Expand All @@ -62,7 +62,7 @@ public function checkMigrationTableExists():bool {
}

public function createMigrationTable():void {
$this->dbClient->executeSql(implode("\n", [
$this->getDatabase()->executeSql(implode("\n", [
"create table if not exists `{$this->tableName}` (",
"`" . self::COLUMN_QUERY_NUMBER . "` int primary key,",
"`" . self::COLUMN_QUERY_HASH . "` varchar(32) null,",
Expand All @@ -74,7 +74,7 @@ public function createMigrationTable():void {

public function getMigrationCount():int {
try {
$result = $this->dbClient->executeSql("select `"
$result = $this->getDatabase()->executeSql("select `"
. self::COLUMN_QUERY_NUMBER
. "` from `{$this->tableName}` "
. "order by `" . self::COLUMN_QUERY_NUMBER . "` desc"
Expand Down Expand Up @@ -191,7 +191,7 @@ public function performMigration(
continue;
}

$this->dbClient->executeSql($sql);
$this->getDatabase()->executeSql($sql);
$this->recordMigrationProgress($fileNumber, $md5, $statementNumber);
}

Expand Down Expand Up @@ -219,10 +219,10 @@ public function selectSchema():void {
$schema = $this->schema;

try {
$this->dbClient->executeSql(
$this->getDatabase()->executeSql(
"create schema if not exists `$schema` default character set {$this->charset} default collate {$this->collate}"
);
$this->dbClient->executeSql(
$this->getDatabase()->executeSql(
"use `$schema`"
);
}
Expand All @@ -249,7 +249,7 @@ protected function recordMigrationProgress(
$existingState = $this->getMigrationProgress($number);

if($existingState) {
$this->dbClient->executeSql(implode("\n", [
$this->getDatabase()->executeSql(implode("\n", [
"update `{$this->tableName}`",
"set `" . self::COLUMN_QUERY_HASH . "` = ?,",
"`" . self::COLUMN_LAST_STATEMENT . "` = ?,",
Expand All @@ -259,7 +259,7 @@ protected function recordMigrationProgress(
return;
}

$this->dbClient->executeSql(implode("\n", [
$this->getDatabase()->executeSql(implode("\n", [
"insert into `{$this->tableName}` (",
"`" . self::COLUMN_QUERY_NUMBER . "`, ",
"`" . self::COLUMN_QUERY_HASH . "`, ",
Expand Down Expand Up @@ -293,7 +293,7 @@ protected function getMigrationProgress(int $fileNumber):?array {
$selectList .= ", null as `" . self::COLUMN_LAST_STATEMENT . "`";
}

$result = $this->dbClient->executeSql(implode("\n", [
$result = $this->getDatabase()->executeSql(implode("\n", [
"select $selectList",
"from `{$this->tableName}`",
"where `" . self::COLUMN_QUERY_NUMBER . "` = ?",
Expand Down Expand Up @@ -331,7 +331,7 @@ protected function resolveLastCompletedStatement(
*/
public function deleteAndRecreateSchema():void {
if($this->driver === Settings::DRIVER_SQLITE) {
unset($this->dbClient);
$this->dbClient = null;

if($this->schema !== Settings::SCHEMA_IN_MEMORY
&& is_file($this->schema)) {
Expand All @@ -343,10 +343,10 @@ public function deleteAndRecreateSchema():void {
}

try {
$this->dbClient->executeSql(
$this->getDatabase()->executeSql(
"drop schema if exists `{$this->schema}`"
);
$this->dbClient->executeSql(
$this->getDatabase()->executeSql(
"create schema if not exists "
. $this->schema
. " default character set "
Expand Down
2 changes: 1 addition & 1 deletion src/PlaceholderValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace GT\Database;

/**
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings("PHPMD.ExcessiveClassComplexity")
*/
class PlaceholderValidator {
/** @param array<string, mixed>|array<mixed> $bindings */
Expand Down
10 changes: 5 additions & 5 deletions src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


/**
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings("PHPMD.ExcessiveClassComplexity")
*/
abstract class Query {
const SPECIAL_BINDINGS = [
Expand Down Expand Up @@ -165,15 +165,15 @@ public function injectSpecialBindings(
return $sql;
}

/** @param array<string, string|array<string, string>> $data */
/** @param array<string, mixed> $data */
public function injectDynamicBindings(string $sql, array &$data):string {
$sql = $this->injectDynamicBindingsValueSet($sql, $data);
$sql = $this->injectDynamicIn($sql, $data);
$sql = $this->injectDynamicOr($sql, $data);
return trim($sql);
}

/** @param array<string, string|array<string, string|array<string>>> $data */
/** @param array<string, mixed> $data */
private function injectDynamicBindingsValueSet(string $sql, array &$data):string {
$pattern = '/\(\s*:__dynamicValueSet\s\)/';
if(!preg_match($pattern, $sql, $matches)) {
Expand Down Expand Up @@ -215,7 +215,7 @@ private function injectDynamicBindingsValueSet(string $sql, array &$data):string
return str_replace($matches[0], $replacementString, $sql);
}

/** @param array<string, string|array<string, string>> $data */
/** @param array<string, mixed> $data */
private function injectDynamicIn(string $sql, array &$data):string {
$pattern = '/\(\s*:__dynamicIn\s\)/';
if(!preg_match($pattern, $sql, $matches)) {
Expand All @@ -237,7 +237,7 @@ private function injectDynamicIn(string $sql, array &$data):string {
return str_replace($matches[0], "( $replacementString )", $sql);
}

/** @param array<string, string|array<string, array<string>>> $data */
/** @param array<string, mixed> $data */
private function injectDynamicOr(string $sql, array &$data):string {
$pattern = '/:__dynamicOr/';
if(!preg_match($pattern, $sql, $matches)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Result/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class ResultSet implements Iterator, Countable {
protected ?Row $currentRow;
protected int $rowIndex;
protected ?int $rowIndex = null;
protected int $iteratorIndex;

public function __construct(
Expand Down Expand Up @@ -114,7 +114,7 @@ public function getFieldList():array {
public function rewind():void {
$this->statement->execute();
$this->currentRow = null;
unset($this->rowIndex);
$this->rowIndex = null;
$this->iteratorIndex = 0;
}

Expand Down
Loading