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

Parser leaves comment string at the end of the query in mysql-specific comment #226

Closed
pszalko opened this issue Feb 15, 2019 · 0 comments · Fixed by #424
Closed

Parser leaves comment string at the end of the query in mysql-specific comment #226

pszalko opened this issue Feb 15, 2019 · 0 comments · Fixed by #424
Assignees
Labels
Milestone

Comments

@pszalko
Copy link
Contributor

pszalko commented Feb 15, 2019

When parsing SQL with MySQL specific comments /*! ... */ (https://dev.mysql.com/doc/refman/5.7/en/comments.html), parser leaves ending comment in parsed query: */.

I would expect either ignore all MySQL specific comments or remove the remaining comment part from
the end of the query.

Here is full PHP unit test for this case:

public function testMysqldump()
{
    $sql = <<<SQL
-- MySQL dump 10.13  Distrib 5.7.24, for Linux (x86_64)ldump e-nocleg-2-dev 01-te
--
-- Host: localhost    Database: x
-- ------------------------------------------------------
-- Server version	5.7.24

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
SQL;

    $expectedSql = [
        'SET  @OLD_CHARACTER_SET_CLIENT = @@CHARACTER_SET_CLIENT',
        'SET  @OLD_CHARACTER_SET_RESULTS = @@CHARACTER_SET_RESULTS',
        'SET  @OLD_COLLATION_CONNECTION = @@COLLATION_CONNECTION',
        'SET NAMES utf8',
        'SET  @OLD_TIME_ZONE = @@TIME_ZONE',
        "SET  TIME_ZONE = '+00:00'",
        'SET  @OLD_UNIQUE_CHECKS = @@UNIQUE_CHECKS, UNIQUE_CHECKS = 0',
        'SET  @OLD_FOREIGN_KEY_CHECKS = @@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS = 0',
        "SET  @OLD_SQL_MODE = @@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO'",
        'SET  @OLD_SQL_NOTES = @@SQL_NOTES, SQL_NOTES = 0',
    ];

    $parser = new Parser($sql, true);
    $parser->parse();

    $sql = [];
    foreach($parser->statements as $stmt) {
        $sql[] = $stmt->__toString();
    }
    
    $this->assertEquals($expectedSql, $sql);
}

Current result is:

Array
(
    [0] => SET  @OLD_CHARACTER_SET_CLIENT = @@CHARACTER_SET_CLIENT */
    [1] => SET  @OLD_CHARACTER_SET_RESULTS = @@CHARACTER_SET_RESULTS */
    [2] => SET  @OLD_COLLATION_CONNECTION = @@COLLATION_CONNECTION */
    [3] => SET NAMES utf8
    [4] => SET  @OLD_TIME_ZONE = @@TIME_ZONE */
    [5] => SET  TIME_ZONE = '+00:00'
    [6] => SET  @OLD_UNIQUE_CHECKS = @@UNIQUE_CHECKS, UNIQUE_CHECKS = 0 */
    [7] => SET  @OLD_FOREIGN_KEY_CHECKS = @@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS = 0 */
    [8] => SET  @OLD_SQL_MODE = @@SQL_MODE, SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO'
    [9] => SET  @OLD_SQL_NOTES = @@SQL_NOTES, SQL_NOTES = 0 */
)

Expected result:

Array
(
    [0] => SET  @OLD_CHARACTER_SET_CLIENT = @@CHARACTER_SET_CLIENT
    [1] => SET  @OLD_CHARACTER_SET_RESULTS = @@CHARACTER_SET_RESULTS
    [2] => SET  @OLD_COLLATION_CONNECTION = @@COLLATION_CONNECTION
    [3] => SET NAMES utf8
    [4] => SET  @OLD_TIME_ZONE = @@TIME_ZONE
    [5] => SET  TIME_ZONE = '+00:00'
    [6] => SET  @OLD_UNIQUE_CHECKS = @@UNIQUE_CHECKS, UNIQUE_CHECKS = 0
    [7] => SET  @OLD_FOREIGN_KEY_CHECKS = @@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS = 0
    [8] => SET  @OLD_SQL_MODE = @@SQL_MODE, SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO'
    [9] => SET  @OLD_SQL_NOTES = @@SQL_NOTES, SQL_NOTES = 0
)
@pszalko pszalko changed the title Parser leaves comment string at end the query in mysql-specific comment Parser leaves comment string at the end of the query in mysql-specific comment Feb 15, 2019
@williamdes williamdes added the bug label Aug 21, 2021
@williamdes williamdes added this to the 4.7.3 milestone Aug 21, 2021
@williamdes williamdes modified the milestones: 4.7.3, 4.7.4 Dec 11, 2021
@williamdes williamdes modified the milestones: 4.7.4, 5.7.0 Nov 21, 2022
@williamdes williamdes modified the milestones: 5.7.0, 5.8.0 Jan 25, 2023
niconoe- added a commit to niconoe-/sql-parser that referenced this issue Feb 28, 2023
@williamdes williamdes self-assigned this Mar 11, 2023
williamdes added a commit that referenced this issue Mar 11, 2023
Ref: #226

Signed-off-by: William Desportes <williamdes@wdes.fr>
williamdes added a commit that referenced this issue Mar 11, 2023
…eries with MySQL-specific commands

Pull-request: #424
Fixes: #226

Signed-off-by: William Desportes <williamdes@wdes.fr>
williamdes added a commit that referenced this issue Mar 11, 2023
Pull-request: #424

Signed-off-by: William Desportes <williamdes@wdes.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants