From 7a6cdc8afad6ec6ccaf4aafe8e5ed9a21a6e0443 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 28 Oct 2008 02:06:55 +0000 Subject: [PATCH] - improved examples --- TODO.txt | 1 - examples/apply-limit.php | 20 +++++++---------- examples/datetime.demo.php | 1 + examples/dump.php | 2 ++ examples/fluent.test.php | 22 +++++++++++++++++- examples/load-sql-dump.php | 2 +- examples/metatypes.php | 8 ++++++- examples/nette-debug.php | 13 +++++++++-- examples/profiler.php | 9 ++++++++ examples/sample.dump.sql.gz | Bin 0 -> 66 bytes examples/sql-builder.php | 43 ++++++++++++++++++++++-------------- examples/sql-condition.php | 10 +++++++-- examples/table-prefix.php | 14 ++++++------ examples/transaction.php | 5 +++-- 14 files changed, 105 insertions(+), 45 deletions(-) delete mode 100644 TODO.txt create mode 100644 examples/sample.dump.sql.gz diff --git a/TODO.txt b/TODO.txt deleted file mode 100644 index 65a6da10..00000000 --- a/TODO.txt +++ /dev/null @@ -1 +0,0 @@ -- event, log, profiler diff --git a/examples/apply-limit.php b/examples/apply-limit.php index 147ceeb6..60b0d3f5 100644 --- a/examples/apply-limit.php +++ b/examples/apply-limit.php @@ -12,23 +12,19 @@ dibi::connect(array( // no limit -$res = dibi::query('SELECT * FROM [products]'); -foreach ($res as $n => $row) { - print_r($row); -} +dibi::test('SELECT * FROM [products]'); +// -> SELECT * FROM [products] + echo '
'; // with limit = 2 -$res = dibi::query('SELECT * FROM [products] %lmt', 2); -foreach ($res as $n => $row) { - print_r($row); -} +dibi::test('SELECT * FROM [products] %lmt', 2); +// -> SELECT * FROM [products] LIMIT 2 + echo '
'; // with limit = 2, offset = 1 -$res = dibi::query('SELECT * FROM [products] %lmt %ofs', 2, 1); -foreach ($res as $n => $row) { - print_r($row); -} +dibi::test('SELECT * FROM [products] %lmt %ofs', 2, 1); +// -> SELECT * FROM [products] LIMIT 2 OFFSET 1 diff --git a/examples/datetime.demo.php b/examples/datetime.demo.php index e17d169d..980278e2 100644 --- a/examples/datetime.demo.php +++ b/examples/datetime.demo.php @@ -26,3 +26,4 @@ INSERT INTO [mytable]", array( 'date' => dibi::date('12.3.2007'), 'stamp' => dibi::dateTime('23.1.2007 10:23'), )); +// -> INSERT INTO [mytable] ([id], [date], [stamp]) VALUES (123, '2007-03-12', '2007-01-23 10-23-00') diff --git a/examples/dump.php b/examples/dump.php index 30ed2d19..8e06ee0e 100644 --- a/examples/dump.php +++ b/examples/dump.php @@ -22,9 +22,11 @@ echo '

dibi::dump()

'; // dump last query (dibi::$sql) dibi::dump(); +// -> SELECT * FROM [products] INNER JOIN [orders] USING ([product_id]) INNER JOIN [customers] USING ([customer_id]) // dump result table echo '

DibiResult::dump()

'; $res->dump(); +// -> [table] diff --git a/examples/fluent.test.php b/examples/fluent.test.php index 75cd2739..c7aad120 100644 --- a/examples/fluent.test.php +++ b/examples/fluent.test.php @@ -12,7 +12,7 @@ dibi::connect(array( $id = 10; $record = array( - 'title' => 'Drtička na trávu', + 'title' => 'Super product', 'price' => 318, 'active' => TRUE, ); @@ -25,6 +25,10 @@ dibi::select('product_id')->as('id') ->innerJoin('customers USING (customer_id)') ->orderBy('title') ->test(); +// -> SELECT [product_id] AS [id] , [title] FROM [products] INNER JOIN [orders] +// USING (product_id) INNER JOIN customers USING (customer_id) ORDER BY [title] + + echo "\n"; @@ -32,6 +36,9 @@ echo "\n"; echo dibi::select('title')->as('id') ->from('products') ->fetchSingle(); +// -> Chair (as result of query: SELECT [title] AS [id] FROM [products]) + + echo "\n"; @@ -39,6 +46,9 @@ echo "\n"; dibi::insert('products', $record) ->setFlag('IGNORE') ->test(); +// -> INSERT IGNORE INTO [products] ([title], [price], [active]) VALUES ('Super product', 318, 1) + + echo "\n"; @@ -46,6 +56,9 @@ echo "\n"; dibi::update('products', $record) ->where('product_id = %d', $id) ->test(); +// -> UPDATE [products] SET [title]='Super product', [price]=318, [active]=1 WHERE product_id = 10 + + echo "\n"; @@ -53,6 +66,9 @@ echo "\n"; dibi::delete('products') ->where('product_id = %d', $id) ->test(); +// -> DELETE FROM [products] WHERE product_id = 10 + + echo "\n"; @@ -62,9 +78,13 @@ dibi::command() ->where('product_id = %d', $id) ->set($record) ->test(); +// -> UPDATE [products] SET [title]='Super product', [price]=318, [active]=1 WHERE product_id = 10 + + echo "\n"; dibi::command() ->truncate('products') ->test(); +// -> TRUNCATE [products] diff --git a/examples/load-sql-dump.php b/examples/load-sql-dump.php index 07071372..62c3fccd 100644 --- a/examples/load-sql-dump.php +++ b/examples/load-sql-dump.php @@ -11,6 +11,6 @@ dibi::connect(array( )); -$count = dibi::loadFile('compress.zlib://dump.sql.gz'); +$count = dibi::loadFile('compress.zlib://sample.dump.sql.gz'); echo 'Number of SQL commands:', $count; \ No newline at end of file diff --git a/examples/metatypes.php b/examples/metatypes.php index 5e4484ff..13c13ec2 100644 --- a/examples/metatypes.php +++ b/examples/metatypes.php @@ -13,9 +13,15 @@ dibi::connect(array( $res = dibi::query('SELECT * FROM [customers]'); -// auto-convert this column to integer +// auto-converts this column to integer $res->setType('customer_id', Dibi::FIELD_INTEGER); $res->setType('added', Dibi::FIELD_DATETIME, 'H:i j.n.Y'); $row = $res->fetch(); var_dump($row); +// outputs: +// object(DibiRow)#3 (3) { +// customer_id => int(1) +// name => string(11) "Dave Lister" +// added => string(15) "17:20 11.3.2007" +// } diff --git a/examples/nette-debug.php b/examples/nette-debug.php index c26c41e7..4767df9c 100644 --- a/examples/nette-debug.php +++ b/examples/nette-debug.php @@ -1,11 +1,20 @@ -

Nette::Debug && dibi example

+

Nette::Debug & dibi example

+ + +

Dibi can display and log exceptions via Nette::Debug. You first need to install Nette Framework. You can download it from here:

+ + + Elapsed time for last query: ms

Total elapsed time: ms

+ +
+ +

Dibi can log to your Firebug Console. You first need to install the Firefox, Firebug and FirePHP extensions. You can install them from here:

+ + \ No newline at end of file diff --git a/examples/sample.dump.sql.gz b/examples/sample.dump.sql.gz new file mode 100644 index 0000000000000000000000000000000000000000..feb68e1a4ef36d4c18cf10b657e8909cc5d616e5 GIT binary patch literal 66 zcmV-I0KNYoiwFpKaRx~M0AzJ-a4vIkYyi{MRY)n#El^0UNX#wBN#*6@ 'Drtika na trvu', - 'price' => 318, - 'active' => TRUE, + REPLACE INTO [products]', array( + 'title' => 'Super product', + 'price' => 318, + 'active' => TRUE, )); +// -> REPLACE INTO [products] ([title], [price], [active]) VALUES ('Super product', 318, 1) + // multiple INSERT command @@ -34,15 +36,19 @@ $array = array( 'created' => dibi::datetime(), ); dibi::test("INSERT INTO [products]", $array, $array, $array); +// -> INSERT INTO [products] ([title], [price], [brand], [created]) VALUES ('Super Product', ...) , (...) , (...) + // dibi detects UPDATE command dibi::test(" -UPDATE [colors] SET", array( - 'color' => 'blue', - 'order' => 12, -), " -WHERE [id]=%i", 123); + UPDATE [colors] SET", array( + 'color' => 'blue', + 'order' => 12, + ), " + WHERE [id]=%i", 123); +// -> UPDATE [colors] SET [color]='blue', [order]=12 WHERE [id]=123 + // SELECT @@ -50,20 +56,24 @@ $ipMask = '192.168.%'; $timestamp = mktime(0, 0, 0, 10, 13, 1997); dibi::test(' -SELECT COUNT(*) as [count] -FROM [comments] -WHERE [ip] LIKE %s', $ipMask, ' -AND [date] > ', dibi::date($timestamp) + SELECT COUNT(*) as [count] + FROM [comments] + WHERE [ip] LIKE %s', $ipMask, ' + AND [date] > ', dibi::date($timestamp) ); +// -> SELECT COUNT(*) as [count] FROM [comments] WHERE [ip] LIKE '192.168.%' AND [date] > 876693600 + // IN array $array = array(1, 2, 3); dibi::test(" -SELECT * -FROM [people] -WHERE [id] IN (", $array, ") + SELECT * + FROM [people] + WHERE [id] IN (", $array, ") "); +// -> SELECT * FROM [people] WHERE [id] IN ( 1, 2, 3 ) + // ORDER BY array @@ -76,3 +86,4 @@ SELECT * FROM [people] ORDER BY %by", $order, " "); +// -> SELECT * FROM [people] ORDER BY [field1] ASC, [field2] DESC diff --git a/examples/sql-condition.php b/examples/sql-condition.php index f17cf123..ffe3c4d6 100644 --- a/examples/sql-condition.php +++ b/examples/sql-condition.php @@ -14,8 +14,8 @@ dibi::connect(array( )); -$cond1 = rand(0,2) < 1; -$cond2 = rand(0,2) < 1; +$cond1 = TRUE; +$cond2 = FALSE; $foo = -1; $bar = 2; @@ -28,6 +28,9 @@ SELECT * FROM [customers] %if', isset($name), 'WHERE [name] LIKE %s', $name, '%end' ); +// -> SELECT * FROM [customers] WHERE [name] LIKE 'K%' + + // if & else & (optional) end @@ -38,6 +41,8 @@ WHERE [id] > 0 %if", ($foo > 0), "AND [foo]=%i", $foo, " %else %if", ($bar > 0), "AND [bar]=%i", $bar, " "); +// -> SELECT * FROM [people] WHERE [id] > 0 AND [bar]=2 + // nested condition @@ -49,3 +54,4 @@ WHERE %if', $cond2, 'AND [admin]=1 %end %else 1 LIMIT 10 %end' ); +// -> SELECT * FROM [customers] WHERE LIMIT 10 \ No newline at end of file diff --git a/examples/table-prefix.php b/examples/table-prefix.php index fc13185c..9391a6c5 100644 --- a/examples/table-prefix.php +++ b/examples/table-prefix.php @@ -10,12 +10,14 @@ dibi::connect(array( )); + + // create new substitution :blog: ==> wp_ dibi::addSubst('blog', 'wp_'); - -// generate and dump SQL dibi::test("UPDATE [:blog:items] SET [text]='Hello World'"); +// -> UPDATE [wp_items] SET [text]='Hello World' + @@ -23,22 +25,20 @@ dibi::test("UPDATE [:blog:items] SET [text]='Hello World'"); // create new substitution :: (empty) ==> my_ dibi::addSubst('', 'my_'); - -// generate and dump SQL dibi::test("UPDATE [database.::table] SET [text]='Hello World'"); +// -> UPDATE [database].[my_table] SET [text]='Hello World' +// create substitution fallback function substFallBack($expr) { return 'the_' . $expr; } -// create substitution fallback dibi::setSubstFallBack('substFallBack'); - -// generate and dump SQL dibi::test("UPDATE [:account:user] SET [name]='John Doe'"); +// -> UPDATE [the_accountuser] SET [name]='John Doe' diff --git a/examples/transaction.php b/examples/transaction.php index 14f43443..77f5734f 100644 --- a/examples/transaction.php +++ b/examples/transaction.php @@ -13,7 +13,7 @@ dibi::connect(array( echo "

Before:

\n"; dibi::query('SELECT * FROM [products]')->dump(); - +// -> 3 rows dibi::begin(); @@ -25,4 +25,5 @@ dibi::rollback(); // or dibi::commit(); echo "

After:

\n"; -dibi::query('SELECT * FROM [products]')->dump(); \ No newline at end of file +dibi::query('SELECT * FROM [products]')->dump(); +// -> 3 rows