Skip to content

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

refactor: move set line

d-bytebasee028686e91d9
diff --git a/backend/plugin/advisor/catalog/walk_through.go b/backend/plugin/advisor/catalog/walk_through.go
index 843e2cf4fa..e9229116aa 100644
--- a/backend/plugin/advisor/catalog/walk_through.go
+++ b/backend/plugin/advisor/catalog/walk_through.go
@@ -16,7 +16,7 @@ import (
"github.com/bytebase/bytebase/backend/plugin/advisor/db"
mysqlbbparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
- parser "github.com/bytebase/bytebase/backend/plugin/parser/sql"
+ tidbbbparser "github.com/bytebase/bytebase/backend/plugin/parser/tidb"
)
// WalkThroughErrorType is the type of WalkThroughError.
@@ -1354,7 +1354,7 @@ func (*DatabaseState) parse(statement string) ([]tidbast.StmtNode, *WalkThroughE
node.SetText(nil, text)
node.SetOriginTextPosition(lastLine)
if n, ok := node.(*tidbast.CreateTableStmt); ok {
- if err := parser.SetLineForMySQLCreateTableStmt(n); err != nil {
+ if err := tidbbbparser.SetLineForMySQLCreateTableStmt(n); err != nil {
return nil, NewParseError(err.Error())
}
}
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 4dfa346a8d..0e97a907f5 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -25,6 +25,7 @@ import (
snowsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
parser "github.com/bytebase/bytebase/backend/plugin/parser/sql"
"github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
+ tidbbbparser "github.com/bytebase/bytebase/backend/plugin/parser/tidb"
tsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/tsql"
// register pg parser.
@@ -714,7 +715,7 @@ func mysqlSyntaxCheck(statement string) (any, []Advice) {
node.SetText(nil, item.Text)
node.SetOriginTextPosition(item.LastLine)
if n, ok := node.(*tidbast.CreateTableStmt); ok {
- if err := parser.SetLineForMySQLCreateTableStmt(n); err != nil {
+ if err := tidbbbparser.SetLineForMySQLCreateTableStmt(n); err != nil {
return nil, append(adviceList, Advice{
Status: Error,
Code: Internal,
diff --git a/backend/plugin/parser/sql/engine/pg/convert.go b/backend/plugin/parser/sql/engine/pg/convert.go
index 36b3862de8..8695a8ec6a 100644
--- a/backend/plugin/parser/sql/engine/pg/convert.go
+++ b/backend/plugin/parser/sql/engine/pg/convert.go
@@ -10,6 +10,7 @@ import (
"github.com/bytebase/bytebase/backend/plugin/parser/base"
parser "github.com/bytebase/bytebase/backend/plugin/parser/sql"
+ "github.com/bytebase/bytebase/backend/plugin/parser/tokenizer"
"github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
)
@@ -22,7 +23,7 @@ func convert(node *pgquery.Node, statement base.SingleSQL) (res ast.Node, err er
res.SetLastLine(statement.LastLine)
switch n := res.(type) {
case *ast.CreateTableStmt:
- err = parser.SetLineForCreateTableStmt(parser.Postgres, n)
+ err = setLineForCreateTableStmt(n)
case *ast.AlterTableStmt:
for _, item := range n.AlterItemList {
item.SetLastLine(n.LastLine())
@@ -799,6 +800,13 @@ func convert(node *pgquery.Node, statement base.SingleSQL) (res ast.Node, err er
return nil, nil
}
+// setLineForCreateTableStmt sets the line for columns and table constraints in CREATE TABLE statements.
+func setLineForCreateTableStmt(node *ast.CreateTableStmt) error {
+ t := tokenizer.NewTokenizer(node.Text())
+ firstLine := node.LastLine() - strings.Count(node.Text(), "\n")
+ return t.SetLineForPGCreateTableStmt(node, firstLine)
+}
+
func convertEnumLabelList(list []*pgquery.Node) ([]string, error) {
var result []string
for _, node := range list {
diff --git a/backend/plugin/parser/sql/engine/pg/convert_test.go b/backend/plugin/parser/sql/engine/pg/convert_test.go
index 71f24bee6a..3706aae6d1 100644
--- a/backend/plugin/parser/sql/engine/pg/convert_test.go
+++ b/backend/plugin/parser/sql/engine/pg/convert_test.go
@@ -3192,3 +3192,93 @@ func TestRenameSchema(t *testing.T) {
runTests(t, tests)
}
+
+type setLineTestData struct {
+ statement string
+ columnLineList []int
+ constraintLineList []int
+}
+
+// TODO(d): fix this test with setLineForCreateTableStmt().
+func TestPGCreateTableSetLine(t *testing.T) {
+ tests := []setLineTestData{
+ {
+ statement: `
+ CREATE TABLE t(
+ a int, B int,
+ C int,
+ "D" int NOT NULL,
+ CONSTRAINT unique_a UNIQUE (a),
+ UNIQUE (b, c),
+ PRIMARY KEY (d),CHECK (a > 0),
+
+ -- 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, 11},
+ },
+ {
+ // test for Windows.
+ statement: "\r\n" +
+ "CREATE TABLE t(" + "\r\n" +
+ "a int, B int," + "\r\n" +
+ "C int," + "\r\n" +
+ `"D" int NOT NULL,` + "\r\n" +
+ "CONSTRAINT unique_a UNIQUE (a)," + "\r\n" +
+ "UNIQUE (b, c)," + "\r\n" +
+ "PRIMARY KEY (d),CHECK (a > 0)," + "\r\n" +
+ "\r\n" +
+ "FOREIGN KEY (a, b, c) REFERENCES t1(a, b, c)" + "\r\n" +
+ ")",
+ columnLineList: []int{3, 3, 4, 5},
+ constraintLineList: []int{6, 7, 8, 8, 10},
+ },
+ {
+ statement: `
+ CREATE TABLE t(
+ a int PRIMARY KEY,
+ b int CHECK(b>1), c int UNIQUE
+ )
+ `,
+ columnLineList: []int{3, 4, 4},
+ constraintLineList: []int{},
+ },
+ {
+ statement: `-- complex example
+ CREATE TABLE t(
+ a int PRIMARY KEY,
+ name varchar(255) DEFAULT 'UNIQUE on (a, b, c)(',
+ UNIQUE(a, name),
+ UNIQUE(name)
+ )
+ `,
+ columnLineList: []int{3, 4},
+ constraintLineList: []int{5, 6},
+ },
+ }
+
+ for _, test := range tests {
+ nodeList, err := parser.Parse(parser.Postgres, parser.ParseContext{}, test.statement)
+ require.NoError(t, err)
+ require.Len(t, nodeList, 1)
+ node, ok := nodeList[0].(*ast.CreateTableStmt)
+ require.True(t, ok)
+ 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.LastLine(), test.columnLineList[i], i)
+ for _, inlineCons := range col.ConstraintList {
+ require.Equal(t, test.columnLineList[i], inlineCons.LastLine())
+ }
+ }
+ for i, cons := range node.ConstraintList {
+ require.Equal(t, cons.LastLine(), test.constraintLineList[i], i)
+ }
+ }
+}
diff --git a/backend/plugin/parser/sql/utils.go b/backend/plugin/parser/sql/utils.go
index 3d4e9c4308..fbb0125580 100644
--- a/backend/plugin/parser/sql/utils.go
+++ b/backend/plugin/parser/sql/utils.go
@@ -8,7 +8,6 @@ import (
"regexp"
"strings"
- tidbast "github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pkg/errors"
@@ -17,7 +16,6 @@ import (
pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
plsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/plsql"
snowparser "github.com/bytebase/bytebase/backend/plugin/parser/snowflake"
- "github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
tidbparser "github.com/bytebase/bytebase/backend/plugin/parser/tidb"
"github.com/bytebase/bytebase/backend/plugin/parser/tokenizer"
tsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/tsql"
@@ -236,30 +234,6 @@ func SplitMultiSQLStream(engineType EngineType, src io.Reader, f func(string) er
return result, nil
}
-// SetLineForCreateTableStmt sets the line for columns and table constraints in CREATE TABLE statements.
-func SetLineForCreateTableStmt(engineType EngineType, node *ast.CreateTableStmt) error {
- switch engineType {
- case Postgres:
- t := tokenizer.NewTokenizer(node.Text())
- firstLine := node.LastLine() - strings.Count(node.Text(), "\n")
- return t.SetLineForPGCreateTableStmt(node, firstLine)
- default:
- return errors.Errorf("engine type is not supported: %s", engineType)
- }
-}
-
-// SetLineForMySQLCreateTableStmt sets the line for columns and table constraints in MySQL CREATE TABLE statments.
-// This is a temporary function. Because we do not convert tidb AST to our AST. So we have to implement this.
-// TODO(rebelice): remove it.
-func SetLineForMySQLCreateTableStmt(node *tidbast.CreateTableStmt) error {
- // exclude CREATE TABLE ... AS and CREATE TABLE ... LIKE statement.
- if len(node.Cols) == 0 {
- return nil
- }
- firstLine := node.OriginTextPosition() - strings.Count(node.Text(), "\n")
- return tokenizer.NewTokenizer(node.Text()).SetLineForMySQLCreateTableStmt(node, firstLine)
-}
-
// ExtractTiDBUnsupportedStmts returns a list of unsupported statements in TiDB extracted from the `stmts`,
// and returns the remaining statements supported by TiDB from `stmts`.
func ExtractTiDBUnsupportedStmts(stmts string) ([]string, string, error) {
diff --git a/backend/plugin/parser/tidb/tidb.go b/backend/plugin/parser/tidb/tidb.go
index 09ebc89470..6016c300ab 100644
--- a/backend/plugin/parser/tidb/tidb.go
+++ b/backend/plugin/parser/tidb/tidb.go
@@ -1,8 +1,12 @@
package tidb
import (
+ "strings"
+
tidbparser "github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
+
+ "github.com/bytebase/bytebase/backend/plugin/parser/tokenizer"
)
// ParseTiDB parses the given SQL statement and returns the AST.
@@ -16,3 +20,15 @@ func ParseTiDB(sql string, charset string, collation string) ([]ast.StmtNode, er
nodes, _, err := p.Parse(sql, charset, collation)
return nodes, err
}
+
+// SetLineForMySQLCreateTableStmt sets the line for columns and table constraints in MySQL CREATE TABLE statments.
+// This is a temporary function. Because we do not convert tidb AST to our AST. So we have to implement this.
+// TODO(rebelice): remove it.
+func SetLineForMySQLCreateTableStmt(node *ast.CreateTableStmt) error {
+ // exclude CREATE TABLE ... AS and CREATE TABLE ... LIKE statement.
+ if len(node.Cols) == 0 {
+ return nil
+ }
+ firstLine := node.OriginTextPosition() - strings.Count(node.Text(), "\n")
+ return tokenizer.NewTokenizer(node.Text()).SetLineForMySQLCreateTableStmt(node, firstLine)
+}
diff --git a/backend/plugin/parser/sql/set_line_for_create_table_test.go b/backend/plugin/parser/tidb/tidb_test.go
similarity index 52%
rename from backend/plugin/parser/sql/set_line_for_create_table_test.go
rename to backend/plugin/parser/tidb/tidb_test.go
index d6dd4b8016..8e3cea6332 100644
--- a/backend/plugin/parser/sql/set_line_for_create_table_test.go
+++ b/backend/plugin/parser/tidb/tidb_test.go
@@ -1,4 +1,4 @@
-package parser_test
+package tidb
import (
"strings"
@@ -7,15 +7,6 @@ import (
tidbparser "github.com/pingcap/tidb/parser"
tidbast "github.com/pingcap/tidb/parser/ast"
"github.com/stretchr/testify/require"
-
- parser "github.com/bytebase/bytebase/backend/plugin/parser/sql"
-
- "github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
-
- // Register postgres parser driver.
- _ "github.com/bytebase/bytebase/backend/plugin/parser/sql/engine/pg"
- // Register pingcap parser driver.
- _ "github.com/pingcap/tidb/types/parser_driver"
)
type setLineTestData struct {
@@ -24,89 +15,6 @@ type setLineTestData struct {
constraintLineList []int
}
-func TestPGCreateTableSetLine(t *testing.T) {
- tests := []setLineTestData{
- {
- statement: `
- CREATE TABLE t(
- a int, B int,
- C int,
- "D" int NOT NULL,
- CONSTRAINT unique_a UNIQUE (a),
- UNIQUE (b, c),
- PRIMARY KEY (d),CHECK (a > 0),
-
- -- 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, 11},
- },
- {
- // test for Windows.
- statement: "\r\n" +
- "CREATE TABLE t(" + "\r\n" +
- "a int, B int," + "\r\n" +
- "C int," + "\r\n" +
- `"D" int NOT NULL,` + "\r\n" +
- "CONSTRAINT unique_a UNIQUE (a)," + "\r\n" +
- "UNIQUE (b, c)," + "\r\n" +
- "PRIMARY KEY (d),CHECK (a > 0)," + "\r\n" +
- "\r\n" +
- "FOREIGN KEY (a, b, c) REFERENCES t1(a, b, c)" + "\r\n" +
- ")",
- columnLineList: []int{3, 3, 4, 5},
- constraintLineList: []int{6, 7, 8, 8, 10},
- },
- {
- statement: `
- CREATE TABLE t(
- a int PRIMARY KEY,
- b int CHECK(b>1), c int UNIQUE
- )
- `,
- columnLineList: []int{3, 4, 4},
- constraintLineList: []int{},
- },
- {
- statement: `-- complex example
- CREATE TABLE t(
- a int PRIMARY KEY,
- name varchar(255) DEFAULT 'UNIQUE on (a, b, c)(',
- UNIQUE(a, name),
- UNIQUE(name)
- )
- `,
- columnLineList: []int{3, 4},
- constraintLineList: []int{5, 6},
- },
- }
-
- for _, test := range tests {
- nodeList, err := parser.Parse(parser.Postgres, parser.ParseContext{}, test.statement)
- require.NoError(t, err)
- require.Len(t, nodeList, 1)
- node, ok := nodeList[0].(*ast.CreateTableStmt)
- require.True(t, ok)
- 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.LastLine(), test.columnLineList[i], i)
- for _, inlineCons := range col.ConstraintList {
- require.Equal(t, test.columnLineList[i], inlineCons.LastLine())
- }
- }
- for i, cons := range node.ConstraintList {
- require.Equal(t, cons.LastLine(), test.constraintLineList[i], i)
- }
- }
-}
-
func TestMySQLCreateTableSetLine(t *testing.T) {
tests := []setLineTestData{
{
@@ -193,7 +101,7 @@ func TestMySQLCreateTableSetLine(t *testing.T) {
require.Equal(t, len(test.columnLineList), len(node.Cols))
require.Equal(t, len(test.constraintLineList), len(node.Constraints))
node.SetOriginTextPosition(strings.Count(test.statement, "\n") + 1)
- err = parser.SetLineForMySQLCreateTableStmt(node)
+ err = SetLineForMySQLCreateTableStmt(node)
require.NoError(t, err)
for i, col := range node.Cols {
require.Equal(t, col.OriginTextPosition(), test.columnLineList[i], i)