monii / specification-sql-adapter
Specification SQL Adapter
Installs: 1 215
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^5.5|~7.0
- monii/specification: ^0.0@dev
Requires (Dev)
- phpunit/phpunit: ^4.8
- squizlabs/php_codesniffer: ^2.3
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.
Requirements
- PHP 5.5+
- monii/specification
- Something to consume SQL queries
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.