[Lunr] Gravity: adding canonical query
Review Request #785 — Created July 23, 2021 and submitted
Information | |
---|---|
d.mendes | |
Lunr | |
gravity/database/mysql/canonicalquery | |
cde92c3... | |
Reviewers | |
lunr | |
Gravity: adding canonical query
Thought process,
First approach, i wrote one main loop that it would check each index of the string and choose which path to take depending on character found, my problem with it was that i had to keep verifying previous and next characters with diferent conditions and child loops, so i felt that the code became a bit complex and hard to read.Second approach, functions like strpos can search for more than a single character and this way i can clean up a lot of the conditions, but the problem is that i we'll have multiples loops instead of one main loop to the entire string, and i'm not sure if the impact is noticible or not and in case it is, if it is better to assume it or not, also for numeric values i still used the first approach (with some cases jumping a few ranges) because there was no clear pattern to search for.
Removes Single line comments "#" or "--"
Removes Block comments "/*" and "*/"
Jumps (Ignores) characters in between:
- Executable comments ("/*M","*/") or ("/*!,*/")
- Backquotes "`"
- Escaped caracters "\"Replaces in betweeen characters:
- Double quotes
- Single quotesReplaces finds and replaces numeric characters:
- Numberic "123"
- Negative "-123"
- Decimals "12.34"
- Exponentials "12.34E11" or "12.34E-11"
- Hexadecimals "0x43"Removes:
- End of Line characters
- Excessive blank spaces
Change Summary:
add description
Description: |
|
---|
Change Summary:
add missing descriptions
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 3 (+961) |
-
-
src/Lunr/Gravity/Database/MySQL/MySQLCanonicalQuery.php (Diff revision 3) This should indicate the type of variable (as you did below), not the name.
-
src/Lunr/Gravity/Database/MySQL/MySQLCanonicalQuery.php (Diff revision 3) Please be consistent in usage of return types
-
src/Lunr/Gravity/Database/MySQL/MySQLCanonicalQuery.php (Diff revision 3) There should be no spaces between the name and the
()
-
src/Lunr/Gravity/Database/MySQL/MySQLCanonicalQuery.php (Diff revision 3) You're checking if
$end_pos
is false above, it'll never be equal tostrlen($string ) - 1 - $start_pos
and even if it is, you're not using the result of the comparison. -
src/Lunr/Gravity/Database/MySQL/MySQLCanonicalQuery.php (Diff revision 3) null isn't allowed in the parameter type
-
src/Lunr/Gravity/Database/MySQL/MySQLCanonicalQuery.php (Diff revision 3) I don't think this needs to be defined twice?
-
-
src/Lunr/Gravity/Database/MySQL/MySQLCanonicalQuery.php (Diff revision 3) How does this find any digits? It's just checking string length right?
-
src/Lunr/Gravity/Database/MySQL/MySQLCanonicalQuery.php (Diff revision 3) It doesn't seem to do this
Change Summary:
thought process
Description: |
|
---|
-
-
src/Lunr/Gravity/Database/MySQL/MySQLCanonicalQuery.php (Diff revision 3) Don't think this is needed. We can already get the query from other places.
-
src/Lunr/Gravity/Database/MySQL/MySQLCanonicalQuery.php (Diff revision 3) You can combine these two, by checking whether the query canonicalization was already done ($this->canonical_query !== NULL).
I would also rename the method to
get
and make a__toString()
magic method that calls it.That way we can get the canonical query using
$canonical_query = (string) $result->canonical_query();
-
src/Lunr/Gravity/Database/MySQL/MySQLCanonicalQuery.php (Diff revision 3) I don't think we need those wrappers.
-
src/Lunr/Gravity/Database/MySQL/MySQLQueryResult.php (Diff revision 3) I'm afraid, doing it like this isn't good.
Dependency Injection demands that we can somehow mock the
canonical_query()
method of theMySQLCanonicalQuery
class. But we can't do that if class instantitation and method call are in the same location. So we have to split this.The best scenario I could think of is if the just return the instantiated class here (still considering that we should only instantiate it on the first call). Combined with the
__toString()
comment I made above this should still make for a nice API.
Change Summary:
new test files, chanfes for revision 3
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 4 (+903) |
-
-
src/Lunr/Gravity/Database/MySQL/MySQLCanonicalQuery.php (Diff revision 4) Some things aren't in the codestyle apparently. We should make a list :D
There should be an empty line between the open tag and the file comment.
-
src/Lunr/Gravity/Database/MySQL/MySQLCanonicalQuery.php (Diff revision 4) empty line between @param and @return
-
-
src/Lunr/Gravity/Database/MySQL/MySQLQueryResult.php (Diff revision 4) common practice for us is that we initialize the property in the constructor. So a simple
$this->canonical_query = NULL;
should be fine here.
-
-
src/Lunr/Gravity/Database/MySQL/Tests/MySQLCanonicalQueryBaseTest.php (Diff revision 4) Put the expected value in the data provider, even if it's always the same. Makes it easier to extend in the future in case we do find a situation where it isn't.
-
src/Lunr/Gravity/Database/MySQL/Tests/MySQLCanonicalQueryTest.php (Diff revision 4) This shouldn't be needed.
$this->class
is supposed to be the class we're testing. -
src/Lunr/Gravity/Database/MySQL/Tests/MySQLCanonicalQueryTest.php (Diff revision 4) you can name the test scenarios by setting a string key, like
$data_provider['first argument not string'] = [['013456789013456789','23','56'], []];
-
src/Lunr/Gravity/Database/MySQL/Tests/MySQLCanonicalQueryTest.php (Diff revision 4) Please change this to read input and expected output from test files, and add more possible scenarios.
There's two possible structure we typically use (in tests/statics/<namespace>/):- foo_input.sql and foo_output.sql
- input/foo.sql and output/foo.sql
Whichever you prefer.
Additional cases here that'd be worth considering are INSERT, UPDATE, REPLACE queries. SHOW would also be nice. Perhaps upserts, CTEs, etc.
-
src/Lunr/Gravity/Database/MySQL/Tests/MySQLQueryResultBaseTest.php (Diff revision 4) one test case per test :)
-
-
src/Lunr/Gravity/Database/MySQL/MySQLCanonicalQuery.php (Diff revision 4) Try to avoid
empty()
since it'll also match things like0
, which you probably don't want here.is_null
is a better check I think -
-
-
src/Lunr/Gravity/Database/MySQL/Tests/MySQLCanonicalQueryTest.php (Diff revision 4) this is equal to
parent::tearDown()
after you implement heinz' comments
Change Summary:
Changed string replace
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 5 (+1080 -3) |
-
-
Perhaps add an example for a query using maxscale query hints: https://mariadb.com/kb/en/mariadb-maxscale-14/maxscale-hint-syntax/
-
tests/statics/Gravity/Database/MySQL/input_backup.sql (Diff revision 5) I'm not sure about this. On the one hand, it's nice to test that the code would support his.
On the other hand, we'd never get to a query like this. We don't support either running multiple queries at once nor do we support fetching results for multiple queries issued at once.
-
tests/statics/Gravity/Database/MySQL/output_insert.sql (Diff revision 5) This is where things get tricky I suppose.
Are these two queries canonically different?
INSERT INTO `database`.`table` (`param1`, `param2`, `param3`) VALUES (?, '?', '?'), (?, '?', '?'), (?, '?', '?'), (?, '?', '?'), (?, '?', '?');
INSERT INTO `database`.`table` (`param1`, `param2`, `param3`) VALUES (?, '?', '?'), (?, '?', '?'), (?, '?', '?'), (?, '?', '?');
Change Summary:
Maxscale test file added
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 6 (+1082 -3) |
Change Summary:
Add mult-rows collapse
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 7 (+1367 -3) |
-
-
src/Lunr/Gravity/Database/MySQL/MySQLCanonicalQuery.php (Diff revisions 6 - 7) You need to handle the case where $offset is an empty array (if I understood the code in
find_positions
correctly).This can be the case in INSERT/REPLACE statements that don't use
VALUES
, likeINSERT INTO table SELECT FROM table2
Change Summary:
- fix for INSERT/REPLACE statements that don't use VALUES
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 8 (+1375 -3) |
-
-
tests/statics/Gravity/Database/MySQL/input_collapse_insert_no_rows.sql (Diff revision 8) I think it would be a better test if we had an actual value here in the WHERE clause :-)
Change Summary:
- Added more tests
- Fix of extended characters in collapse function
Commit: |
|
||||
---|---|---|---|---|---|
Diff: |
Revision 9 (+1477 -3) |
-
-
Unit tests are failing:
There was 1 error: 1) Lunr\Gravity\Database\MySQL\Tests\MySQLQueryResultBaseTest::testCanonicalQuery Undefined property: Lunr\Gravity\Database\MySQL\Tests\MySQLQueryResultBaseTest::$result_reflection /mnt/progs/projects/m2mobi/backend/lunr/src/Lunr/Gravity/Database/MySQL/Tests/MySQLQueryResultBaseTest.php:182 -- There was 1 warning: 1) Warning No tests found in class "Lunr\Gravity\Database\MySQL\Tests\MySQLCanonicalQueryTest". -- There were 2 failures: 1) Lunr\Gravity\Filesystem\Tests\PhysicalFilesystemAccessObjectListDirectoriesTest::testGetListOfDirectoriesInAccessibleDirectory Failed asserting that two arrays are equal. --- Expected +++ Actual @@ @@ Array ( - 0 => 'folder1' - 1 => 'folder2' + 0 => 'Database' + 1 => 'folder1' + 2 => 'folder2' ) /mnt/progs/projects/m2mobi/backend/lunr/src/Lunr/Gravity/Filesystem/Tests/PhysicalFilesystemAccessObjectListDirectoriesTest.php:40 2) Lunr\Gravity\Filesystem\Tests\PhysicalFilesystemAccessObjectDirectoryTest::testGetListingOfAccessibleDirectory Failed asserting that two arrays are equal. --- Expected +++ Actual @@ @@ Array ( - 0 => 'file1' - 1 => 'file2' - 2 => 'file3' - 3 => 'folder1' - 4 => 'folder2' + 0 => 'Database' + 1 => 'file1' + 2 => 'file2' + 3 => 'file3' + 4 => 'folder1' + 5 => 'folder2' ) /mnt/progs/projects/m2mobi/backend/lunr/src/Lunr/Gravity/Filesystem/Tests/PhysicalFilesystemAccessObjectDirectoryTest.php:41
Change Summary:
fix tests
- filesystem
- update functions new lunr test case
Change Summary:
- merge master
- adapt MySQLQueryResultBaseTest to new version
Commit: |
|
||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 11 (+1475 -5)
|