zendframework/zf2 — pull request #934 by ralphschindler (2012-03-16 17:49:18+00:00)
Title: Feature/db story #41

Changes in this Pull Request
============================

Expression Objects
------------------

The expression object was created to abstract the concept of "SQL fragments". This was originally implemented in `Zend\Db\Sql\Predicate\Literal`, but `Literal` turns out to be a bad name, and the concept of an "expression" needs to be generic enough to apply to columns as well. For example:

```php
COUNT("column_name") + ? AS "foo"
```

### Using it with `Zend\Db\Sql\Select`

```php
from('foo')
->columns(array(
'bar' => new Expression('COUNT(some_column)')
)
);

$select->getSqlString(); // SELECT COUNT(some_column) AS "bar" FROM "foo"
```

More complex, with values:

```php
from('foo')->columns(
array(
new Expression(
'(COUNT(?) + ?) AS ?',
array('some_column', 5, 'bar'),
array(Expression::TYPE_IDENTIFIER, Expression::TYPE_VALUE, Expression::TYPE_IDENTIFIER)
)
)
);

/**
* SQL: SELECT (COUNT("some_column") + ?) AS "bar" FROM "foo"
* Params: array(5)
*/
$select->prepareStatement(…);
```

Other interesting use cases can be found in the `SelectTest.php` class:

https://github.com/ralphschindler/zf2/blob/feature/db-story-41/tests/Zend/Db/Sql/SelectTest.php#L248-L340

**BC ISSUES**:

* `Zend\Db\Sql\Predicate\Literal` has become `Zend\Db\Sql\Predicate\Expression`

Verbiage
--------

No longer using "databaseOrSchema", or "databaseSchema". Instead, always using "schema". Regardless of implementation, terminology of the heirarchy is:

schema.table.column

TableGateway using Select objects
---------------------------------

TableGateway now has a `selectWith(Select $select)` public method. The allows one to create their own `Select` objects and pass them into to this method for producing a result set. The only stipulation is that both the $table and $schema inside the `Select` object **MUST** match that inside TableGateway object.

Internal Stuffs
---------------

* Better unit testing, particularly for `ZendTest\Db\Adapter\AdapterTest` and `ZendTest\Db\Sql\SelectTest`
* Created a `Zend\Db\Sql\Sql` factory object. To be used by `TableGateway` in the future.
* Added getPredicates() to `PredicateSet`
* `Where` object is now simply an extension of `PredicateSet`. In other words, a facade.
* Cleaned up internal implementations of Sql specifications and joins to use string keys as associative array
* Fixed PDO Driver Connection to create proper SQLite connection string (was broken by previous patch)