query-language-basic-examples.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <!DOCTYPE html><link rel="stylesheet" href="data/style.css">
  2. <h1>Query Language Basic Examples | dibi</h1>
  3. <?php
  4. require __DIR__ . '/../src/loader.php';
  5. date_default_timezone_set('Europe/Prague');
  6. dibi::connect([
  7. 'driver' => 'sqlite3',
  8. 'database' => 'data/sample.s3db',
  9. ]);
  10. // SELECT
  11. $ipMask = '192.168.%';
  12. $timestamp = mktime(0, 0, 0, 10, 13, 1997);
  13. dibi::test('
  14. SELECT COUNT(*) as [count]
  15. FROM [comments]
  16. WHERE [ip] LIKE ?', $ipMask, '
  17. AND [date] > ', new Dibi\DateTime($timestamp)
  18. );
  19. // -> SELECT COUNT(*) as [count] FROM [comments] WHERE [ip] LIKE '192.168.%' AND [date] > 876693600
  20. // dibi detects INSERT or REPLACE command
  21. dibi::test('
  22. REPLACE INTO products', [
  23. 'title' => 'Super product',
  24. 'price' => 318,
  25. 'active' => true,
  26. ]);
  27. // -> REPLACE INTO products ([title], [price], [active]) VALUES ('Super product', 318, 1)
  28. // multiple INSERT command
  29. $array = [
  30. 'title' => 'Super Product',
  31. 'price' => 12,
  32. 'brand' => null,
  33. 'created' => new DateTime,
  34. ];
  35. dibi::test('INSERT INTO products', $array, $array, $array);
  36. // -> INSERT INTO products ([title], [price], [brand], [created]) VALUES ('Super Product', ...) , (...) , (...)
  37. // dibi detects UPDATE command
  38. dibi::test('
  39. UPDATE colors SET', [
  40. 'color' => 'blue',
  41. 'order' => 12,
  42. ], '
  43. WHERE id=?', 123);
  44. // -> UPDATE colors SET [color]='blue', [order]=12 WHERE id=123
  45. // modifier applied to array
  46. $array = [1, 2, 3];
  47. dibi::test('
  48. SELECT *
  49. FROM people
  50. WHERE id IN (?)', $array
  51. );
  52. // -> SELECT * FROM people WHERE id IN ( 1, 2, 3 )
  53. // modifier %by for ORDER BY
  54. $order = [
  55. 'field1' => 'asc',
  56. 'field2' => 'desc',
  57. ];
  58. dibi::test('
  59. SELECT *
  60. FROM people
  61. ORDER BY %by', $order, '
  62. ');
  63. // -> SELECT * FROM people ORDER BY [field1] ASC, [field2] DESC
  64. // indentifiers and strings syntax mix
  65. dibi::test('UPDATE [table] SET `item` = "5 1/4"" diskette"');
  66. // -> UPDATE [table] SET [item] = '5 1/4" diskette'