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::buildRaw preg_replace_callback invalid regexp #892

Closed dmachehin closed 3 years ago

dmachehin commented 4 years ago

Medoo::buildRaw has bad regexp /((FROM|TABLE|INTO|UPDATE)\s*)?\<([a-zA-Z0-9_\.]+)\>/i

For example query

INSERT INTO test ('TEXT FROM <.> TEXT')

Generate error Incorrect column name "." in file /vendor/catfan/medoo/src/Medoo.php at line 521

Why do this change? (where is using this table name syntax <table_name>?)

Suggests fixed:

  1. Delete this replace
  2. Make this preg_replace optional
  3. Fix regexp in column name capture (for example ([a-zA-Z0-9_\.]+) replace to ([a-z][a-zA-Z0-9_\.]*)) ..... But bug is still exists after fix (partial fixed only)
  4. Fix regexp by add first ^[a-z\s]* Replace to: /^[a-z\s]*((FROM|TABLE|INTO|UPDATE)\s*)?\<([a-zA-Z0-9_\.]+)\>/i
catfan commented 4 years ago

Thanks. I fixed this on e03bc3d.

It will not match if the quotation syntax is inside abc <column> abc or "abc <column> abc".

And it also not match <.>.

However, it should use placeholder for data in query sentence instead of quoting data by hand.

BrainStormDevel commented 4 years ago

Hello, this fix broken MariaDB compatibility. I have update composer but still use my repo, because this fix broken my code. This is my query to test the issue:

$db->query("SELECT , table22.a_table2 FROM LEFT JOIN ( SELECT CONCAT('[', GROUP_CONCAT(JSON_OBJECT('lid', ) ORDER BY , ),']') as a_table2, , FROM WHERE = 1 GROUP BY ) table22 ON = table22.id")->fetchAll();

This query with last commit didn't work, but work with my last suggestion in the other closed thread without the applied regex.

catfan commented 4 years ago

@BrainStormDevel Change your single quote to double quote for

SELECT CONCAT("[", GROUP_CONCAT(JSON_OBJECT("lid", <table2.lid>) ORDER BY <table2.id>, <table2.lid>),"]")

I will test more for the regex for complex case. But this may fix your case from now.

omexlu commented 4 years ago

Hello,

I also have a problem with the RAW function in the current version: https://github.com/catfan/Medoo/issues/912

Can anyone here help me? Looks like a bug.