1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-01 11:50:15 +02:00

update to 0.6

This commit is contained in:
David Grudl
2006-06-07 15:50:32 +00:00
parent 8361028ff3
commit 0951ea574c
13 changed files with 246 additions and 273 deletions

View File

@@ -2,38 +2,46 @@
require_once '../dibi/dibi.php';
// use two connections:
// first connection to mysql
// connects to mysql
$state = dibi::connect(array(
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
'password' => '***',
'password' => 'xxx', // change to real password!
'database' => 'test',
'charset' => 'utf8',
), 1);
));
if ($state instanceof Exception) {
echo $state;
}
if (!dibi::isConnected()) {
die();
}
// second connection to odbc
/* connects to ODBC
dibi::connect(array(
'driver' => 'odbc',
'username' => 'root',
'password' => '***',
'database' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\Database.mdb',
), 3);
));
*/
echo dibi::isConnected();
// check status
if (!dibi::isConnected()) {
echo 'dibi::isConnected(): Not connected';
echo "<br>\n";
}
// or checked status this way
if (is_error($state)) {
// $state can be FALSE or Exception
if ($state instanceof Exception)
echo $state;
else
echo 'FALSE';
echo "<br>\n";
}

View File

@@ -0,0 +1,76 @@
<?php
require_once '../dibi/dibi.php';
/**
* Pseudotype for UNIX timestamp representation
*/
class TDateTime implements IDibiVariable
{
/**
* Unix timestamp
* @var int
*/
protected $time;
public function __construct($time = NULL)
{
if ($time === NULL)
$this->time = time(); // current time
elseif (is_string($time))
$this->time = strtotime($time); // try convert to timestamp
else
$this->time = (int) $time;
}
/**
* Format for SQL
*
* @param object destination DibiDriver
* @param string optional modifier
* @return string
*/
public function toSQL($driver, $modifier = NULL)
{
return date(
$driver->formats['datetime'], // format according to driver's spec.
$this->time
);
}
}
// connects to mysqli
dibi::connect(array(
'driver' => 'mysqli',
'host' => 'localhost',
'username' => 'root',
'password' => 'xxx', // change to real password!
'charset' => 'utf8',
));
// generate and dump SQL
dibi::test("
INSERT INTO [test]", array(
'A' => 12,
'B' => NULL,
'C' => new TDateTime(31542), // using out class
'D' => 'any string',
));
?>

View File

@@ -10,7 +10,7 @@ dibi::connect(array(
'driver' => 'mysqli',
'host' => 'localhost',
'username' => 'root',
'password' => '***',
'password' => 'xxx', // change to real password!
'database' => 'test',
'charset' => 'utf8',
));
@@ -22,6 +22,7 @@ if (!dibi::isConnected())
$res = dibi::query('SELECT * FROM table');
// fetch a single value
$value = $res->fetchSingle();

View File

@@ -1,14 +1,14 @@
Successfully connected to DB 'mysql'
SELECT * FROM `nucleus_item` WHERE `inumber` = 38;
-- Result: object(DibiMySqlResult) rows: 1
-- Takes: 4.994 ms
-- Result: Query error: Table 'test.nucleus_item' doesn't exist
-- Takes: 1.357 ms
SELECT * FROM `nucleus_item` WHERE `inumber` < 38;
-- Result: object(DibiMySqlResult) rows: 29
-- Takes: 135.842 ms
-- Result: Query error: Table 'test.nucleus_item' doesn't exist
-- Takes: 2.013 ms
SELECT * FROM `*nucleus_item` WHERE `inumber` < 38;
-- Result: Query error: Can't find file: '.\dgx\*nucleus_item.frm' (errno: 22)
-- Takes: 121.454 ms
-- Result: Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22)
-- Takes: 75.413 ms

View File

@@ -4,6 +4,7 @@
require_once '../dibi/dibi.php';
// enable logging to this file
dibi::$logfile = 'log.sql';
@@ -12,7 +13,7 @@ dibi::connect(array(
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
'password' => '***',
'password' => 'xxx', // change to real password!
'database' => 'test',
'charset' => 'utf8',
));

View File

@@ -9,15 +9,16 @@ dibi::connect(array(
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
'password' => '***',
'password' => 'xxx', // change to real password!
'database' => 'test',
'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))
die('SQL error');
// auto-convert this field to integer
$res->setType('inumber', DibiResult::FIELD_INTEGER);

View File

@@ -1,3 +1,6 @@
<style>
pre.dibi { padding-bottom: 10px; }
</style>
<pre>
<?php
@@ -9,35 +12,37 @@ dibi::connect(array(
'driver' => 'mysqli',
'host' => 'localhost',
'username' => 'root',
'password' => '***',
'database' => 'test',
'password' => 'xxx', // change to real password!
'charset' => 'utf8',
));
$arr1 = array(1, 2, 3);
$arr2 = array('one', 'two', 'three');
$arr3 = array(
'a' => 'one',
'b' => 'two',
'c' => 'three',
'col1' => 'one',
'col2' => 'two',
'col3' => 'three',
);
$arr4 = array(
'A' => 12,
'B' => NULL,
'C' => new TDateTime(31542),
'D' => 'string',
'a' => 12,
'b' => NULL,
'c%T' => time(), // modifier 'T' means datetime
'd' => 'any string',
);
$arr5 = array('RAND()', 'ANY SQL');
dibi::test(
"
dibi::test("
SELECT *
FROM [test]
WHERE ([test.a] LIKE %T", '1995-03-01', "
OR [b1] IN (", $arr1, ")
OR [b2] IN (", $arr2, ")
OR [b3] IN (%N", $arr3, ")
OR [b4] IN %V", $arr4, "
FROM [db.table]
WHERE ([test.a] LIKE %D", '1995-03-01', "
OR [b1] IN (", $arr1, ")
OR [b2] IN (%s", $arr1, ")
OR [b3] IN (", $arr2, ")
OR [b4] IN (%n", $arr3, ")
OR [b4] IN (%p", $arr5, ")
AND [c] = 'embedded '' string'
OR [d]=%d", 10.3, "
OR [true]=", true, "
@@ -45,4 +50,17 @@ WHERE ([test.a] LIKE %T", '1995-03-01', "
OR [null]=", NULL, "
LIMIT 10");
// dibi detects INSERT or REPLACE command
dibi::test("INSERT INTO [test]", $arr4);
// dibi detects UPDATE command
$n = 123;
dibi::test("UPDATE [test] SET", $arr4, " WHERE [id]=%n", $n);
// array with modifier %d - means strings
dibi::test("UPDATE [test] SET%s", $arr4, " WHERE [id]=%n", $n);
?>