From efa2db60f25e5660a643ea90b7dece998e607b6a Mon Sep 17 00:00:00 2001 From: akulikov Date: Thu, 7 May 2009 08:20:46 +0000 Subject: [PATCH] restoration after wrong rollback git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/trunk@89 4cb57b5f-5bbd-dd11-951b-001d605cbbc5 --- classes/DBConnector.class.php | 48 +++++++++++++++++++++++++++++++++++++++++++ classes/Env.class.php | 6 +++--- classes/Model.class.php | 22 ++++++++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/classes/DBConnector.class.php b/classes/DBConnector.class.php index c26f3e8..e6d4e5e 100644 --- a/classes/DBConnector.class.php +++ b/classes/DBConnector.class.php @@ -13,6 +13,15 @@ */ class DBConnector { + + /** + * PDO constant values. + */ + const FETCH_ASSOC = 2; + const FETCH_BOTH = 4; + const FETCH_NUM = 3; + const FETCH_OBJ = 5; + static private $handlers = array(); static public $queries = array(); @@ -78,6 +87,45 @@ class DBConnector return $class_name ? mysqli_fetch_object($result, $class_name) : mysqli_fetch_object($result); } + /** + * Fetches a row from the result set. + * + * @param mysqli_result $result + * @param int $style OPTIONAL Fetch mode for this fetch operation. + * @return mixed Array, object, or scalar depending on fetch mode. + * @throws Exception + */ + static public function fetchArray($result, $style = null) + { + if (!$result) { + return false; + } + + if ($style === null) { + $style = self::FETCH_ASSOC; + } + + $row = false; + switch ($style) { + case self::FETCH_NUM: + $row = mysqli_fetch_array($result, MYSQLI_NUM); + break; + case self::FETCH_ASSOC: + $row = mysqli_fetch_array($result, MYSQLI_ASSOC); + break; + case self::FETCH_BOTH: + $row = mysqli_fetch_array($result, MYSQLI_BOTH); + break; + case self::FETCH_OBJ: + $row = mysqli_fetch_object($result, MYSQLI_BOTH); + break; + default: + throw new Exception('Invalid fetch mode "' . $style . '" specified'); + break; + } + return $row; + } + static public function numRows($result) { return mysqli_num_rows($result); diff --git a/classes/Env.class.php b/classes/Env.class.php index b3104ab..38ff0d7 100644 --- a/classes/Env.class.php +++ b/classes/Env.class.php @@ -33,15 +33,15 @@ final class Env { return isset($_SESSION[$var]) ? $_SESSION[$var] : $default; } - + static function setSession($var, $value) { $_SESSION[$var] = $value; } - + /** * Unsets session var - * + * * @param string $var */ static function unsetSession($var) diff --git a/classes/Model.class.php b/classes/Model.class.php index eabd935..9e3ea93 100644 --- a/classes/Model.class.php +++ b/classes/Model.class.php @@ -183,6 +183,28 @@ class ModelSelectResult extends ModelResult return $array; } + /** + * Fetches all SQL result rows as an array of key-value pairs. + * + * The first column is the key, the second column is the + * value. + * + * @return array + */ + public function fetchPairs() + { + if (!method_exists('DBConnector', 'fetchArray')) { + throw new Exception('Method not implemented yet.'); + } + + $data = array(); + while ($row = DBConnector::fetchArray($this->result, DBConnector::FETCH_NUM)) { + $data[$row[0]] = $row[1]; + } + return $data; + } + + function count() { return DBConnector::numRows($this->result);