feat: use PDOWithFilters instead of Gbili\Db\Req

This commit is contained in:
Guillermo Pages
2021-09-13 12:48:18 +02:00
parent 6069a65956
commit b64b7f72fb
3 changed files with 40 additions and 27 deletions

View File

@@ -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": {

View File

@@ -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() {}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace Bsr\Db\Exception;
/**
* Exception raised when an invalid attribute name is accessed
*/
class SqlException extends \Exception { }