snapperben / gulp-mysql-command-file-processor

This a gulp npm module that allows DDL sql files to be run into a MySql server as part of a controlled release
8 stars 4 forks source link

Some additional fixes and changes #10

Closed iseus closed 8 years ago

iseus commented 8 years ago

This is a heavier change for comment blocks, yesterday it was too late night here to get the best solution. So I cleaned it up correctly. Sorry for rewriting it again.

I tested the code on valid, big SQL dumps also.

The rest is in the commit messages. It would be nice if you could update npm package also.

Thanks, Steve

snapperben commented 8 years ago

Steve, I had a thought last night and realized that a single line comment (starting '#' or '--') would not be detected and therefore a ';' in a single line comment would cause problems. Also the EOL needs to be detected to end the comment line so that the logic comes out of the commented state. Do you agree with my analysis?

Cheers Ben

iseus commented 8 years ago

Ben,

You are right in this. MySQL server only cares about comment blocks because you can control version specific queries with them, but single line comments are totally ignored by the server. So in your case of this issue I can add support for # and -- to ignore the data after these symbols to the end of the line. I'll push an update for this in a hour.

Steve

iseus commented 8 years ago

Okay so I added this one line comment thing. Here are my tests just to clarify how it works "by standard(?)"

/*
# foo; bar
*/

-- foobar;
-- barbar
# other-- foobar /* comment block */

INSERT INTO `currency` (`name`, `default`) VALUES
    ('HUF# ', 1),
    ('EUR-- ', 0),
    ('USD', 0), # test;
    ('CHF', 0), -- another comment /* foo */
    ('GBP', 0);

The results where it detects comment lines:

-- foobar;
-- barbar
# other-- foobar /* comment block */
# test;
-- another comment /* foo */

As you see inside a comment block, in another comment line or in a field value comment signs are ignored, also if the last character was an escape. One line comments are ignored from the query buffer.

MariaDB [temp]> select * from currency; +----+-------+---------+ | id | name | default | +----+-------+---------+ | 1 | HUF# | 1 | | 2 | EUR-- | 0 | | 3 | USD | 0 | | 4 | CHF | 0 | | 5 | GBP | 0 | +----+-------+---------+ 5 rows in set (0.00 sec)

PS: it works even if you comment a line from insert, like # ('USD', 0), # test;. It just ignored from the query so skipped in the table also. Of course the rest of the query must remain valid. :)

snapperben commented 8 years ago

That looks good....good test...I don't think it needs any more as this is never going to be full prod code.

If you upload your changes I'll merge them in.

Cheers

Ben

On Mon, Apr 4, 2016 at 3:56 PM, iseus notifications@github.com wrote:

Okay so I added this one line comment thing. Here are my tests just to clarify how it works "by standard(?)"

/# foo; bar/ -- foobar;-- barbar# other-- foobar /* comment block / INSERT INTO currency (name, default) VALUES ('HUF# ', 1), ('EUR-- ', 0), ('USD', 0), # test; ('CHF', 0), -- another comment / foo */ ('GBP', 0);

The results where it detects comment lines:

-- foobar; -- barbar

other-- foobar /* comment block */

test;

-- another comment /* foo */

As you see inside a comment block, in another comment line or in a field value comment signs are ignored, also if the last character was an escape. One line comments are ignored from the query buffer.

MariaDB [temp]> select * from currency; +----+-------+---------+ | id | name | default | +----+-------+---------+ | 1 | HUF# | 1 | | 2 | EUR-- | 0 | | 3 | USD | 0 | | 4 | CHF | 0 | | 5 | GBP | 0 | +----+-------+---------+ 5 rows in set (0.00 sec)

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/snapperben/gulp-mysql-command-file-processor/pull/10#issuecomment-205335145

iseus commented 8 years ago

I think everything is in the original pull request, both 5 commits.

PS: Yes you can merge it, let me know if you have any issues.

Thanks

snapperben commented 8 years ago

Steve,

I've been using it today and came across a few issues that you might have dealt with..

1) If there is a '/' (and possibly '') in a string is initiates a comment 2) If there is a '/' (and possibly '') in a single or multiline comment

To fix 1) I added !inString test to the comment initiation test --> if (char === '/' && !inString) To get around 2) I simply put all '/' characters in strings in the comments.

Cheers

Ben

On Tue, Apr 5, 2016 at 1:14 PM, iseus notifications@github.com wrote:

I think everything is in the original pull request, both 4 commits.

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/snapperben/gulp-mysql-command-file-processor/pull/10#issuecomment-205773140

iseus commented 8 years ago

could you write an exact example? In the current version on your repo this issue can happen, but I rewrote the comment block detection, you can check the changes in the pull request or in my repo here.

} else if (data.substr(dataOffset, 2) === '/*' && !inString && !isEscaped) {
    isCommentBlock++;
} else if (data.substr(dataOffset, 2) === '*/' && !inString && !isEscaped) {
    isCommentBlock--;

As you see now I check if the current character is already in a string or if it is escaped. Also this version allows comment block nesting, with some issues, but SQL standards not support it beside it is useless anyway. The only problem could be that in this case if you open block twice but close it once you won't exit from the comment block. I could change it if you think.

snapperben commented 8 years ago

I think that's too much of an edge case...and v bad SQL as well!

snapperben commented 8 years ago

As You're now looking for the strings "/" & "/" the issues I found would have gone.

I'll merge it in and put a new version on NPM.

Cheers

Ben

On Tue, Apr 5, 2016 at 3:17 PM, iseus notifications@github.com wrote:

could you write an exact example? In the current version on your repo this issue can happen, but I rewrote the comment block detection, you can check the changes in the pull request or in my repo here https://github.com/iseus/gulp-mysql-command-file-processor.

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/snapperben/gulp-mysql-command-file-processor/pull/10#issuecomment-205829873