1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-22 09:53:11 +01:00
php-dibi/examples/query-language-basic-examples.php

93 lines
2.0 KiB
PHP
Raw Normal View History

2017-07-21 21:34:37 +02:00
<?php
declare(strict_types=1);
?>
2010-08-03 12:28:07 +02:00
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
2010-08-03 11:48:51 +02:00
<h1>Query Language Basic Examples | dibi</h1>
2010-08-03 12:28:07 +02:00
2008-07-17 03:51:29 +00:00
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
2008-07-17 03:51:29 +00:00
date_default_timezone_set('Europe/Prague');
2018-04-17 10:03:43 +02:00
$dibi = new Dibi\Connection([
2015-06-19 03:11:36 +02:00
'driver' => 'sqlite3',
2012-12-16 19:31:37 +01:00
'database' => 'data/sample.s3db',
2015-10-06 01:39:01 +02:00
]);
2008-07-17 03:51:29 +00:00
2010-08-03 11:48:51 +02:00
// SELECT
$ipMask = '192.168.%';
$timestamp = mktime(0, 0, 0, 10, 13, 1997);
2018-04-17 10:03:43 +02:00
$dibi->test('
2010-08-03 11:48:51 +02:00
SELECT COUNT(*) as [count]
FROM [comments]
WHERE [ip] LIKE ?', $ipMask, '
AND [date] > ', new Dibi\DateTime($timestamp)
2010-08-03 11:48:51 +02:00
);
// -> SELECT COUNT(*) as [count] FROM [comments] WHERE [ip] LIKE '192.168.%' AND [date] > 876693600
2008-07-17 03:51:29 +00:00
// dibi detects INSERT or REPLACE command
2018-04-17 10:03:43 +02:00
$dibi->test('
2015-10-06 01:39:01 +02:00
REPLACE INTO products', [
2010-08-03 11:48:51 +02:00
'title' => 'Super product',
'price' => 318,
'active' => true,
2015-10-06 01:39:01 +02:00
]);
// -> REPLACE INTO products ([title], [price], [active]) VALUES ('Super product', 318, 1)
2008-10-28 02:06:55 +00:00
2008-07-17 03:51:29 +00:00
// multiple INSERT command
2015-10-06 01:39:01 +02:00
$array = [
2010-08-03 11:48:51 +02:00
'title' => 'Super Product',
'price' => 12,
'brand' => null,
'created' => new DateTime,
2015-10-06 01:39:01 +02:00
];
2018-04-17 10:03:43 +02:00
$dibi->test('INSERT INTO products', $array, $array, $array);
// -> INSERT INTO products ([title], [price], [brand], [created]) VALUES ('Super Product', ...) , (...) , (...)
2008-10-28 02:06:55 +00:00
2008-07-17 03:51:29 +00:00
// dibi detects UPDATE command
2018-04-17 10:03:43 +02:00
$dibi->test('
2015-10-06 01:39:01 +02:00
UPDATE colors SET', [
2008-10-28 02:06:55 +00:00
'color' => 'blue',
'order' => 12,
2015-10-06 01:39:01 +02:00
], '
2015-06-19 03:11:36 +02:00
WHERE id=?', 123);
// -> UPDATE colors SET [color]='blue', [order]=12 WHERE id=123
2008-10-28 02:06:55 +00:00
2008-07-17 03:51:29 +00:00
2010-08-03 11:48:51 +02:00
// modifier applied to array
2015-10-06 01:39:01 +02:00
$array = [1, 2, 3];
2018-04-17 10:03:43 +02:00
$dibi->test('
2008-10-28 02:06:55 +00:00
SELECT *
FROM people
2015-06-19 03:11:36 +02:00
WHERE id IN (?)', $array
);
// -> SELECT * FROM people WHERE id IN ( 1, 2, 3 )
2008-10-28 02:06:55 +00:00
2010-08-03 11:48:51 +02:00
// modifier %by for ORDER BY
2015-10-06 01:39:01 +02:00
$order = [
'field1' => 'asc',
'field2' => 'desc',
2015-10-06 01:39:01 +02:00
];
2018-04-17 10:03:43 +02:00
$dibi->test('
2010-08-03 11:48:51 +02:00
SELECT *
FROM people
2015-06-19 03:11:36 +02:00
ORDER BY %by', $order, '
');
// -> SELECT * FROM people ORDER BY [field1] ASC, [field2] DESC
2010-08-03 11:48:51 +02:00
// indentifiers and strings syntax mix
2018-04-17 10:03:43 +02:00
$dibi->test('UPDATE [table] SET `item` = "5 1/4"" diskette"');
2010-08-03 11:48:51 +02:00
// -> UPDATE [table] SET [item] = '5 1/4" diskette'