Skip to content
c9s edited this page Dec 13, 2014 · 24 revisions

Installation

To use SQLBuilder, you can require the package in your composer.json file:

{
    "require": {
        "corneltek/sqlbuilder": "2~"
    }
}

And then, you can run the command below if you're in the development environment:

composer install --dev --prefer-source

For production environment you can run:

composer install --optimize-autoloader

Defining Driver Object

SQLBuilder detect the database platform (mysql, pgsql, sqlite) by checking the driver class instance you pass to it. There are 3 pre-defined driver classes:

  • SQLBuilder\Driver\MySQLDriver
  • SQLBuilder\Driver\PgSQLDriver
  • SQLBuilder\Driver\SQLiteDriver

The internal implementation is using the code below to check the driver type, hence it's faster than comparing strings:

if ($driver instanceof MySQLDriver) {
   // .. do something with MySQL
}

To initialize a driver object is easy:

use SQLBuilder\Driver\MySQLDriver;
$driver = new MySQLDriver;

And you're done.

These pre-defined driver classes derives from SQLBuilder\Driver\BaseDriver, and SQLBuilder\Driver\BaseDriver defines the core methods that can quote strings, quote identifiers, deflate values... and so on.

Setting up string quote handler

To quote strings into SQL queries, you can specify the quote handler to your driver object, for example, PDO::quote method:

$driver->setQuoter(array($pdo, 'quote'));

But if you're binding your string value with parameter marker, you don't actually need it. we will describe how to bind a value the later section.

Preparing ArgumentArray

SQLBuilder deflates your variables into the parameter markers, hence you need to provide an object that can register arguments, that is, ArgumentArray.

Driver object and ArgumentArray is only used when calling toSQl method.

use SQLBuilder\ArgumentArray;
$args = new ArgumentArray;

$query->toSql($driver, $args);
Clone this wiki locally