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
|
|
|
|
|
2015-01-13 16:27:01 +01:00
|
|
|
require __DIR__ . '/../src/loader.php';
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
dibi::connect(array(
|
2015-06-19 03:11:36 +02:00
|
|
|
'driver' => 'sqlite3',
|
2012-10-18 23:00:12 +02:00
|
|
|
'database' => 'data/sample.s3db',
|
2008-07-17 03:51:29 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
// create new substitution :blog: ==> wp_
|
2011-01-25 17:47:31 +01:00
|
|
|
dibi::getSubstitutes()->blog = 'wp_';
|
2008-07-17 03:51:29 +00:00
|
|
|
|
2015-06-19 03:11:36 +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
|
|
|
|
2010-08-05 23:55:59 +02:00
|
|
|
// create new substitution :: (empty) ==> my_
|
2011-01-25 17:47:31 +01:00
|
|
|
dibi::getSubstitutes()->{''} = 'my_';
|
2010-08-05 23:55:59 +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);
|
2009-03-08 23:27:31 +00:00
|
|
|
} else {
|
2010-08-03 11:48:51 +02:00
|
|
|
throw new Exception("Undefined substitution :$expr:");
|
2009-03-08 23:27:31 +00:00
|
|
|
}
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
2010-08-03 11:48:51 +02:00
|
|
|
// define callback
|
2011-01-25 17:47:31 +01: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);
|
|
|
|
|
|
|
|
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
|