Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 53 lines (37 sloc) 1.158 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">

<h1>Using Substitutions | dibi</h1>

<?php

require __DIR__ . '/../dibi/dibi.php';


dibi::connect(array(
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
));


// create new substitution :blog: ==> wp_
dibi::getSubstitutes()->blog = 'wp_';

dibi::test("SELECT * FROM [:blog:items]");
// -> SELECT * FROM [wp_items]


// create new substitution :: (empty) ==> my_
dibi::getSubstitutes()->{''} = 'my_';

dibi::test("UPDATE ::table SET [text]='Hello World'");
// -> UPDATE my_table SET [text]='Hello World'


// create substitutions using fallback callback
function substFallBack($expr)
{
$const = 'SUBST_' . strtoupper($expr);
if (defined($const)) {
return constant($const);
} else {
throw new Exception("Undefined substitution :$expr:");
}
}

// define callback
dibi::getSubstitutes()->setCallback('substFallBack');

// define substitutes as constants
define('SUBST_ACCOUNT', 'eshop_');
define('SUBST_ACTIVE', 7);

dibi::test("
UPDATE :account:user
SET name='John Doe', status=:active:
WHERE id=", 7
);
// -> UPDATE eshop_user SET name='John Doe', status=7 WHERE id= 7
Something went wrong with that request. Please try again.