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

Can't coerce a Medoo::raw value to JSON #1009

Open
logicalor opened this issue Sep 10, 2021 · 0 comments
Open

Can't coerce a Medoo::raw value to JSON #1009

logicalor opened this issue Sep 10, 2021 · 0 comments

Comments

@logicalor
Copy link

logicalor commented Sep 10, 2021

Information

  • Version of Medoo: 2.1.2
  • Type of Database: MySQL 5.7
  • System (Liunx\Windows\Mac): Linux

Describe the Problem
If JSON type is applied to a column label but the value is a Medoo::raw statement, the value resolves to a string, not a JSON object.

Detail Code
In some circumstances, it is useful to generate a JSON from the result of a Medoo::raw statement. One such example is where one is performing a 1:many join, and one wishes to build a JSON object by using GROUP_CONCAT. An example is as follows:

$select = [
  ...
  'aggregated [JSON]' => Medoo::raw("CONCAT('[', GROUP_CONCAT(JSON_OBJECT('somevalue',`joined_table`.`somevalue`,'anothervalue',`joined_table`.`anothervalue`)), ']')
];

In line 1349 of Medoo.php, the preg_match for column keys doesn't match Object or JSON types.

...
                    [$columnKey, 'String'];
            } elseif ($this->isRaw($value)) {
                preg_match('/([\p{L}_][\p{L}\p{N}@$#\-_]*\.)?(?<column>[\p{L}_][\p{L}\p{N}@$#\-_]*)(\s*\[(?<type>(String|Bool|Int|Number|Object|JSON))\])?/u', $key, $keyMatch);
                $columnKey = $keyMatch['column'];
...

So the above mapping ends up producing a String in Medoo's result.

I am not sure if the Object and JSON type keywords are missing from the pattern intentionally or accidentally.

Expected output

A column with the type set to JSON and a value that is a Medoo::raw statement should return JSON, not a string.

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

No branches or pull requests

1 participant