';
- echo 'Row | ';
- $fieldCount = $res->fieldCount();
- for ($i = 0; $i < $fieldCount; $i++) {
- $info = $res->fieldMeta($i);
- echo ''.htmlSpecialChars($info['name']).' | ';
- }
+ echo '#row | ';
+ foreach ($res->getFields() as $field)
+ echo '' . $field . ' | ';
echo '
';
foreach ($res as $row => $fields) {
diff --git a/dibi/libs/driver.php b/dibi/libs/driver.php
index f5d2b97a..d879edbc 100644
--- a/dibi/libs/driver.php
+++ b/dibi/libs/driver.php
@@ -49,9 +49,9 @@ abstract class DibiDriver
/**
* DibiDriver factory: creates object and connects to a database
*
- * @param array connect configuration
+ * @param array connect configuration
* @return DibiDriver
- * @throw DibiException
+ * @throw DibiException
*/
/*abstract disallowed since PHP 5.2*/ static public function connect($config) {}
diff --git a/dibi/libs/exception.php b/dibi/libs/exception.php
index 5ac25c30..9634a8c7 100644
--- a/dibi/libs/exception.php
+++ b/dibi/libs/exception.php
@@ -28,31 +28,44 @@ if (!defined('DIBI')) die();
class DibiException extends Exception
{
private
- $info;
+ $sql,
+ $dbError;
- public function __construct($message, $info=NULL) {
-
- $this->info = $info;
-
- if (isset($info['message']))
- $message = "$message: $info[message]";
-
+ public function __construct($message, $dbError=NULL, $sql=NULL)
+ {
+ $this->dbError = $dbError;
+ $this->sql = $sql;
parent::__construct($message);
}
public function getSql()
{
- return isset($this->info['sql']) ? $this->info['sql'] : NULL;
+ return $this->sql;
+ }
+
+
+ public function getDbError()
+ {
+ return $this->dbError;
}
public function __toString()
{
$s = parent::__toString();
- if (isset($this->info['sql']))
- $s .= "\nSQL: " . $this->info['sql'];
+
+ if ($this->dbError) {
+ $s .= "\nERROR: ";
+ if (isset($this->dbError['code']))
+ $s .= "[" . $this->dbError['code'] . "] ";
+
+ $s .= $this->dbError['message'];
+ }
+
+ if ($this->sql) $s .= "\nSQL: " . $this->sql;
+
return $s;
}
diff --git a/dibi/libs/resultset.php b/dibi/libs/resultset.php
index 973c18d5..73e5ec6a 100644
--- a/dibi/libs/resultset.php
+++ b/dibi/libs/resultset.php
@@ -200,6 +200,11 @@ abstract class DibiResult implements IteratorAggregate, Countable
return array(); // empty resultset
$assocBy = func_get_args();
+
+ // check function parameters - !!! ignore or throw error?
+ foreach ($assocBy as $n => $assoc) //
+ if (!array_key_exists($assoc, $rec)) unset($assocBy[$n]);
+
$arr = array();
do { // make associative arrays
@@ -234,6 +239,9 @@ abstract class DibiResult implements IteratorAggregate, Countable
if (!$rec)
return array(); // empty resultset
+ if (!array_key_exists($key, $rec) ||
+ !array_key_exists($value, $rec)) return FALSE;
+
$arr = array();
do {
$arr[ $rec[$key] ] = $rec[$value];
diff --git a/dibi/libs/translator.php b/dibi/libs/translator.php
index 98f0c6bd..99307d35 100644
--- a/dibi/libs/translator.php
+++ b/dibi/libs/translator.php
@@ -52,7 +52,7 @@ class DibiTranslator
*
* @param array
* @return string
- * @throw DibiException
+ * @throw DibiException
*/
public function translate($args)
{
@@ -115,10 +115,6 @@ class DibiTranslator
$this->sql = $sql;
return !$this->hasError;
- if ($this->hasError)
- throw new DibiException('Errors during generating SQL', array('sql' => $sql));
-
- return $sql;
}
diff --git a/examples/connect.php b/examples/connect.php
index bc782136..296b1fe2 100644
--- a/examples/connect.php
+++ b/examples/connect.php
@@ -4,55 +4,53 @@ require_once '../dibi/dibi.php';
try {
- // connects using DSN
+
+ // connects to MySQL using DSN
dibi::connect('driver=mysql&host=localhost&username=root&password=xxx&database=test&charset=utf8');
+
+ // connects to MySQL / MySQLi
+ dibi::connect(array(
+ 'driver' => 'mysql', // or 'mysqli'
+ 'host' => 'localhost',
+ 'username' => 'root',
+ 'password' => 'xxx',
+ 'database' => 'dibi',
+ 'charset' => 'utf8',
+ ));
+
+
+ // connects to ODBC
+ dibi::connect(array(
+ 'driver' => 'odbc',
+ 'username' => 'root',
+ 'password' => '***',
+ 'database' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\Database.mdb',
+ ));
+
+
+ // connects to SQlite
+ dibi::connect(array(
+ 'driver' => 'sqlite',
+ 'database' => 'mydb.sdb',
+ ));
+
+
+ // connects to PostgreSql
+ dibi::connect(array(
+ 'driver' => 'postgre',
+ 'string' => 'host=localhost port=5432 dbname=mary',
+ 'persistent' => TRUE,
+ ));
+
+
} catch (DibiException $e) {
- echo "DibiException: " . $e->getMessage();
- die();
+
+ echo "DibiException: ", $e;
+
}
-// connects to mysql
-dibi::connect(array(
- 'driver' => 'mysql',
- 'host' => 'localhost',
- 'username' => 'root',
- 'password' => 'xxx', // change to real password!
- 'database' => 'test',
- 'charset' => 'utf8',
-));
-
-/* connects to ODBC
-dibi::connect(array(
- 'driver' => 'odbc',
- 'username' => 'root',
- 'password' => '***',
- 'database' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\Database.mdb',
-));
-*/
-
-/* connects to SQlite
-dibi::connect(array(
- 'driver' => 'sqlite',
- 'database' => 'mydb.sdb',
-));
-*/
-
-/* connects to PostgreSql
-dibi::connect(array(
- 'driver' => 'postgre',
- 'string' => 'host=localhost port=5432 dbname=mary',
- 'persistent' => TRUE,
-));
-*/
-// check status
-if (!dibi::isConnected()) {
- echo 'dibi::isConnected(): Not connected';
-} else {
- echo 'Connected';
-}
-?>
\ No newline at end of file
diff --git a/examples/date.type.demo.php b/examples/date.type.demo.php
index b2d6f388..eceeb196 100644
--- a/examples/date.type.demo.php
+++ b/examples/date.type.demo.php
@@ -4,7 +4,7 @@ require_once '../dibi/dibi.php';
// required since PHP 5.1.0
if (function_exists('date_default_timezone_set'))
- date_default_timezone_set('Europe/Prague'); // or 'GMT'
+ date_default_timezone_set('Europe/Prague');
/**
@@ -55,12 +55,13 @@ class TDateTime implements IDibiVariable
-// connects to mysqli
+// CHANGE TO REAL PARAMETERS!
dibi::connect(array(
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
- 'password' => 'xxx', // change to real password!
+ 'password' => 'xxx',
+ 'database' => 'dibi',
'charset' => 'utf8',
));
@@ -68,7 +69,7 @@ dibi::connect(array(
// generate and dump SQL
dibi::test("
-INSERT INTO [test]", array(
+INSERT INTO [mytable]", array(
'A' => 12,
'B' => NULL,
'C' => new TDateTime(31542), // using out class
@@ -76,4 +77,3 @@ INSERT INTO [test]", array(
));
-?>
\ No newline at end of file
diff --git a/examples/dump.php b/examples/dump.php
new file mode 100644
index 00000000..c99b3159
--- /dev/null
+++ b/examples/dump.php
@@ -0,0 +1,38 @@
+ 'mysql',
+ 'host' => 'localhost',
+ 'username' => 'root',
+ 'password' => 'xxx',
+ 'database' => 'dibi',
+ 'charset' => 'utf8',
+));
+
+
+
+$res = dibi::query('SELECT * FROM [mytable]');
+
+// get last SQL
+$sql = dibi::$sql;
+
+
+// dump it
+echo 'dibi::dump()
';
+
+dibi::dump($sql);
+
+
+// dump result table
+echo 'dibi::dumpResult()
';
+
+dibi::dumpResult($res);
+
+
diff --git a/examples/fetch.php b/examples/fetch.php
index fb1ff849..b2ec10a7 100644
--- a/examples/fetch.php
+++ b/examples/fetch.php
@@ -4,18 +4,18 @@
require_once '../dibi/dibi.php';
-// mysql
+// CHANGE TO REAL PARAMETERS!
dibi::connect(array(
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
- 'password' => 'xxx', // change to real password!
- 'database' => 'test',
+ 'password' => 'xxx',
+ 'database' => 'dibi',
'charset' => 'utf8',
));
-$res = dibi::query('SELECT * FROM table');
+$res = dibi::query('SELECT * FROM [mytable]');
if (!$res) die('SQL error');
@@ -46,4 +46,3 @@ foreach ($res->getIterator(2, 3) as $row => $fields) {
-?>
\ No newline at end of file
diff --git a/examples/logger.php b/examples/logger.php
index 9567f1b3..927c9d69 100644
--- a/examples/logger.php
+++ b/examples/logger.php
@@ -1,4 +1,3 @@
-
'mysql',
'host' => 'localhost',
'username' => 'root',
- 'password' => 'xxx', // change to real password!
- 'database' => 'xxx',
+ 'password' => 'xxx',
+ 'database' => 'dibi',
'charset' => 'utf8',
));
-$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] = %i', 38);
+// generate user-level errors
+dibi::$throwExceptions = FALSE;
+echo 'User-level errors
';
-$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] < %i', 38);
+$res = dibi::query('SELECT * FROM [mytable] WHERE [inumber] = %i', 38);
-$res = dibi::query('SELECT * FROM [*nucleus_item] WHERE [inumber] < %i', 38);
+$res = dibi::query('SELECT * FROM [mytable] WHERE [inumber] < %i', 38);
-echo 'See file ', dibi::$logFile;
-?>
\ No newline at end of file
+$res = dibi::query('SELECT FROM [mytable] WHERE [inumber] < %i', 38);
+
+echo "
See file ", dibi::$logFile;
+
+
+
+// generate DibiException
+dibi::$throwExceptions = TRUE;
+echo 'DibiException
';
+
+try {
+
+ $res = dibi::query('SELECT FROM [mytable] WHERE [inumber] < %i', 38);
+
+} catch (DibiException $e) {
+
+ echo '', $e, '
';
+
+ echo '$e->getSql()
';
+ $sql = $e->getSql();
+ echo "SQL: $sql\n";
+
+ echo '$e->getDbError()
';
+ $error = $e->getDbError();
+ echo '';
+ print_r($error);
+ echo '
';
+
+}
\ No newline at end of file
diff --git a/examples/metatypes.php b/examples/metatypes.php
index 97cdd2bb..305bb0bc 100644
--- a/examples/metatypes.php
+++ b/examples/metatypes.php
@@ -4,18 +4,18 @@
require_once '../dibi/dibi.php';
-// mysql
+// CHANGE TO REAL PARAMETERS!
dibi::connect(array(
- 'driver' => 'mysqli',
+ 'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
- 'password' => 'xxx', // change to real password!
- 'database' => 'dgx',
+ 'password' => 'xxx',
+ 'database' => 'dibi',
'charset' => 'utf8',
));
-$res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] <> %i', 38);
+$res = dibi::query('SELECT * FROM [mytable] WHERE [inumber] <> %i', 38);
if (!$res) die('SQL error');
@@ -30,5 +30,3 @@ $res->setType(TRUE);
$record = $res->fetch();
var_dump($record);
-
-?>
diff --git a/examples/sql-builder.php b/examples/sql-builder.php
index a1f5e4eb..57f911b6 100644
--- a/examples/sql-builder.php
+++ b/examples/sql-builder.php
@@ -11,12 +11,13 @@ if (function_exists('date_default_timezone_set'))
date_default_timezone_set('Europe/Prague'); // or 'GMT'
-// mysql
+// CHANGE TO REAL PARAMETERS!
dibi::connect(array(
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
- 'password' => 'xxx', // change to real password!
+ 'password' => 'xxx',
+ 'database' => 'dibi',
'charset' => 'utf8',
));
@@ -59,16 +60,16 @@ LIMIT 10");
// dibi detects INSERT or REPLACE command
-dibi::test("INSERT INTO [test]", $arr4);
+dibi::test("INSERT INTO [mytable]", $arr4);
// dibi detects UPDATE command
$n = 123;
-dibi::test("UPDATE [test] SET", $arr4, " WHERE [id]=%i", $n);
+dibi::test("UPDATE [mytable] SET", $arr4, " WHERE [id]=%i", $n);
// array with modifier %a - assoc
-dibi::test("UPDATE [test] SET%a", $arr4, " WHERE [id]=%i", $n);
+dibi::test("UPDATE [mytable] SET%a", $arr4, " WHERE [id]=%i", $n);
diff --git a/examples/sql-condition.php b/examples/sql-condition.php
index f6b51a12..37d3aea2 100644
--- a/examples/sql-condition.php
+++ b/examples/sql-condition.php
@@ -7,12 +7,13 @@ pre.dibi { padding-bottom: 10px; }
require_once '../dibi/dibi.php';
-// mysql
+// CHANGE TO REAL PARAMETERS!
dibi::connect(array(
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
- 'password' => 'xxx', // change to real password!
+ 'password' => 'xxx',
+ 'database' => 'dibi',
'charset' => 'utf8',
));
@@ -26,7 +27,7 @@ $user = $cond1 ? 'Davidek' : NULL;
dibi::test('
SELECT *
-FROM [table]
+FROM [mytable]
%if', isset($user), 'WHERE [user]=%s', $user, '%end'
);
@@ -41,11 +42,11 @@ FROM %if', $cond1, '[one_table] %else [second_table]'
// nested condition
dibi::test('
SELECT *
-FROM [test]
+FROM [mytable]
WHERE
%if', isset($user), '[user]=%s', $user, '
%if', $cond2, 'AND [admin]=1 %end
%else LIMIT 10 %end'
);
-?>
+
diff --git a/examples/table-prefix.php b/examples/table-prefix.php
index 5c53d2d3..16bb257d 100644
--- a/examples/table-prefix.php
+++ b/examples/table-prefix.php
@@ -3,12 +3,13 @@
require_once '../dibi/dibi.php';
-// connects to mysqli
+// CHANGE TO REAL PARAMETERS!
dibi::connect(array(
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
- 'password' => 'xxx', // change to real password!
+ 'password' => 'xxx',
+ 'database' => 'dibi',
'charset' => 'utf8',
));
@@ -29,4 +30,3 @@ dibi::addSubst('', 'my_');
dibi::test("UPDATE [database.::table] SET [text]='Hello World'");
-?>
\ No newline at end of file
diff --git a/version.txt b/version.txt
index c962eef5..97934c05 100644
--- a/version.txt
+++ b/version.txt
@@ -1 +1 @@
-Dibi Version 0.7b
+Dibi Version 0.7c