monii/specification-sql-adapter

This package is abandoned and no longer maintained. No replacement package was suggested.

Specification SQL Adapter

dev-master / 0.0.x-dev 2016-02-24 11:30 UTC

This package is not auto-updated.

Last update: 2016-12-03 16:03:12 UTC


README

A SQL Adapter for Monii's implementation of the Specification pattern. It provides the tools to render a Specification into a SQL query.

Latest Stable Version Total Downloads Latest Unstable Version License
Build Status

Requirements

Installation

$> composer require monii/specification-sql-adapter

Until a stable version has been released or if a development version is preferred, use:

$> composer require monii/specification-sql-adapter:@dev

Limitations

This library does not currently support joins and it may never do so. The underlying SqlQuery class is very simple and needs more testing. It works well for what we've designed it to do but it could still use a lot of work.

If you need something far more robust you could consider writing your own implementation. The Specification library is intended to support specialized rendering adapters to suite specific needs.

Example Usage

Repository

An easy example of how this renderer can be used with Monii's Specification package is by looking at an example Doctrine DBAL repository implementation. In this case, our query builder is setup to know how to map two properties to their corresponding table columns. We also assume that objectsFromRows is shared such that it can take an array of plain arrays to construct the objects that were selected.

class DbalContactRepository implements ContactRepository
{
    /**
     * @var Connection
     */
    private $connection;

    /**
     * @var SqlSpecificationQueryBuilder
     */
    private $sqlSpecificationQueryBuilder;

    /**
     * @var string
     */
    private $tableName;

    /**
     * @param string $tableName
     */
    public function __construct(
        Connection $connection,
        SqlSpecificationQueryBuilder $sqlSpecificationQueryBuilder,
        $tableName = 'terse_contact'
    ) {
        $this->connection = $connection;
        $this->sqlSpecificationQueryBuilder = $sqlSpecificationQueryBuilder;
        $this->tableName = $tableName;
    }

    /**
     * {@inheritdoc}
     */
    public function findMatching(Specification $specification)
    {
        $sqlQuery = $this->sqlSpecificationQueryBuilder->buildQuery($specification, [
            'person.firstName' => 'person_first_name',
            'person.lastName' => 'person_last_name',
        ]);

        $statement = $this->connection->prepare($sqlQuery->getSql($this->tableName));
        $statement->execute($sqlQuery->getValues());
        $results = $statement->fetchAll();

        return $this->objectsFromRows($results);
    }
}

License

MIT, see LICENSE.

Community

Want to get involved? Here are a few ways:

  • Find us in the #monii IRC channel on irc.freenode.org.
  • Mention @moniidev on Twitter.