H2-Research icon indicating copy to clipboard operation
H2-Research copied to clipboard

CreateTableTest 单元测试报错

Open fengsmith opened this issue 3 years ago • 3 comments

建表语句报错了:

CREATE TABLE IF NOT EXISTS mytable
(
    f2  IDENTITY(1, 10)
        CONSTRAINT pk PRIMARY KEY HASH AUTO_INCREMENT,
    f5  int NOT NULL UNIQUE NOT NULL,
    f6  int NULL CHECK f6 > 10,
    f7  int
        CONSTRAINT c8 NOT NULL REFERENCES mytable (f2) ON DELETE CASCADE,
    f99 int
)

报错信息:

Exception in thread "main" org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "CREATE TABLE IF NOT EXISTS MYTABLE (F2 IDENTITY(1, 10) CONSTRAINT PK PRIMARY KEY HASH AUTO_INCREMENT[*], F5 INT NOT NULL UNIQUE NOT NULL, F6 INT NULL CHECK F6>10, F7 INT CONSTRAINT C8 NOT NULL REFERENCES MYTABLE(F2) ON DELETE CASCADE, F99 INT)"; expected "CONSTRAINT, COMMENT, UNIQUE, NOT, NULL, CHECK, REFERENCES, ., )"; SQL statement:
CREATE TABLE IF NOT EXISTS mytable (f2 IDENTITY(1, 10) CONSTRAINT pk PRIMARY KEY HASH AUTO_INCREMENT, f5 int NOT NULL UNIQUE NOT NULL, f6 int NULL CHECK f6>10, f7 int CONSTRAINT c8 NOT NULL REFERENCES mytable(f2) ON DELETE CASCADE, f99 int) [42001-201]

解析完 AUTO_INCREMENT 之后就报错了,根据 debug 发现第 11668 行后

                if (column.getIdentityOptions() != null || !parseCompatibilityIdentity(column, mode)) {
                    return;
                }

由于 column.getIdentityOptions() != null 为真,导致 parseCompatibilityIdentity(column, mode) 没有执行,从而没有解析下一个 token ‘,’ ,等到处理下一次循环,读取更多的时候

    private boolean readIfMore() {
        if (readIf(COMMA)) {
            return true;
        }
        read(CLOSE_PAREN);
        return false;
    }

尝试读取 逗号 失败,下次读取比括号的时候就报错了。好像是 h2 的一个 bug 。

fengsmith avatar May 10 '22 06:05 fengsmith

把第 11668 行代码改成:

                if (!parseCompatibilityIdentity(column, mode) || column.getIdentityOptions() != null) {
                    return;
                }

就不报错了。

fengsmith avatar May 10 '22 10:05 fengsmith

h2 的新版本有不少原有的测试用例跑不了了,这个很正常,测试用例是在老版本的基础上写的,这个项目的代码我跟官方 h2 同步更新过几次,所以测试用例可能不兼容了。

codefollower avatar May 10 '22 10:05 codefollower

fengsmith avatar May 10 '22 10:05 fengsmith