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

Fix for broken regex at SQLBind::parseSQL #6

Closed
wants to merge 3 commits into from

Conversation

jackraymund
Copy link

@jackraymund jackraymund commented Dec 31, 2020

In project byjq/migrate I just want to migrate plain queries extracted from MySQL general log.
Where data contains ' symbols. And it dont realy match with "'.*?'" regex from SQLBInd::parseSQL function.
And it destroys sql's.
There is example sql
INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_content, post_excerpt, comment_status, post_title, post_name, post_modified, post_modified_gmt, guid, post_type, to_ping, pinged, post_content_filtered) VALUES (1, '2020-12-31 01:41:02', '2020-12-31 01:41:02', '<!-- wp:paragraph -->\n<p>This is an example page. It\'s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:quote -->\n<blockquote class=\"wp-block-quote\"><p>Hi there! I\'m a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like pi&#241;a coladas. (And gettin\' caught in the rain.)</p></blockquote>\n<!-- /wp:quote -->\n\n<!-- wp:paragraph -->\n<p>...or something like this:</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:quote -->\n<blockquote class=\"wp-block-quote\"><p>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</p></blockquote>\n<!-- /wp:quote -->\n\n<!-- wp:paragraph -->\n<p>As a new WordPress user, you should go to <a href=\"http://home.home.lcl/wordpress/wp-admin/\">your dashboard</a> to delete this page and create new pages for your content. Have fun!</p>\n<!-- /wp:paragraph -->', '', 'closed', 'Sample Page', 'sample-page', '2020-12-31 01:41:02', '2020-12-31 01:41:02', 'http://xxx.lcl/wordpress/?page_id=2', 'page', '', '', '');

parseSQL have ``$sqlAlter = preg_replace("~'.*?'~", "", $sql);`` which destroys $sql query.
@byjg byjg self-assigned this Dec 31, 2020
byjg added a commit that referenced this pull request Jan 2, 2021
byjg added a commit that referenced this pull request Jan 2, 2021
@byjg
Copy link
Owner

byjg commented Jan 2, 2021

Hello, thank you for your PR. I created some unit tests to validate the scenario you described in the migration project and as you can see in the next version byjg/php-migration#31 and you are right!! The anydataset-db destroys the SQL when escaping single-quotes as we can confirm in the unit test in travis-ci

So, I digged into the RegEx and I could fix it in the PR #7 in the line 43:

'.*?((\\\\'|'').*?)*'

If you just run composer update to update the migration dependecies you'll can get the corrections as you can see this new build on travis-ci

I'll merge your PR to have you as code contributor, however, I can't maintain your changes in the code because the parseSql handles absent parameters by setting them as null.

And again, I really appreciate the heads up and your time contributing to the project. Thank you 👍

@jackraymund
Copy link
Author

@byjg Just wanted to say, it still dont work.
"name": "byjg/migration","version": "4.1.0"
"name": "byjg/anydataset-db","version": "4.1.2"
image

Again needed to fix it by my changes.

@jackraymund
Copy link
Author

jackraymund commented Sep 1, 2021

I truncated the code, cause its too long, but there is a sample, where it fails:
(539,'cs','ga','Czech'),(540,'cs','he','צ'כית'),(541,'cs','hi','Czech'),(542,'cs','hr','češki'),(543,'cs','hu','Cseh'),(544,'cs','hy','Czech'),(545,'cs','id','Czech')
Dump is created from mysqldump.
Value which is destroying this regex: צ'כית
Also, tried to change value at HeidiSQL, and it escape it by same way, so...

@byjg
Copy link
Owner

byjg commented Sep 2, 2021

I am having difficulty creating a test for this particular scenario. The code you send to me is written from right to left, and when I try to paste it into the Workbench, I am getting syntax errors.

Do you mind sending me a file with a reduced version of this code and is working in the MySQL Workbench?

@jackraymund
Copy link
Author

https://regex101.com/r/lHf0Ps/1
also tested it with MySQL Workbench.

@byjg
Copy link
Owner

byjg commented Sep 3, 2021

I believe I won't be able to fix it. So instead, I am accepting your solution with some modifications.

I started a new branch here. Adding this parameter, I can control when I do not want to use the parseSql.

You can do something like this:

$uri = Uri::getInstanceFromUri("mysql://root:password@localhost")
                ->withQueryKeyValue(DbPdoDriver::DONT_BIND_PARAM , "");

Some tests are failing, but MySQL is working. To use this code, you can change your composer file with:

  "prefer-stable": false,
  "minimum-stability": "dev",

OR, you can force temporarily in your composer.json the version 4.1.3.x-dev of the byjg/anydataset-db

Please, please let me know if it worked for you.

@byjg
Copy link
Owner

byjg commented Jun 30, 2022

PR #12 Fix this Issue.

$uri = Uri::getInstanceFromUri("mysql://root:password@localhost")
                ->withQueryKeyValue(DbPdoDriver::DONT_PARSE_PARAM , "");

It will be available on release 4.1.3 alongside other features.

@byjg byjg closed this Jun 4, 2024
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

Successfully merging this pull request may close these issues.

2 participants