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:
File diff suppressed because one or more lines are too long
@@ -10,7 +10,7 @@
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @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
|
||||
* @package dibi
|
||||
* @category Database
|
||||
@@ -178,7 +178,7 @@ class dibi
|
||||
/** like $conn = $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)
|
||||
throw self::$error;
|
||||
|
||||
|
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @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
|
||||
* @package dibi
|
||||
* @category Database
|
||||
|
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @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
|
||||
* @package dibi
|
||||
* @category Database
|
||||
|
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @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
|
||||
* @package dibi
|
||||
* @category Database
|
||||
|
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @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
|
||||
* @package dibi
|
||||
* @category Database
|
||||
|
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @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
|
||||
* @package dibi
|
||||
* @category Database
|
||||
|
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @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
|
||||
* @package dibi
|
||||
* @category Database
|
||||
|
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @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
|
||||
* @package dibi
|
||||
* @category Database
|
||||
|
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @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
|
||||
* @package dibi
|
||||
* @category Database
|
||||
@@ -38,8 +38,8 @@ if (!interface_exists('Countable', false)) {
|
||||
* $result = dibi::query('SELECT * FROM [table]');
|
||||
* $value = $result->fetchSingle();
|
||||
* $all = $result->fetchAll();
|
||||
* $assoc = $result->fetchAll('id');
|
||||
* $assoc = $result->fetchAll('active', 'id');
|
||||
* $assoc = $result->fetchAssoc('id');
|
||||
* $assoc = $result->fetchAssoc('active', 'id');
|
||||
* unset($result);
|
||||
* </code>
|
||||
*/
|
||||
@@ -52,6 +52,15 @@ abstract class DibiResult implements IteratorAggregate, Countable
|
||||
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
|
||||
* @param string associative colum [, param, ... ]
|
||||
* @return array
|
||||
*/
|
||||
final function fetchAll()
|
||||
@@ -160,18 +168,40 @@ abstract class DibiResult implements IteratorAggregate, Countable
|
||||
if (!$rec)
|
||||
return array(); // empty resultset
|
||||
|
||||
$assocBy = func_get_args();
|
||||
$arr = array();
|
||||
|
||||
if (!$assocBy) { // no associative array
|
||||
$value = count($rec) == 1 ? key($rec) : NULL;
|
||||
if (count($rec) == 1) {
|
||||
$key = key($rec);
|
||||
do {
|
||||
$arr[] = $value === NULL ? $rec : $rec[$value];
|
||||
$arr[] = $rec[$key];
|
||||
} while ($rec = $this->fetch());
|
||||
|
||||
} 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
|
||||
foreach ($assocBy as $n => $assoc) {
|
||||
$val[$n] = $rec[$assoc];
|
||||
@@ -193,7 +223,6 @@ abstract class DibiResult implements IteratorAggregate, Countable
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Fetches all records from table like $key => $value pairs
|
||||
* @return array
|
||||
@@ -246,23 +275,13 @@ abstract class DibiResult implements IteratorAggregate, Countable
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function convert($value, $type)
|
||||
{
|
||||
if ($value === NULL || $value === FALSE)
|
||||
return $value;
|
||||
|
||||
static $conv = 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',
|
||||
);
|
||||
|
||||
if (isset($conv[$type])) {
|
||||
settype($value, $conv[$type]);
|
||||
if (isset(self::$meta[$type])) {
|
||||
settype($value, self::$meta[$type]);
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* @author David Grudl aka -dgx- <dave@dgx.cz>
|
||||
* @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
|
||||
* @package dibi
|
||||
* @category Database
|
||||
|
@@ -17,8 +17,7 @@ dibi::connect(array(
|
||||
|
||||
|
||||
$res = dibi::query('SELECT * FROM table');
|
||||
if (is_error($res))
|
||||
die('SQL error');
|
||||
if (!$res) die('SQL error');
|
||||
|
||||
|
||||
// fetch a single value
|
||||
@@ -28,9 +27,9 @@ $value = $res->fetchSingle();
|
||||
$all = $res->fetchAll();
|
||||
|
||||
// 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
|
||||
$pairs = $res->fetchPairs('id', 'name');
|
||||
|
@@ -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'
|
||||
|
||||
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: 1.357 ms
|
||||
-- Takes: 2.527 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.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;
|
||||
-- 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
|
||||
|
||||
|
||||
|
||||
|
@@ -6,18 +6,17 @@ require_once '../dibi/dibi.php';
|
||||
|
||||
// mysql
|
||||
dibi::connect(array(
|
||||
'driver' => 'mysql',
|
||||
'driver' => 'mysqli',
|
||||
'host' => 'localhost',
|
||||
'username' => 'root',
|
||||
'password' => 'xxx', // change to real password!
|
||||
'database' => 'test',
|
||||
'database' => 'dgx',
|
||||
'charset' => 'utf8',
|
||||
));
|
||||
|
||||
|
||||
$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] <> %i', 38);
|
||||
if (is_error($res))
|
||||
die('SQL error');
|
||||
if (!$res) die('SQL error');
|
||||
|
||||
|
||||
// auto-convert this field to integer
|
||||
|
Reference in New Issue
Block a user