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

Encountered unexpected token: "IF" "IF" when drop table if exists `table1` #960

Open
skypan opened this issue Mar 11, 2020 · 1 comment
Open

Comments

@skypan
Copy link

@skypan skypan commented Mar 11, 2020

Describe the bug
Encountered unexpected token: "IF" "IF" when drop table if exists table1

To Reproduce
Steps to reproduce the behavior:

  1. Example SQL
    drop table if exists table1

  2. Parsing this SQL using JSqlParser with this statements
    String sqlTemp = "drop table if exists table1 ";
    CCJSqlParserManager parserManager = new CCJSqlParserManager();
    Statement parser = parserManager.parse(new StringReader(sqlTemp));

  3. Exception
    Encountered unexpected token: "IF" "IF" when drop table if exists table1

Expected behavior
parser.toString() == "drop table if exists table1", not exception

System

  • Database mysql 5.6
  • JDK1.8
  • JSqlParser 0.9.2

### resolve it, modify the code :
final public Drop Drop() throws ParseException {
Drop drop = new Drop();
Token tk = null;
List dropArgs = new ArrayList();
jj_consume_token(K_DROP);
switch ((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
case S_IDENTIFIER: {
tk = jj_consume_token(S_IDENTIFIER);
break;
}
case K_TABLE: {
tk = jj_consume_token(K_TABLE);
break;
}
case K_INDEX: {
tk = jj_consume_token(K_INDEX);
break;
}
default:
jj_la1[257] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
drop.setType(tk.image);

    // TODO : 需要判断 IF EXISTS
    switch ((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
        case K_IF: {
            jj_consume_token(K_IF);
            jj_consume_token(K_EXISTS);
            drop.setIfExists(true);
            break;
        }
    }

    // TODO : 可能是带引号的字符串
    switch ((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
        case S_IDENTIFIER: {
            tk = jj_consume_token(S_IDENTIFIER);
            break;
        }
        case S_QUOTED_IDENTIFIER: {
            tk = jj_consume_token(S_QUOTED_IDENTIFIER);
            break;
        }
    }

    // tk = jj_consume_token(S_IDENTIFIER);
    drop.setName(tk.image);
    label_55:
    while (true) {
        switch ((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case S_IDENTIFIER: {
                ;
                break;
            }
            default:
                jj_la1[258] = jj_gen;
                break label_55;
        }
        tk = jj_consume_token(S_IDENTIFIER);
        dropArgs.add(tk.image);
    }
    if (dropArgs.size() > 0)
        drop.setParameters(dropArgs);
    {
        if ("" != null) return drop;
    }
    throw new Error("Missing return statement in function");
}

and Drop.java
public class Drop implements Statement {

private String type;
private String name;
private List<String> parameters;

// TODO : 添加ifExists
private boolean ifExists = false;

@Override
public void accept(StatementVisitor statementVisitor) {
    statementVisitor.visit(this);
}

public String getName() {
    return name;
}

public List<String> getParameters() {
    return parameters;
}

public String getType() {
    return type;
}

public void setName(String string) {
    name = string;
}

public void setParameters(List<String> list) {
    parameters = list;
}

public void setType(String string) {
    type = string;
}

@Override
public String toString() {
    // TODO : 加入IF EXISTS判断
    String sql = "DROP " + type;
    if (ifExists) {
        sql = sql + " IF EXISTS ";
    }
    sql = sql + " " + name;

    if (parameters != null && parameters.size() > 0) {
        sql += " " + PlainSelect.getStringList(parameters);
    }

    return sql;
}

public boolean isIfExists() {
    return ifExists;
}

public void setIfExists(boolean ifExists) {
    this.ifExists = ifExists;
}
@wumpz
Copy link
Member

@wumpz wumpz commented Mar 12, 2020

You are using a quite old version of JSqlParser. Version 3.1 successfully parses your statement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.