From dfb581d21a0185b166af631a4365152e38477f42 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Fri, 3 Mar 2023 23:17:08 -0300 Subject: [PATCH] Database result object --- system/database/autoload.php | 14 +-- system/database/databases/mpdo.php | 10 ++- system/database/databases/mssql.php | 2 +- system/database/databases/mysql_legacy.php | 8 +- system/database/databases/mysql_pdo.php | 9 +- system/database/databases/mysqli.php | 15 ++-- system/database/databases/nullStatement.php | 7 +- system/database/databases/object/result.php | 87 +++++++++++++++++++ .../databases/object/resultInterface.php | 65 ++++++++++++++ system/database/databases/oracle.php | 19 ++-- system/database/databases/postgre.php | 13 +-- system/database/databases/sqlite3_db.php | 13 +-- system/database/databases/sqlsrv.php | 10 ++- system/database/databases/sqlsrvpdo.php | 15 ++-- system/engine/interfaces/databases.php | 2 +- 15 files changed, 235 insertions(+), 54 deletions(-) create mode 100644 system/database/databases/object/result.php create mode 100644 system/database/databases/object/resultInterface.php diff --git a/system/database/autoload.php b/system/database/autoload.php index ae1f7a2..1c9bda2 100644 --- a/system/database/autoload.php +++ b/system/database/autoload.php @@ -105,15 +105,15 @@ final class Database { public function __destruct() { unset($this->driver); } - - /** + + /** * Execute the SQL Query * - * @param string $sql - * @param bool $cacheUse - * @return object|\Phacil\Framework\Database::Cache - * @throws PhpfastcacheInvalidArgumentException - */ + * @param string $sql + * @param bool $cacheUse + * @return \Phacil\Framework\Databases\Object\ResultInterface|\Phacil\Framework\Database::Cache + * @throws PhpfastcacheInvalidArgumentException + */ public function query($sql, $cacheUse = true) { if(Config::SQL_CACHE() && $cacheUse == true) { diff --git a/system/database/databases/mpdo.php b/system/database/databases/mpdo.php index 97b3177..f48089a 100644 --- a/system/database/databases/mpdo.php +++ b/system/database/databases/mpdo.php @@ -22,6 +22,12 @@ final class mPDO implements Databases { */ private $connection = null; + /** + * + * @var int + */ + private $rowCount; + /** * * @var mixed @@ -89,7 +95,7 @@ final class mPDO implements Databases { // free up resources $this->statement->closeCursor(); $this->statement = null; - $result = new \stdClass(); + $result = new \Phacil\Framework\Databases\Object\Result(); $result->row = (isset($data[0]) ? $data[0] : array()); $result->rows = $data; $result->num_rows = $this->rowCount; @@ -100,7 +106,7 @@ final class mPDO implements Databases { if ($result) { return $result; } else { - $result = new \stdClass(); + $result = new \Phacil\Framework\Databases\Object\Result(); $result->row = array(); $result->rows = array(); $result->num_rows = 0; diff --git a/system/database/databases/mssql.php b/system/database/databases/mssql.php index aafe7b6..3519f02 100644 --- a/system/database/databases/mssql.php +++ b/system/database/databases/mssql.php @@ -50,7 +50,7 @@ final class MSSQL implements \Phacil\Framework\Interfaces\Databases mssql_free_result($resource); - $query = new stdClass(); + $query = new \Phacil\Framework\Databases\Object\Result(); $query->row = isset($data[0]) ? $data[0] : array(); $query->rows = $data; $query->num_rows = $i; diff --git a/system/database/databases/mysql_legacy.php b/system/database/databases/mysql_legacy.php index 70c4d67..cf4a979 100644 --- a/system/database/databases/mysql_legacy.php +++ b/system/database/databases/mysql_legacy.php @@ -34,6 +34,12 @@ final class MySQL_legacy implements \Phacil\Framework\Interfaces\Databases { public function isConnected() { } + /** + * + * @param string $sql + * @return \Phacil\Framework\Databases\Object\ResultInterface|true + * @throws \Phacil\Framework\Exception + */ public function query($sql) { $resource = mysql_query($sql, $this->connection); @@ -51,7 +57,7 @@ final class MySQL_legacy implements \Phacil\Framework\Interfaces\Databases { mysql_free_result($resource); - $query = new stdClass(); + $query = new \Phacil\Framework\Databases\Object\Result(); $query->row = isset($data[0]) ? $data[0] : array(); $query->rows = $data; $query->num_rows = $i; diff --git a/system/database/databases/mysql_pdo.php b/system/database/databases/mysql_pdo.php index da970e1..1a9f9c1 100644 --- a/system/database/databases/mysql_pdo.php +++ b/system/database/databases/mysql_pdo.php @@ -83,16 +83,17 @@ final class MYSQL_PDO implements Databases throw new \Phacil\Framework\Exception($exception->getMessage()); } } + /** * Query the database - * - * @param string $sql - * @return \stdClass + * @param string $sql + * @return \Phacil\Framework\Databases\Object\ResultInterface|true + * @throws \PDOException */ public function query($sql = null) { if ($this->dbh) { - $data = new \stdClass; + $data = new \Phacil\Framework\Databases\Object\Result(); $sth=$this->dbh->prepare($sql); $sth->execute(); diff --git a/system/database/databases/mysqli.php b/system/database/databases/mysqli.php index f90e4ca..831d9a4 100644 --- a/system/database/databases/mysqli.php +++ b/system/database/databases/mysqli.php @@ -54,11 +54,10 @@ class MySQLi implements Databases { } /** - * Execute the SQL Query. - * + * Execute the SQl Query * @param string $sql - * @return stdClass|true - * @throws Exception + * @return \Phacil\Framework\Databases\Object\ResultInterface|true + * @throws \Exception */ public function query($sql) { $query = $this->connection->query($sql); @@ -68,10 +67,10 @@ class MySQLi implements Databases { while ($row = $query->fetch_assoc()) { $data[] = $row; } - $result = new \stdClass(); - $result->num_rows = $query->num_rows; - $result->row = isset($data[0]) ? $data[0] : array(); - $result->rows = $data; + $result = new \Phacil\Framework\Databases\Object\Result(); + $result->setNumRows($query->num_rows); + $result->setRow(isset($data[0]) ? $data[0] : []); + $result->setRows($data); $query->close(); return $result; } else { diff --git a/system/database/databases/nullStatement.php b/system/database/databases/nullStatement.php index 800a59d..22524d9 100644 --- a/system/database/databases/nullStatement.php +++ b/system/database/databases/nullStatement.php @@ -27,8 +27,13 @@ final class nullStatement implements Databases { return false; } + /** + * + * @param string $sql + * @return \Phacil\Framework\Databases\Object\ResultInterface|true + */ public function query($sql) { - $result = new \stdClass(); + $result = new \Phacil\Framework\Databases\Object\Result(); $result->num_rows = NULL; $result->row = NULL; $result->rows = NULL; diff --git a/system/database/databases/object/result.php b/system/database/databases/object/result.php new file mode 100644 index 0000000..58e6fe8 --- /dev/null +++ b/system/database/databases/object/result.php @@ -0,0 +1,87 @@ +getRow($numRow) : $this->getRows(); + } + + /** + * {@inheritdoc} + */ + public function setRows($rows){ + $this->rows = $rows; + return $this; + } + + /** + * {@inheritdoc} + */ + public function getRows(){ + return $this->rows; + } + + /** + * {@inheritdoc} + */ + public function setRow($row){ + $this->row = $row; + return $this; + } + + /** + * {@inheritdoc} + */ + public function getRow($numRow = false){ + return $numRow ? ($this->rows[$numRow + 1]?? null ) : $this->row; + } + + /** + * + * {@inheritdoc} + */ + public function setNumRows($num){ + $this->num_rows = $num; + return $this; + } + + /** + * {@inheritdoc} + */ + public function getNumRows(){ + return $this->num_rows; + } + +} \ No newline at end of file diff --git a/system/database/databases/object/resultInterface.php b/system/database/databases/object/resultInterface.php new file mode 100644 index 0000000..e225267 --- /dev/null +++ b/system/database/databases/object/resultInterface.php @@ -0,0 +1,65 @@ +connection, $sql); @@ -57,10 +64,10 @@ final class Oracle implements Databases { if (!$this->connection) { oci_fetch_all($stid, $res); - $result = new \stdClass(); - $result->num_rows = oci_num_rows($stid); - $result->row = isset($res[0]) ? $res[0] : array(); - $result->rows = $res; + $result = new \Phacil\Framework\Databases\Object\Result(); + $result->setNumRows(oci_num_rows($stid)); + $result->setRow(isset($res[0]) ? $res[0] : []); + $result->setRows($res); return $result; diff --git a/system/database/databases/postgre.php b/system/database/databases/postgre.php index e8b4626..626fb37 100644 --- a/system/database/databases/postgre.php +++ b/system/database/databases/postgre.php @@ -43,9 +43,10 @@ final class Postgre implements Databases { } /** + * * @param string $sql - * @return stdClass|true - * @throws Exception + * @return \Phacil\Framework\Databases\Object\ResultInterface|true + * @throws \Exception */ public function query($sql) { $resource = pg_query($this->link, $sql); @@ -58,10 +59,10 @@ final class Postgre implements Databases { $i++; } pg_free_result($resource); - $query = new \stdClass(); - $query->row = isset($data[0]) ? $data[0] : array(); - $query->rows = $data; - $query->num_rows = $i; + $query = new \Phacil\Framework\Databases\Object\Result(); + $query->setRow(isset($data[0]) ? $data[0] : array()); + $query->setRows($data); + $query->setNumRows($i); unset($data); return $query; } else { diff --git a/system/database/databases/sqlite3_db.php b/system/database/databases/sqlite3_db.php index 0e8676b..785cae5 100644 --- a/system/database/databases/sqlite3_db.php +++ b/system/database/databases/sqlite3_db.php @@ -29,9 +29,10 @@ final class Sqlite3_db implements Databases { } /** + * * @param string $sql - * @return true|stdClass - * @throws Exception + * @return \Phacil\Framework\Databases\Object\ResultInterface|true + * @throws \Exception */ public function query($sql){ //$query = $this->connection->query($sql); @@ -48,10 +49,10 @@ final class Sqlite3_db implements Databases { while ($row = $query->fetchArray(SQLITE3_ASSOC)) { $data[] = $row; } - $result = new \stdClass(); - $result->num_rows = (!empty($data)) ? count($data) : 0; - $result->row = isset($data[0]) ? $data[0] : array(); - $result->rows = $data; + $result = new \Phacil\Framework\Databases\Object\Result(); + $result->setNumRows((!empty($data)) ? count($data) : 0); + $result->setRow(isset($data[0]) ? $data[0] : array()); + $result->setRows($data); $query->finalize(); return $result; diff --git a/system/database/databases/sqlsrv.php b/system/database/databases/sqlsrv.php index bfeecfb..9efe9e8 100644 --- a/system/database/databases/sqlsrv.php +++ b/system/database/databases/sqlsrv.php @@ -47,8 +47,8 @@ final class SQLSRV implements Databases { } */ - sqlsrv_query($this->link,"SET NAMES 'utf8'"); - sqlsrv_query($this->link, "SET CHARACTER SET utf8"); + \sqlsrv_query($this->link,"SET NAMES 'utf8'"); + \sqlsrv_query($this->link, "SET CHARACTER SET utf8"); } public function isConnected() { @@ -56,8 +56,10 @@ final class SQLSRV implements Databases { } /** + * * @param string $sql - * @return stdClass|true + * @return \Phacil\Framework\Databases\Object\ResultInterface|true + * @throws \Phacil\Framework\Exception */ public function query($sql) { $resource = \sqlsrv_query($this->link, $sql); @@ -76,7 +78,7 @@ final class SQLSRV implements Databases { \sqlsrv_free_stmt($resource); - $query = new \stdClass(); + $query = new \Phacil\Framework\Databases\Object\Result(); $query->row = isset($data[0]) ? $data[0] : array(); $query->rows = $data; $query->num_rows = $i; diff --git a/system/database/databases/sqlsrvpdo.php b/system/database/databases/sqlsrvpdo.php index c3bf563..e4ad363 100644 --- a/system/database/databases/sqlsrvpdo.php +++ b/system/database/databases/sqlsrvpdo.php @@ -81,10 +81,11 @@ final class sqlsrvPDO implements Databases { } /** + * * @param string $sql * @param array $params - * @return stdClass - * @throws Exception + * @return \Phacil\Framework\Databases\Object\ResultInterface|true + * @throws \Exception */ public function query($sql, $params = array()) { $this->statement = $this->connection->prepare($sql); @@ -99,10 +100,10 @@ final class sqlsrvPDO implements Databases { $data[] = $row; } - $result = new \stdClass(); - $result->row = (isset($data[0]) ? $data[0] : array()); - $result->rows = $data; - $result->num_rows = $this->statement->rowCount(); + $result = new \Phacil\Framework\Databases\Object\Result(); + $result->setRow((isset($data[0])) ? $data[0] : array()); + $result->setRows($data); + $result->setNumRows($this->statement->rowCount()); } } catch (\PDOException $e) { throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . '
' . $sql); @@ -111,7 +112,7 @@ final class sqlsrvPDO implements Databases { if ($result) { return $result; } else { - $result = new \stdClass(); + $result = new \Phacil\Framework\Databases\Object\Result(); $result->row = array(); $result->rows = array(); $result->num_rows = 0; diff --git a/system/engine/interfaces/databases.php b/system/engine/interfaces/databases.php index 1a41e9c..9c686cb 100644 --- a/system/engine/interfaces/databases.php +++ b/system/engine/interfaces/databases.php @@ -29,7 +29,7 @@ * Execute the SQL Query. * * @param string $sql - * @return \stdClass|true + * @return \Phacil\Framework\Databases\Object\ResultInterface|true * @throws Exception */ public function query($sql);