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 class constants as type in sql queries output #610
Fix class constants as type in sql queries output #610
Conversation
f2ed81f
to
dcbc151
Compare
@@ -407,8 +443,8 @@ public function testDryRunCausesSqlToBeOutputViaTheOutputWriter() | |||
$ow = new OutputWriter(function ($msg) use (&$messages) { | |||
$messages[] = trim($msg); | |||
}); | |||
$config = new Configuration($this->getSqliteConnection(), $ow); | |||
$version = new Version( | |||
$config = new Configuration($this->getSqliteConnection(), $ow); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this already fixed in master
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(handled by #608)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be great to rebase the branch to clean the diff here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about this?
@@ -461,7 +460,7 @@ private function formatParamsForOutput(array $params, array $types) | |||
if (Type::hasType($type)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we can simplify some things, what do you think about this:
private function formatParamsForOutput(array $params, array $types): string
{
if (empty($params)) {
return '';
}
$out = [];
foreach ($params as $key => $value) {
$formatted = $this->formatParameter($value, $types[$key] ?? 'string');
$out[] = is_string($key) ? sprintf(':%s => %s', $key, $formatted) : $formatted;
}
return sprintf('with parameters (%s)', implode(', ', $out));
}
private function formatParameter($value, string $type) : string
{
if (Type::hasType($type)) {
return Type::getType($type)->convertToDatabaseValue(
$value,
$this->connection->getDatabasePlatform()
);
}
// This is quite important since `$value` and `$type` are things provided
// by the user, so we can't really ensure that `$value` will always be an array.
if (is_array($value)) {
return implode(', ', array_map([$this, 'parameterToString'], $value));
}
return $this->parameterToString($value);
}
private function parameterToString($value) : string
{
if (is_int($value) || is_string($value)) {
return (string) $value;
}
if (is_bool($value)) {
return $value === true ? 'true' : 'false';
}
return '?';
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will take it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also make parameterToString()
recursive, if needed... like:
private function formatParameter($value, string $type) : string
{
if (Type::hasType($type)) {
return Type::getType($type)->convertToDatabaseValue(
$value,
$this->connection->getDatabasePlatform()
);
}
return $this->parameterToString($value);
}
private function parameterToString($value) : string
{
if (is_array($value)) {
return implode(', ', array_map([$this, 'parameterToString'], $value));
}
if (is_int($value) || is_string($value)) {
return (string) $value;
}
if (is_bool($value)) {
return $value === true ? 'true' : 'false';
}
return '?';
}
Can you show what the new output looks like ? |
@stof I did not run that on an actual migrations but it basically turns some
into
|
Looks fine to me |
@mikeSimonson I think that imploding the array might be confusing when you have more parameters, like: INSERT INTO test VALUES (?, ?) with parameters (1, 2, 3, 4) Which values are related to the first parameter and which are to the second? Maybe this helps: - INSERT INTO test VALUES (?, ?) with parameters (1, 2, 3, 4)
+ INSERT INTO test VALUES (?, ?) with parameters ([1, 2], [3, 4]) |
I will try to add a test for this one too. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @stof mentioned, could you rebase the branch to sync it with master
?
|
||
if (count($out) > 1) { | ||
$out = array_map(function ($element) { | ||
return '[' . $element . ']'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't we solve this when generating the value (in line 458) instead of re-iterating $out
? Like:
- $outval = $this->formatParameter($value, $type);
+ $outval = '[' . $this->formatParameter($value, $type) . ']';
Or only when dealing with arrays, in line 497:
- return implode(', ', array_map([$this, 'parameterToString'], $value));
+ return '[' . implode(', ', array_map([$this, 'parameterToString'], $value)) . ']';
The output would be a bit more understandable.
541ddbe
to
ce4b461
Compare
No description provided.