auraphp / Aura.SqlQuery

Independent query builders for MySQL, PostgreSQL, SQLite, and Microsoft SQL Server.
MIT License
452 stars 86 forks source link

Placeholder triples for where() and having() clauses #164

Open dc2xl opened 6 years ago

dc2xl commented 6 years ago

Hi all,

In PR #142 @pmjones suggests a more concise way to handle prepared statement placeholders because the 3.x version feels clunky and verbose. His suggestion uses sequential placeholders and we prefer to use named placeholders for documentation and easier debugging.

So here is a suggestion, which supports a single named placeholder only, but works very well for us because of its conciseness. Usage is like this

$select->where("foo = ", ":FOO", "value")->where("bar IN ", "(:BARS)", [1, 2, 3]);
// AND WHERE foo = :FOO AND WHERE bar IN (:BARS)

Interface looks like this:

/**
 *
 * ADD a WHERE condition with a prepared statement placeholder and value to the query.
 *
 * @param string $cond the first part of the where condition without the placeholder. e.g. "foo = " or "bar IN"
 * @param string $placeholder the placeholder as a string. e.g. ":FOO" or "(:BARS)"
 * @param (string|int|float|array) $value the value to bind to the placeholder. e.g. "abc" or [1, 2, 3]
 * @return $this
 */
public function where(string $statement, string $placeholder, $bindValue);

Of course there should corresponding versions for orWhere, having .

This is a feature/discussion request. We have extended the ExtendedPDO inhouse to provide the request functionality. I could polish it up, add missing parts and contribute a PR if the project is interested.