From 8f96fe36c774ce5eb022becd60e08ae050f6e4a1 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 17 May 2024 19:40:15 +0000 Subject: [PATCH] fix tests NO_CI --- DataStorage/Database/BuilderAbstract.php | 22 ++++++++ DataStorage/Database/Query/Builder.php | 22 -------- DataStorage/Database/Schema/Builder.php | 56 ++++++++++++++++++- .../SpreadsheetDatabaseMapperTest.php | 54 +++++++++--------- 4 files changed, 102 insertions(+), 52 deletions(-) diff --git a/DataStorage/Database/BuilderAbstract.php b/DataStorage/Database/BuilderAbstract.php index cde979c5f..e74806314 100755 --- a/DataStorage/Database/BuilderAbstract.php +++ b/DataStorage/Database/BuilderAbstract.php @@ -151,4 +151,26 @@ abstract public function toSql() : string; * @since 1.0.0 */ abstract public function execute() : ?\PDOStatement; + + /** + * Get bind parameter type. + * + * @param mixed $value Value to bind + * + * @return int + * + * @throws \Exception + * + * @since 1.0.0 + */ + public static function getBindParamType(mixed $value) : int + { + if (\is_int($value)) { + return \PDO::PARAM_INT; + } elseif (\is_string($value) || \is_float($value)) { + return \PDO::PARAM_STR; + } + + throw new \Exception(); + } } diff --git a/DataStorage/Database/Query/Builder.php b/DataStorage/Database/Query/Builder.php index 9aef03b64..c8b491c99 100755 --- a/DataStorage/Database/Query/Builder.php +++ b/DataStorage/Database/Query/Builder.php @@ -1423,28 +1423,6 @@ public function prepare() : ?\PDOStatement return $sth; } - /** - * Get bind parameter type. - * - * @param mixed $value Value to bind - * - * @return int - * - * @throws \Exception - * - * @since 1.0.0 - */ - public static function getBindParamType(mixed $value) : int - { - if (\is_int($value)) { - return \PDO::PARAM_INT; - } elseif (\is_string($value) || \is_float($value)) { - return \PDO::PARAM_STR; - } - - throw new \Exception(); - } - /** * Get column name * diff --git a/DataStorage/Database/Schema/Builder.php b/DataStorage/Database/Schema/Builder.php index e5365c3b1..4616f1955 100755 --- a/DataStorage/Database/Schema/Builder.php +++ b/DataStorage/Database/Schema/Builder.php @@ -378,7 +378,7 @@ public function addConstraint(string $key, string $foreignTable, string $foreign /** * {@inheritdoc} */ - public function execute() : ?\PDOStatement + public function prepare() : ?\PDOStatement { $sth = null; $sql = ''; @@ -389,7 +389,49 @@ public function execute() : ?\PDOStatement return null; } - $sth->execute(); + foreach ($this->binds as $key => $bind) { + if (!isset($bind['type'])) { + $bind['type'] = self::getBindParamType($bind['value']); + } + + $sth->bindParam(\is_int($key) ? $key + 1 : $key, $bind['value'], $bind['type']); + } + } catch (\Throwable $t) { + // @codeCoverageIgnoreStart + \phpOMS\Log\FileLogger::getInstance()->error( + \phpOMS\Log\FileLogger::MSG_FULL, [ + 'message' => $t->getMessage() . ':' . $sql, + 'line' => __LINE__, + 'file' => self::class, + ] + ); + + \phpOMS\Log\FileLogger::getInstance()->error( + \phpOMS\Log\FileLogger::MSG_FULL, [ + 'message' => \json_encode($this->binds), + 'line' => __LINE__, + 'file' => self::class, + ] + ); + + $sth = null; + // @codeCoverageIgnoreEnd + } + + return $sth; + } + + /** + * {@inheritdoc} + */ + public function execute() : ?\PDOStatement + { + $sth = null; + try { + $sth = $this->prepare(); + if ($sth !== null) { + $sth->execute(); + } if ($this->hasPostQuery) { $sqls = $this->grammar->compilePostQueries($this); @@ -402,7 +444,15 @@ public function execute() : ?\PDOStatement // @codeCoverageIgnoreStart \phpOMS\Log\FileLogger::getInstance()->error( \phpOMS\Log\FileLogger::MSG_FULL, [ - 'message' => $t->getMessage() . ':' . $sql, + 'message' => $t->getMessage() . ':' . $this->toSql(), + 'line' => __LINE__, + 'file' => self::class, + ] + ); + + \phpOMS\Log\FileLogger::getInstance()->error( + \phpOMS\Log\FileLogger::MSG_FULL, [ + 'message' => \json_encode($this->binds), 'line' => __LINE__, 'file' => self::class, ] diff --git a/tests/Utils/IO/Spreadsheet/SpreadsheetDatabaseMapperTest.php b/tests/Utils/IO/Spreadsheet/SpreadsheetDatabaseMapperTest.php index 90f54af6a..363f67e0a 100755 --- a/tests/Utils/IO/Spreadsheet/SpreadsheetDatabaseMapperTest.php +++ b/tests/Utils/IO/Spreadsheet/SpreadsheetDatabaseMapperTest.php @@ -72,8 +72,8 @@ protected function tearDown() : void #[\PHPUnit\Framework\Attributes\TestDox('Data can be inserted into a database from an ods files')] public function testInsertOds() : void { - $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.ods'); - $mapper->insert(); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite); + $mapper->import(__DIR__ . '/insert.ods', 'insert_1'); $builder = new Builder($this->sqlite, true); $data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? []; @@ -104,8 +104,8 @@ public function testInsertOds() : void #[\PHPUnit\Framework\Attributes\TestDox('Data can be inserted into a database from a xls files')] public function testInsertXls() : void { - $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xls'); - $mapper->insert(); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite); + $mapper->import(__DIR__ . '/insert.xls', 'insert_1'); $builder = new Builder($this->sqlite, true); $data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? []; @@ -136,8 +136,8 @@ public function testInsertXls() : void #[\PHPUnit\Framework\Attributes\TestDox('Data can be inserted into a database from a xlsx files')] public function testInsertXlsx() : void { - $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xlsx'); - $mapper->insert(); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite); + $mapper->import(__DIR__ . '/insert.xlsx', 'insert_1'); $builder = new Builder($this->sqlite, true); $data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? []; @@ -168,8 +168,8 @@ public function testInsertXlsx() : void #[\PHPUnit\Framework\Attributes\TestDox('Data can be updated in a database from an ods files')] public function testUpdateOds() : void { - $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.ods'); - $mapper->insert(); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite); + $mapper->import(__DIR__ . '/insert.ods', 'insert_1'); $builder = new Builder($this->sqlite, true); $data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? []; @@ -195,8 +195,8 @@ public function testUpdateOds() : void $data ); - $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/update.ods'); - $mapper->update(); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite); + $mapper->update(__DIR__ . '/update.ods', 'insert_1'); $builder = new Builder($this->sqlite, true); $data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? []; @@ -227,8 +227,8 @@ public function testUpdateOds() : void #[\PHPUnit\Framework\Attributes\TestDox('Data can be updated in a database from a xls files')] public function testUpdateXls() : void { - $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xls'); - $mapper->insert(); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite); + $mapper->import(__DIR__ . '/insert.xls', 'insert_1'); $builder = new Builder($this->sqlite, true); $data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? []; @@ -254,8 +254,8 @@ public function testUpdateXls() : void $data ); - $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/update.xls'); - $mapper->update(); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite); + $mapper->update(__DIR__ . '/update.xls', 'insert_1'); $builder = new Builder($this->sqlite, true); $data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? []; @@ -286,8 +286,8 @@ public function testUpdateXls() : void #[\PHPUnit\Framework\Attributes\TestDox('Data can be updated in a database from a xlsx files')] public function testUpdateXlsx() : void { - $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xlsx'); - $mapper->insert(); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite); + $mapper->import(__DIR__ . '/insert.xlsx', 'insert_1'); $builder = new Builder($this->sqlite, true); $data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? []; @@ -313,8 +313,8 @@ public function testUpdateXlsx() : void $data ); - $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/update.xlsx'); - $mapper->update(); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite); + $mapper->update(__DIR__ . '/update.xlsx', 'insert_1'); $builder = new Builder($this->sqlite, true); $data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? []; @@ -349,8 +349,8 @@ public function testSelectOds() : void \unlink(__DIR__ . '/select.ods'); } - $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.ods'); - $mapper->insert(); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite); + $mapper->import(__DIR__ . '/insert.ods', 'insert_1'); $builder = new Builder($this->sqlite, true); $data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? []; @@ -381,7 +381,7 @@ public function testSelectOds() : void $builder = new Builder($this->sqlite, true); $data = $builder->select('int', 'decimal', 'bool', 'varchar', 'datetime')->from('insert_1'); - $mapper->select([$builder]); + $mapper->export(__DIR__ . '/select.ods', [$builder]); self::assertTrue($this->compareSelectInsertSheet(__DIR__ . '/select.ods', __DIR__ . '/insert.ods')); @@ -398,8 +398,8 @@ public function testSelectXls() : void \unlink(__DIR__ . '/select.xls'); } - $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xls'); - $mapper->insert(); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite); + $mapper->import(__DIR__ . '/insert.xls', 'insert_1'); $builder = new Builder($this->sqlite, true); $data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? []; @@ -430,7 +430,7 @@ public function testSelectXls() : void $builder = new Builder($this->sqlite, true); $data = $builder->select('int', 'decimal', 'bool', 'varchar', 'datetime')->from('insert_1'); - $mapper->select([$builder]); + $mapper->export(__DIR__ . '/select.xls', [$builder]); self::assertTrue($this->compareSelectInsertSheet(__DIR__ . '/select.xls', __DIR__ . '/insert.xls')); @@ -447,8 +447,8 @@ public function testSelectXlsx() : void \unlink(__DIR__ . '/select.xlsx'); } - $mapper = new SpreadsheetDatabaseMapper($this->sqlite, __DIR__ . '/insert.xlsx'); - $mapper->insert(); + $mapper = new SpreadsheetDatabaseMapper($this->sqlite); + $mapper->import(__DIR__ . '/insert.xlsx', 'insert_1'); $builder = new Builder($this->sqlite, true); $data = $builder->select('insert_1.*')->from('insert_1')->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? []; @@ -479,7 +479,7 @@ public function testSelectXlsx() : void $builder = new Builder($this->sqlite, true); $data = $builder->select('int', 'decimal', 'bool', 'varchar', 'datetime')->from('insert_1'); - $mapper->select([$builder]); + $mapper->export(__DIR__ . '/select.xlsx', [$builder]); self::assertTrue($this->compareSelectInsertSheet(__DIR__ . '/select.xlsx', __DIR__ . '/insert.xlsx'));