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

buildRaw() with duplicated column name #1069

Closed
webgoto opened this issue Nov 26, 2022 · 0 comments
Closed

buildRaw() with duplicated column name #1069

webgoto opened this issue Nov 26, 2022 · 0 comments
Labels

Comments

@webgoto
Copy link

webgoto commented Nov 26, 2022

Information

  • Version of Medoo: 2.1.7
  • Version of PHP: 7.3
  • Type of Database (MySQL, MSSQL, SQLite...): MySQL
  • System (Liunx|Windows|Mac): Windows 10

Describe the Problem
Column names other than the first disappear.

Detail Code
table test

os
win
mac
win
ios
$db->get('test', ['pc'=>Medoo::raw("count(<os>='win' OR <os>='mac' OR NULL)")]);

SQL with the second "os" column disappeared is executed

SELECT count("os"='win' OR ='mac' OR NULL) AS `pc` FROM `test` LIMIT 1

desired SQL

SELECT count("os"='win' OR "os"='mac' OR NULL) AS `pc` FROM `test` LIMIT 1

Solution I tried

I thought there was a problem with the "buildRaw" method

the value of the variable in buildRaw method

[$raw->value]
count(<os>='win' OR <os>='mac' OR NULL)

[$matches]
Array
(
    [0] => <os>
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => os
    [6] => os
)
Array
(
    [0] => 'win' OR <os>='
    [1] => 'win' OR 
    [2] => '
    [3] => 
    [4] => 
    [5] => os
    [6] => os
    [7] => 
    [8] => ='
)

[$query]
count("os"='win' OR ='mac' OR NULL)

"os" is missing here

This issue was resolved by doing the following.

            function ($matches) {
                // add && empty($matches[5])
                if (!empty($matches[2]) && isset($matches[8]) && empty($matches[5])) {
                    return $matches[0];
                }

                if (!empty($matches[4])) {
                    return $matches[1] . $matches[4] . ' ' . $this->tableQuote($matches[5]);
                }

                // add . ($matches[8]??'')
                return $matches[1] . $this->columnQuote($matches[5]) . ($matches[8]??'');
            },
@catfan catfan added the bug label Nov 27, 2022
@catfan catfan changed the title [Bug]Cannot use multiple columns with raw() buildRaw() with duplicated column name Jan 22, 2024
@catfan catfan closed this as completed Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants