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 class constants as type in sql queries output #610

Merged

Conversation

mikeSimonson
Copy link
Contributor

No description provided.

@mikeSimonson mikeSimonson self-assigned this Apr 3, 2018
@@ -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);
Copy link
Member

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?

Copy link
Member

Choose a reason for hiding this comment

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

(handled by #608)

Copy link
Member

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

Copy link
Member

@lcobucci lcobucci left a 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)) {
Copy link
Member

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 '?';
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will take it

Copy link
Member

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 '?';
}

@stof
Copy link
Member

stof commented Apr 3, 2018

Can you show what the new output looks like ?

@mikeSimonson
Copy link
Contributor Author

@stof I did not run that on an actual migrations but it basically turns some

INSERT INTO test VALUES (?) with parameters (?)

into

INSERT INTO test VALUES (?) with parameters (1, 2, 3)

@stof
Copy link
Member

stof commented Apr 4, 2018

Looks fine to me

@lcobucci
Copy link
Member

lcobucci commented Apr 4, 2018

@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])

@mikeSimonson
Copy link
Contributor Author

I will try to add a test for this one too.

Copy link
Member

@lcobucci lcobucci left a 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 . ']';
Copy link
Member

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.

@mikeSimonson mikeSimonson merged commit cc51212 into doctrine:master Apr 8, 2018
@mikeSimonson mikeSimonson deleted the show-sql-stmt-params-updated branch April 8, 2018 21:07
@mikeSimonson mikeSimonson added this to the 1.7 milestone Apr 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants