View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFPROP:Proposal Zone Template}

Zend_Db_Expr Function Abstraction

[Benjamin Eberlei|]


1.0 - 3 August 2009: Initial Draft.

Currently Zend_Db_Select does not allow to write completly portable applications when it comes to
abstraction of SQL functions. This proposal aims to close the gap as much as possible by offering
an Expression object inside Zend_Db_Select and Zend_Db_Adapter_Abstract. The largest possible
subset of functions between all the current supported vendors is integrated, possible a subset of ANSI-SQL 92.

* [SQL Functions programmers reference|]
* [ezComponents Query Expression Object API|]

* This component *MUST* abstract as much SQL functions as possible that are common between: MySQL, PgSql, SqLite, Oracle, IBM Db2, SqlSrv
* This component *MUST* be nested inside each Zend_Db_Select instance and globally inside the Adapter
* The Adapter *SHOULD* enforce the expression object to be created only once.
* This component *WILL* offer a non-compliant modus where additional vendor-specific functions are nested in
* This component *WILL* use quoting/escaping facilities internally.
* This component *WILL* enforce escaping of all variables and columns in each expression.
* Use of this component with Zend_Db_Select is *OPTIONAL*, the old way of implementing expressions stays the same.

* Zend_Db_Adapter_Abstract
* Zend_Db_Select

You can use the Query Expression object to generate Zend_Db_Expr instances with specific supported SQL functions.

The question is, how would this query expression object be available to the consumer? There are several possible APIs:


I think the expr() method is the best choice although its the longest, because other programming languages like
C# or Java use Expression as a name for objects that perform actions on values.

Use of it would follow the lines of:

$select = $db->select();
->where( $select->expr()->eq("columnName", $value) );
->where( $select->expr()->like("columnName2", $value2."%") );
->where( $select->expr()->notEq($select->expr()->upper("col3", $value3)) );

In a first version I would opt to implement a common subset of functions accross all major database vendors, probably
ANSI SQL 92 is a good start. Additionally if "non" compatible modus will be included that abstracts as much functions
from each vendor as possible.

Because some functions have conflicting names in different vendor languages, i want to go away from sql naming
in some cases and use programming language names on some issues. I guess the hardest part of this proposal is the naming
issue, implementation should be easy if everything is agreed on.

* Milestone 1: Community Review
* Milestone 2: Zend Acceptance
* Milestone 3: Implementation & Documentation

* Zend_Db_Expr_ExpressionAbstract
* Zend_Db_Expr_Mysql
* Zend_Db_Expr_Sqlite
* Zend_Db_Expr_Oracle
* Zend_Db_Expr_IbmDb2
* Zend_Db_Expr_PgSql
* Zend_Db_Expr_MsSqlServer



{card:label=Use-Case 1: Comparison Operators}
$select->fn()->eq("foo", $var, Zend_Db::PARAM_INT); // $db->quoteInto("foo = ?", $var, Zend_Db::PARAM_INT);
$select->fn()->notEq("foo", $var, Zend_Db::PARAM_INT); // $db->quoteInto("foo != ?", $var, Zend_Db::PARAM_INT);
$select->fn()->lt("foo", $var); // $db->quoteInto("foo < ?", $var);
$select->fn()->gt("foo", $var); // $db->quoteInto("foo > ?", $var);
$select->fn()->lte("foo", $var); // $db->quoteInto("foo <= ?", $var);
$select->fn()->gte("foo", $var); // $db->quoteInto("foo >= ?", $var);

{card:label=Use-Case 2: String Functions}
$select->fn()->upper("foo"); // new Zend_Db_Expr("UPPER(".$db->quoteIdentifier("foo").")");
$select->fn()->lower("foo"); // new Zend_Db_Expr("LOWER(".$db->quoteIdentifier("foo").")");
$select->fn()->trim("foo"); // new Zend_Db_Expr("RTRIM(LTRIM(".$db->quoteIdentifier("foo")."))");
$select->fn()->substr("foo", 0, 2);
$select->fn()->strpos("foobar", "bar");

{card:label=Use-Case 3: Group Functions}

{card:label=Use Case 4: Math Functions}


{card:label=Use-Case 5: Range Operations}
$select->fn()->in("column", array("foo", "bar", "baz"));
$select->fn()->between("column", 1000, 2000, Zend_Db::PARAM_INT);

{card:label=Use-Case 5: NULL Operations}
$select->fn()->eqNull("column"); // $db->quoteIdentifier("column")." IS NULL";

{card:label=Use-Case 6: Conditional Functions}
$select->fn()->if( $select->fn()->eq("foo", 42), "a", "b");
$select->fn()->eqNull($var, "1234", Zend_Db::PARAM_INT);
array(), array(), ...

{card:label=Use-Case 7: Date and Time Functions}

{card:label=Use-Case 8: Aggregation Example}
$select->columns(array("country", $select->fn()->sum("population")))

// SELECT, SUM(cities.population)
// FROM cities

{card:label=Use-Case 9: String Example}
$select->columns($select->fn()->substr("firstname", 0, 1), $select->fn()->count())
->group($select->fn()->substr("firstname", 0, 1));

// SELECT SUBSTRING(persons.firstname, 0, 1), count(*)
// FROM persons
// GROUP BY SUBSTRING(persons.firstname, 0, 1)

{card:label=Use-Case 10: Comparison Operators}
->where($select->fn()->eq("name", $name))
->where($select->fn()->gt("age", $greaterThanAge))
->where($select->fn()->in("eyeColor", array("blue", "green"))
->where($select->fn()->between("salary", 20000, 50000);

// SELECT persons.* FROM persons
// WHERE name = 'Foo' AND
// age > 20 AND
// eyeColor IN ('blue', 'green') AND
// salary BETWEEN 20000, 50000;

{card:label=Non-Verbose UC-10}
// Low Verbosity:
$fn = $q->fn();
->where($fn->eq("name", $name))
->where($fn->gt("age", $greaterThanAge, Zend_Db::PARAM_INT))
->where($fn->in("eyeColor", array("blue", "green"))
->where($fn->between("salary", 20000, 50000);