1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-02 12:21:06 +02:00

+ DibiResult::fetchAll()

* changed year in headers (2007)
This commit is contained in:
David Grudl
2007-01-08 00:55:11 +00:00
parent 3b8766d376
commit ac980fe8c9
14 changed files with 143 additions and 49 deletions

File diff suppressed because one or more lines are too long

View File

@@ -10,7 +10,7 @@
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/ * @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl * @copyright Copyright (c) 2005-2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
* @category Database * @category Database
@@ -178,7 +178,7 @@ class dibi
/** like $conn = $className::connect($config); */ /** like $conn = $className::connect($config); */
self::$conn = self::$registry[$name] = call_user_func(array($className, 'connect'), $config); self::$conn = self::$registry[$name] = call_user_func(array($className, 'connect'), $config);
dibi::log("Successfully connected to DB '$config[driver]'"); if (dibi::$debug) dibi::log("Successfully connected to DB '$config[driver]'");
} }
@@ -280,6 +280,13 @@ class dibi
); );
} }
if (dibi::$debug)
{
echo self::$error ? "\n[ERROR] " : "\n[OK] ";
echo htmlSpecialChars(trim(strtr(self::$sql, "\r\n\t", ' ')));
echo "\n<br />";
}
if (self::$error && dibi::$errorMode === self::ERR_EXCEPTION) if (self::$error && dibi::$errorMode === self::ERR_EXCEPTION)
throw self::$error; throw self::$error;

View File

@@ -8,7 +8,7 @@
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/ * @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl * @copyright Copyright (c) 2005-2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
* @category Database * @category Database

View File

@@ -8,7 +8,7 @@
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/ * @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl * @copyright Copyright (c) 2005-2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
* @category Database * @category Database

View File

@@ -8,7 +8,7 @@
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/ * @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl * @copyright Copyright (c) 2005-2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
* @category Database * @category Database

View File

@@ -8,7 +8,7 @@
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/ * @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl * @copyright Copyright (c) 2005-2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
* @category Database * @category Database

View File

@@ -8,7 +8,7 @@
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/ * @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl * @copyright Copyright (c) 2005-2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
* @category Database * @category Database

View File

@@ -8,7 +8,7 @@
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/ * @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl * @copyright Copyright (c) 2005-2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
* @category Database * @category Database

View File

@@ -8,7 +8,7 @@
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/ * @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl * @copyright Copyright (c) 2005-2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
* @category Database * @category Database

View File

