Skip to content

Commit

Permalink
Merge pull request #162 from tractorcow/pulls/32only
Browse files Browse the repository at this point in the history
API Take advantage of parameterised queries
  • Loading branch information
Damian Mooyman committed Nov 6, 2015
2 parents 7e10795 + 38063f5 commit aa54d01
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ php:
- 5.3

env:
- DB=MYSQL CORE_RELEASE=3.1
- DB=MYSQL CORE_RELEASE=3.2

matrix:
include:
- php: 5.5
env: DB=PGSQL CORE_RELEASE=3.1
env: DB=PGSQL CORE_RELEASE=3.2
- php: 5.6
env: DB=MYSQL CORE_RELEASE=3.2
- php: 5.6
Expand Down
37 changes: 27 additions & 10 deletions code/extensions/FluentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,6 @@ protected function localiseSelect($class, $select, $fallback) {
}

public function augmentSQL(SQLQuery &$query, DataQuery &$dataQuery = null) {

// Get locale and translation zone to use
$default = Fluent::default_locale();
$locale = $dataQuery->getQueryParam('Fluent.Locale') ?: Fluent::current_locale();
Expand Down Expand Up @@ -539,28 +538,46 @@ public function augmentSQL(SQLQuery &$query, DataQuery &$dataQuery = null) {
$query->selectField($defaultExpression, $defaultField);
}

// Rewrite where conditions
$where = $query->getWhere();
// Rewrite where conditions with parameterised query (3.2 +)
$where = $query
->toAppropriateExpression()
->getWhere();
foreach($where as $index => $condition) {
// Extract parameters from condition
if($condition instanceof SQLConditionGroup) {
$parameters = array();
$predicate = $condition->conditionSQL($parameters);
} else {
$parameters = array_values(reset($condition));
$predicate = key($condition);
}

// determine the table/column this condition is against
$filterColumn = $this->detectFilterColumn($condition, $includedTables, $locale);
if(empty($filterColumn)) continue;
$filterColumn = $this->detectFilterColumn($predicate, $includedTables, $locale);
if(empty($filterColumn)) {
continue;
}

// Duplicate the condition with all localisable fields replaced
$localisedCondition = $this->localiseFilterCondition($condition, $includedTables, $locale);
if($localisedCondition === $condition) continue;
$localisedPredicate = $this->localiseFilterCondition($predicate, $includedTables, $locale);
if($localisedPredicate === $predicate) {
continue;
}

// Generate new condition that conditionally executes one of the two conditions
// depending on field nullability.
// If the filterColumn is null or empty, then it's considered untranslated, and
// thus the query should continue running on the default column unimpeded.
$castColumn = "COALESCE(CAST($filterColumn AS CHAR), '')";
$where[$index] = "
($castColumn != '' AND $castColumn != '0' AND ($localisedCondition))
$newPredicate = "
($castColumn != '' AND $castColumn != '0' AND ($localisedPredicate))
OR (
($castColumn = '' OR $castColumn = '0') AND ($condition)
($castColumn = '' OR $castColumn = '0') AND ($predicate)
)";
// Duplicate this condition with parameters duplicated
$where[$index] = array(
$newPredicate => array_merge($parameters, $parameters)
);
}
$query->setWhere($where);

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
"issues": "http://github.com/tractorcow/silverstripe-fluent/issues"
},
"require": {
"silverstripe/framework": ">=3.1.1",
"silverstripe/cms": ">=3.1.1",
"silverstripe/framework": "~3.2",
"silverstripe/cms": "~3.2",
"composer/installers": "*"
},
"extra": {
"branch-alias": {
"dev-master": "3.2.x-dev"
"dev-master": "3.3.x-dev"
}
},
"extra": {
Expand Down

0 comments on commit aa54d01

Please sign in to comment.