From 790f79530eae17399286f8d43cf12e038a7dc21d Mon Sep 17 00:00:00 2001 From: Stephan Kasdorf Date: Fri, 6 Nov 2020 17:34:36 +0100 Subject: [PATCH] Version 0.9.5 beta - Updated the MySQL adapter in order to load complete result sets by where selection. --- core/a/mysql.db.php | 27 ++++++++++++------- core/c/pdo.php | 63 +++++++++++++++++++++++++++++++++++++++------ core/i/db.php | 9 +++++++ 3 files changed, 82 insertions(+), 17 deletions(-) diff --git a/core/a/mysql.db.php b/core/a/mysql.db.php index ccca137..9f93934 100644 --- a/core/a/mysql.db.php +++ b/core/a/mysql.db.php @@ -43,15 +43,6 @@ abstract class Db implements IDb } } - /** - * will return the last inserted id of the given table - * @return int - */ - public function lastInsertId() - { - return Pdo::getLastInsertedID(); - } - /** * @param bool $user_name * @return mixed @@ -117,6 +108,24 @@ abstract class Db implements IDb } } + /** + * @param array $fieldWhere + * @return mixed|void + */ + public function selectDatasetByFieldWhere($fieldWhere = array()) + { + return Pdo::selectDatasetByFieldAndValue(self::$table['table'], $fieldWhere); + } + + /** + * will return the last inserted id of the given table + * @return int + */ + public function lastInsertId() + { + return Pdo::getLastInsertedID(); + } + public function nextInsertIndex() { // TODO: Implement nextInsertIndex() method. diff --git a/core/c/pdo.php b/core/c/pdo.php index 0353649..6c5b22c 100644 --- a/core/c/pdo.php +++ b/core/c/pdo.php @@ -11,12 +11,12 @@ namespace Nibiru; final class Pdo extends Mysql implements IPdo { private static $section = false; - + public static function settingsSection( $section = IOdbc::SETTINGS_DATABASE ) { self::$section = $section; } - + private static function getSettingsSection() { return self::$section; @@ -57,7 +57,35 @@ final class Pdo extends Mysql implements IPdo public static function selectDatasetByFieldAndValue($tablename = self::PLACE_TABLE_NAME, $fieldAndValue = array() ) { $result = parent::getInstance( self::getSettingsSection() )->getConn()->query("SELECT * FROM " . $tablename . " WHERE " . $fieldAndValue['name'] . " = '" . $fieldAndValue['value'] . "';"); - return $result->fetchAll(); + $result = $result->fetchAll(); + $resultset = []; + if(array_key_exists(0, $result)) + { + foreach($result as $rowset) + { + $set = []; + foreach($rowset as $key=>$row) + { + if(!is_numeric($key)) + { + $set[$key] = $row; + } + } + $resultset[] = $set; + } + } + else + { + foreach($result as $key=>$row) + { + if(!is_numeric($row)) + { + $resultset[$key] = $row; + } + } + } + + return $resultset; } public static function updateColumnByFieldWhere( $tablename = self::PLACE_TABLE_NAME, @@ -121,14 +149,33 @@ final class Pdo extends Mysql implements IPdo $prepare->execute(); $r = $prepare->fetchAll(); $rowset = array_shift( $r ); - foreach(array_keys($rowset) as $entry) - { - if(is_string($entry)) + try{ + if(is_array($rowset)) { - $result[$entry] = $rowset[$entry]; + if(sizeof($rowset)>0) + { + foreach(array_keys($rowset) as $entry) + { + if(is_string($entry)) + { + $result[$entry] = $rowset[$entry]; + } + } + return $result; + } + } + else + { + return false; } } - return $result; + catch (\Exception $e) + { + echo '
';
+            print_r($e);
+            echo '
'; + die(); + } } /** diff --git a/core/i/db.php b/core/i/db.php index 87f0777..841d2d2 100644 --- a/core/i/db.php +++ b/core/i/db.php @@ -29,6 +29,7 @@ interface IDb * @return mixed */ public function loadTableAsArray(); + /** * @desc Has to select a given Rowset by the index ID of the table * @param bool $id @@ -61,6 +62,14 @@ interface IDb */ public function insertArrayIntoTable( $dataset = array() ); + /** + * @desc will return a result array from the searched where field from the database + * containing the entire dataset rows + * @param array $fieldWhere + * @return mixed + */ + public function selectDatasetByFieldWhere( $fieldWhere = array() ); + /** * @desc selects a row by the fieldname and the given value, should be * array('fieldname' => 'value')