It seems like a strange feature to remove conditions from a query if the bound value is null. I guess I think of this as application logic for building special kinds of queries such as searches based on user input.
That may be the case, however I am not removing conditionals, so much as I am abstracting them, so they are expressed in a more declarative way, as opposed to imperative. Just like SQL itself, when performing queries you do not worry about 'how' just the 'what', I see this as yet another layer of abstraction over top of SQL.
NULL is just the most logical default, but you can override that default to be whatever you want, if a string = 'ALEX' you could chose to remove it from the query. Actually I think I am going to look into changing this to isset() as a default, that way, if you post with 10 query fields set to something, the query would use all 10.
I see what your saying though, where do you draw the line? Do you use only the most limited SQL features, such as:
Then do you iterate the records and apply business logic, such as, showing only male students, age 25 to 30, whose weigh exceeds 200Lbs? Or do you throw that code into the query and be done with it?
I considered this long and hard and ultimately decided that, while keeping some business logic in SQL means I am moving it from the business logic layer and into the data access layer, I favor code readability and maintainability over seperation of concerns and testability. Reading declarative code such as:
Code: Select all
SELECT * FROM students WHERE gender = 'male' AND age > 25 AND age < 30 AND weight > 200
I find much easier to read, maintain and comprehend than a dozen or so lines of conditional testing, within in a foreach() all my abstraction does is further shift some of the responsibility from business logic into a data abstraction layer.
Code: Select all
$gender = null;
$weight = 200;
$gateway->select($table, array('gender' => $gender, 'weight' => $weight));
No conditional testing for whether the weight is specified or gender, and whether they need to be included in the resulting query, that is done for you by the underlying data abstraction layer. Again whether the default is NULL or isset() or some random value is of no significance.
Cheers,
Alex