Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore(parser): set the last line for AST node instead of the first line (#2729)

rebelice771f63585ba9
diff --git a/plugin/advisor/catalog/walk_through.go b/plugin/advisor/catalog/walk_through.go
index b4fdcd5815..6ed136da20 100644
--- a/plugin/advisor/catalog/walk_through.go
+++ b/plugin/advisor/catalog/walk_through.go
@@ -1115,7 +1115,7 @@ func (d *databaseState) parse(stmts string) ([]tidbast.StmtNode, *WalkThroughErr
for i, node := range nodeList {
node.SetText(nil, strings.TrimSpace(node.Text()))
- node.SetOriginTextPosition(sqlList[i].Line)
+ node.SetOriginTextPosition(sqlList[i].LastLine)
if n, ok := node.(*tidbast.CreateTableStmt); ok {
if err := parser.SetLineForMySQLCreateTableStmt(n); err != nil {
return nil, NewParseError(err.Error())
diff --git a/plugin/advisor/mysql/advisor_column_required_test.go b/plugin/advisor/mysql/advisor_column_required_test.go
index 4ae776b8ac..2cfc4c3706 100644
--- a/plugin/advisor/mysql/advisor_column_required_test.go
+++ b/plugin/advisor/mysql/advisor_column_required_test.go
@@ -158,7 +158,7 @@ func TestColumnRequirement(t *testing.T) {
Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `book` requires columns: updater_id",
- Line: 1,
+ Line: 5,
},
},
},
@@ -188,21 +188,24 @@ func TestColumnRequirement(t *testing.T) {
CREATE TABLE student(
id int,
created_ts timestamp,
- updated_ts timestamp);`,
+ updated_ts timestamp)
+
+
+ `,
Want: []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `book` requires columns: creator_id",
- Line: 1,
+ Line: 5,
},
{
Status: advisor.Warn,
Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table `student` requires columns: creator_id, updater_id",
- Line: 6,
+ Line: 9,
},
},
},
diff --git a/plugin/advisor/mysql/advisor_table_disallow_create_table_as_test.go b/plugin/advisor/mysql/advisor_table_disallow_create_table_as_test.go
index 0ed76d6e05..a3a5992311 100644
--- a/plugin/advisor/mysql/advisor_table_disallow_create_table_as_test.go
+++ b/plugin/advisor/mysql/advisor_table_disallow_create_table_as_test.go
@@ -22,7 +22,7 @@ func TestTableDisallowCreateTableAs(t *testing.T) {
Code: advisor.StatementCreateTableAs,
Title: "statement.create-table.no-create-table-as",
Content: "cannot create table `t` by using CREATE TABLE ... [AS] SELECT ...",
- Line: 1,
+ Line: 4,
},
},
},
diff --git a/plugin/advisor/mysql/parser.go b/plugin/advisor/mysql/parser.go
index 7e3ea718b8..bec1d9e27e 100644
--- a/plugin/advisor/mysql/parser.go
+++ b/plugin/advisor/mysql/parser.go
@@ -67,7 +67,7 @@ func parseStatement(statement string, charset string, collation string) ([]ast.S
for i, node := range root {
node.SetText(nil, strings.TrimSpace(node.Text()))
- node.SetOriginTextPosition(sqlList[i].Line)
+ node.SetOriginTextPosition(sqlList[i].LastLine)
if n, ok := node.(*ast.CreateTableStmt); ok {
if err := parser.SetLineForMySQLCreateTableStmt(n); err != nil {
return nil, []advisor.Advice{
diff --git a/plugin/advisor/pg/advisor_column_no_null.go b/plugin/advisor/pg/advisor_column_no_null.go
index 81f0e57295..fc92795936 100644
--- a/plugin/advisor/pg/advisor_column_no_null.go
+++ b/plugin/advisor/pg/advisor_column_no_null.go
@@ -101,7 +101,7 @@ func (checker *columnNoNullChecker) Visit(node ast.Node) ast.Visitor {
// CREATE TABLE
case *ast.CreateTableStmt:
for _, column := range n.ColumnList {
- checker.addColumn(n.Name, column.ColumnName, column.Line())
+ checker.addColumn(n.Name, column.ColumnName, column.LastLine())
checker.removeColumnByConstraintList(n.Name, column.ConstraintList)
}
checker.removeColumnByConstraintList(n.Name, n.ConstraintList)
@@ -112,7 +112,7 @@ func (checker *columnNoNullChecker) Visit(node ast.Node) ast.Visitor {
// ALTER TABLE ADD COLUMN
case *ast.AddColumnListStmt:
for _, column := range cmd.ColumnList {
- checker.addColumn(n.Table, column.ColumnName, n.Line())
+ checker.addColumn(n.Table, column.ColumnName, n.LastLine())
checker.removeColumnByConstraintList(n.Table, column.ConstraintList)
}
// ALTER TABLE ALTER COLUMN SET NOT NULL
@@ -120,7 +120,7 @@ func (checker *columnNoNullChecker) Visit(node ast.Node) ast.Visitor {
checker.removeColumn(n.Table, cmd.ColumnName)
// ALTER TABLE ALTER COLUMN DROP NOT NULL
case *ast.DropNotNullStmt:
- checker.addColumn(n.Table, cmd.ColumnName, n.Line())
+ checker.addColumn(n.Table, cmd.ColumnName, n.LastLine())
// ALTER TABLE ADD CONSTRAINT
case *ast.AddConstraintStmt:
checker.removeColumnByConstraintList(n.Table, []*ast.ConstraintDef{cmd.Constraint})
diff --git a/plugin/advisor/pg/advisor_column_required.go b/plugin/advisor/pg/advisor_column_required.go
index 70542f52a5..4557f8fb7c 100644
--- a/plugin/advisor/pg/advisor_column_required.go
+++ b/plugin/advisor/pg/advisor_column_required.go
@@ -111,7 +111,7 @@ func (checker *columnRequirementChecker) Visit(node ast.Node) ast.Visitor {
Code: advisor.NoRequiredColumn,
Title: checker.title,
Content: fmt.Sprintf("Table %q requires columns: %s", table.Name, strings.Join(missingColumns, ", ")),
- Line: node.Line(),
+ Line: node.LastLine(),
})
}
diff --git a/plugin/advisor/pg/advisor_migration_compatibility.go b/plugin/advisor/pg/advisor_migration_compatibility.go
index b2eaff556e..bafa3bf975 100644
--- a/plugin/advisor/pg/advisor_migration_compatibility.go
+++ b/plugin/advisor/pg/advisor_migration_compatibility.go
@@ -126,7 +126,7 @@ func (checker *compatibilityChecker) Visit(node ast.Node) ast.Visitor {
Code: code,
Title: checker.title,
Content: fmt.Sprintf("\"%s\" may cause incompatibility with the existing data and code", checker.text),
- Line: node.Line(),
+ Line: node.LastLine(),
})
}
return checker
diff --git a/plugin/advisor/pg/advisor_naming_column.go b/plugin/advisor/pg/advisor_naming_column.go
index 2bcbc17991..b9e1c3a0d5 100644
--- a/plugin/advisor/pg/advisor_naming_column.go
+++ b/plugin/advisor/pg/advisor_naming_column.go
@@ -84,7 +84,7 @@ func (checker *namingColumnConventionChecker) Visit(node ast.Node) ast.Visitor {
for _, col := range n.ColumnList {
columnList = append(columnList, columnData{
name: col.ColumnName,
- line: col.Line(),
+ line: col.LastLine(),
})
}
// ALTER TABLE ADD COLUMN
@@ -93,7 +93,7 @@ func (checker *namingColumnConventionChecker) Visit(node ast.Node) ast.Visitor {
for _, col := range n.ColumnList {
columnList = append(columnList, columnData{
name: col.ColumnName,
- line: n.Line(),
+ line: n.LastLine(),
})
}
// ALTER TABLE RENAME COLUMN
@@ -101,7 +101,7 @@ func (checker *namingColumnConventionChecker) Visit(node ast.Node) ast.Visitor {
tableName = n.Table.Name
columnList = append(columnList, columnData{
name: n.NewName,
- line: n.Line(),
+ line: n.LastLine(),
})
}
diff --git a/plugin/advisor/pg/advisor_naming_foreign_key_convention.go b/plugin/advisor/pg/advisor_naming_foreign_key_convention.go
index 2cd75c81bb..3c52af9d2a 100644
--- a/plugin/advisor/pg/advisor_naming_foreign_key_convention.go
+++ b/plugin/advisor/pg/advisor_naming_foreign_key_convention.go
@@ -124,26 +124,26 @@ func (*namingFKConventionChecker) getMetaDataList(in ast.Node) []*indexMetaData
switch node := in.(type) {
case *ast.CreateTableStmt:
for _, constraint := range node.ConstraintList {
- if metadata := getForeignKeyMetadata(constraint, node.Name.Name, constraint.Line()); metadata != nil {
+ if metadata := getForeignKeyMetadata(constraint, node.Name.Name, constraint.LastLine()); metadata != nil {
res = append(res, metadata)
}
}
for _, column := range node.ColumnList {
for _, constraint := range column.ConstraintList {
- if metadata := getForeignKeyMetadata(constraint, node.Name.Name, column.Line()); metadata != nil {
+ if metadata := getForeignKeyMetadata(constraint, node.Name.Name, column.LastLine()); metadata != nil {
res = append(res, metadata)
}
}
}
case *ast.AddConstraintStmt:
constraint := node.Constraint
- if metadata := getForeignKeyMetadata(constraint, node.Table.Name, node.Line()); metadata != nil {
+ if metadata := getForeignKeyMetadata(constraint, node.Table.Name, node.LastLine()); metadata != nil {
res = append(res, metadata)
}
case *ast.AddColumnListStmt:
for _, column := range node.ColumnList {
for _, constraint := range column.ConstraintList {
- if metadata := getForeignKeyMetadata(constraint, node.Table.Name, node.Line()); metadata != nil {
+ if metadata := getForeignKeyMetadata(constraint, node.Table.Name, node.LastLine()); metadata != nil {
res = append(res, metadata)
}
}
diff --git a/plugin/advisor/pg/advisor_naming_index_convention.go b/plugin/advisor/pg/advisor_naming_index_convention.go
index 7fd90559a7..d8f66e3c29 100644
--- a/plugin/advisor/pg/advisor_naming_index_convention.go
+++ b/plugin/advisor/pg/advisor_naming_index_convention.go
@@ -95,7 +95,7 @@ func (checker *namingIndexConventionChecker) Visit(node ast.Node) ast.Visitor {
Code: advisor.NamingIndexConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf("Index in table %q mismatches the naming convention, expect %q but found %q", indexData.tableName, regex, indexData.indexName),
- Line: node.Line(),
+ Line: node.LastLine(),
})
}
if checker.maxLength > 0 && len(indexData.indexName) > checker.maxLength {
@@ -104,7 +104,7 @@ func (checker *namingIndexConventionChecker) Visit(node ast.Node) ast.Visitor {
Code: advisor.NamingIndexConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf("Index %q in table %q mismatches the naming convention, its length should be within %d characters", indexData.indexName, indexData.tableName, checker.maxLength),
- Line: node.Line(),
+ Line: node.LastLine(),
})
}
}
diff --git a/plugin/advisor/pg/advisor_naming_primary_key_convention.go b/plugin/advisor/pg/advisor_naming_primary_key_convention.go
index bffd6a6da3..dcce6583bc 100644
--- a/plugin/advisor/pg/advisor_naming_primary_key_convention.go
+++ b/plugin/advisor/pg/advisor_naming_primary_key_convention.go
@@ -119,26 +119,26 @@ func (checker *namingPKConventionChecker) getMetaDataList(in ast.Node) []*indexM
switch node := in.(type) {
case *ast.CreateTableStmt:
for _, constraint := range node.ConstraintList {
- if metadata := checker.getPrimaryKeyMetadata(node.Name.Schema, node.Name.Name, constraint, constraint.Line()); metadata != nil {
+ if metadata := checker.getPrimaryKeyMetadata(node.Name.Schema, node.Name.Name, constraint, constraint.LastLine()); metadata != nil {
res = append(res, metadata)
}
}
for _, column := range node.ColumnList {
for _, constraint := range column.ConstraintList {
- if metadata := checker.getPrimaryKeyMetadata(node.Name.Schema, node.Name.Name, constraint, column.Line()); metadata != nil {
+ if metadata := checker.getPrimaryKeyMetadata(node.Name.Schema, node.Name.Name, constraint, column.LastLine()); metadata != nil {
res = append(res, metadata)
}
}
}
case *ast.AddConstraintStmt:
constraint := node.Constraint
- if metadata := checker.getPrimaryKeyMetadata(node.Table.Schema, node.Table.Name, constraint, in.Line()); metadata != nil {
+ if metadata := checker.getPrimaryKeyMetadata(node.Table.Schema, node.Table.Name, constraint, in.LastLine()); metadata != nil {
res = append(res, metadata)
}
case *ast.AddColumnListStmt:
for _, column := range node.ColumnList {
for _, constraint := range column.ConstraintList {
- if metadata := checker.getPrimaryKeyMetadata(node.Table.Schema, node.Table.Name, constraint, in.Line()); metadata != nil {
+ if metadata := checker.getPrimaryKeyMetadata(node.Table.Schema, node.Table.Name, constraint, in.LastLine()); metadata != nil {
res = append(res, metadata)
}
}
@@ -153,7 +153,7 @@ func (checker *namingPKConventionChecker) getMetaDataList(in ast.Node) []*indexM
res = append(res, &indexMetaData{
indexName: node.NewName,
tableName: node.Table.Name,
- line: in.Line(),
+ line: in.LastLine(),
metaData: metaData,
})
}
@@ -168,7 +168,7 @@ func (checker *namingPKConventionChecker) getMetaDataList(in ast.Node) []*indexM
res = append(res, &indexMetaData{
indexName: node.NewName,
tableName: tableName,
- line: in.Line(),
+ line: in.LastLine(),
metaData: metaData,
})
}
diff --git a/plugin/advisor/pg/advisor_naming_table.go b/plugin/advisor/pg/advisor_naming_table.go
index 0c253b6c51..ae97267df3 100644
--- a/plugin/advisor/pg/advisor_naming_table.go
+++ b/plugin/advisor/pg/advisor_naming_table.go
@@ -88,7 +88,7 @@ func (checker *namingTableConventionChecker) Visit(node ast.Node) ast.Visitor {
Code: advisor.NamingTableConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf(`"%s" mismatches table naming convention, naming format should be %q`, tableName, checker.format),
- Line: node.Line(),
+ Line: node.LastLine(),
})
}
if checker.maxLength > 0 && len(tableName) > checker.maxLength {
@@ -97,7 +97,7 @@ func (checker *namingTableConventionChecker) Visit(node ast.Node) ast.Visitor {
Code: advisor.NamingTableConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf("\"%s\" mismatches table naming convention, its length should be within %d characters", tableName, checker.maxLength),
- Line: node.Line(),
+ Line: node.LastLine(),
})
}
}
diff --git a/plugin/advisor/pg/advisor_naming_unique_key_convention.go b/plugin/advisor/pg/advisor_naming_unique_key_convention.go
index 724057808b..d269f001be 100644
--- a/plugin/advisor/pg/advisor_naming_unique_key_convention.go
+++ b/plugin/advisor/pg/advisor_naming_unique_key_convention.go
@@ -119,26 +119,26 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
switch node := in.(type) {
case *ast.CreateTableStmt:
for _, constraint := range node.ConstraintList {
- if metadata := checker.getUniqueKeyMetadata(node.Name.Schema, node.Name.Name, constraint, constraint.Line()); metadata != nil {
+ if metadata := checker.getUniqueKeyMetadata(node.Name.Schema, node.Name.Name, constraint, constraint.LastLine()); metadata != nil {
res = append(res, metadata)
}
}
for _, column := range node.ColumnList {
for _, constraint := range column.ConstraintList {
- if metadata := checker.getUniqueKeyMetadata(node.Name.Schema, node.Name.Name, constraint, column.Line()); metadata != nil {
+ if metadata := checker.getUniqueKeyMetadata(node.Name.Schema, node.Name.Name, constraint, column.LastLine()); metadata != nil {
res = append(res, metadata)
}
}
}
case *ast.AddConstraintStmt:
constraint := node.Constraint
- if metadata := checker.getUniqueKeyMetadata(node.Table.Schema, node.Table.Name, constraint, node.Line()); metadata != nil {
+ if metadata := checker.getUniqueKeyMetadata(node.Table.Schema, node.Table.Name, constraint, node.LastLine()); metadata != nil {
res = append(res, metadata)
}
case *ast.AddColumnListStmt:
for _, column := range node.ColumnList {
for _, constraint := range column.ConstraintList {
- if metadata := checker.getUniqueKeyMetadata(node.Table.Schema, node.Table.Name, constraint, node.Line()); metadata != nil {
+ if metadata := checker.getUniqueKeyMetadata(node.Table.Schema, node.Table.Name, constraint, node.LastLine()); metadata != nil {
res = append(res, metadata)
}
}
@@ -156,7 +156,7 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
res = append(res, &indexMetaData{
indexName: node.Index.Name,
tableName: node.Index.Table.Name,
- line: node.Line(),
+ line: node.LastLine(),
metaData: metaData,
})
}
@@ -170,7 +170,7 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
res = append(res, &indexMetaData{
indexName: node.NewName,
tableName: node.Table.Name,
- line: node.Line(),
+ line: node.LastLine(),
metaData: metaData,
})
}
@@ -185,7 +185,7 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
res = append(res, &indexMetaData{
indexName: node.NewName,
tableName: tableName,
- line: node.Line(),
+ line: node.LastLine(),
metaData: metaData,
})
}
diff --git a/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like.go b/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like.go
index 480cb2d658..257be56b28 100644
--- a/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like.go
+++ b/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like.go
@@ -53,7 +53,7 @@ func (*NoLeadingWildcardLikeAdvisor) Check(ctx advisor.Context, statement string
Code: advisor.StatementLeadingWildcardLike,
Title: checker.title,
Content: fmt.Sprintf("\"%s\" uses leading wildcard LIKE", checker.text),
- Line: stmt.Line(),
+ Line: stmt.LastLine(),
})
}
}
diff --git a/plugin/advisor/pg/advisor_stmt_no_select_all.go b/plugin/advisor/pg/advisor_stmt_no_select_all.go
index b6d5180ff8..fddc6f3d87 100644
--- a/plugin/advisor/pg/advisor_stmt_no_select_all.go
+++ b/plugin/advisor/pg/advisor_stmt_no_select_all.go
@@ -39,7 +39,7 @@ func (*NoSelectAllAdvisor) Check(ctx advisor.Context, statement string) ([]advis
}
for _, stmt := range stmts {
checker.text = stmt.Text()
- checker.line = stmt.Line()
+ checker.line = stmt.LastLine()
ast.Walk(checker, stmt)
}
diff --git a/plugin/advisor/pg/advisor_stmt_where_required.go b/plugin/advisor/pg/advisor_stmt_where_required.go
index d76eb4356b..ff3bafb223 100644
--- a/plugin/advisor/pg/advisor_stmt_where_required.go
+++ b/plugin/advisor/pg/advisor_stmt_where_required.go
@@ -39,7 +39,7 @@ func (*WhereRequirementAdvisor) Check(ctx advisor.Context, statement string) ([]
for _, stmt := range stmts {
checker.text = stmt.Text()
- checker.line = stmt.Line()
+ checker.line = stmt.LastLine()
ast.Walk(checker, stmt)
}
diff --git a/plugin/advisor/pg/advisor_table_no_fk.go b/plugin/advisor/pg/advisor_table_no_fk.go
index 24622b8b9e..f4cae104b0 100644
--- a/plugin/advisor/pg/advisor_table_no_fk.go
+++ b/plugin/advisor/pg/advisor_table_no_fk.go
@@ -93,7 +93,7 @@ func (checker *tableNoFKChecker) Visit(node ast.Node) ast.Visitor {
tableHasFK.Name,
checker.text,
),
- Line: node.Line(),
+ Line: node.LastLine(),
})
}
diff --git a/plugin/advisor/pg/advisor_table_require_pk.go b/plugin/advisor/pg/advisor_table_require_pk.go
index 212dc6987e..3fd729d664 100644
--- a/plugin/advisor/pg/advisor_table_require_pk.go
+++ b/plugin/advisor/pg/advisor_table_require_pk.go
@@ -117,7 +117,7 @@ func (checker *tableRequirePKChecker) Visit(node ast.Node) ast.Visitor {
missingPK.Name,
checker.text,
),
- Line: node.Line(),
+ Line: node.LastLine(),
})
}
diff --git a/plugin/parser/ast/node.go b/plugin/parser/ast/node.go
index 43a61936a5..bce3a9b45c 100644
--- a/plugin/parser/ast/node.go
+++ b/plugin/parser/ast/node.go
@@ -8,14 +8,14 @@ var (
type Node interface {
Text() string
SetText(text string)
- Line() int
- SetLine(line int)
+ LastLine() int
+ SetLastLine(line int)
}
// node is the base struct for all Node.
type node struct {
- text string
- line int
+ text string
+ lastline int
}
// Text implements the Node interface.
@@ -28,10 +28,10 @@ func (n *node) SetText(text string) {
n.text = text
}
-func (n *node) Line() int {
- return n.line
+func (n *node) LastLine() int {
+ return n.lastline
}
-func (n *node) SetLine(line int) {
- n.line = line
+func (n *node) SetLastLine(line int) {
+ n.lastline = line
}
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index 14dd749a85..7df690a39f 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -15,14 +15,14 @@ import (
func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, err error) {
defer func() {
if err == nil && res != nil {
- res.SetText(statement.Text)
- res.SetLine(statement.Line)
+ res.SetText(strings.TrimSpace(statement.Text))
+ res.SetLastLine(statement.LastLine)
switch n := res.(type) {
case *ast.CreateTableStmt:
err = parser.SetLineForCreateTableStmt(parser.Postgres, n)
case *ast.AlterTableStmt:
for _, item := range n.AlterItemList {
- item.SetLine(n.Line())
+ item.SetLastLine(n.LastLine())
}
}
}
diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.go
index c7129ccba5..552b123e67 100644
--- a/plugin/parser/engine/pg/convert_test.go
+++ b/plugin/parser/engine/pg/convert_test.go
@@ -25,22 +25,22 @@ func runTests(t *testing.T, tests []testData) {
require.NoError(t, err)
for i := range test.want {
test.want[i].SetText(test.statementList[i].Text)
- test.want[i].SetLine(test.statementList[i].Line)
+ test.want[i].SetLastLine(test.statementList[i].LastLine)
switch n := test.want[i].(type) {
case *ast.CreateTableStmt:
for j, col := range n.ColumnList {
- col.SetLine(test.columnLine[i][j])
+ col.SetLastLine(test.columnLine[i][j])
for _, inlineCons := range col.ConstraintList {
- inlineCons.SetLine(col.Line())
+ inlineCons.SetLastLine(col.LastLine())
}
}
for j, cons := range n.ConstraintList {
- cons.SetLine(test.constraintLine[i][j])
+ cons.SetLastLine(test.constraintLine[i][j])
}
case *ast.AlterTableStmt:
for _, item := range n.AlterItemList {
- item.SetLine(n.Line())
+ item.SetLastLine(n.LastLine())
}
}
}
@@ -222,7 +222,7 @@ func TestPGConvertCreateTableStmt(t *testing.T) {
s serial4,
t decimal,
u "user defined data type")`,
- Line: 1,
+ LastLine: 22,
},
},
columnLine: [][]int{
@@ -265,8 +265,8 @@ func TestPGConvertCreateTableStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "CREATE TABLE \"techBook\" (a int NOT NULL, b int CONSTRAINT b_not_null NOT NULL)",
- Line: 1,
+ Text: "CREATE TABLE \"techBook\" (a int NOT NULL, b int CONSTRAINT b_not_null NOT NULL)",
+ LastLine: 1,
},
},
columnLine: [][]int{
@@ -296,8 +296,8 @@ func TestPGConvertCreateTableStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "CREATE TABLE IF NOT EXISTS techBook (\"A\" int, b int)",
- Line: 1,
+ Text: "CREATE TABLE IF NOT EXISTS techBook (\"A\" int, b int)",
+ LastLine: 1,
},
},
columnLine: [][]int{
@@ -329,8 +329,8 @@ func TestPGConvertCreateTableStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "CREATE TABLE tech_book(a INT CONSTRAINT t_pk_a PRIMARY KEY)",
- Line: 1,
+ Text: "CREATE TABLE tech_book(a INT CONSTRAINT t_pk_a PRIMARY KEY)",
+ LastLine: 1,
},
},
columnLine: [][]int{
@@ -373,8 +373,8 @@ func TestPGConvertCreateTableStmt(t *testing.T) {
},
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,
+ Text: "CREATE TABLE tech_book(a INT, b int CONSTRAINT uk_b UNIQUE, CONSTRAINT t_pk_a PRIMARY KEY(a))",
+ LastLine: 1,
},
},
columnLine: [][]int{
@@ -430,8 +430,8 @@ func TestPGConvertCreateTableStmt(t *testing.T) {
},
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,
+ Text: "CREATE TABLE tech_book(a INT CONSTRAINT fk_a REFERENCES people(id), CONSTRAINT fk_a_people_b FOREIGN KEY (a) REFERENCES people(b))",
+ LastLine: 1,
},
},
columnLine: [][]int{
@@ -474,8 +474,8 @@ func TestPGAddColumnStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE techbook ADD COLUMN a int",
- Line: 1,
+ Text: "ALTER TABLE techbook ADD COLUMN a int",
+ LastLine: 1,
},
},
},
@@ -512,8 +512,8 @@ func TestPGAddColumnStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE techbook ADD COLUMN a int CONSTRAINT uk_techbook_a UNIQUE",
- Line: 1,
+ Text: "ALTER TABLE techbook ADD COLUMN a int CONSTRAINT uk_techbook_a UNIQUE",
+ LastLine: 1,
},
},
},
@@ -545,8 +545,8 @@ func TestPGRenameTableStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE techbook RENAME TO \"techBook\"",
- Line: 1,
+ Text: "ALTER TABLE techbook RENAME TO \"techBook\"",
+ LastLine: 1,
},
},
},
@@ -571,8 +571,8 @@ func TestPGRenameTableStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER VIEW techbook RENAME TO \"techBook\"",
- Line: 1,
+ Text: "ALTER VIEW techbook RENAME TO \"techBook\"",
+ LastLine: 1,
},
},
},
@@ -605,8 +605,8 @@ func TestPGRenameColumnStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE techbook RENAME abc TO \"ABC\"",
- Line: 1,
+ Text: "ALTER TABLE techbook RENAME abc TO \"ABC\"",
+ LastLine: 1,
},
},
},
@@ -632,8 +632,8 @@ func TestPGRenameColumnStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER VIEW techbook RENAME abc TO \"ABC\"",
- Line: 1,
+ Text: "ALTER VIEW techbook RENAME abc TO \"ABC\"",
+ LastLine: 1,
},
},
},
@@ -666,8 +666,8 @@ func TestPGRenameConstraintStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE tech_book RENAME CONSTRAINT uk_tech_a to \"UK_TECH_A\"",
- Line: 1,
+ Text: "ALTER TABLE tech_book RENAME CONSTRAINT uk_tech_a to \"UK_TECH_A\"",
+ LastLine: 1,
},
},
},
@@ -697,8 +697,8 @@ func TestPGCreateIndexStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "CREATE INDEX idx_id ON tech_book (id)",
- Line: 1,
+ Text: "CREATE INDEX idx_id ON tech_book (id)",
+ LastLine: 1,
},
},
},
@@ -721,8 +721,8 @@ func TestPGCreateIndexStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "CREATE UNIQUE INDEX idx_id ON tech_book (id)",
- Line: 1,
+ Text: "CREATE UNIQUE INDEX idx_id ON tech_book (id)",
+ LastLine: 1,
},
},
},
@@ -748,8 +748,8 @@ func TestPGDropIndexStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "DROP INDEX xschema.idx_id, idx_x",
- Line: 1,
+ Text: "DROP INDEX xschema.idx_id, idx_x",
+ LastLine: 1,
},
},
},
@@ -771,8 +771,8 @@ func TestPGAlterIndexStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER INDEX xschema.idx_id RENAME TO \"IDX_ID\"",
- Line: 1,
+ Text: "ALTER INDEX xschema.idx_id RENAME TO \"IDX_ID\"",
+ LastLine: 1,
},
},
},
@@ -787,8 +787,8 @@ func TestPGAlterIndexStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER INDEX idx_id RENAME TO \"IDX_ID\"",
- Line: 1,
+ Text: "ALTER INDEX idx_id RENAME TO \"IDX_ID\"",
+ LastLine: 1,
},
},
},
@@ -820,8 +820,8 @@ func TestPGDropConstraintStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE tech_book DROP CONSTRAINT uk_tech_a",
- Line: 1,
+ Text: "ALTER TABLE tech_book DROP CONSTRAINT uk_tech_a",
+ LastLine: 1,
},
},
},
@@ -858,8 +858,8 @@ func TestPGAddConstraintStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE tech_book ADD CONSTRAINT check_a_bigger_than_b CHECK (a > b) NOT VALID",
- Line: 1,
+ Text: "ALTER TABLE tech_book ADD CONSTRAINT check_a_bigger_than_b CHECK (a > b) NOT VALID",
+ LastLine: 1,
},
},
},
@@ -888,8 +888,8 @@ func TestPGAddConstraintStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id UNIQUE (id)",
- Line: 1,
+ Text: "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id UNIQUE (id)",
+ LastLine: 1,
},
},
},
@@ -918,8 +918,8 @@ func TestPGAddConstraintStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_id PRIMARY KEY (id)",
- Line: 1,
+ Text: "ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_id PRIMARY KEY (id)",
+ LastLine: 1,
},
},
},
@@ -955,8 +955,8 @@ func TestPGAddConstraintStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE tech_book ADD CONSTRAINT fk_tech_book_id FOREIGN KEY (id) REFERENCES people(id)",
- Line: 1,
+ Text: "ALTER TABLE tech_book ADD CONSTRAINT fk_tech_book_id FOREIGN KEY (id) REFERENCES people(id)",
+ LastLine: 1,
},
},
},
@@ -985,8 +985,8 @@ func TestPGAddConstraintStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id UNIQUE USING INDEX uk_id",
- Line: 1,
+ Text: "ALTER TABLE tech_book ADD CONSTRAINT uk_tech_book_id UNIQUE USING INDEX uk_id",
+ LastLine: 1,
},
},
},
@@ -1015,8 +1015,8 @@ func TestPGAddConstraintStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_id PRIMARY KEY USING INDEX pk_id",
- Line: 1,
+ Text: "ALTER TABLE tech_book ADD CONSTRAINT pk_tech_book_id PRIMARY KEY USING INDEX pk_id",
+ LastLine: 1,
},
},
},
@@ -1048,8 +1048,8 @@ func TestPGDropColumnStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE tech_book DROP COLUMN a",
- Line: 1,
+ Text: "ALTER TABLE tech_book DROP COLUMN a",
+ LastLine: 1,
},
},
},
@@ -1079,8 +1079,8 @@ func TestPGDropTableStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "DROP TABLE tech_book, xschema.user",
- Line: 1,
+ Text: "DROP TABLE tech_book, xschema.user",
+ LastLine: 1,
},
},
},
@@ -1103,8 +1103,8 @@ func TestPGDropTableStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "DROP VIEW tech_book, xschema.user",
- Line: 1,
+ Text: "DROP VIEW tech_book, xschema.user",
+ LastLine: 1,
},
},
},
@@ -1136,8 +1136,8 @@ func TestPGNotNullStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE tech_book ALTER COLUMN id SET NOT NULL",
- Line: 1,
+ Text: "ALTER TABLE tech_book ALTER COLUMN id SET NOT NULL",
+ LastLine: 1,
},
},
},
@@ -1162,8 +1162,8 @@ func TestPGNotNullStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE tech_book ALTER COLUMN id DROP NOT NULL",
- Line: 1,
+ Text: "ALTER TABLE tech_book ALTER COLUMN id DROP NOT NULL",
+ LastLine: 1,
},
},
},
@@ -1213,8 +1213,8 @@ func TestPGSelectStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "SELECT public.t.a, t.*, t1.* FROM (SELECT * FROM t) t, t1",
- Line: 1,
+ Text: "SELECT public.t.a, t.*, t1.* FROM (SELECT * FROM t) t, t1",
+ LastLine: 1,
},
},
},
@@ -1244,8 +1244,8 @@ func TestPGSelectStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "SELECT public.t.a, t.*, * FROM t",
- Line: 1,
+ Text: "SELECT public.t.a, t.*, * FROM t",
+ LastLine: 1,
},
},
},
@@ -1362,7 +1362,7 @@ func TestPGSelectStmt(t *testing.T) {
(SELECT * FROM t1 WHERE x LIKE b)
UNION
SELECT * FROM t`,
- Line: 2,
+ LastLine: 13,
},
},
},
@@ -1382,8 +1382,8 @@ func TestPGDropDatabaseStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "DROP DATABASE test",
- Line: 1,
+ Text: "DROP DATABASE test",
+ LastLine: 1,
},
},
},
@@ -1397,8 +1397,8 @@ func TestPGDropDatabaseStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "DROP DATABASE IF EXISTS test",
- Line: 1,
+ Text: "DROP DATABASE IF EXISTS test",
+ LastLine: 1,
},
},
},
@@ -1435,8 +1435,8 @@ func TestUpdateStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "UPDATE tech_book SET a = 1 FROM (SELECT * FROM t) t WHERE a > 1",
- Line: 1,
+ Text: "UPDATE tech_book SET a = 1 FROM (SELECT * FROM t) t WHERE a > 1",
+ LastLine: 1,
},
},
},
@@ -1452,8 +1452,8 @@ func TestUpdateStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "UPDATE tech_book SET a = 1",
- Line: 1,
+ Text: "UPDATE tech_book SET a = 1",
+ LastLine: 1,
},
},
},
@@ -1470,8 +1470,8 @@ func TestUpdateStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "UPDATE tech_book SET a = 1 WHERE a > 1",
- Line: 1,
+ Text: "UPDATE tech_book SET a = 1 WHERE a > 1",
+ LastLine: 1,
},
},
},
@@ -1493,8 +1493,8 @@ func TestDeleteStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "DELETE FROM tech_book",
- Line: 1,
+ Text: "DELETE FROM tech_book",
+ LastLine: 1,
},
},
},
@@ -1511,8 +1511,8 @@ func TestDeleteStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "DELETE FROM tech_book WHERE a > 1",
- Line: 1,
+ Text: "DELETE FROM tech_book WHERE a > 1",
+ LastLine: 1,
},
},
},
@@ -1544,8 +1544,8 @@ func TestSetSchemaStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE tech_book SET SCHEMA new_schema",
- Line: 1,
+ Text: "ALTER TABLE tech_book SET SCHEMA new_schema",
+ LastLine: 1,
},
},
},
@@ -1570,8 +1570,8 @@ func TestSetSchemaStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER VIEW tech_book SET SCHEMA new_schema",
- Line: 1,
+ Text: "ALTER VIEW tech_book SET SCHEMA new_schema",
+ LastLine: 1,
},
},
},
@@ -1599,8 +1599,8 @@ func TestExplainStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "EXPLAIN SELECT * FROM tech_book",
- Line: 1,
+ Text: "EXPLAIN SELECT * FROM tech_book",
+ LastLine: 1,
},
},
},
@@ -1632,8 +1632,8 @@ func TestAlterColumnType(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "ALTER TABLE tech_book ALTER COLUMN a TYPE string",
- Line: 1,
+ Text: "ALTER TABLE tech_book ALTER COLUMN a TYPE string",
+ LastLine: 1,
},
},
},
@@ -1665,8 +1665,8 @@ func TestInsertStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "INSERT INTO tech_book SELECT * FROM book WHERE type='tech'",
- Line: 1,
+ Text: "INSERT INTO tech_book SELECT * FROM book WHERE type='tech'",
+ LastLine: 1,
},
},
},
@@ -1682,8 +1682,8 @@ func TestInsertStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "INSERT INTO tech_book VALUES(1, 2, 3, 4, 5)",
- Line: 1,
+ Text: "INSERT INTO tech_book VALUES(1, 2, 3, 4, 5)",
+ LastLine: 1,
},
},
},
@@ -1707,8 +1707,8 @@ func TestCopyStmt(t *testing.T) {
},
statementList: []parser.SingleSQL{
{
- Text: "COPY tech_book FROM '/file/path/in/file/system'",
- Line: 1,
+ Text: "COPY tech_book FROM '/file/path/in/file/system'",
+ LastLine: 1,
},
},
},
@@ -1724,8 +1724,8 @@ func TestUnconvertStmt(t *testing.T) {
want: []ast.Node{&ast.UnconvertedStmt{}},
statementList: []parser.SingleSQL{
{
- Text: "SHOW TABLES",
- Line: 1,
+ Text: "SHOW TABLES",
+ LastLine: 1,
},
},
},
diff --git a/plugin/parser/set_line_for_create_table_test.go b/plugin/parser/set_line_for_create_table_test.go
index 6ca199d46b..ee304d60ac 100644
--- a/plugin/parser/set_line_for_create_table_test.go
+++ b/plugin/parser/set_line_for_create_table_test.go
@@ -1,6 +1,7 @@
package parser_test
import (
+ "strings"
"testing"
tidbparser "github.com/pingcap/tidb/parser"
@@ -36,10 +37,14 @@ func TestPGCreateTableSetLine(t *testing.T) {
-- it's a comment.
FOREIGN KEY (a, b, c) REFERENCES t1(a, b, c)
+
+
+
+
)
`,
columnLineList: []int{3, 3, 4, 5},
- constraintLineList: []int{6, 7, 8, 8, 10},
+ constraintLineList: []int{6, 7, 8, 8, 11},
},
{
// test for Windows.
@@ -90,13 +95,13 @@ func TestPGCreateTableSetLine(t *testing.T) {
require.Equal(t, len(test.columnLineList), len(node.ColumnList))
require.Equal(t, len(test.constraintLineList), len(node.ConstraintList))
for i, col := range node.ColumnList {
- require.Equal(t, col.Line(), test.columnLineList[i], i)
+ require.Equal(t, col.LastLine(), test.columnLineList[i], i)
for _, inlineCons := range col.ConstraintList {
- require.Equal(t, test.columnLineList[i], inlineCons.Line())
+ require.Equal(t, test.columnLineList[i], inlineCons.LastLine())
}
}
for i, cons := range node.ConstraintList {
- require.Equal(t, cons.Line(), test.constraintLineList[i], i)
+ require.Equal(t, cons.LastLine(), test.constraintLineList[i], i)
}
}
}
@@ -127,10 +132,14 @@ func TestMySQLCreateTableSetLine(t *testing.T) {
-- it's a comment.
FOREIGN KEY (a, b, c) REFERENCES t1(a, b, c)
+
+
+
+
)
`,
columnLineList: []int{3, 3, 4, 5},
- constraintLineList: []int{6, 7, 8, 9, 10, 10, 12},
+ constraintLineList: []int{6, 7, 8, 9, 10, 10, 13},
},
{
// test for Windows.
@@ -182,7 +191,7 @@ func TestMySQLCreateTableSetLine(t *testing.T) {
require.True(t, ok)
require.Equal(t, len(test.columnLineList), len(node.Cols))
require.Equal(t, len(test.constraintLineList), len(node.Constraints))
- node.SetOriginTextPosition(1)
+ node.SetOriginTextPosition(strings.Count(test.statement, "\n") + 1)
err = parser.SetLineForMySQLCreateTableStmt(node)
require.NoError(t, err)
for i, col := range node.Cols {
diff --git a/plugin/parser/split_multi_sql_test.go b/plugin/parser/split_multi_sql_test.go
index 77bb264ae2..c003ef3655 100644
--- a/plugin/parser/split_multi_sql_test.go
+++ b/plugin/parser/split_multi_sql_test.go
@@ -24,12 +24,12 @@ func TestPGSplitMultiSQL(t *testing.T) {
want: resData{
res: []SingleSQL{
{
- Text: "CREATE TABLE t(a int);",
- Line: 1,
+ Text: "CREATE TABLE t(a int);",
+ LastLine: 1,
},
{
- Text: "CREATE TABLE t1(a int)",
- Line: 1,
+ Text: "CREATE TABLE t1(a int)",
+ LastLine: 1,
},
},
},
@@ -40,12 +40,12 @@ func TestPGSplitMultiSQL(t *testing.T) {
want: resData{
res: []SingleSQL{
{
- Text: `CREATE TABLE "tech_Book"(id int, name varchar(255));`,
- Line: 1,
+ Text: `CREATE TABLE "tech_Book"(id int, name varchar(255));`,
+ LastLine: 1,
},
{
- Text: `INSERT INTO "tech_Book" VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\'jdfl;"ka');`,
- Line: 2,
+ Text: `INSERT INTO "tech_Book" VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\'jdfl;"ka');`,
+ LastLine: 2,
},
},
},
@@ -61,12 +61,12 @@ func TestPGSplitMultiSQL(t *testing.T) {
{
Text: `/* this is the comment. */
CREATE /* inline comment */TABLE "tech_Book"(id int, name varchar(255));`,
- Line: 2,
+ LastLine: 3,
},
{
Text: `-- this is the comment.
INSERT INTO "tech_Book" VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\'jdfl;"ka');`,
- Line: 4,
+ LastLine: 5,
},
},
},
@@ -92,11 +92,11 @@ func TestPGSplitMultiSQL(t *testing.T) {
-- this is the comment
INSERT INTO tbl VALUES ('fasf_bkdjlfa');
$$;`,
- Line: 1,
+ LastLine: 8,
},
{
- Text: `CREATE TABLE t(a int);`,
- Line: 9,
+ Text: `CREATE TABLE t(a int);`,
+ LastLine: 9,
},
},
},
@@ -122,11 +122,11 @@ func TestPGSplitMultiSQL(t *testing.T) {
-- this is the comment
INSERT INTO tbl VALUES ('fasf_bkdjlfa');
$tag_name$;`,
- Line: 1,
+ LastLine: 8,
},
{
- Text: `CREATE TABLE t(a int);`,
- Line: 9,
+ Text: `CREATE TABLE t(a int);`,
+ LastLine: 9,
},
},
},
@@ -137,12 +137,12 @@ func TestPGSplitMultiSQL(t *testing.T) {
want: resData{
res: []SingleSQL{
{
- Text: "CREATE TABLE t\r\n(a int);",
- Line: 1,
+ Text: "CREATE TABLE t\r\n(a int);",
+ LastLine: 2,
},
{
- Text: "CREATE TABLE t1(b int);",
- Line: 3,
+ Text: "CREATE TABLE t1(b int);",
+ LastLine: 3,
},
},
},
@@ -161,7 +161,7 @@ func TestPGSplitMultiSQL(t *testing.T) {
(12,'table column name () { :xna,sydfn,,kasdfyn;}; /////test string/// 0'),
(133,'knuandfan public table id\';create table t(a int, b int);set @text=\'\\\\kdaminxkljasdfiebkla.unkonwn\'+\'abcdef.xyz\\\'; local xxxyy.abcddd.mysql @text;------- '),
(1444,'table t xyz abc a\'a\\\\\\\\\'b"c>?>xxxxxx%}}%%>c<[[?${12344556778990{%}}cake\\');`,
- Line: 1,
+ LastLine: 5,
},
},
},
@@ -216,12 +216,12 @@ func TestMySQLSplitMultiSQL(t *testing.T) {
want: resData{
res: []SingleSQL{
{
- Text: "CREATE TABLE t(a int);",
- Line: 1,
+ Text: "CREATE TABLE t(a int);",
+ LastLine: 1,
},
{
- Text: "CREATE TABLE t1(a int)",
- Line: 1,
+ Text: "CREATE TABLE t1(a int)",
+ LastLine: 1,
},
},
},
@@ -232,12 +232,12 @@ func TestMySQLSplitMultiSQL(t *testing.T) {
want: resData{
res: []SingleSQL{
{
- Text: "CREATE TABLE `tech_Book`(id int, name varchar(255));",
- Line: 1,
+ Text: "CREATE TABLE `tech_Book`(id int, name varchar(255));",
+ LastLine: 1,
},
{
- Text: "INSERT INTO `tech_Book` VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\\'jdfl;\"ka');",
- Line: 2,
+ Text: "INSERT INTO `tech_Book` VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\\'jdfl;\"ka');",
+ LastLine: 2,
},
},
},
@@ -255,17 +255,17 @@ func TestMySQLSplitMultiSQL(t *testing.T) {
{
Text: `/* this is the comment. */
CREATE /* inline comment */TABLE tech_Book(id int, name varchar(255));`,
- Line: 2,
+ LastLine: 3,
},
{
Text: `-- this is the comment.
INSERT INTO tech_Book VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\'jdfl;"ka');`,
- Line: 4,
+ LastLine: 5,
},
{
Text: `# this is the comment.
INSERT INTO tech_Book VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\'jdfl;"ka');`,
- Line: 6,
+ LastLine: 7,
},
},
},
@@ -288,7 +288,7 @@ func TestMySQLSplitMultiSQL(t *testing.T) {
{
Text: `# test for defining stored programs
delimiter //`,
- Line: 1,
+ LastLine: 2,
},
{
Text: `CREATE PROCEDURE dorepeat(p1 INT)
@@ -297,19 +297,19 @@ func TestMySQLSplitMultiSQL(t *testing.T) {
REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
END
//`,
- Line: 3,
+ LastLine: 8,
},
{
- Text: `delimiter ;`,
- Line: 9,
+ Text: `delimiter ;`,
+ LastLine: 9,
},
{
- Text: `CALL dorepeat(1000);`,
- Line: 10,
+ Text: `CALL dorepeat(1000);`,
+ LastLine: 10,
},
{
- Text: `SELECT @x;`,
- Line: 11,
+ Text: `SELECT @x;`,
+ LastLine: 11,
},
},
},
@@ -333,7 +333,7 @@ func TestMySQLSplitMultiSQL(t *testing.T) {
{
Text: `# test for defining stored programs
delimiter //`,
- Line: 1,
+ LastLine: 2,
},
{
Text: `CREATE PROCEDURE dorepeat(p1 INT)
@@ -343,19 +343,19 @@ func TestMySQLSplitMultiSQL(t *testing.T) {
REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
END
//`,
- Line: 3,
+ LastLine: 9,
},
{
- Text: `delimiter ;`,
- Line: 10,
+ Text: `delimiter ;`,
+ LastLine: 10,
},
{
- Text: `CALL dorepeat(1000);`,
- Line: 11,
+ Text: `CALL dorepeat(1000);`,
+ LastLine: 11,
},
{
- Text: `SELECT @x;`,
- Line: 12,
+ Text: `SELECT @x;`,
+ LastLine: 12,
},
},
},
@@ -366,12 +366,12 @@ func TestMySQLSplitMultiSQL(t *testing.T) {
want: resData{
res: []SingleSQL{
{
- Text: "CREATE TABLE t\r\n(a int);",
- Line: 1,
+ Text: "CREATE TABLE t\r\n(a int);",
+ LastLine: 2,
},
{
- Text: "CREATE TABLE t1(b int);",
- Line: 3,
+ Text: "CREATE TABLE t1(b int);",
+ LastLine: 3,
},
},
},
diff --git a/plugin/parser/tokenizer.go b/plugin/parser/tokenizer.go
index 3d744e1889..585c111746 100644
--- a/plugin/parser/tokenizer.go
+++ b/plugin/parser/tokenizer.go
@@ -24,11 +24,10 @@ var (
)
type tokenizer struct {
- buffer []rune
- cursor uint
- len uint
- line int
- startLine int
+ buffer []rune
+ cursor uint
+ len uint
+ line int
// steaming API specific field
scanner *bufio.Scanner
@@ -40,10 +39,9 @@ type tokenizer struct {
// Notice: we append an additional eofRune in the statement. This is a sentinel rune.
func newTokenizer(statement string) *tokenizer {
t := &tokenizer{
- buffer: []rune(statement),
- cursor: 0,
- line: 1,
- startLine: 1,
+ buffer: []rune(statement),
+ cursor: 0,
+ line: 1,
}
t.len = uint(len(t.buffer))
// append an additional eofRune.
@@ -70,11 +68,10 @@ func scanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
func newStreamTokenizer(src io.Reader, f func(string) error) *tokenizer {
t := &tokenizer{
- cursor: 0,
- line: 1,
- startLine: 1,
- scanner: bufio.NewScanner(src),
- f: f,
+ cursor: 0,
+ line: 1,
+ scanner: bufio.NewScanner(src),
+ f: f,
}
t.scanner.Split(scanLines)
if t.scanner.Scan() {
@@ -87,7 +84,7 @@ func newStreamTokenizer(src io.Reader, f func(string) error) *tokenizer {
return t
}
-func (t *tokenizer) setLineForMySQLCreateTableStmt(node *tidbast.CreateTableStmt) error {
+func (t *tokenizer) setLineForMySQLCreateTableStmt(node *tidbast.CreateTableStmt, firstLine int) error {
// We assume that the parser will parse the columns and table constraints according to the order of the raw SQL statements
// and the identifiers don't equal any keywords in CREATE TABLE statements.
// If it breaks our assumption, we set the line for columns and table constraints to the first line of the CREATE TABLE statement.
@@ -109,7 +106,6 @@ func (t *tokenizer) setLineForMySQLCreateTableStmt(node *tidbast.CreateTableStmt
parentheses := 1
t.skipBlank()
startPos := t.pos()
- t.startLine = t.line
for {
switch {
case t.char(0) == '\n':
@@ -146,10 +142,29 @@ func (t *tokenizer) setLineForMySQLCreateTableStmt(node *tidbast.CreateTableStmt
def := strings.ToLower(t.getString(startPos, t.pos()-startPos))
if columnPos < len(node.Cols) &&
strings.Contains(def, node.Cols[columnPos].Name.Name.L) {
- node.Cols[columnPos].SetOriginTextPosition(t.startLine + node.OriginTextPosition() - 1)
+ // Consider this text:
+ // CREATE TABLE t(
+ // a int
+ // )
+ //
+ // Our current location is ')'.
+ // The line (t.line + firstLine - 1) is the line of ')',
+ // but we want to get the line of 'a int'.
+ // So we need minus the aboveNonBlankLineDistance.
+ node.Cols[columnPos].SetOriginTextPosition(t.line + firstLine - 1 - t.aboveNonBlankLineDistance())
} else if constraintPos < len(node.Constraints) &&
matchMySQLTableConstraint(def, node.Constraints[constraintPos]) {
- node.Constraints[constraintPos].SetOriginTextPosition(t.startLine + node.OriginTextPosition() - 1)
+ // Consider this text:
+ // CREATE TABLE t(
+ // a int,
+ // UNIQUE (a)
+ // )
+ //
+ // Our current location is ')'.
+ // The line (t.line + firstLine - 1) is the line of ')',
+ // but we want to get the line of 'UNIQUE (a)'.
+ // So we need minus the aboveNonBlankLineDistance.
+ node.Constraints[constraintPos].SetOriginTextPosition(t.line + firstLine - 1 - t.aboveNonBlankLineDistance())
}
return nil
}
@@ -169,17 +184,16 @@ func (t *tokenizer) setLineForMySQLCreateTableStmt(node *tidbast.CreateTableStmt
def := strings.ToLower(t.getString(startPos, t.pos()-startPos))
if columnPos < len(node.Cols) &&
strings.Contains(def, node.Cols[columnPos].Name.Name.L) {
- node.Cols[columnPos].SetOriginTextPosition(t.startLine + node.OriginTextPosition() - 1)
+ node.Cols[columnPos].SetOriginTextPosition(t.line + firstLine - 1)
columnPos++
} else if constraintPos < len(node.Constraints) &&
matchMySQLTableConstraint(def, node.Constraints[constraintPos]) {
- node.Constraints[constraintPos].SetOriginTextPosition(t.startLine + node.OriginTextPosition() - 1)
+ node.Constraints[constraintPos].SetOriginTextPosition(t.line + firstLine - 1)
constraintPos++
}
t.skip(1)
t.skipBlank()
startPos = t.pos()
- t.startLine = t.line
case t.char(0) == eofRune:
return nil
default:
@@ -213,18 +227,18 @@ func matchMySQLTableConstraint(text string, cons *tidbast.Constraint) bool {
}
// setLineForPGCreateTableStmt sets the line for columns and table constraints in CREATE TABLE statements.
-func (t *tokenizer) setLineForPGCreateTableStmt(node *ast.CreateTableStmt) error {
+func (t *tokenizer) setLineForPGCreateTableStmt(node *ast.CreateTableStmt, firstLine int) error {
// We assume that the parser will parse the columns and table constraints according to the order of the raw SQL statements
// and the identifiers don't equal any keywords in CREATE TABLE statements.
// If it breaks our assumption, we set the line for columns and table constraints to the first line of the CREATE TABLE statement.
for _, col := range node.ColumnList {
- col.SetLine(node.Line())
+ col.SetLastLine(node.LastLine())
for _, inlineCons := range col.ConstraintList {
- inlineCons.SetLine(node.Line())
+ inlineCons.SetLastLine(node.LastLine())
}
}
for _, cons := range node.ConstraintList {
- cons.SetLine(node.Line())
+ cons.SetLastLine(node.LastLine())
}
columnPos := 0
@@ -238,7 +252,6 @@ func (t *tokenizer) setLineForPGCreateTableStmt(node *ast.CreateTableStmt) error
parentheses := 1
t.skipBlank()
startPos := t.pos()
- t.startLine = t.line
for {
switch {
case t.char(0) == '\n':
@@ -275,13 +288,32 @@ func (t *tokenizer) setLineForPGCreateTableStmt(node *ast.CreateTableStmt) error
def := strings.ToLower(t.getString(startPos, t.pos()-startPos))
if columnPos < len(node.ColumnList) &&
strings.Contains(def, strings.ToLower(node.ColumnList[columnPos].ColumnName)) {
- node.ColumnList[columnPos].SetLine(t.startLine + node.Line() - 1)
+ // Consider this text:
+ // CREATE TABLE t(
+ // a int
+ // )
+ //
+ // Our current location is ')'.
+ // The line (t.line + firstLine - 1) is the line of ')',
+ // but we want to get the line of 'a int'.
+ // So we need minus the aboveNonBlankLineDistance.
+ node.ColumnList[columnPos].SetLastLine(t.line + firstLine - 1 - t.aboveNonBlankLineDistance())
for _, inlineConstraint := range node.ColumnList[columnPos].ConstraintList {
- inlineConstraint.SetLine(node.ColumnList[columnPos].Line())
+ inlineConstraint.SetLastLine(node.ColumnList[columnPos].LastLine())
}
} else if constraintPos < len(node.ConstraintList) &&
matchTableConstraint(def, node.ConstraintList[constraintPos]) {
- node.ConstraintList[constraintPos].SetLine(t.startLine + node.Line() - 1)
+ // Consider this text:
+ // CREATE TABLE t(
+ // a int,
+ // UNIQUE (a)
+ // )
+ //
+ // Our current location is ')'.
+ // The line (t.line + firstLine - 1) is the line of ')',
+ // but we want to get the line of 'UNIQUE (a)'.
+ // So we need minus the aboveNonBlankLineDistance.
+ node.ConstraintList[constraintPos].SetLastLine(t.line + firstLine - 1 - t.aboveNonBlankLineDistance())
}
return nil
}
@@ -301,20 +333,19 @@ func (t *tokenizer) setLineForPGCreateTableStmt(node *ast.CreateTableStmt) error
def := strings.ToLower(t.getString(startPos, t.pos()-startPos))
if columnPos < len(node.ColumnList) &&
strings.Contains(def, strings.ToLower(node.ColumnList[columnPos].ColumnName)) {
- node.ColumnList[columnPos].SetLine(t.startLine + node.Line() - 1)
+ node.ColumnList[columnPos].SetLastLine(t.line + firstLine - 1)
for _, inlineConstraint := range node.ColumnList[columnPos].ConstraintList {
- inlineConstraint.SetLine(node.ColumnList[columnPos].Line())
+ inlineConstraint.SetLastLine(node.ColumnList[columnPos].LastLine())
}
columnPos++
} else if constraintPos < len(node.ConstraintList) &&
matchTableConstraint(def, node.ConstraintList[constraintPos]) {
- node.ConstraintList[constraintPos].SetLine(t.startLine + node.Line() - 1)
+ node.ConstraintList[constraintPos].SetLastLine(t.line + firstLine - 1)
constraintPos++
}
t.skip(1)
t.skipBlank()
startPos = t.pos()
- t.startLine = t.line
case t.char(0) == eofRune:
return nil
default:
@@ -323,6 +354,20 @@ func (t *tokenizer) setLineForPGCreateTableStmt(node *ast.CreateTableStmt) error
}
}
+func (t *tokenizer) aboveNonBlankLineDistance() int {
+ pos := uint(1)
+ dis := 0
+ for {
+ c := t.preChar(pos)
+ if c == '\n' {
+ dis++
+ } else if !emptyRune(c) {
+ return dis
+ }
+ pos++
+ }
+}
+
// matchTableConstraint matches text as lowercase.
func matchTableConstraint(text string, cons *ast.ConstraintDef) bool {
text = strings.ToLower(text)
@@ -348,7 +393,6 @@ func (t *tokenizer) splitMySQLMultiSQL() ([]SingleSQL, error) {
delimiter := []rune{';'}
t.skipBlank()
- t.startLine = t.line
startPos := t.cursor
for {
switch {
@@ -358,7 +402,19 @@ func (t *tokenizer) splitMySQLMultiSQL() ([]SingleSQL, error) {
if t.f == nil {
res = append(res, SingleSQL{
Text: s,
- Line: t.startLine,
+ // Consider this text:
+ // CREATE TABLE t(
+ // a int
+ // )
+ //
+ // EOF line
+ //
+ // Our current location is the EOF line.
+ // The line t.line is the line of ')',
+ // but we want to get the line of last line of the SQL
+ // which means the line of ')'.
+ // So we need minus the aboveNonBlankLineDistance.
+ LastLine: t.line - t.aboveNonBlankLineDistance(),
})
}
if err := t.processStreaming(s); err != nil {
@@ -371,15 +427,14 @@ func (t *tokenizer) splitMySQLMultiSQL() ([]SingleSQL, error) {
text := t.getString(startPos, t.pos()-startPos)
if t.f == nil {
res = append(res, SingleSQL{
- Text: text,
- Line: t.startLine,
+ Text: text,
+ LastLine: t.line,
})
}
t.skipBlank()
if err := t.processStreaming(text); err != nil {
return nil, err
}
- t.startLine = t.line
startPos = t.pos()
// deal with the DELIMITER statement, see https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html
case t.equalWordCaseInsensitive(delimiterRuneList):
@@ -391,15 +446,14 @@ func (t *tokenizer) splitMySQLMultiSQL() ([]SingleSQL, error) {
text := t.getString(startPos, t.pos()-startPos)
if t.f == nil {
res = append(res, SingleSQL{
- Text: text,
- Line: t.startLine,
+ Text: text,
+ LastLine: t.line,
})
}
t.skipBlank()
if err := t.processStreaming(text); err != nil {
return nil, err
}
- t.startLine = t.line
startPos = t.pos()
case t.char(0) == '/' && t.char(1) == '*':
if err := t.scanComment(); err != nil {
@@ -450,7 +504,6 @@ func (t *tokenizer) splitPostgreSQLMultiSQL() ([]SingleSQL, error) {
var res []SingleSQL
t.skipBlank()
- t.startLine = t.line
startPos := t.cursor
for {
switch {
@@ -479,15 +532,14 @@ func (t *tokenizer) splitPostgreSQLMultiSQL() ([]SingleSQL, error) {
text := t.getString(startPos, t.pos()-startPos)
if t.f == nil {
res = append(res, SingleSQL{
- Text: text,
- Line: t.startLine,
+ Text: text,
+ LastLine: t.line,
})
}
t.skipBlank()
if err := t.processStreaming(text); err != nil {
return nil, err
}
- t.startLine = t.line
startPos = t.pos()
case t.char(0) == eofRune:
s := t.getString(startPos, t.pos())
@@ -495,7 +547,19 @@ func (t *tokenizer) splitPostgreSQLMultiSQL() ([]SingleSQL, error) {
if t.f == nil {
res = append(res, SingleSQL{
Text: s,
- Line: t.startLine,
+ // Consider this text:
+ // CREATE TABLE t(
+ // a int
+ // )
+ //
+ // EOF line
+ //
+ // Our current location is the EOF line.
+ // The line t.line is the line of ')',
+ // but we want to get the line of last line of the SQL
+ // which means the line of ')'.
+ // So we need minus the aboveNonBlankLineDistance.
+ LastLine: t.line - t.aboveNonBlankLineDistance(),
})
}
if err := t.processStreaming(s); err != nil {
@@ -728,6 +792,14 @@ func (t *tokenizer) char(after uint) rune {
return t.buffer[t.cursor+after]
}
+func (t *tokenizer) preChar(before uint) rune {
+ if t.cursor < before {
+ return eofRune
+ }
+
+ return t.buffer[t.cursor-before]
+}
+
func (t *tokenizer) skip(step uint) {
t.cursor += step
for t.cursor > t.len && t.scanner != nil {
@@ -796,7 +868,7 @@ func (t *tokenizer) equalWordCaseInsensitive(word []rune) bool {
}
func emptyRune(r rune) bool {
- return r != ' ' && r != '\n' && r != '\t' && r != '\r'
+ return r == ' ' || r == '\n' || r == '\t' || r == '\r'
}
func emptyString(s string) bool {
diff --git a/plugin/parser/utils.go b/plugin/parser/utils.go
index d146f2bcc9..d40eef8ce3 100644
--- a/plugin/parser/utils.go
+++ b/plugin/parser/utils.go
@@ -2,6 +2,7 @@ package parser
import (
"io"
+ "strings"
tidbast "github.com/pingcap/tidb/parser/ast"
"github.com/pkg/errors"
@@ -11,8 +12,8 @@ import (
// SingleSQL is a separate SQL split from multi-SQL.
type SingleSQL struct {
- Text string
- Line int
+ Text string
+ LastLine int
}
// SplitMultiSQL splits statement into a slice of the single SQL.
@@ -48,7 +49,8 @@ func SetLineForCreateTableStmt(engineType EngineType, node *ast.CreateTableStmt)
switch engineType {
case Postgres:
t := newTokenizer(node.Text())
- return t.setLineForPGCreateTableStmt(node)
+ firstLine := node.LastLine() - strings.Count(node.Text(), "\n")
+ return t.setLineForPGCreateTableStmt(node, firstLine)
default:
return errors.Errorf("engine type is not supported: %s", engineType)
}
@@ -62,5 +64,6 @@ func SetLineForMySQLCreateTableStmt(node *tidbast.CreateTableStmt) error {
if len(node.Cols) == 0 {
return nil
}
- return newTokenizer(node.Text()).setLineForMySQLCreateTableStmt(node)
+ firstLine := node.OriginTextPosition() - strings.Count(node.Text(), "\n")
+ return newTokenizer(node.Text()).setLineForMySQLCreateTableStmt(node, firstLine)
}