mirror of
https://github.com/dg/dibi.git
synced 2025-08-07 06:36:44 +02:00
* removed $throwExceptions (always throws)
* added DibiLogger, dibi::notify(), dibi::startLogger() * miniprofiler dibi::$numOfQueries, $totalTime, $elapsedTime * simplified DibiException, added DibiDatabaseException * Dibi::nativeQuery splitted into DibiDriver::doQuery & nativeQuery() * moved dibi::dumpResult -> DibiResult::dump() * moved dibi::test() -> DibiDriver::test() * DibiTranslator generates $mask
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<pre>
|
||||
<h1>dibi connect example</h1>
|
||||
<?php
|
||||
|
||||
require_once '../dibi/dibi.php';
|
||||
@@ -10,9 +10,10 @@ try {
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'sample.sdb',
|
||||
));
|
||||
echo '<p>Connected to Sqlite</p>';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo 'DibiException: ', $e;
|
||||
echo '<pre>', $e, '</pre>';
|
||||
}
|
||||
|
||||
|
||||
@@ -20,9 +21,10 @@ try {
|
||||
// connects to MySQL using DSN
|
||||
try {
|
||||
dibi::connect('driver=mysql&host=localhost&username=root&password=xxx&database=test&charset=utf8');
|
||||
echo '<p>Connected to MySQL</p>';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo 'DibiException: ', $e;
|
||||
echo '<pre>', $e, '</pre>';
|
||||
}
|
||||
|
||||
|
||||
@@ -38,9 +40,10 @@ try {
|
||||
'database' => 'dibi',
|
||||
'charset' => 'utf8',
|
||||
));
|
||||
echo '<p>Connected to MySQL</p>';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo 'DibiException: ', $e;
|
||||
echo '<pre>', $e, '</pre>';
|
||||
}
|
||||
|
||||
|
||||
@@ -52,11 +55,12 @@ try {
|
||||
'driver' => 'odbc',
|
||||
'username' => 'root',
|
||||
'password' => '***',
|
||||
'database' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\Database.mdb',
|
||||
'database' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq='.dirname(__FILE__).'/sample.mdb',
|
||||
));
|
||||
echo '<p>Connected to ODBC</p>';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo 'DibiException: ', $e;
|
||||
echo '<pre>', $e, '</pre>';
|
||||
}
|
||||
|
||||
|
||||
@@ -69,9 +73,10 @@ try {
|
||||
'string' => 'host=localhost port=5432 dbname=mary',
|
||||
'persistent' => TRUE,
|
||||
));
|
||||
echo '<p>Connected to PostgreSql</p>';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo 'DibiException: ', $e;
|
||||
echo '<pre>', $e, '</pre>';
|
||||
}
|
||||
|
||||
|
||||
@@ -83,9 +88,10 @@ try {
|
||||
'driver' => 'pdo',
|
||||
'dsn' => 'sqlite2::memory:',
|
||||
));
|
||||
echo '<p>Connected to Sqlite via PDO</p>';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo 'DibiException: ', $e;
|
||||
echo '<pre>', $e, '</pre>';
|
||||
}
|
||||
|
||||
|
||||
@@ -98,7 +104,8 @@ try {
|
||||
'username' => 'root',
|
||||
'password' => 'xxx',
|
||||
));
|
||||
echo '<p>Connected to MS SQL</p>';
|
||||
|
||||
} catch (DibiException $e) {
|
||||
echo 'DibiException: ', $e;
|
||||
echo '<pre>', $e, '</pre>';
|
||||
}
|
||||
|
@@ -1,10 +1,13 @@
|
||||
<h1>dibi user datatype example</h1>
|
||||
<?php
|
||||
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
// required since PHP 5.1.0
|
||||
if (function_exists('date_default_timezone_set'))
|
||||
if (function_exists('date_default_timezone_set')) {
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
@@ -1,10 +1,8 @@
|
||||
<h1>dibi dump example</h1>
|
||||
<?php
|
||||
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
if (function_exists('date_default_timezone_set'))
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite',
|
||||
@@ -19,17 +17,14 @@ INNER JOIN [orders] USING ([product_id])
|
||||
INNER JOIN [customers] USING ([customer_id])
|
||||
');
|
||||
|
||||
// get last SQL
|
||||
$sql = dibi::$sql;
|
||||
|
||||
echo '<h2>dibi::dump()</h2>';
|
||||
|
||||
// dump it
|
||||
echo '<h1>dibi::dump()</h1>';
|
||||
|
||||
dibi::dump($sql);
|
||||
// dump last query (dibi::$sql)
|
||||
dibi::dump();
|
||||
|
||||
|
||||
// dump result table
|
||||
echo '<h1>dibi::dumpResult()</h1>';
|
||||
echo '<h2>DibiResult::dump()</h2>';
|
||||
|
||||
dibi::dumpResult($res);
|
||||
$res->dump();
|
||||
|
@@ -1,3 +1,4 @@
|
||||
<h1>dibi fetch example</h1>
|
||||
<pre>
|
||||
<?php
|
||||
|
||||
|
@@ -1,24 +0,0 @@
|
||||
OK: connected to DB 'sqlite'
|
||||
|
||||
OK: SELECT * FROM [customers] WHERE [customer_id] = 1;
|
||||
-- result: object(DibiSqliteResult) rows: 1
|
||||
-- takes: 0.331 ms
|
||||
-- driver: sqlite
|
||||
-- 2007-05-12 00:14:11
|
||||
|
||||
OK: SELECT * FROM [customers] WHERE [customer_id] < 5;
|
||||
-- result: object(DibiSqliteResult) rows: 4
|
||||
-- takes: 0.324 ms
|
||||
-- driver: sqlite
|
||||
-- 2007-05-12 00:14:11
|
||||
|
||||
ERROR: [1] SQL logic error or missing database
|
||||
-- SQL: SELECT FROM [customers] WHERE [customer_id] < 5
|
||||
-- driver: sqlite;
|
||||
-- 2007-05-12 00:14:11
|
||||
|
||||
ERROR: [1] SQL logic error or missing database
|
||||
-- SQL: SELECT FROM [customers] WHERE [customer_id] < 38
|
||||
-- driver: sqlite;
|
||||
-- 2007-05-12 00:14:11
|
||||
|
||||
|
@@ -1,21 +1,10 @@
|
||||
<h1>dibi logger example</h1>
|
||||
<?php
|
||||
|
||||
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'
|
||||
|
||||
|
||||
// enable log to this file
|
||||
dibi::$logFile = 'log.sql';
|
||||
|
||||
// append mode
|
||||
dibi::$logMode = 'a';
|
||||
|
||||
// log all queries
|
||||
dibi::$logAll = TRUE;
|
||||
// enable log to this file, TRUE means "log all queries"
|
||||
dibi::startLogger('log.sql', TRUE);
|
||||
|
||||
|
||||
|
||||
@@ -26,43 +15,20 @@ dibi::connect(array(
|
||||
|
||||
|
||||
|
||||
// generate user-level errors
|
||||
dibi::$throwExceptions = FALSE;
|
||||
echo '<h1>User-level errors</h1>';
|
||||
|
||||
|
||||
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] = %i', 1);
|
||||
|
||||
|
||||
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] < %i', 5);
|
||||
|
||||
|
||||
$res = dibi::query('SELECT FROM [customers] WHERE [customer_id] < %i', 5);
|
||||
|
||||
echo "<br />See file ", dibi::$logFile;
|
||||
|
||||
|
||||
|
||||
// generate DibiException
|
||||
dibi::$throwExceptions = TRUE;
|
||||
echo '<h1>DibiException</h1>';
|
||||
|
||||
try {
|
||||
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] = %i', 1);
|
||||
|
||||
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] < %i', 5);
|
||||
|
||||
$res = dibi::query('SELECT FROM [customers] WHERE [customer_id] < %i', 38);
|
||||
|
||||
} catch (DibiException $e) {
|
||||
|
||||
echo '<h2>Dibi Exception:</h2>';
|
||||
echo '<pre>', $e, '</pre>';
|
||||
}
|
||||
|
||||
echo '<h2>$e->getSql()</h2>';
|
||||
$sql = $e->getSql();
|
||||
echo "SQL: $sql\n";
|
||||
|
||||
echo '<h2>$e->getDbError()</h2>';
|
||||
$error = $e->getDbError();
|
||||
echo '<pre>';
|
||||
print_r($error);
|
||||
echo '</pre>';
|
||||
echo "<h2>File log.sql:</h2>";
|
||||
|
||||
}
|
||||
echo '<pre>', file_get_contents('log.sql'), '</pre>';
|
||||
|
@@ -1,3 +1,4 @@
|
||||
<h1>dibi metatypes example</h1>
|
||||
<pre>
|
||||
<?php
|
||||
|
||||
|
25
examples/profiler.php
Normal file
25
examples/profiler.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<h1>Dibi profiler example</h1>
|
||||
<?php
|
||||
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
for ($i=0; $i<20; $i++) {
|
||||
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] < %i', $i);
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
<p>Last query: <strong><?php echo dibi::$sql; ?></strong></p>
|
||||
|
||||
<p>Number of queries: <strong><?php echo dibi::$numOfQueries; ?></strong></p>
|
||||
|
||||
<p>Elapsed time for last query: <strong><?php echo sprintf('%0.3f', dibi::$elapsedTime * 1000); ?> ms</strong></p>
|
||||
|
||||
<p>Total elapsed time: <strong><?php echo sprintf('%0.3f', dibi::$totalTime * 1000); ?> ms</strong></p>
|
BIN
examples/sample.mdb
Normal file
BIN
examples/sample.mdb
Normal file
Binary file not shown.
@@ -1,14 +1,16 @@
|
||||
<style>
|
||||
pre.dibi { padding-bottom: 10px; }
|
||||
</style>
|
||||
<h1>dibi SQL builder example</h1>
|
||||
<pre>
|
||||
<?php
|
||||
|
||||
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'
|
||||
if (function_exists('date_default_timezone_set')) {
|
||||
date_default_timezone_set('Europe/Prague');
|
||||
}
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
@@ -79,3 +81,6 @@ dibi::test("SELECT %f", '-.12345678912345678912345678e10');
|
||||
|
||||
// hex numbers
|
||||
dibi::test("SELECT %i", '0x11');
|
||||
|
||||
// invalid input
|
||||
dibi::test("SELECT %s", (object) array(123), ', %m', 123);
|
||||
|
@@ -1,6 +1,7 @@
|
||||
<style>
|
||||
pre.dibi { padding-bottom: 10px; }
|
||||
</style>
|
||||
<h1>dibi conditional SQL example</h1>
|
||||
<pre>
|
||||
<?php
|
||||
|
||||
|
@@ -1,3 +1,4 @@
|
||||
<h1>dibi prefix & substitute example</h1>
|
||||
<?php
|
||||
|
||||
require_once '../dibi/dibi.php';
|
||||
|
Reference in New Issue
Block a user