1.2.1. Getting Started

This library provides query statement builders for MySQL, Postgres, SQLite, and Microsoft SQL Server. The statements are independent of any particular database connection, though they work best with PDO, PDO wrappers such asAtlas.Pdo, or query performers such as Atlas.Query.

1.2.1.1. Installation

This package is installable and autoloadable via Composer as atlas/statement.

$ composer require atlas/statement ^1.0

1.2.1.2. Instantiation

Instantiate the relevant Statement object using its static new() method, and pass the name of the database driver to use for identifier quoting, limit clauses, etc:

use Atlas\Statement\Select;
use Atlas\Statement\Insert;
use Atlas\Statement\Update;
use Atlas\Statement\Delete;

$select = Select::new('sqlite');
$insert = Insert::new('sqlite');
$udpate = Update::new('sqlite');
$delete = Delete::new('sqlite');

1.2.1.3. Execution

Note that you will need to transfer the Statement query string and bound values to a database connection of your choice to actually execute the query. Here is one example example of how to do so:

use PDO;
use Atlas\Statement\Select;

$select = Select::new('sqlite');

// ...

$pdo = new PDO('sqlite::memory:');
$sth = $pdo->prepare($select->getQueryString());

foreach ($select->getBindValueObjects() as $name => $value) {
    $sth->bindValue($name, $value->getValue(), $value->getType());
}

$sth->execute();

Tip:

The Atlas.Query package extends this library to add query execution methods directly to Statement objects, thus removing the need to transfer the query string and bound values to a database connection.