Skip to content

Commit

Permalink
some method cleanup and methods for sql-success, errorCode, errorMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
Max committed Apr 9, 2020
1 parent 585fe3e commit ab6bc90
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 18 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"require": {
"ext-json": "*",
"ext-pdo": "*"
},
"require-dev": {
"phpunit/phpunit": "^8"
}
}
83 changes: 67 additions & 16 deletions src/BetterStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use PDO;
use PDOStatement;

class BetterStatement {
class BetterStatement
{

/** @var PDO $server */
private $pdo;
Expand All @@ -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)) {
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
26 changes: 24 additions & 2 deletions src/DatabaseWrapUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,4 +43,25 @@ public static function assocToObject($classPath, $assocData) {
private static function snakeToPascalCase($target) {
return str_replace('_', '', ucwords($target, '_'));
}
}

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;
}
}
36 changes: 36 additions & 0 deletions tests/DatabaseWrapUtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace de\maaxgr\betterpdo;

use gr\maax\betterpdo\DatabaseWrapUtil;
use PHPUnit\Framework\TestCase;

class DatabaseWrapUtilTest extends TestCase
{

public function test()
{
$input = array(
'username' => '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;
}

0 comments on commit ab6bc90

Please sign in to comment.