From b64b7f72fbf5d97ec8b201670b7559f4f05c4443 Mon Sep 17 00:00:00 2001 From: Guillermo Pages Date: Mon, 13 Sep 2021 12:48:18 +0200 Subject: [PATCH] feat: use PDOWithFilters instead of Gbili\Db\Req --- composer.json | 4 +-- src/Db/Connection.php | 55 +++++++++++++++++-------------- src/Db/Exception/SqlException.php | 8 +++++ 3 files changed, 40 insertions(+), 27 deletions(-) create mode 100644 src/Db/Exception/SqlException.php diff --git a/composer.json b/composer.json index fa05957..cda9248 100644 --- a/composer.json +++ b/composer.json @@ -16,12 +16,12 @@ }, { "type" : "vcs", - "url" : "https://usrpath@bitbucket.org/usrpath/gbilidbreq.git" + "url" : "https://usrpath@bitbucket.org/usrpath/pdowithfilters.git" } ], "require" : { "bsr/utils" : "dev-master", - "gbili/dbreq" : "dev-master" + "gbili/pdowithfilters" : "dev-master" }, "minimum-stability" : "dev", "autoload": { diff --git a/src/Db/Connection.php b/src/Db/Connection.php index 0e0547e..1b34af1 100644 --- a/src/Db/Connection.php +++ b/src/Db/Connection.php @@ -3,15 +3,22 @@ namespace Bsr\Db; use Bsr\Db\Exception\SqlException; use Gbili\Db\Req\Req; +use Gbili\PDOWithFilters\PDOWithFilters; class Connection { /** - * @var Gbili\Db\Req\Req - */ - private static $req; - - /** + * All params are treated as \PDO::PARAM_STR + * Manually bindParam with a specific \PDO::PARAM_TYPE + * if \PDO::PARAM_STR does not work for you. + * To do it manually copy paste the code below and replace the + * $statement = PDOWithFilters::prepare($sql); + * $success = $statement->execute($values); + * With: + * $statement = PDOWithFilters::prepare($sql); + * $statement->bindParam(':myParam', $values[':myParam'], \PDO::PARAM_TYPE); + * $success = $statement->execute(); + * * @param $query * @param bool $throw_error * @return OdbcResultSet|resource|string @@ -19,7 +26,24 @@ class Connection */ public static function execute($sql, $values = array(), $fetchMode = \PDO::FETCH_ASSOC) { - return new OdbcResultset(self::getReq()->getResultSet($sql, $values, $fetchMode)); + $statement = PDOWithFilters::prepare($sql); + $success = $statement->execute($values); + + if (!$success) { + $statement->closeCursor(); + throw new SqlException('SQL There was an error executing the request'); + } + + $rows = $statement->fetchAll($fetchMode); + + if (!is_array($rows)) { + $statement->closeCursor(); + throw new SqlException('PDO Unable to retrieve the result'); + } + + $statement->closeCursor(); + + return new OdbcResultset($rows); } /** @@ -38,24 +62,5 @@ class Connection return $res; } - /** - * @return PDO connection - */ - public static function get() - { - return self::getReq()->getAdapter(); - } - - /** - * @return Gbili\Db\Req\Req - */ - public static function getReq() - { - if (null === self::$req) { - self::$req = new Req(); - } - return self::$req; - } - private function __clone() {} } diff --git a/src/Db/Exception/SqlException.php b/src/Db/Exception/SqlException.php new file mode 100644 index 0000000..64c1c99 --- /dev/null +++ b/src/Db/Exception/SqlException.php @@ -0,0 +1,8 @@ +