feat: use PDOWithFilters instead of Gbili\Db\Req
This commit is contained in:
@@ -16,12 +16,12 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type" : "vcs",
|
"type" : "vcs",
|
||||||
"url" : "https://usrpath@bitbucket.org/usrpath/gbilidbreq.git"
|
"url" : "https://usrpath@bitbucket.org/usrpath/pdowithfilters.git"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require" : {
|
"require" : {
|
||||||
"bsr/utils" : "dev-master",
|
"bsr/utils" : "dev-master",
|
||||||
"gbili/dbreq" : "dev-master"
|
"gbili/pdowithfilters" : "dev-master"
|
||||||
},
|
},
|
||||||
"minimum-stability" : "dev",
|
"minimum-stability" : "dev",
|
||||||
"autoload": {
|
"autoload": {
|
||||||
|
|||||||
@@ -3,15 +3,22 @@ namespace Bsr\Db;
|
|||||||
|
|
||||||
use Bsr\Db\Exception\SqlException;
|
use Bsr\Db\Exception\SqlException;
|
||||||
use Gbili\Db\Req\Req;
|
use Gbili\Db\Req\Req;
|
||||||
|
use Gbili\PDOWithFilters\PDOWithFilters;
|
||||||
|
|
||||||
class Connection
|
class Connection
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Gbili\Db\Req\Req
|
* All params are treated as \PDO::PARAM_STR
|
||||||
*/
|
* Manually bindParam with a specific \PDO::PARAM_TYPE
|
||||||
private static $req;
|
* 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 $query
|
||||||
* @param bool $throw_error
|
* @param bool $throw_error
|
||||||
* @return OdbcResultSet|resource|string
|
* @return OdbcResultSet|resource|string
|
||||||
@@ -19,7 +26,24 @@ class Connection
|
|||||||
*/
|
*/
|
||||||
public static function execute($sql, $values = array(), $fetchMode = \PDO::FETCH_ASSOC)
|
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 $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() {}
|
private function __clone() {}
|
||||||
}
|
}
|
||||||
|
|||||||
8
src/Db/Exception/SqlException.php
Normal file
8
src/Db/Exception/SqlException.php
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Bsr\Db\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception raised when an invalid attribute name is accessed
|
||||||
|
*/
|
||||||
|
class SqlException extends \Exception { }
|
||||||
Reference in New Issue
Block a user