Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: add the line information when SplitMultiSQL (#2294)
rebelice62da0165c1ce
diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.goindex 7528100327..be1a79ba5d 100644--- a/plugin/db/pg/pg.go+++ b/plugin/db/pg/pg.go@@ -223,8 +223,8 @@ func (driver *Driver) Execute(ctx context.Context, statement string) error {return err}var remainingStmts []string- for _, stmt := range statements {- stmt = strings.TrimLeft(stmt, " \t")+ for _, statement := range statements {+ stmt := strings.TrimLeft(statement.Text, " \t")// We don't use transaction for creating / altering databases in Postgres.// https://github.com/bytebase/bytebase/issues/202if strings.HasPrefix(stmt, "CREATE DATABASE ") {diff --git a/plugin/parser/ast/node.go b/plugin/parser/ast/node.goindex 17a17911e2..43a61936a5 100644--- a/plugin/parser/ast/node.go+++ b/plugin/parser/ast/node.go@@ -8,11 +8,14 @@ var (type Node interface {Text() stringSetText(text string)+ Line() int+ SetLine(line int)}// node is the base struct for all Node.type node struct {text string+ line int}// Text implements the Node interface.@@ -24,3 +27,11 @@ func (n *node) Text() string {func (n *node) SetText(text string) {n.text = text}++func (n *node) Line() int {+ return n.line+}++func (n *node) SetLine(line int) {+ n.line = line+}diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.goindex 5cd3363fc2..6f7fdee78a 100644--- a/plugin/parser/engine/pg/convert.go+++ b/plugin/parser/engine/pg/convert.go@@ -9,10 +9,11 @@ import ()// convert converts the pg_query.Node to ast.Node.-func convert(node *pgquery.Node, text string) (res ast.Node, err error) {+func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, err error) {defer func() {if err == nil && res != nil {- res.SetText(text)+ res.SetText(statement.Text)+ res.SetLine(statement.Line)}}()switch in := node.Node.(type) {diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.goindex ee9d0a41a4..4a5de58cdc 100644--- a/plugin/parser/engine/pg/convert_test.go+++ b/plugin/parser/engine/pg/convert_test.go@@ -9,9 +9,9 @@ import ()type testData struct {- stmt string- want []ast.Node- textList []string+ stmt string+ want []ast.Node+ statementList []parser.SingleSQL}func runTests(t *testing.T, tests []testData) {@@ -21,7 +21,8 @@ func runTests(t *testing.T, tests []testData) {res, err := p.Parse(parser.Context{}, test.stmt)require.NoError(t, err)for i := range test.want {- test.want[i].SetText(test.textList[i])+ test.want[i].SetText(test.statementList[i].Text)+ test.want[i].SetLine(test.statementList[i].Line)}require.Equal(t, test.want, res, test.stmt)}@@ -61,8 +62,11 @@ func TestPGConvertCreateTableStmt(t *testing.T) {},},},- textList: []string{- "CREATE TABLE \"techBook\" (a int NOT NULL, b int CONSTRAINT b_not_null NOT NULL)",+ statementList: []parser.SingleSQL{+ {+ Text: "CREATE TABLE \"techBook\" (a int NOT NULL, b int CONSTRAINT b_not_null NOT NULL)",+ Line: 1,+ },},},{@@ -80,8 +84,11 @@ func TestPGConvertCreateTableStmt(t *testing.T) {},},},- textList: []string{- "CREATE TABLE IF NOT EXISTS techBook (\"A\" int, b int)",+ statementList: []parser.SingleSQL{+ {+ Text: "CREATE TABLE IF NOT EXISTS techBook (\"A\" int, b int)",+ Line: 1,+ },},},{@@ -106,8 +113,11 @@ func TestPGConvertCreateTableStmt(t *testing.T) {},},},- textList: []string{- "CREATE TABLE tech_book(a INT CONSTRAINT t_pk_a PRIMARY KEY)",+ statementList: []parser.SingleSQL{+ {+ Text: "CREATE TABLE tech_book(a INT CONSTRAINT t_pk_a PRIMARY KEY)",+ Line: 1,+ },},},{@@ -142,8 +152,11 @@ func TestPGConvertCreateTableStmt(t *testing.T) {},},},- textList: []string{- "CREATE TABLE tech_book(a INT, b int CONSTRAINT uk_b UNIQUE, CONSTRAINT t_pk_a PRIMARY KEY(a))",+ statementList: []parser.SingleSQL{+ {+ Text: "CREATE TABLE tech_book(a INT, b int CONSTRAINT uk_b UNIQUE, CONSTRAINT t_pk_a PRIMARY KEY(a))",+ Line: 1,+ },},},{@@ -189,8 +202,11 @@ func TestPGConvertCreateTableStmt(t *testing.T) {},},},- textList: []string{- "CREATE TABLE tech_book(a INT CONSTRAINT fk_a REFERENCES people(id), CONSTRAINT fk_a_people_b FOREIGN KEY (a) REFERENCES people(b))",+ statementList: []parser.SingleSQL{+ {+ Text: "CREATE TABLE tech_book(a INT CONSTRAINT fk_a REFERENCES people(id), CONSTRAINT fk_a_people_b FOREIGN KEY (a) REFERENCES people(b))",+ Line: 1,+ },},},}@@ -221,8 +237,11 @@ func TestPGAddColumnStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE techbook ADD COLUMN a int",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE techbook ADD COLUMN a int",+ Line: 1,+ },},},{@@ -255,8 +274,11 @@ func TestPGAddColumnStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE techbook ADD COLUMN a int CONSTRAINT uk_techbook_a UNIQUE",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE techbook ADD COLUMN a int CONSTRAINT uk_techbook_a UNIQUE",+ Line: 1,+ },},},}@@ -285,8 +307,11 @@ func TestPGRenameTableStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE techbook RENAME TO \"techBook\"",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE techbook RENAME TO \"techBook\"",+ Line: 1,+ },},},{@@ -308,8 +333,11 @@ func TestPGRenameTableStmt(t *testing.T) {},},},- textList: []string{- "ALTER VIEW techbook RENAME TO \"techBook\"",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER VIEW techbook RENAME TO \"techBook\"",+ Line: 1,+ },},},}@@ -339,8 +367,11 @@ func TestPGRenameColumnStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE techbook RENAME abc TO \"ABC\"",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE techbook RENAME abc TO \"ABC\"",+ Line: 1,+ },},},{@@ -363,8 +394,11 @@ func TestPGRenameColumnStmt(t *testing.T) {},},},- textList: []string{- "ALTER VIEW techbook RENAME abc TO \"ABC\"",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER VIEW techbook RENAME abc TO \"ABC\"",+ Line: 1,+ },},},}@@ -394,8 +428,11 @@ func TestPGRenameConstraintStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE tech_book RENAME CONSTRAINT uk_tech_a to \"UK_TECH_A\"",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE tech_book RENAME CONSTRAINT uk_tech_a to \"UK_TECH_A\"",+ Line: 1,+ },},},}@@ -422,8 +459,11 @@ func TestPGCreateIndexStmt(t *testing.T) {},},},- textList: []string{- "CREATE INDEX idx_id ON tech_book (id)",+ statementList: []parser.SingleSQL{+ {+ Text: "CREATE INDEX idx_id ON tech_book (id)",+ Line: 1,+ },},},{@@ -443,8 +483,11 @@ func TestPGCreateIndexStmt(t *testing.T) {},},},- textList: []string{- "CREATE UNIQUE INDEX idx_id ON tech_book (id)",+ statementList: []parser.SingleSQL{+ {+ Text: "CREATE UNIQUE INDEX idx_id ON tech_book (id)",+ Line: 1,+ },},},}@@ -467,8 +510,11 @@ func TestPGDropIndexStmt(t *testing.T) {},},},- textList: []string{- "DROP INDEX xschema.idx_id, idx_x",+ statementList: []parser.SingleSQL{+ {+ Text: "DROP INDEX xschema.idx_id, idx_x",+ Line: 1,+ },},},}@@ -487,8 +533,11 @@ func TestPGAlterIndexStmt(t *testing.T) {NewName: "IDX_ID",},},- textList: []string{- "ALTER INDEX xschema.idx_id RENAME TO \"IDX_ID\"",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER INDEX xschema.idx_id RENAME TO \"IDX_ID\"",+ Line: 1,+ },},},{@@ -500,8 +549,11 @@ func TestPGAlterIndexStmt(t *testing.T) {NewName: "IDX_ID",},},- textList: []string{- "ALTER INDEX idx_id RENAME TO \"IDX_ID\"",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER INDEX idx_id RENAME TO \"IDX_ID\"",+ Line: 1,+ },},},}@@ -530,8 +582,11 @@ func TestPGDropConstraintStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE tech_book DROP CONSTRAINT uk_tech_a",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE tech_book DROP CONSTRAINT uk_tech_a",+ Line: 1,+ },},},}@@ -565,8 +620,11 @@ func TestPGAddConstraintStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE tech_book ADD CONSTRAINT check_a_bigger_than_b CHECK (a > b) NOT VALID",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE tech_book ADD CONSTRAINT check_a_bigger_than_b CHECK (a > b) NOT VALID",+ Line: 1,+ },},},{@@ -592,8 +650,11 @@ func TestPGAddConstraintStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id UNIQUE (id)",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id UNIQUE (id)",+ Line: 1,+ },},},{@@ -619,8 +680,11 @@ func TestPGAddConstraintStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_id PRIMARY KEY (id)",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_id PRIMARY KEY (id)",+ Line: 1,+ },},},{@@ -653,8 +717,11 @@ func TestPGAddConstraintStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE tech_book ADD CONSTRAINT fk_tech_book_id FOREIGN KEY (id) REFERENCES people(id)",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE tech_book ADD CONSTRAINT fk_tech_book_id FOREIGN KEY (id) REFERENCES people(id)",+ Line: 1,+ },},},{@@ -680,8 +747,11 @@ func TestPGAddConstraintStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id UNIQUE USING INDEX uk_id",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id UNIQUE USING INDEX uk_id",+ Line: 1,+ },},},{@@ -707,8 +777,11 @@ func TestPGAddConstraintStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_id PRIMARY KEY USING INDEX pk_id",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_id PRIMARY KEY USING INDEX pk_id",+ Line: 1,+ },},},}@@ -737,8 +810,11 @@ func TestPGDropColumnStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE tech_book DROP COLUMN a",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE tech_book DROP COLUMN a",+ Line: 1,+ },},},}@@ -765,8 +841,11 @@ func TestPGDropTableStmt(t *testing.T) {},},},- textList: []string{- "DROP TABLE tech_book, xschema.user",+ statementList: []parser.SingleSQL{+ {+ Text: "DROP TABLE tech_book, xschema.user",+ Line: 1,+ },},},{@@ -786,8 +865,11 @@ func TestPGDropTableStmt(t *testing.T) {},},},- textList: []string{- "DROP VIEW tech_book, xschema.user",+ statementList: []parser.SingleSQL{+ {+ Text: "DROP VIEW tech_book, xschema.user",+ Line: 1,+ },},},}@@ -816,8 +898,11 @@ func TestPGNotNullStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE tech_book ALTER COLUMN id SET NOT NULL",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE tech_book ALTER COLUMN id SET NOT NULL",+ Line: 1,+ },},},{@@ -839,8 +924,11 @@ func TestPGNotNullStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE tech_book ALTER COLUMN id DROP NOT NULL",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE tech_book ALTER COLUMN id DROP NOT NULL",+ Line: 1,+ },},},}@@ -887,8 +975,11 @@ func TestPGSelectStmt(t *testing.T) {},},},- textList: []string{- "SELECT public.t.a, t.*, t1.* FROM (SELECT * FROM t) t, t1",+ statementList: []parser.SingleSQL{+ {+ Text: "SELECT public.t.a, t.*, t1.* FROM (SELECT * FROM t) t, t1",+ Line: 1,+ },},},{@@ -915,8 +1006,11 @@ func TestPGSelectStmt(t *testing.T) {},},},- textList: []string{- "SELECT public.t.a, t.*, * FROM t",+ statementList: []parser.SingleSQL{+ {+ Text: "SELECT public.t.a, t.*, * FROM t",+ Line: 1,+ },},},{@@ -1018,8 +1112,9 @@ func TestPGSelectStmt(t *testing.T) {},},},- textList: []string{- `SELECT+ statementList: []parser.SingleSQL{+ {+ Text: `SELECTpublic.t.a, b, lower(a), b>aFROMt@@ -1031,6 +1126,8 @@ func TestPGSelectStmt(t *testing.T) {(SELECT * FROM t1 WHERE x LIKE b)UNIONSELECT * FROM t`,+ Line: 2,+ },},},}@@ -1047,8 +1144,11 @@ func TestPGDropDatabaseStmt(t *testing.T) {DatabaseName: "test",},},- textList: []string{- "DROP DATABASE test",+ statementList: []parser.SingleSQL{+ {+ Text: "DROP DATABASE test",+ Line: 1,+ },},},{@@ -1059,8 +1159,11 @@ func TestPGDropDatabaseStmt(t *testing.T) {IfExists: true,},},- textList: []string{- "DROP DATABASE IF EXISTS test",+ statementList: []parser.SingleSQL{+ {+ Text: "DROP DATABASE IF EXISTS test",+ Line: 1,+ },},},}@@ -1094,8 +1197,11 @@ func TestUpdateStmt(t *testing.T) {},},},- textList: []string{- "UPDATE tech_book SET a = 1 FROM (SELECT * FROM t) t WHERE a > 1",+ statementList: []parser.SingleSQL{+ {+ Text: "UPDATE tech_book SET a = 1 FROM (SELECT * FROM t) t WHERE a > 1",+ Line: 1,+ },},},{@@ -1108,8 +1214,11 @@ func TestUpdateStmt(t *testing.T) {},},},- textList: []string{- "UPDATE tech_book SET a = 1",+ statementList: []parser.SingleSQL{+ {+ Text: "UPDATE tech_book SET a = 1",+ Line: 1,+ },},},{@@ -1123,8 +1232,11 @@ func TestUpdateStmt(t *testing.T) {WhereClause: &ast.UnconvertedExpressionDef{},},},- textList: []string{- "UPDATE tech_book SET a = 1 WHERE a > 1",+ statementList: []parser.SingleSQL{+ {+ Text: "UPDATE tech_book SET a = 1 WHERE a > 1",+ Line: 1,+ },},},}@@ -1143,8 +1255,11 @@ func TestDeleteStmt(t *testing.T) {},},},- textList: []string{- "DELETE FROM tech_book",+ statementList: []parser.SingleSQL{+ {+ Text: "DELETE FROM tech_book",+ Line: 1,+ },},},{@@ -1158,8 +1273,11 @@ func TestDeleteStmt(t *testing.T) {WhereClause: &ast.UnconvertedExpressionDef{},},},- textList: []string{- "DELETE FROM tech_book WHERE a > 1",+ statementList: []parser.SingleSQL{+ {+ Text: "DELETE FROM tech_book WHERE a > 1",+ Line: 1,+ },},},}@@ -1188,8 +1306,11 @@ func TestSetSchemaStmt(t *testing.T) {},},},- textList: []string{- "ALTER TABLE tech_book SET SCHEMA new_schema",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE tech_book SET SCHEMA new_schema",+ Line: 1,+ },},},{@@ -1211,8 +1332,11 @@ func TestSetSchemaStmt(t *testing.T) {},},},- textList: []string{- "ALTER VIEW tech_book SET SCHEMA new_schema",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER VIEW tech_book SET SCHEMA new_schema",+ Line: 1,+ },},},}@@ -1237,8 +1361,11 @@ func TestExplainStmt(t *testing.T) {},},},- textList: []string{- "EXPLAIN SELECT * FROM tech_book",+ statementList: []parser.SingleSQL{+ {+ Text: "EXPLAIN SELECT * FROM tech_book",+ Line: 1,+ },},},}@@ -1267,8 +1394,11 @@ func TestAlterColumnType(t *testing.T) {},},},- textList: []string{- "ALTER TABLE tech_book ALTER COLUMN a TYPE string",+ statementList: []parser.SingleSQL{+ {+ Text: "ALTER TABLE tech_book ALTER COLUMN a TYPE string",+ Line: 1,+ },},},}@@ -1297,8 +1427,11 @@ func TestInsertStmt(t *testing.T) {},},},- textList: []string{- "INSERT INTO tech_book SELECT * FROM book WHERE type='tech'",+ statementList: []parser.SingleSQL{+ {+ Text: "INSERT INTO tech_book SELECT * FROM book WHERE type='tech'",+ Line: 1,+ },},},{@@ -1311,8 +1444,11 @@ func TestInsertStmt(t *testing.T) {},},},- textList: []string{- "INSERT INTO tech_book VALUES(1, 2, 3, 4, 5)",+ statementList: []parser.SingleSQL{+ {+ Text: "INSERT INTO tech_book VALUES(1, 2, 3, 4, 5)",+ Line: 1,+ },},},}@@ -1333,8 +1469,11 @@ func TestCopyStmt(t *testing.T) {FilePath: "/file/path/in/file/system",},},- textList: []string{- "COPY tech_book FROM '/file/path/in/file/system'",+ statementList: []parser.SingleSQL{+ {+ Text: "COPY tech_book FROM '/file/path/in/file/system'",+ Line: 1,+ },},},}diff --git a/plugin/parser/split_multi_sql_test.go b/plugin/parser/split_multi_sql_test.goindex af6f8c823f..08b4c45e3d 100644--- a/plugin/parser/split_multi_sql_test.go+++ b/plugin/parser/split_multi_sql_test.go@@ -13,7 +13,7 @@ type testData struct {}type resData struct {- res []string+ res []SingleSQLerr error}@@ -22,9 +22,15 @@ func TestPGSplitMultiSQL(t *testing.T) {{statement: " CREATE TABLE t(a int); CREATE TABLE t1(a int)",want: resData{- res: []string{- "CREATE TABLE t(a int);",- "CREATE TABLE t1(a int)",+ res: []SingleSQL{+ {+ Text: "CREATE TABLE t(a int);",+ Line: 1,+ },+ {+ Text: "CREATE TABLE t1(a int)",+ Line: 1,+ },},},},@@ -32,9 +38,15 @@ func TestPGSplitMultiSQL(t *testing.T) {statement: `CREATE TABLE "tech_Book"(id int, name varchar(255));INSERT INTO "tech_Book" VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\'jdfl;"ka');`,want: resData{- res: []string{- `CREATE TABLE "tech_Book"(id int, name varchar(255));`,- `INSERT INTO "tech_Book" VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\'jdfl;"ka');`,+ res: []SingleSQL{+ {+ Text: `CREATE TABLE "tech_Book"(id int, name varchar(255));`,+ Line: 1,+ },+ {+ Text: `INSERT INTO "tech_Book" VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\'jdfl;"ka');`,+ Line: 2,+ },},},},@@ -45,11 +57,17 @@ func TestPGSplitMultiSQL(t *testing.T) {-- this is the comment.INSERT INTO "tech_Book" VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\'jdfl;"ka');`,want: resData{- res: []string{- `/* this is the comment. */+ res: []SingleSQL{+ {+ Text: `/* this is the comment. */CREATE /* inline comment */TABLE "tech_Book"(id int, name varchar(255));`,- `-- this is the comment.+ Line: 2,+ },+ {+ Text: `-- this is the comment.INSERT INTO "tech_Book" VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\'jdfl;"ka');`,+ Line: 4,+ },},},},@@ -64,7 +82,9 @@ func TestPGSplitMultiSQL(t *testing.T) {$$;CREATE TABLE t(a int);`,want: resData{- res: []string{`CREATE PROCEDURE insert_data(a varchar(50), b varchar(50))+ res: []SingleSQL{+ {+ Text: `CREATE PROCEDURE insert_data(a varchar(50), b varchar(50))LANGUAGE SQLAS $$/*this is the comment */@@ -72,7 +92,12 @@ func TestPGSplitMultiSQL(t *testing.T) {-- this is the commentINSERT INTO tbl VALUES ('fasf_bkdjlfa');$$;`,- `CREATE TABLE t(a int);`,+ Line: 1,+ },+ {+ Text: `CREATE TABLE t(a int);`,+ Line: 9,+ },},},},@@ -87,7 +112,9 @@ func TestPGSplitMultiSQL(t *testing.T) {$tag_name$;CREATE TABLE t(a int);`,want: resData{- res: []string{`CREATE PROCEDURE insert_data(a varchar(50), b varchar(50))+ res: []SingleSQL{+ {+ Text: `CREATE PROCEDURE insert_data(a varchar(50), b varchar(50))LANGUAGE SQLAS $tag_name$/*this is the comment */@@ -95,7 +122,28 @@ func TestPGSplitMultiSQL(t *testing.T) {-- this is the commentINSERT INTO tbl VALUES ('fasf_bkdjlfa');$tag_name$;`,- `CREATE TABLE t(a int);`,+ Line: 1,+ },+ {+ Text: `CREATE TABLE t(a int);`,+ Line: 9,+ },+ },+ },+ },+ {+ // test for Windows+ statement: `CREATE TABLE t` + "\r\n" + `(a int);` + "\r\n" + `CREATE TABLE t1(b int);`,+ want: resData{+ res: []SingleSQL{+ {+ Text: "CREATE TABLE t\r\n(a int);",+ Line: 1,+ },+ {+ Text: "CREATE TABLE t1(b int);",+ Line: 3,+ },},},},diff --git a/plugin/parser/tokenizer.go b/plugin/parser/tokenizer.goindex cf4c4b2bb8..99f25fd777 100644--- a/plugin/parser/tokenizer.go+++ b/plugin/parser/tokenizer.go@@ -18,6 +18,8 @@ type tokenizer struct {statement []runecursor uintlen uint+ line int+ startLine int}// newTokenizer creates a new tokenizer.@@ -26,6 +28,8 @@ func newTokenizer(statement string) *tokenizer {t := &tokenizer{statement: []rune(statement),cursor: 0,+ line: 1,+ startLine: 1,}t.len = uint(len(t.statement))// append an additional eofRune.@@ -48,10 +52,11 @@ func newTokenizer(statement string) *tokenizer {// - We support PostgreSQL CREATE PROCEDURE statement with $$ $$ style,// but do not support BEGIN ATOMIC ... END; style.// See https://www.postgresql.org/docs/14/sql-createprocedure.html.-func (t *tokenizer) splitPostgreSQLMultiSQL() ([]string, error) {- var res []string+func (t *tokenizer) splitPostgreSQLMultiSQL() ([]SingleSQL, error) {+ var res []SingleSQLt.skipBlank()+ t.startLine = t.linestartPos := t.cursorfor {switch {@@ -77,13 +82,20 @@ func (t *tokenizer) splitPostgreSQLMultiSQL() ([]string, error) {}case t.char(0) == ';':t.skip(1)- res = append(res, t.getString(startPos, t.pos()-startPos))+ res = append(res, SingleSQL{+ Text: t.getString(startPos, t.pos()-startPos),+ Line: t.startLine,+ })t.skipBlank()+ t.startLine = t.linestartPos = t.pos()case t.char(0) == eofRune:s := t.getString(startPos, t.pos())if !emptyString(s) {- res = append(res, s)+ res = append(res, SingleSQL{+ Text: s,+ Line: t.startLine,+ })}return res, nil// return error when meeting BEGIN ATOMIC.@@ -93,6 +105,9 @@ func (t *tokenizer) splitPostgreSQLMultiSQL() ([]string, error) {if t.equalWordCaseInsensitive(atomicRuneList) {return nil, fmt.Errorf("not support BEGIN ATOMIC ... END in PostgreSQL CREATE PROCEDURE statement, please use double doller style($$ or $tag$) instead of it")}+ case t.char(0) == '\n':+ t.line+++ t.skip(1)default:t.skip(1)}@@ -146,6 +161,9 @@ func (t *tokenizer) scanString(delimiter rune) error {case '\\':// skip two because we want to skip \' and \\.t.skip(2)+ case '\n':+ t.line+++ t.skip(1)default:t.skip(1)}@@ -179,6 +197,9 @@ func (t *tokenizer) scanComment() error {return nilcase t.char(0) == eofRune:return fmt.Errorf("invalid comment: not found */, but found EOF")+ case t.char(0) == '\n':+ t.line+++ t.skip(1)default:t.skip(1)}@@ -188,6 +209,7 @@ func (t *tokenizer) scanComment() error {for {switch t.char(0) {case '\n':+ t.line++t.skip(1)return nilcase eofRune:@@ -237,6 +259,9 @@ func (t *tokenizer) scanTo(delimiter []rune) error {if t.char(0) == eofRune {return fmt.Errorf("scanTo failed: delimiter %q not found", string(delimiter))}+ if t.char(0) == '\n' {+ t.line+++ }if t.char(0) == delimiter[pos] {pos++t.skip(1)@@ -269,6 +294,9 @@ func (t *tokenizer) skipBlank() {r := t.char(0)for r == ' ' || r == '\n' || r == '\r' || r == '\t' {t.skip(1)+ if r == '\n' {+ t.line+++ }r = t.char(0)}}diff --git a/plugin/parser/utils.go b/plugin/parser/utils.goindex 4e53d8679a..333d385ae1 100644--- a/plugin/parser/utils.go+++ b/plugin/parser/utils.go@@ -2,8 +2,14 @@ package parserimport "fmt"+// SingleSQL is a separate SQL split from multi-SQL.+type SingleSQL struct {+ Text string+ Line int+}+// SplitMultiSQL splits statement into a slice of the single SQL.-func SplitMultiSQL(engineType EngineType, statement string) ([]string, error) {+func SplitMultiSQL(engineType EngineType, statement string) ([]SingleSQL, error) {switch engineType {case Postgres:t := newTokenizer(statement)