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

60 lines
1.2 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>Using Substitutions | 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
2018-04-17 10:03:43 +02:00
$dibi = new Dibi\Connection([
2015-06-19 03:11:36 +02:00
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
2015-10-06 01:39:01 +02:00
]);
2008-07-17 03:51:29 +00:00
// create new substitution :blog: ==> wp_
2018-04-17 10:03:43 +02:00
$dibi->getSubstitutes()->blog = 'wp_';
2008-07-17 03:51:29 +00:00
2018-04-17 10:03:43 +02:00
$dibi->test('SELECT * FROM [:blog:items]');
2010-08-03 11:48:51 +02:00
// -> SELECT * FROM [wp_items]
2008-10-28 02:06:55 +00:00
2008-07-17 03:51:29 +00:00
// create new substitution :: (empty) ==> my_
2018-04-17 10:03:43 +02:00
$dibi->getSubstitutes()->{''} = 'my_';
2018-04-17 10:03:43 +02:00
$dibi->test("UPDATE ::table SET [text]='Hello World'");
// -> UPDATE my_table SET [text]='Hello World'
2010-08-03 11:48:51 +02:00
// create substitutions using fallback callback
2008-07-17 03:51:29 +00:00
function substFallBack($expr)
{
2010-08-03 11:48:51 +02:00
$const = 'SUBST_' . strtoupper($expr);
if (defined($const)) {
return constant($const);
} else {
2010-08-03 11:48:51 +02:00
throw new Exception("Undefined substitution :$expr:");
}
2008-07-17 03:51:29 +00:00
}
2017-07-24 14:46:58 +02:00
2010-08-03 11:48:51 +02:00
// define callback
2018-04-17 10:03:43 +02:00
$dibi->getSubstitutes()->setCallback('substFallBack');
2008-07-17 03:51:29 +00:00
2010-08-03 11:48:51 +02:00
// define substitutes as constants
define('SUBST_ACCOUNT', 'eshop_');
define('SUBST_ACTIVE', 7);
2018-04-17 10:03:43 +02:00
$dibi->test("
2010-08-05 23:56:56 +02:00
UPDATE :account:user
SET name='John Doe', status=:active:
2010-08-03 11:48:51 +02:00
WHERE id=", 7
);
2010-08-05 23:56:56 +02:00
// -> UPDATE eshop_user SET name='John Doe', status=7 WHERE id= 7