using-substitutions.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <!DOCTYPE html><link rel="stylesheet" href="data/style.css">
  2. <h1>Using Substitutions | dibi</h1>
  3. <?php
  4. require __DIR__ . '/../src/loader.php';
  5. dibi::connect([
  6. 'driver' => 'sqlite3',
  7. 'database' => 'data/sample.s3db',
  8. ]);
  9. // create new substitution :blog: ==> wp_
  10. dibi::getSubstitutes()->blog = 'wp_';
  11. dibi::test('SELECT * FROM [:blog:items]');
  12. // -> SELECT * FROM [wp_items]
  13. // create new substitution :: (empty) ==> my_
  14. dibi::getSubstitutes()->{''} = 'my_';
  15. dibi::test("UPDATE ::table SET [text]='Hello World'");
  16. // -> UPDATE my_table SET [text]='Hello World'
  17. // create substitutions using fallback callback
  18. function substFallBack($expr)
  19. {
  20. $const = 'SUBST_' . strtoupper($expr);
  21. if (defined($const)) {
  22. return constant($const);
  23. } else {
  24. throw new Exception("Undefined substitution :$expr:");
  25. }
  26. }
  27. // define callback
  28. dibi::getSubstitutes()->setCallback('substFallBack');
  29. // define substitutes as constants
  30. define('SUBST_ACCOUNT', 'eshop_');
  31. define('SUBST_ACTIVE', 7);
  32. dibi::test("
  33. UPDATE :account:user
  34. SET name='John Doe', status=:active:
  35. WHERE id=", 7
  36. );
  37. // -> UPDATE eshop_user SET name='John Doe', status=7 WHERE id= 7