Table prefixes

Use the TablePrefixer class to add prefixes to all the tables in a query:

use Finesse\QueryScribe\PostProcessors\TablePrefixer;
use Finesse\QueryScribe\Query;

$prefixer = new TablePrefixer('prefix_'); // Needs to be created once

$query = (new Query)
    ->whereExists(function ($query) {
            ->from('comments', 'c')
            ->whereColumn('c.post_id', '');
    ->where('', '>', '2017-11-11');

$prefixedQuery = $query->apply($prefixer);

    SELECT * FROM "prefix_posts" 
        SELECT * FROM "prefix_comments" AS "c" 
        WHERE "c"."post_id" = "prefix_posts"."id"
    ) AND "prefix_posts"."date" > ?

As you can see table aliases are not prefixed. Prefixer automatically detects which identifiers are table aliases.

Table prefixes are not added in raw expressions. You can use the helper methods to add a prefix:

(new Query)
    ->from(new Raw('MAGIC('.$prefixer->addTablePrefix('my_table').')'))
    ->addSelect(new Raw('REPLACE('.$prefixer->addTablePrefixToColumn('').', ?, ?)', ['small', 'big']));

Prefixer doesn't modify a given Query object therefore doing this is safe:

$prefixer1 = new TablePrefixer('prefix1_');
$prefixer2 = new TablePrefixer('prefix2_');

$query = (new Query)/* -> ... */;

$prefixedQuery1 = $query->apply($prefixer1); // The prefix is `prefix1_`
$prefixedQuery2 = $query->apply($prefixer2); // The prefix is `prefix2_`