From ab6bc90a1cae925bc23fd5c245ef2dd4e509b703 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 9 Apr 2020 22:23:27 +0200 Subject: [PATCH] some method cleanup and methods for sql-success, errorCode, errorMessage --- composer.json | 3 ++ src/BetterStatement.php | 83 +++++++++++++++++++++++++++------- src/DatabaseWrapUtil.php | 26 ++++++++++- tests/DatabaseWrapUtilTest.php | 36 +++++++++++++++ 4 files changed, 130 insertions(+), 18 deletions(-) create mode 100644 tests/DatabaseWrapUtilTest.php diff --git a/composer.json b/composer.json index e7956c5..d7db9b8 100644 --- a/composer.json +++ b/composer.json @@ -9,5 +9,8 @@ "require": { "ext-json": "*", "ext-pdo": "*" + }, + "require-dev": { + "phpunit/phpunit": "^8" } } diff --git a/src/BetterStatement.php b/src/BetterStatement.php index 071f019..fc3999f 100644 --- a/src/BetterStatement.php +++ b/src/BetterStatement.php @@ -5,7 +5,8 @@ use PDO; use PDOStatement; -class BetterStatement { +class BetterStatement +{ /** @var PDO $server */ private $pdo; @@ -16,12 +17,25 @@ class BetterStatement { /** @var bool $executed */ private $executed = false; - function __construct($pdo, $sql) { + private $success = null; + + //error code in ansi standard + private $errorCodeAnsi = null; + + //driver specific error code + private $errorCode = null; + + //driver specific error code + private $errorMessage = null; + + function __construct($pdo, $sql) + { $this->pdo = $pdo; $this->statement = $this->pdo->prepare($sql); } - public function addParam($param, $value) { + public function addParam($param, $value) + { $stmt = $this->statement; if (is_bool($value)) { @@ -34,28 +48,64 @@ public function addParam($param, $value) { return $this; } - public function bindParam($param, $value) { - return $this->addParam($param, $value); - } - - public function execute($array = null) { + public function execute() + { $this->executed = true; - $this->statement->execute($array); + $this->success = $this->statement->execute(); + + $errorInfo = $this->statement->errorInfo(); + + $this->errorCodeAnsi = $errorInfo[0]; + $this->errorCode = $errorInfo[1]; + $this->errorMessage = $errorInfo[2]; + return $this; } - /** - * @return bool - */ - public function isExecuted(): bool { + public function isExecuted(): bool + { return $this->executed; } - public function getInsertedId() { + public function getSuccess(): bool + { + if (!isset($this->success)) { + throw new BetterPdoException('Statement not executed yet!'); + } + return $this->success; + } + + public function getErrorCodeAnsi() + { + if (!isset($this->success)) { + throw new BetterPdoException('Statement not executed yet!'); + } + return $this->errorCodeAnsi; + } + + public function getErrorCode() + { + if (!isset($this->success)) { + throw new BetterPdoException('Statement not executed yet!'); + } + return $this->errorCode; + } + + public function getErrorMessage() + { + if (!isset($this->success)) { + throw new BetterPdoException('Statement not executed yet!'); + } + return $this->errorMessage; + } + + public function getInsertedId() + { return $this->pdo->lastInsertId(); } - public function fetch($fetchStyle, $extraParam = null) { + public function fetch($fetchStyle, $extraParam = null) + { if (!$this->isExecuted()) { $this->execute(); } @@ -79,7 +129,8 @@ public function fetch($fetchStyle, $extraParam = null) { return $this->statement->fetch($fetchStyle); } - public function fetchAll($fetchStyle, $extraParam = null) { + public function fetchAll($fetchStyle, $extraParam = null) + { if (!$this->isExecuted()) { $this->execute(); } diff --git a/src/DatabaseWrapUtil.php b/src/DatabaseWrapUtil.php index 7430906..2585679 100644 --- a/src/DatabaseWrapUtil.php +++ b/src/DatabaseWrapUtil.php @@ -25,12 +25,13 @@ public static function assocToObject($classPath, $assocData) { foreach ($assocData as $key => $value) { $fieldName = self::snakeToPascalCase($key); - $property = $clazz->getProperty($fieldName); + $property = self::findPropertyByName($clazz, $fieldName); if ($property != null) { $property->setAccessible(true); $property->setValue($instance, $value); } + } return $instance; @@ -42,4 +43,25 @@ public static function assocToObject($classPath, $assocData) { private static function snakeToPascalCase($target) { return str_replace('_', '', ucwords($target, '_')); } -} \ No newline at end of file + + private static function findPropertyByName(ReflectionClass $clazz, string $name) { + $lcName = strtolower($name); + + foreach ($clazz->getProperties() as $property) { + + $lcPropName = strtolower($property->name); + + //try with same name + if ($lcName == $lcPropName) { + return $property; + } + + //try without underscores + if (str_replace('_', '', $lcName) == str_replace('_', '', $lcPropName)) { + return $property; + } + } + + return null; + } +} diff --git a/tests/DatabaseWrapUtilTest.php b/tests/DatabaseWrapUtilTest.php new file mode 100644 index 0000000..8166491 --- /dev/null +++ b/tests/DatabaseWrapUtilTest.php @@ -0,0 +1,36 @@ + 'maaxgr', + 'password' => '1234', + 'first_name' => 'Max', + 'last_name' => 'Gr' + ); + + /** @var TestUser $obj */ + $obj = DatabaseWrapUtil::assocToObject(TestUser::class, $input); + + $this->assertEquals('Max', $obj->firstName); + $this->assertEquals('Gr', $obj->lastName); + $this->assertEquals('1234', $obj->password); + $this->assertEquals('maaxgr', $obj->user_name); + } + +} + +class TestUser { + public $user_name; + public $password; + public $firstName; + public $lastName; +}