From 90ce8763788acb5d81aa1011528c6576a0b0d153 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 26 Jan 2016 10:45:29 +0100 Subject: Adjust comments to the new constants --- lib/private/db/querybuilder/expressionbuilder.php | 2 +- lib/private/db/querybuilder/ociexpressionbuilder.php | 4 +++- lib/public/db/querybuilder/iexpressionbuilder.php | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/private/db/querybuilder/expressionbuilder.php b/lib/private/db/querybuilder/expressionbuilder.php index 1e86db5a081..a4621da1a4e 100644 --- a/lib/private/db/querybuilder/expressionbuilder.php +++ b/lib/private/db/querybuilder/expressionbuilder.php @@ -109,7 +109,7 @@ class ExpressionBuilder implements IExpressionBuilder { * * @param mixed $x The left expression. * @param mixed $y The right expression. - * @param int|null $type one of the \PDO::PARAM_* constants + * @param int|null $type one of the IQueryBuilder::PARAM_* constants * required when comparing text fields for oci compatibility * * @return string diff --git a/lib/private/db/querybuilder/ociexpressionbuilder.php b/lib/private/db/querybuilder/ociexpressionbuilder.php index 742611bf719..62cb5aa62ad 100644 --- a/lib/private/db/querybuilder/ociexpressionbuilder.php +++ b/lib/private/db/querybuilder/ociexpressionbuilder.php @@ -21,11 +21,13 @@ namespace OC\DB\QueryBuilder; +use OCP\DB\QueryBuilder\IQueryBuilder; + class OCIExpressionBuilder extends ExpressionBuilder { public function eq($x, $y, $type = null) { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); - if ($type === \PDO::PARAM_STR) { + if ($type === IQueryBuilder::PARAM_STR) { $x = new QueryFunction('to_char(' . $x . ')'); } return $this->expressionBuilder->eq($x, $y); diff --git a/lib/public/db/querybuilder/iexpressionbuilder.php b/lib/public/db/querybuilder/iexpressionbuilder.php index 0549d3f0125..6163680c126 100644 --- a/lib/public/db/querybuilder/iexpressionbuilder.php +++ b/lib/public/db/querybuilder/iexpressionbuilder.php @@ -84,7 +84,7 @@ interface IExpressionBuilder { * * @param mixed $x The left expression. * @param mixed $y The right expression. - * @param int|null $type @since 9.0.0 one of the \PDO::PARAM_* constants + * @param int|null $type @since 9.0.0 one of the IQueryBuilder::PARAM_* constants * required when comparing text fields for oci compatibility. * * @return string -- cgit v1.2.3 From 751469539916a3b81f4d6a0bf29c848e951e70d8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 26 Jan 2016 11:27:35 +0100 Subject: Fix Oracle comparisons --- lib/private/db/querybuilder/expressionbuilder.php | 46 ++++-- .../db/querybuilder/ociexpressionbuilder.php | 167 ++++++++++++++++++++- lib/public/db/querybuilder/iexpressionbuilder.php | 98 +++++++++--- .../lib/db/querybuilder/expressionbuildertest.php | 56 ++++--- 4 files changed, 311 insertions(+), 56 deletions(-) (limited to 'lib') diff --git a/lib/private/db/querybuilder/expressionbuilder.php b/lib/private/db/querybuilder/expressionbuilder.php index a4621da1a4e..b688ebfabbe 100644 --- a/lib/private/db/querybuilder/expressionbuilder.php +++ b/lib/private/db/querybuilder/expressionbuilder.php @@ -86,12 +86,14 @@ class ExpressionBuilder implements IExpressionBuilder { * Creates a comparison expression. * * @param mixed $x The left expression. - * @param string $operator One of the ExpressionBuilder::* constants. + * @param string $operator One of the IExpressionBuilder::* constants. * @param mixed $y The right expression. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string */ - public function comparison($x, $operator, $y) { + public function comparison($x, $operator, $y, $type = null) { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); return $this->expressionBuilder->comparison($x, $operator, $y); @@ -109,7 +111,7 @@ class ExpressionBuilder implements IExpressionBuilder { * * @param mixed $x The left expression. * @param mixed $y The right expression. - * @param int|null $type one of the IQueryBuilder::PARAM_* constants + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants * required when comparing text fields for oci compatibility * * @return string @@ -131,10 +133,12 @@ class ExpressionBuilder implements IExpressionBuilder { * * @param mixed $x The left expression. * @param mixed $y The right expression. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string */ - public function neq($x, $y) { + public function neq($x, $y, $type = null) { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); return $this->expressionBuilder->neq($x, $y); @@ -151,10 +155,12 @@ class ExpressionBuilder implements IExpressionBuilder { * * @param mixed $x The left expression. * @param mixed $y The right expression. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string */ - public function lt($x, $y) { + public function lt($x, $y, $type = null) { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); return $this->expressionBuilder->lt($x, $y); @@ -171,10 +177,12 @@ class ExpressionBuilder implements IExpressionBuilder { * * @param mixed $x The left expression. * @param mixed $y The right expression. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string */ - public function lte($x, $y) { + public function lte($x, $y, $type = null) { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); return $this->expressionBuilder->lte($x, $y); @@ -191,10 +199,12 @@ class ExpressionBuilder implements IExpressionBuilder { * * @param mixed $x The left expression. * @param mixed $y The right expression. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string */ - public function gt($x, $y) { + public function gt($x, $y, $type = null) { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); return $this->expressionBuilder->gt($x, $y); @@ -211,10 +221,12 @@ class ExpressionBuilder implements IExpressionBuilder { * * @param mixed $x The left expression. * @param mixed $y The right expression. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string */ - public function gte($x, $y) { + public function gte($x, $y, $type = null) { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); return $this->expressionBuilder->gte($x, $y); @@ -249,10 +261,12 @@ class ExpressionBuilder implements IExpressionBuilder { * * @param string $x Field in string format to be inspected by LIKE() comparison. * @param mixed $y Argument to be used in LIKE() comparison. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string */ - public function like($x, $y) { + public function like($x, $y, $type = null) { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); return $this->expressionBuilder->like($x, $y); @@ -263,10 +277,12 @@ class ExpressionBuilder implements IExpressionBuilder { * * @param string $x Field in string format to be inspected by NOT LIKE() comparison. * @param mixed $y Argument to be used in NOT LIKE() comparison. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string */ - public function notLike($x, $y) { + public function notLike($x, $y, $type = null) { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); return $this->expressionBuilder->notLike($x, $y); @@ -277,10 +293,12 @@ class ExpressionBuilder implements IExpressionBuilder { * * @param string $x The field in string format to be inspected by IN() comparison. * @param string|array $y The placeholder or the array of values to be used by IN() comparison. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string */ - public function in($x, $y) { + public function in($x, $y, $type = null) { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnNames($y); return $this->expressionBuilder->in($x, $y); @@ -291,10 +309,12 @@ class ExpressionBuilder implements IExpressionBuilder { * * @param string $x The field in string format to be inspected by NOT IN() comparison. * @param string|array $y The placeholder or the array of values to be used by NOT IN() comparison. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string */ - public function notIn($x, $y) { + public function notIn($x, $y, $type = null) { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnNames($y); return $this->expressionBuilder->notIn($x, $y); @@ -304,7 +324,7 @@ class ExpressionBuilder implements IExpressionBuilder { * Quotes a given input parameter. * * @param mixed $input The parameter to be quoted. - * @param string|null $type The type of the parameter. + * @param mixed|null $type One of the IQueryBuilder::PARAM_* constants * * @return Literal */ diff --git a/lib/private/db/querybuilder/ociexpressionbuilder.php b/lib/private/db/querybuilder/ociexpressionbuilder.php index 62cb5aa62ad..59ac90e2b13 100644 --- a/lib/private/db/querybuilder/ociexpressionbuilder.php +++ b/lib/private/db/querybuilder/ociexpressionbuilder.php @@ -21,15 +21,180 @@ namespace OC\DB\QueryBuilder; +use OCP\DB\QueryBuilder\ILiteral; +use OCP\DB\QueryBuilder\IParameter; use OCP\DB\QueryBuilder\IQueryBuilder; class OCIExpressionBuilder extends ExpressionBuilder { + + /** + * @inheritdoc + */ + public function comparison($x, $operator, $y, $type = null) { + $x = $this->helper->quoteColumnName($x); + if ($type === IQueryBuilder::PARAM_STR) { + $x = new QueryFunction('to_char(' . $x . ')'); + } + + if ($type === IQueryBuilder::PARAM_STR && !($y instanceof IParameter) && !($y instanceof ILiteral)) { + $y = $this->helper->quoteColumnName($y); + $y = new QueryFunction('to_char(' . $y . ')'); + } else { + $y = $this->helper->quoteColumnName($y); + } + + return $this->expressionBuilder->comparison($x, $operator, $y); + } + + /** + * @inheritdoc + */ public function eq($x, $y, $type = null) { $x = $this->helper->quoteColumnName($x); - $y = $this->helper->quoteColumnName($y); if ($type === IQueryBuilder::PARAM_STR) { $x = new QueryFunction('to_char(' . $x . ')'); } + + if ($type === IQueryBuilder::PARAM_STR && !($y instanceof IParameter) && !($y instanceof ILiteral)) { + $y = $this->helper->quoteColumnName($y); + $y = new QueryFunction('to_char(' . $y . ')'); + } else { + $y = $this->helper->quoteColumnName($y); + } + return $this->expressionBuilder->eq($x, $y); } + + /** + * @inheritdoc + */ + public function neq($x, $y, $type = null) { + $x = $this->helper->quoteColumnName($x); + if ($type === IQueryBuilder::PARAM_STR) { + $x = new QueryFunction('to_char(' . $x . ')'); + } + + if ($type === IQueryBuilder::PARAM_STR && !($y instanceof IParameter) && !($y instanceof ILiteral)) { + $y = $this->helper->quoteColumnName($y); + $y = new QueryFunction('to_char(' . $y . ')'); + } else { + $y = $this->helper->quoteColumnName($y); + } + + return $this->expressionBuilder->neq($x, $y); + } + + /** + * @inheritdoc + */ + public function lt($x, $y, $type = null) { + $x = $this->helper->quoteColumnName($x); + if ($type === IQueryBuilder::PARAM_STR) { + $x = new QueryFunction('to_char(' . $x . ')'); + } + + if ($type === IQueryBuilder::PARAM_STR && !($y instanceof IParameter) && !($y instanceof ILiteral)) { + $y = $this->helper->quoteColumnName($y); + $y = new QueryFunction('to_char(' . $y . ')'); + } else { + $y = $this->helper->quoteColumnName($y); + } + + return $this->expressionBuilder->lt($x, $y); + } + + /** + * @inheritdoc + */ + public function lte($x, $y, $type = null) { + $x = $this->helper->quoteColumnName($x); + if ($type === IQueryBuilder::PARAM_STR) { + $x = new QueryFunction('to_char(' . $x . ')'); + } + + if ($type === IQueryBuilder::PARAM_STR && !($y instanceof IParameter) && !($y instanceof ILiteral)) { + $y = $this->helper->quoteColumnName($y); + $y = new QueryFunction('to_char(' . $y . ')'); + } else { + $y = $this->helper->quoteColumnName($y); + } + + return $this->expressionBuilder->lte($x, $y); + } + + /** + * @inheritdoc + */ + public function gt($x, $y, $type = null) { + $x = $this->helper->quoteColumnName($x); + if ($type === IQueryBuilder::PARAM_STR) { + $x = new QueryFunction('to_char(' . $x . ')'); + } + + if ($type === IQueryBuilder::PARAM_STR && !($y instanceof IParameter) && !($y instanceof ILiteral)) { + $y = $this->helper->quoteColumnName($y); + $y = new QueryFunction('to_char(' . $y . ')'); + } else { + $y = $this->helper->quoteColumnName($y); + } + + return $this->expressionBuilder->gt($x, $y); + } + + /** + * @inheritdoc + */ + public function gte($x, $y, $type = null) { + $x = $this->helper->quoteColumnName($x); + if ($type === IQueryBuilder::PARAM_STR) { + $x = new QueryFunction('to_char(' . $x . ')'); + } + + if ($type === IQueryBuilder::PARAM_STR && !($y instanceof IParameter) && !($y instanceof ILiteral)) { + $y = $this->helper->quoteColumnName($y); + $y = new QueryFunction('to_char(' . $y . ')'); + } else { + $y = $this->helper->quoteColumnName($y); + } + + return $this->expressionBuilder->gte($x, $y); + } + + /** + * @inheritdoc + */ + public function in($x, $y, $type = null) { + $x = $this->helper->quoteColumnName($x); + if ($type === IQueryBuilder::PARAM_STR) { + $x = new QueryFunction('to_char(' . $x . ')'); + } + + if ($type === IQueryBuilder::PARAM_STR && !is_array($y) && !($y instanceof IParameter) && !($y instanceof ILiteral)) { + $y = $this->helper->quoteColumnName($y); + $y = new QueryFunction('to_char(' . $y . ')'); + } else { + $y = $this->helper->quoteColumnNames($y); + } + + return $this->expressionBuilder->in($x, $y); + } + + /** + * @inheritdoc + */ + public function notIn($x, $y, $type = null) { + $x = $this->helper->quoteColumnName($x); + if ($type === IQueryBuilder::PARAM_STR) { + $x = new QueryFunction('to_char(' . $x . ')'); + } + + if ($type === IQueryBuilder::PARAM_STR && !is_array($y) && !($y instanceof IParameter) && !($y instanceof ILiteral)) { + $y = $this->helper->quoteColumnName($y); + $y = new QueryFunction('to_char(' . $y . ')'); + } else { + $y = $this->helper->quoteColumnNames($y); + } + + return $this->expressionBuilder->notIn($x, $y); + } } diff --git a/lib/public/db/querybuilder/iexpressionbuilder.php b/lib/public/db/querybuilder/iexpressionbuilder.php index 6163680c126..a53ae3846c2 100644 --- a/lib/public/db/querybuilder/iexpressionbuilder.php +++ b/lib/public/db/querybuilder/iexpressionbuilder.php @@ -21,11 +21,39 @@ namespace OCP\DB\QueryBuilder; + +use Doctrine\DBAL\Query\Expression\ExpressionBuilder; + /** * This class provides a wrapper around Doctrine's ExpressionBuilder * @since 8.2.0 */ interface IExpressionBuilder { + /** + * @since 9.0.0 + */ + const EQ = ExpressionBuilder::EQ; + /** + * @since 9.0.0 + */ + const NEQ = ExpressionBuilder::NEQ; + /** + * @since 9.0.0 + */ + const LT = ExpressionBuilder::LT; + /** + * @since 9.0.0 + */ + const LTE = ExpressionBuilder::LTE; + /** + * @since 9.0.0 + */ + const GT = ExpressionBuilder::GT; + /** + * @since 9.0.0 + */ + const GTE = ExpressionBuilder::GTE; + /** * Creates a conjunction of the given boolean expressions. * @@ -64,13 +92,15 @@ interface IExpressionBuilder { * Creates a comparison expression. * * @param mixed $x The left expression. - * @param string $operator One of the ExpressionBuilder::* constants. + * @param string $operator One of the IExpressionBuilder::* constants. * @param mixed $y The right expression. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string - * @since 8.2.0 + * @since 8.2.0 - Parameter $type was added in 9.0.0 */ - public function comparison($x, $operator, $y); + public function comparison($x, $operator, $y, $type = null); /** * Creates an equality comparison expression with the given arguments. @@ -84,11 +114,11 @@ interface IExpressionBuilder { * * @param mixed $x The left expression. * @param mixed $y The right expression. - * @param int|null $type @since 9.0.0 one of the IQueryBuilder::PARAM_* constants - * required when comparing text fields for oci compatibility. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string - * @since 8.2.0 + * @since 8.2.0 - Parameter $type was added in 9.0.0 */ public function eq($x, $y, $type = null); @@ -103,11 +133,13 @@ interface IExpressionBuilder { * * @param mixed $x The left expression. * @param mixed $y The right expression. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string - * @since 8.2.0 + * @since 8.2.0 - Parameter $type was added in 9.0.0 */ - public function neq($x, $y); + public function neq($x, $y, $type = null); /** * Creates a lower-than comparison expression with the given arguments. @@ -120,11 +152,13 @@ interface IExpressionBuilder { * * @param mixed $x The left expression. * @param mixed $y The right expression. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string - * @since 8.2.0 + * @since 8.2.0 - Parameter $type was added in 9.0.0 */ - public function lt($x, $y); + public function lt($x, $y, $type = null); /** * Creates a lower-than-equal comparison expression with the given arguments. @@ -137,11 +171,13 @@ interface IExpressionBuilder { * * @param mixed $x The left expression. * @param mixed $y The right expression. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string - * @since 8.2.0 + * @since 8.2.0 - Parameter $type was added in 9.0.0 */ - public function lte($x, $y); + public function lte($x, $y, $type = null); /** * Creates a greater-than comparison expression with the given arguments. @@ -154,11 +190,13 @@ interface IExpressionBuilder { * * @param mixed $x The left expression. * @param mixed $y The right expression. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string - * @since 8.2.0 + * @since 8.2.0 - Parameter $type was added in 9.0.0 */ - public function gt($x, $y); + public function gt($x, $y, $type = null); /** * Creates a greater-than-equal comparison expression with the given arguments. @@ -171,11 +209,13 @@ interface IExpressionBuilder { * * @param mixed $x The left expression. * @param mixed $y The right expression. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string - * @since 8.2.0 + * @since 8.2.0 - Parameter $type was added in 9.0.0 */ - public function gte($x, $y); + public function gte($x, $y, $type = null); /** * Creates an IS NULL expression with the given arguments. @@ -202,50 +242,58 @@ interface IExpressionBuilder { * * @param string $x Field in string format to be inspected by LIKE() comparison. * @param mixed $y Argument to be used in LIKE() comparison. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string - * @since 8.2.0 + * @since 8.2.0 - Parameter $type was added in 9.0.0 */ - public function like($x, $y); + public function like($x, $y, $type = null); /** * Creates a NOT LIKE() comparison expression with the given arguments. * * @param string $x Field in string format to be inspected by NOT LIKE() comparison. * @param mixed $y Argument to be used in NOT LIKE() comparison. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string - * @since 8.2.0 + * @since 8.2.0 - Parameter $type was added in 9.0.0 */ - public function notLike($x, $y); + public function notLike($x, $y, $type = null); /** * Creates a IN () comparison expression with the given arguments. * * @param string $x The field in string format to be inspected by IN() comparison. * @param string|array $y The placeholder or the array of values to be used by IN() comparison. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string - * @since 8.2.0 + * @since 8.2.0 - Parameter $type was added in 9.0.0 */ - public function in($x, $y); + public function in($x, $y, $type = null); /** * Creates a NOT IN () comparison expression with the given arguments. * * @param string $x The field in string format to be inspected by NOT IN() comparison. * @param string|array $y The placeholder or the array of values to be used by NOT IN() comparison. + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants + * required when comparing text fields for oci compatibility * * @return string - * @since 8.2.0 + * @since 8.2.0 - Parameter $type was added in 9.0.0 */ - public function notIn($x, $y); + public function notIn($x, $y, $type = null); /** * Quotes a given input parameter. * * @param mixed $input The parameter to be quoted. - * @param string|null $type The type of the parameter. + * @param mixed|null $type One of the IQueryBuilder::PARAM_* constants * * @return string * @since 8.2.0 diff --git a/tests/lib/db/querybuilder/expressionbuildertest.php b/tests/lib/db/querybuilder/expressionbuildertest.php index fe5b8492786..8310c4adf68 100644 --- a/tests/lib/db/querybuilder/expressionbuildertest.php +++ b/tests/lib/db/querybuilder/expressionbuildertest.php @@ -350,16 +350,26 @@ class ExpressionBuilderTest extends TestCase { public function dataClobComparisons() { return [ - ['eq', '1', IQueryBuilder::PARAM_STR, 1], - ['neq', '1', IQueryBuilder::PARAM_STR, 2], - ['lt', '2', IQueryBuilder::PARAM_STR, 1], - ['lte', '2', IQueryBuilder::PARAM_STR, 2], - ['gt', '2', IQueryBuilder::PARAM_STR, 1], - ['gte', '2', IQueryBuilder::PARAM_STR, 2], - ['like', '%2%', IQueryBuilder::PARAM_STR, 1], - ['notLike', '%2%', IQueryBuilder::PARAM_STR, 2], - ['in', ['2'], IQueryBuilder::PARAM_STR_ARRAY, 1], - ['notIn', ['2'], IQueryBuilder::PARAM_STR_ARRAY, 2], + ['eq', '5', IQueryBuilder::PARAM_STR, false, 3], + ['eq', '5', IQueryBuilder::PARAM_STR, true, 1], + ['neq', '5', IQueryBuilder::PARAM_STR, false, 6], + ['neq', '5', IQueryBuilder::PARAM_STR, true, 4], + ['lt', '5', IQueryBuilder::PARAM_STR, false, 3], + ['lt', '5', IQueryBuilder::PARAM_STR, true, 1], + ['lte', '5', IQueryBuilder::PARAM_STR, false, 6], + ['lte', '5', IQueryBuilder::PARAM_STR, true, 4], + ['gt', '5', IQueryBuilder::PARAM_STR, false, 3], + ['gt', '5', IQueryBuilder::PARAM_STR, true, 1], + ['gte', '5', IQueryBuilder::PARAM_STR, false, 6], + ['gte', '5', IQueryBuilder::PARAM_STR, true, 4], + ['like', '%5%', IQueryBuilder::PARAM_STR, false, 3], + ['like', '%5%', IQueryBuilder::PARAM_STR, true, 1], + ['notLike', '%5%', IQueryBuilder::PARAM_STR, false, 6], + ['notLike', '%5%', IQueryBuilder::PARAM_STR, true, 4], + ['in', ['5'], IQueryBuilder::PARAM_STR_ARRAY, false, 3], + ['in', ['5'], IQueryBuilder::PARAM_STR_ARRAY, true, 1], + ['notIn', ['5'], IQueryBuilder::PARAM_STR_ARRAY, false, 6], + ['notIn', ['5'], IQueryBuilder::PARAM_STR_ARRAY, true, 4], ]; } @@ -368,19 +378,31 @@ class ExpressionBuilderTest extends TestCase { * @param string $function * @param mixed $value * @param mixed $type + * @param bool $compareKeyToValue * @param int $expected */ - public function testClobComparisons($function, $value, $type, $expected) { + public function testClobComparisons($function, $value, $type, $compareKeyToValue, $expected) { $appId = $this->getUniqueID('testing'); - $this->createConfig($appId, 1); - $this->createConfig($appId, 2); - $this->createConfig($appId, 3); + $this->createConfig($appId, 1, 4); + $this->createConfig($appId, 2, 5); + $this->createConfig($appId, 3, 6); + $this->createConfig($appId, 4, 4); + $this->createConfig($appId, 5, 5); + $this->createConfig($appId, 6, 6); + $this->createConfig($appId, 7, 4); + $this->createConfig($appId, 8, 5); + $this->createConfig($appId, 9, 6); $query = $this->connection->getQueryBuilder(); $query->select($query->createFunction('COUNT(*) AS `count`')) ->from('appconfig') ->where($query->expr()->eq('appid', $query->createNamedParameter($appId))) - ->andWhere(call_user_func([$query->expr(), $function], 'configvalue', $query->createNamedParameter($value, $type))); + ->andWhere(call_user_func([$query->expr(), $function], 'configvalue', $query->createNamedParameter($value, $type), IQueryBuilder::PARAM_STR)); + + if ($compareKeyToValue) { + $query->andWhere(call_user_func([$query->expr(), $function], 'configkey', 'configvalue', IQueryBuilder::PARAM_STR)); + } + $result = $query->execute(); $this->assertEquals(['count' => $expected], $result->fetch()); @@ -392,12 +414,12 @@ class ExpressionBuilderTest extends TestCase { ->execute(); } - protected function createConfig($appId, $value) { + protected function createConfig($appId, $key, $value) { $query = $this->connection->getQueryBuilder(); $query->insert('appconfig') ->values([ 'appid' => $query->createNamedParameter($appId), - 'configkey' => $query->createNamedParameter((string) $value), + 'configkey' => $query->createNamedParameter((string) $key), 'configvalue' => $query->createNamedParameter((string) $value), ]) ->execute(); -- cgit v1.2.3 From a0e8a9de612abb0fd5f147d33e69c774d54e5f29 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 26 Jan 2016 12:07:23 +0100 Subject: Introduce helper method --- .../db/querybuilder/ociexpressionbuilder.php | 133 ++++++--------------- 1 file changed, 34 insertions(+), 99 deletions(-) (limited to 'lib') diff --git a/lib/private/db/querybuilder/ociexpressionbuilder.php b/lib/private/db/querybuilder/ociexpressionbuilder.php index 59ac90e2b13..4c127bd752d 100644 --- a/lib/private/db/querybuilder/ociexpressionbuilder.php +++ b/lib/private/db/querybuilder/ociexpressionbuilder.php @@ -21,6 +21,7 @@ namespace OC\DB\QueryBuilder; + use OCP\DB\QueryBuilder\ILiteral; use OCP\DB\QueryBuilder\IParameter; use OCP\DB\QueryBuilder\IQueryBuilder; @@ -28,20 +29,26 @@ use OCP\DB\QueryBuilder\IQueryBuilder; class OCIExpressionBuilder extends ExpressionBuilder { /** - * @inheritdoc + * @param mixed $column + * @param mixed|null $type + * @return array|QueryFunction|string */ - public function comparison($x, $operator, $y, $type = null) { - $x = $this->helper->quoteColumnName($x); - if ($type === IQueryBuilder::PARAM_STR) { - $x = new QueryFunction('to_char(' . $x . ')'); - } - - if ($type === IQueryBuilder::PARAM_STR && !($y instanceof IParameter) && !($y instanceof ILiteral)) { - $y = $this->helper->quoteColumnName($y); - $y = new QueryFunction('to_char(' . $y . ')'); + protected function prepareColumn($column, $type) { + if ($type === IQueryBuilder::PARAM_STR && !is_array($column) && !($column instanceof IParameter) && !($column instanceof ILiteral)) { + $column = $this->helper->quoteColumnName($column); + $column = new QueryFunction('to_char(' . $column . ')'); } else { - $y = $this->helper->quoteColumnName($y); + $column = $this->helper->quoteColumnNames($column); } + return $column; + } + + /** + * @inheritdoc + */ + public function comparison($x, $operator, $y, $type = null) { + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->comparison($x, $operator, $y); } @@ -50,17 +57,8 @@ class OCIExpressionBuilder extends ExpressionBuilder { * @inheritdoc */ public function eq($x, $y, $type = null) { - $x = $this->helper->quoteColumnName($x); - if ($type === IQueryBuilder::PARAM_STR) { - $x = new QueryFunction('to_char(' . $x . ')'); - } - - if ($type === IQueryBuilder::PARAM_STR && !($y instanceof IParameter) && !($y instanceof ILiteral)) { - $y = $this->helper->quoteColumnName($y); - $y = new QueryFunction('to_char(' . $y . ')'); - } else { - $y = $this->helper->quoteColumnName($y); - } + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->eq($x, $y); } @@ -69,17 +67,8 @@ class OCIExpressionBuilder extends ExpressionBuilder { * @inheritdoc */ public function neq($x, $y, $type = null) { - $x = $this->helper->quoteColumnName($x); - if ($type === IQueryBuilder::PARAM_STR) { - $x = new QueryFunction('to_char(' . $x . ')'); - } - - if ($type === IQueryBuilder::PARAM_STR && !($y instanceof IParameter) && !($y instanceof ILiteral)) { - $y = $this->helper->quoteColumnName($y); - $y = new QueryFunction('to_char(' . $y . ')'); - } else { - $y = $this->helper->quoteColumnName($y); - } + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->neq($x, $y); } @@ -88,17 +77,8 @@ class OCIExpressionBuilder extends ExpressionBuilder { * @inheritdoc */ public function lt($x, $y, $type = null) { - $x = $this->helper->quoteColumnName($x); - if ($type === IQueryBuilder::PARAM_STR) { - $x = new QueryFunction('to_char(' . $x . ')'); - } - - if ($type === IQueryBuilder::PARAM_STR && !($y instanceof IParameter) && !($y instanceof ILiteral)) { - $y = $this->helper->quoteColumnName($y); - $y = new QueryFunction('to_char(' . $y . ')'); - } else { - $y = $this->helper->quoteColumnName($y); - } + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->lt($x, $y); } @@ -107,17 +87,8 @@ class OCIExpressionBuilder extends ExpressionBuilder { * @inheritdoc */ public function lte($x, $y, $type = null) { - $x = $this->helper->quoteColumnName($x); - if ($type === IQueryBuilder::PARAM_STR) { - $x = new QueryFunction('to_char(' . $x . ')'); - } - - if ($type === IQueryBuilder::PARAM_STR && !($y instanceof IParameter) && !($y instanceof ILiteral)) { - $y = $this->helper->quoteColumnName($y); - $y = new QueryFunction('to_char(' . $y . ')'); - } else { - $y = $this->helper->quoteColumnName($y); - } + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->lte($x, $y); } @@ -126,17 +97,8 @@ class OCIExpressionBuilder extends ExpressionBuilder { * @inheritdoc */ public function gt($x, $y, $type = null) { - $x = $this->helper->quoteColumnName($x); - if ($type === IQueryBuilder::PARAM_STR) { - $x = new QueryFunction('to_char(' . $x . ')'); - } - - if ($type === IQueryBuilder::PARAM_STR && !($y instanceof IParameter) && !($y instanceof ILiteral)) { - $y = $this->helper->quoteColumnName($y); - $y = new QueryFunction('to_char(' . $y . ')'); - } else { - $y = $this->helper->quoteColumnName($y); - } + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->gt($x, $y); } @@ -145,17 +107,8 @@ class OCIExpressionBuilder extends ExpressionBuilder { * @inheritdoc */ public function gte($x, $y, $type = null) { - $x = $this->helper->quoteColumnName($x); - if ($type === IQueryBuilder::PARAM_STR) { - $x = new QueryFunction('to_char(' . $x . ')'); - } - - if ($type === IQueryBuilder::PARAM_STR && !($y instanceof IParameter) && !($y instanceof ILiteral)) { - $y = $this->helper->quoteColumnName($y); - $y = new QueryFunction('to_char(' . $y . ')'); - } else { - $y = $this->helper->quoteColumnName($y); - } + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->gte($x, $y); } @@ -164,17 +117,8 @@ class OCIExpressionBuilder extends ExpressionBuilder { * @inheritdoc */ public function in($x, $y, $type = null) { - $x = $this->helper->quoteColumnName($x); - if ($type === IQueryBuilder::PARAM_STR) { - $x = new QueryFunction('to_char(' . $x . ')'); - } - - if ($type === IQueryBuilder::PARAM_STR && !is_array($y) && !($y instanceof IParameter) && !($y instanceof ILiteral)) { - $y = $this->helper->quoteColumnName($y); - $y = new QueryFunction('to_char(' . $y . ')'); - } else { - $y = $this->helper->quoteColumnNames($y); - } + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->in($x, $y); } @@ -183,17 +127,8 @@ class OCIExpressionBuilder extends ExpressionBuilder { * @inheritdoc */ public function notIn($x, $y, $type = null) { - $x = $this->helper->quoteColumnName($x); - if ($type === IQueryBuilder::PARAM_STR) { - $x = new QueryFunction('to_char(' . $x . ')'); - } - - if ($type === IQueryBuilder::PARAM_STR && !is_array($y) && !($y instanceof IParameter) && !($y instanceof ILiteral)) { - $y = $this->helper->quoteColumnName($y); - $y = new QueryFunction('to_char(' . $y . ')'); - } else { - $y = $this->helper->quoteColumnNames($y); - } + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->notIn($x, $y); } -- cgit v1.2.3 From 76f506e264473feec5534fa7aa8c2c33ef8ddb23 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 26 Jan 2016 12:14:55 +0100 Subject: Use our OCP constants --- lib/private/db/connection.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/private/db/connection.php b/lib/private/db/connection.php index 6c4f518dfb5..db5c66501b8 100644 --- a/lib/private/db/connection.php +++ b/lib/private/db/connection.php @@ -31,6 +31,7 @@ use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\Common\EventManager; use OC\DB\QueryBuilder\ExpressionBuilder; use OC\DB\QueryBuilder\QueryBuilder; +use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use OCP\PreconditionNotMetException; @@ -244,11 +245,11 @@ class Connection extends \Doctrine\DBAL\Connection implements IDBConnection { private function getType($value) { if (is_bool($value)) { - return \PDO::PARAM_BOOL; + return IQueryBuilder::PARAM_BOOL; } else if (is_int($value)) { - return \PDO::PARAM_INT; + return IQueryBuilder::PARAM_INT; } else { - return \PDO::PARAM_STR; + return IQueryBuilder::PARAM_STR; } } -- cgit v1.2.3 From 01164e89a4fb18d45b63bc33236cf979fabaafd8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 26 Jan 2016 12:15:18 +0100 Subject: Move parameter to the right function call --- lib/private/db/connection.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/private/db/connection.php b/lib/private/db/connection.php index db5c66501b8..82b52bbc9d7 100644 --- a/lib/private/db/connection.php +++ b/lib/private/db/connection.php @@ -29,7 +29,6 @@ use Doctrine\DBAL\Driver; use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\Common\EventManager; -use OC\DB\QueryBuilder\ExpressionBuilder; use OC\DB\QueryBuilder\QueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; @@ -279,7 +278,7 @@ class Connection extends \Doctrine\DBAL\Connection implements IDBConnection { $updateQb = $this->getQueryBuilder(); $updateQb->update($table); foreach ($values as $name => $value) { - $updateQb->set($name, $updateQb->createNamedParameter($value), $this->getType($value)); + $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value))); } $where = $updateQb->expr()->andx(); $whereValues = array_merge($keys, $updatePreconditionValues); -- cgit v1.2.3