Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix handling of numeric values for SQLite in comparisons #207

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/Jackalope/Transport/DoctrineDBAL/Query/QOMWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,10 +624,7 @@ public function walkNumComparisonConstraint(QOM\PropertyValueInterface $property
$alias = $this->getTableAlias($propertyOperand->getSelectorName() . '.' . $propertyOperand->getPropertyName());
$property = $propertyOperand->getPropertyName();

return
$this->sqlXpathExtractNumValue($alias, $property) . " " .
$operator . " " .
$literalOperand->getLiteralValue();
return $this->sqlXpathCompareNumValue($alias, $property, $this->getLiteralValue($literalOperand), $operator);
}

/**
Expand Down Expand Up @@ -803,13 +800,23 @@ private function sqlXpathExtractValue($alias, $property)
throw new NotImplementedException("Xpath evaluations cannot be executed with '" . $this->platform->getName() . "' yet.");
}

private function sqlXpathExtractNumValue($alias, $property)
private function sqlXpathCompareNumValue($alias, $property, $value, $operator)
{
if ($this->platform instanceof SqlitePlatform) {
$expression = "EXTRACTVALUE($alias.props, 'count(//sv:property[@sv:name=\"" . $property . "\"]/sv:value[text()%s%s]) > 0')";

return sprintf($expression, $this->walkOperator($operator), Xpath::escape($value));
}

if ($this->platform instanceof PostgreSqlPlatform) {
return "(xpath('//sv:property[@sv:name=\"" . $property . "\"]/sv:value[1]/text()', CAST($alias.props AS xml), ".$this->sqlXpathPostgreSQLNamespaces()."))[1]::text::int";
$expression = "(xpath('//sv:property[@sv:name=\"" . $property . "\"]/sv:value[1]/text()', CAST($alias.props AS xml), ".$this->sqlXpathPostgreSQLNamespaces()."))[1]::text::int";
} elseif ($this->platform instanceof MySqlPlatform) {
$expression = $this->sqlXpathExtractValue($alias, $property);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to have a method here when it only works for mysql? and it looks like the BC way would be to do this for all platforms we did not previously handle, not only msql. (not sure if users could sneak oracle or such in and if that previously worked)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't support Oracle yet .. I am sure we will need to do some explict code for each RDBMS we support

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or in other words .. right now I rather have the code explicitly say which code path is for what RDBMS

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed.

} else {
throw new NotImplementedException("Xpath evaluations cannot be executed with '" . $this->platform->getName() . "' yet.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this not more confusing than anything else to a user? he was not trying to do xpath, he was doing query builder or sql2 - its our implementation detail that we use xpath for what he is doing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah .. but imho we should clean that up in another PR as we do this in several places

}

return $this->sqlXpathExtractValue($alias, $property);
return "$expression $operator $value";
}

private function sqlXpathExtractValueAttribute($alias, $property, $attribute, $valueIndex = 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function testQueryWithPropertyComparisonConstraintNumericLiteral()
);
list($selectors, $selectorAliases, $sql) = $this->walker->walkQOMQuery($query);

$this->assertContains('> 100', $sql);
$this->assertContains('100', $sql);
}

public function testQueryWithAndConstraint()
Expand Down