Skip to content
Snippets Groups Projects
Verified Commit 1dc27c12 authored by Jan Kuchař's avatar Jan Kuchař Committed by Jiří Pudil
Browse files

Draft: Tried to fix CompositeType to use PostgreSQL native syntax of ROW(1,'a',null)::"someType"

parent 66845d06
No related branches found
No related tags found
1 merge request!40CompositeType: add explicit type cast to support composite values in WHERE clause
......@@ -45,37 +45,33 @@ abstract class CompositeType implements Type
protected function tupleToDatabase(array $value): Literal
{
\assert(\count($value) === \count($this->types));
$dbValue = \sprintf(
'(%s)',
\implode(
',',
map(
$value,
function ($item, $index) {
if ($item === null) {
return '';
}
$mappedItem = $this->types[$index]->toDatabase($item);
if ($mappedItem === '') {
return '""';
}
if (\preg_match('/[,\s"()]/', (string) $mappedItem)) {
return \sprintf(
'"%s"',
\str_replace(['\\', '"'], ['\\\\', '\\"'], (string) $mappedItem),
);
}
return $mappedItem;
},
return new Literal(
\sprintf(
'ROW(%s)::%s',
\implode(
',',
map(
$value,
function ($item, $index) {
if ($item === null) {
return 'null';
}
$result = $this->types[$index]->toDatabase($item);
// @todo: we need connection here to call escape functiom ?!
if (is_string($result)) {
// @todo remove me! This is re-implementation of escaping
// @todo Should be escaped by TestType at a first place, hmm?
// @todo Shouldn't there be Literal as a required return type of toDatabse?
return "'" . str_replace("'", "\\'", $result) . "'";
}
return $result;
},
),
),
$this->databaseType->toSql()
)
);
return new Literal(\sprintf('%s::%s', $dbValue, $this->databaseType->toSql()));
}
/**
......
......@@ -45,8 +45,8 @@ $composite = new class extends CompositeType {
}
};
$dbValue = $composite->toDatabase([42, null, 'com\\ple"x', '(', '', null]);
$dbValue = $composite->toDatabase([42, null, 'com\\ple"\'x', '(', '', null]);
Assert::type(Literal::class, $dbValue);
Assert::same('(42,,"com\\\\ple\\"x","(","",)::"databaseType"', (string) $dbValue);
Assert::same("ROW(42,null,'com\\ple\"\\'x','(','',null)::\"databaseType\"", (string) $dbValue);
Assert::same([42, null, 'com\\ple"x', '(', '', null], $composite->fromDatabase('(42,,"com\\\\ple""x","(","",)'));
Assert::same([42, null, 'com\\ple"\'x', '(', '', null], $composite->fromDatabase('(42,,"com\\\\ple""\'x","(","",)'));
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment