catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

Medoo is having issues with table names containing hyphens (minus sign “-”) #877

Closed lanyizi closed 3 years ago

lanyizi commented 4 years ago

When containing hyphens (-), sometimes table names cannot be properly quoted by the tableQuote($table) method because they (table names) are not even being matched by the previous preg_match().

Information

Detail Code The following code

$qqs = $database->select('nss-avatars', [
    '[<]nss-players' => 'qq'
], [
    'qq',
], [
    'ORDER' => [
        'nss-avatars.lastUpdate' => 'ASC'
    ],
    'LIMIT' => $count
]);

Produces

SELECT `qq` FROM "nss-avatars" 
RIGHT JOIN `nss` USING (`qq`) 
ORDER BY "nss-avatars".`lastUpdate` ASC 
LIMIT 2

Expected output

SELECT `qq` FROM "nss-avatars" 
RIGHT JOIN `nss-players` USING (`qq`) 
ORDER BY "nss-avatars".`lastUpdate` ASC 
LIMIT 2

At line 1062 of Medoo.php, I see there is a preg_match() matching table name with [a-zA-Z0-9_]+. By adding a hyphen there ([a-zA-Z0-9_-]+), it will output a correct table name in the join statement. But I'm not sure how many else places need to be updated, and I'm also unsure if this would bring any side effects. Otherwise I could already make a pull request.

chasedig commented 4 years ago

I remember having this issue a while back. I hope this bug gets fixed, but I solved it by just using underscores.

assbach commented 4 years ago

Oh thank you, this had me digging for errors for hours now. I hate using underscores but it works now... this issue is still present as of today (2020-01-22, v 1.7.8)

assbach commented 4 years ago

just realized it's not only table-names but also column-names with "-" that cause problems.