@@ -8,7 +8,7 @@
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/ * @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl * @copyright Copyright (c) 2005-2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
* @category Database * @category Database
@@ -38,8 +38,8 @@ if (!interface_exists('Countable', false)) {
* $result = dibi::query('SELECT * FROM [table]'); * $result = dibi::query('SELECT * FROM [table]');
* $value = $result->fetchSingle(); * $value = $result->fetchSingle();
* $all = $result->fetchAll(); * $all = $result->fetchAll();
* $assoc = $result->fetchAll('id'); * $assoc = $result->fetchAssoc('id');
* $assoc = $result->fetchAll('active', 'id'); * $assoc = $result->fetchAssoc('active', 'id');
* unset($result); * unset($result);
* </code> * </code>
*/ */
@@ -52,6 +52,15 @@ abstract class DibiResult implements IteratorAggregate, Countable
protected $convert; protected $convert;
static private $meta = array(
dibi::FIELD_TEXT => 'string',
dibi::FIELD_BINARY => 'string',
dibi::FIELD_BOOL => 'bool',
dibi::FIELD_INTEGER => 'int',
dibi::FIELD_FLOAT => 'float',
dibi::FIELD_COUNTER => 'int',
);
/** /**
@@ -150,7 +159,6 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** /**
* Fetches all records from table. Records , but returns only first field * Fetches all records from table. Records , but returns only first field
* @param string associative colum [, param, ... ]
* @return array * @return array
*/ */
final function fetchAll() final function fetchAll()
@@ -160,18 +168,40 @@ abstract class DibiResult implements IteratorAggregate, Countable
if (!$rec) if (!$rec)
return array(); // empty resultset return array(); // empty resultset
$assocBy = func_get_args();
$arr = array(); $arr = array();
if (count($rec) == 1) {
if (!$assocBy) { // no associative array $key = key($rec);
$value = count($rec) == 1 ? key($rec) : NULL;
do { do {
$arr[] = $value === NULL ? $rec : $rec[$value]; $arr[] = $rec[$key];
} while ($rec = $this->fetch()); } while ($rec = $this->fetch());
return $arr; } else {
do {
$arr[] = $rec;
} while ($rec = $this->fetch());
} }
return $arr;
}
/**
* Fetches all records from table. Records , but returns only first field
* @param string associative colum [, param, ... ]
* @return array
*/
final function fetchAssoc($assocBy)
{
@$this->seek(0);
$rec = $this->fetch();
if (!$rec)
return array(); // empty resultset
$assocBy = func_get_args();
$arr = array();
do { // make associative arrays do { // make associative arrays
foreach ($assocBy as $n => $assoc) { foreach ($assocBy as $n => $assoc) {
$val[$n] = $rec[$assoc]; $val[$n] = $rec[$assoc];
@@ -193,7 +223,6 @@ abstract class DibiResult implements IteratorAggregate, Countable
} }
/** /**
* Fetches all records from table like $key => $value pairs * Fetches all records from table like $key => $value pairs
* @return array * @return array
@@ -246,23 +275,13 @@ abstract class DibiResult implements IteratorAggregate, Countable
} }
public function convert($value, $type) public function convert($value, $type)
{ {
if ($value === NULL || $value === FALSE) if ($value === NULL || $value === FALSE)
return $value; return $value;
static $conv = array( if (isset(self::$meta[$type])) {
dibi::FIELD_TEXT => 'string', settype($value, self::$meta[$type]);
dibi::FIELD_BINARY => 'string',
dibi::FIELD_BOOL => 'bool',
dibi::FIELD_INTEGER => 'int',
dibi::FIELD_FLOAT => 'float',
dibi::FIELD_COUNTER => 'int',
);
if (isset($conv[$type])) {
settype($value, $conv[$type]);
return $value; return $value;
} }

View File

@@ -8,7 +8,7 @@
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/ * @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl * @copyright Copyright (c) 2005-2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
* @category Database * @category Database

View File

@@ -17,8 +17,7 @@ dibi::connect(array(
$res = dibi::query('SELECT * FROM table'); $res = dibi::query('SELECT * FROM table');
if (is_error($res)) if (!$res) die('SQL error');
die('SQL error');
// fetch a single value // fetch a single value
@@ -28,9 +27,9 @@ $value = $res->fetchSingle();
$all = $res->fetchAll(); $all = $res->fetchAll();
// fetch complete result set like association array // fetch complete result set like association array
$assoc = $res->fetchAll('id'); $assoc = $res->fetchAssoc('id');
$assoc = $res->fetchAll('id', 'id2'); $assoc = $res->fetchAssoc('id', 'id2');
// fetch complete result set like pairs key => value // fetch complete result set like pairs key => value
$pairs = $res->fetchPairs('id', 'name'); $pairs = $res->fetchPairs('id', 'name');

View File

@@ -1,14 +1,84 @@
SELECT * FROM `*nucleus_item` WHERE `inumber` < 38;
-- Result: Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22)
-- Takes: 178.920 ms
Successfully connected to DB 'mysql' Successfully connected to DB 'mysql'
Query error: Table 'test.nucleus_item' doesn't exist
SELECT * FROM `nucleus_item` WHERE `inumber` = 38; SELECT * FROM `nucleus_item` WHERE `inumber` = 38;
-- Result: Query error: Table 'test.nucleus_item' doesn't exist -- Result: Query error: Table 'test.nucleus_item' doesn't exist
-- Takes: 1.357 ms -- Takes: 2.527 ms
Query error: Table 'test.nucleus_item' doesn't exist
SELECT * FROM `nucleus_item` WHERE `inumber` < 38; SELECT * FROM `nucleus_item` WHERE `inumber` < 38;
-- Result: Query error: Table 'test.nucleus_item' doesn't exist -- Result: Query error: Table 'test.nucleus_item' doesn't exist
-- Takes: 2.013 ms -- Takes: 3.806 ms
Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22)
SELECT * FROM `*nucleus_item` WHERE `inumber` < 38; SELECT * FROM `*nucleus_item` WHERE `inumber` < 38;
-- Result: Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22) -- Result: Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22)
-- Takes: 75.413 ms -- Takes: 5.017 ms
Successfully connected to DB 'mysql'
Query error: Table 'test.nucleus_item' doesn't exist
SELECT * FROM `nucleus_item` WHERE `inumber` = 38;
-- Result: Query error: Table 'test.nucleus_item' doesn't exist
-- Takes: 4.560 ms
Query error: Table 'test.nucleus_item' doesn't exist
SELECT * FROM `nucleus_item` WHERE `inumber` < 38;
-- Result: Query error: Table 'test.nucleus_item' doesn't exist
-- Takes: 2.001 ms
Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22)
SELECT * FROM `*nucleus_item` WHERE `inumber` < 38;
-- Result: Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22)
-- Takes: 39.903 ms
Successfully connected to DB 'mysql'
Query error: Table 'test.nucleus_item' doesn't exist
SELECT * FROM `nucleus_item` WHERE `inumber` = 38;
-- Result: Query error: Table 'test.nucleus_item' doesn't exist
-- Takes: 2.183 ms
Query error: Table 'test.nucleus_item' doesn't exist
SELECT * FROM `nucleus_item` WHERE `inumber` < 38;
-- Result: Query error: Table 'test.nucleus_item' doesn't exist
-- Takes: 2.513 ms
Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22)
SELECT * FROM `*nucleus_item` WHERE `inumber` < 38;
-- Result: Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22)
-- Takes: 5.509 ms

View File

@@ -6,18 +6,17 @@ require_once '../dibi/dibi.php';
// mysql // mysql
dibi::connect(array( dibi::connect(array(
'driver' => 'mysql', 'driver' => 'mysqli',
'host' => 'localhost', 'host' => 'localhost',
'username' => 'root', 'username' => 'root',
'password' => 'xxx', // change to real password! 'password' => 'xxx', // change to real password!
'database' => 'test', 'database' => 'dgx',
'charset' => 'utf8', 'charset' => 'utf8',
)); ));
$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] <> %i', 38); $res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] <> %i', 38);
if (is_error($res)) if (!$res) die('SQL error');
die('SQL error');
// auto-convert this field to integer // auto-convert this field to integer