Skip to content

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

refactor: remove unused code (#15974)

h3n4l9c729992dd42
diff --git a/backend/plugin/parser/mysql/mysql.go b/backend/plugin/parser/mysql/mysql.go
index c102010e63..df331660a1 100644
--- a/backend/plugin/parser/mysql/mysql.go
+++ b/backend/plugin/parser/mysql/mysql.go
@@ -23,8 +23,8 @@ type ParseResult struct {
}
// ParseMySQL parses the given SQL statement and returns the AST.
-func ParseMySQL(statement string, options ...tokenizer.Option) ([]*ParseResult, error) {
- statement, err := DealWithDelimiter(statement, options...)
+func ParseMySQL(statement string) ([]*ParseResult, error) {
+ statement, err := DealWithDelimiter(statement)
if err != nil {
return nil, err
}
@@ -37,8 +37,8 @@ func ParseMySQL(statement string, options ...tokenizer.Option) ([]*ParseResult,
}
// DealWithDelimiter converts the delimiter statement to comment, also converts the following statement's delimiter to semicolon(`;`).
-func DealWithDelimiter(statement string, options ...tokenizer.Option) (string, error) {
- has, list, err := hasDelimiter(statement, options...)
+func DealWithDelimiter(statement string) (string, error) {
+ has, list, err := hasDelimiter(statement)
if err != nil {
return "", err
}
@@ -275,9 +275,9 @@ func ExtractDelimiter(stmt string) (string, error) {
return "", errors.Errorf("cannot extract delimiter from %q", stmt)
}
-func hasDelimiter(statement string, options ...tokenizer.Option) (bool, []base.SingleSQL, error) {
+func hasDelimiter(statement string) (bool, []base.SingleSQL, error) {
// use splitTiDBMultiSQL to check if the statement has delimiter
- t := tokenizer.NewTokenizer(statement, options...)
+ t := tokenizer.NewTokenizer(statement)
list, err := t.SplitTiDBMultiSQL()
if err != nil {
return false, nil, errors.Errorf("failed to split multi sql: %v", err)
diff --git a/backend/plugin/parser/tidb/tidb.go b/backend/plugin/parser/tidb/tidb.go
index 0b2121709f..0da4774e92 100644
--- a/backend/plugin/parser/tidb/tidb.go
+++ b/backend/plugin/parser/tidb/tidb.go
@@ -45,8 +45,8 @@ type ParseResult struct {
BaseLine int
}
-func ANTLRParseTiDB(statement string, options ...tokenizer.Option) ([]*ParseResult, error) {
- statement, err := DealWithDelimiter(statement, options...)
+func ANTLRParseTiDB(statement string) ([]*ParseResult, error) {
+ statement, err := DealWithDelimiter(statement)
if err != nil {
return nil, err
}
@@ -140,8 +140,8 @@ func isEmptyStatement(tokens *antlr.CommonTokenStream) bool {
}
// DealWithDelimiter converts the delimiter statement to comment, also converts the following statement's delimiter to semicolon(`;`).
-func DealWithDelimiter(statement string, options ...tokenizer.Option) (string, error) {
- has, list, err := hasDelimiter(statement, options...)
+func DealWithDelimiter(statement string) (string, error) {
+ has, list, err := hasDelimiter(statement)
if err != nil {
return "", err
}
@@ -189,9 +189,9 @@ func ExtractDelimiter(stmt string) (string, error) {
return "", errors.Errorf("cannot extract delimiter from %q", stmt)
}
-func hasDelimiter(statement string, options ...tokenizer.Option) (bool, []base.SingleSQL, error) {
+func hasDelimiter(statement string) (bool, []base.SingleSQL, error) {
// use splitTiDBMultiSQL to check if the statement has delimiter
- t := tokenizer.NewTokenizer(statement, options...)
+ t := tokenizer.NewTokenizer(statement)
list, err := t.SplitTiDBMultiSQL()
if err != nil {
return false, nil, errors.Errorf("failed to split multi sql: %v", err)
diff --git a/backend/plugin/parser/tokenizer/testdata/test_split_tidb_multi_sql.yaml b/backend/plugin/parser/tokenizer/testdata/test_split_tidb_multi_sql.yaml
deleted file mode 100644
index 64ad99d3c3..0000000000
--- a/backend/plugin/parser/tokenizer/testdata/test_split_tidb_multi_sql.yaml
+++ /dev/null
@@ -1,174 +0,0 @@
-- description: Split comment and delimiter
- statement: |-
- -- comment
- DELIMITER ;
- expected:
- - text: -- comment
- baseline: 0
- start: null
- end:
- line: 1
- column: 0
- empty: true
- byteoffsetstart: 0
- byteoffsetend: 0
- - text: DELIMITER ;
- baseline: 0
- start: null
- end:
- line: 2
- column: 0
- empty: false
- byteoffsetstart: 11
- byteoffsetend: 22
-- description: empty statement
- statement: ' '
- expected: []
-- description: sss
- statement: |-
- drop table t3;create table t3 (
- id int
- );
-
- insert into t3 values (1), (2), (3);
- insert into t3 values (1);
- delete from t3 where id = 1;
-
- update t3 set id = 4 where id = 3;
- expected:
- - text: drop table t3;
- baseline: 0
- start: null
- end:
- line: 1
- column: 0
- empty: false
- byteoffsetstart: 0
- byteoffsetend: 14
- - text: |-
- create table t3 (
- id int
- );
- baseline: 0
- start: null
- end:
- line: 3
- column: 0
- empty: false
- byteoffsetstart: 14
- byteoffsetend: 42
- - text: ""
- baseline: 3
- start: null
- end:
- line: 4
- column: 0
- empty: true
- byteoffsetstart: 0
- byteoffsetend: 0
- - text: insert into t3 values (1), (2), (3);
- baseline: 0
- start: null
- end:
- line: 5
- column: 0
- empty: false
- byteoffsetstart: 44
- byteoffsetend: 80
- - text: insert into t3 values (1);
- baseline: 0
- start: null
- end:
- line: 6
- column: 0
- empty: false
- byteoffsetstart: 81
- byteoffsetend: 107
- - text: delete from t3 where id = 1;
- baseline: 0
- start: null
- end:
- line: 7
- column: 0
- empty: false
- byteoffsetstart: 108
- byteoffsetend: 136
- - text: ""
- baseline: 7
- start: null
- end:
- line: 8
- column: 0
- empty: true
- byteoffsetstart: 0
- byteoffsetend: 0
- - text: update t3 set id = 4 where id = 3;
- baseline: 0
- start: null
- end:
- line: 9
- column: 0
- empty: false
- byteoffsetstart: 138
- byteoffsetend: 172
-- description: multi-newline
- statement: |-
- CREATE TABLE `u` (
- `b` blob NOT NULL,
- );
-
- /*
-
-
-
- */
-
- --
- -- Table structure for `t`
- --
- CREATE TABLE `t` (
- `b` blob NOT NULL,
- );
- expected:
- - text: |-
- CREATE TABLE `u` (
- `b` blob NOT NULL,
- );
- baseline: 0
- start: null
- end:
- line: 3
- column: 0
- empty: false
- byteoffsetstart: 0
- byteoffsetend: 42
- - text: ""
- baseline: 3
- start: null
- end:
- line: 4
- column: 0
- empty: true
- byteoffsetstart: 0
- byteoffsetend: 0
- - text: |-
- /*
-
-
-
- */
-
- --
- -- Table structure for `t`
- --
- CREATE TABLE `t` (
- `b` blob NOT NULL,
- );
- baseline: 0
- start: null
- end:
- line: 16
- column: 0
- empty: false
- byteoffsetstart: 44
- byteoffsetend: 129
diff --git a/backend/plugin/parser/tokenizer/tokenizer.go b/backend/plugin/parser/tokenizer/tokenizer.go
index 24e260707a..f85dac44fe 100644
--- a/backend/plugin/parser/tokenizer/tokenizer.go
+++ b/backend/plugin/parser/tokenizer/tokenizer.go
@@ -25,47 +25,6 @@ var (
delimiterRuneList = []rune{'D', 'E', 'L', 'I', 'M', 'I', 'T', 'E', 'R'}
)
-type Option func(*Tokenizer)
-
-// KeepEmptyBlocks is used to keep empty lines between the blocks.
-// Currently, it is only available for SplitTiDBMultiSQL.
-// The following example shows how it affects the result:
-//
-// CREATE TABLE t1 (
-// a int
-// );
-//
-//
-// CREATE TABLE t2 (
-// a int
-// );
-//
-// If we set KeepEmptyBlocks, the result will be:
-// [
-//
-// "CREATE TABLE t1 (
-// a int
-// );",
-// "\n",
-// "CREATE TABLE t2 (
-// a int
-// );"
-//
-// ]
-// Note, the sum of the empty lines in that block will be 1(2-1) instead of 2, the reason is
-// that do not break the current caller.
-func KeepEmptyBlocks() Option {
- return func(t *Tokenizer) {
- t.keepEmptyBlocks = true
- }
-}
-
-func SplitCommentBeforeDelimiter() Option {
- return func(t *Tokenizer) {
- t.splitCommentBeforeDelimiter = true
- }
-}
-
type Tokenizer struct {
buffer []rune
bufferByteOffset []int
@@ -78,17 +37,11 @@ type Tokenizer struct {
reader *bufio.Reader
f func(string) error
readErr error
-
- // Options.
- // keepEmptyBlocks is used to keep empty lines between the blocks.
- keepEmptyBlocks bool
- // splitCommentBeforeDelimiter is used to split comment before delimiter.
- splitCommentBeforeDelimiter bool
}
// NewTokenizer creates a new tokenizer.
// Notice: we append an additional eofRune in the statement. This is a sentinel rune.
-func NewTokenizer(statement string, options ...Option) *Tokenizer {
+func NewTokenizer(statement string) *Tokenizer {
t := &Tokenizer{
buffer: []rune(statement),
cursor: 0,
@@ -102,10 +55,6 @@ func NewTokenizer(statement string, options ...Option) *Tokenizer {
t.buffer = append(t.buffer, eofRune)
t.bufferByteOffset = append(t.bufferByteOffset, len(statement))
- for _, option := range options {
- option(t)
- }
-
return t
}
@@ -452,24 +401,6 @@ func (t *Tokenizer) SplitTiDBMultiSQL() ([]base.SingleSQL, error) {
}
}
- if t.keepEmptyBlocks {
- newRes := make([]base.SingleSQL, 0, len(res))
- baseline := 0
- for _, sql := range res {
- lines := strings.Split(sql.Text, "\n")
- if lap := int(sql.End.GetLine()) - len(lines) - baseline; lap > 0 {
- newRes = append(newRes, base.SingleSQL{
- Text: strings.Repeat("\n", lap-1),
- BaseLine: baseline,
- End: &store.Position{Line: int32(baseline + lap)},
- Empty: true,
- })
- }
- newRes = append(newRes, sql)
- baseline = int(sql.End.GetLine())
- }
- return newRes, t.readErr
- }
return res, t.readErr
case t.equalWordCaseInsensitive(delimiter):
t.skip(uint(len(delimiter)))
@@ -491,23 +422,6 @@ func (t *Tokenizer) SplitTiDBMultiSQL() ([]base.SingleSQL, error) {
t.emptyStatement = true
// deal with the DELIMITER statement, see https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html
case t.equalWordCaseInsensitive(delimiterRuneList):
- if t.splitCommentBeforeDelimiter {
- if v := t.getString(startPos, t.pos()-startPos); v != "" {
- line := t.line
- // Any better way to handle comment before delimiter, and we want to get two
- // blocks of SQL, one is the comment, the other is the delimiter.
- if strings.HasSuffix(v, "\n") {
- v = v[:len(v)-1]
- line--
- }
- res = append(res, base.SingleSQL{
- Text: v,
- End: &store.Position{Line: int32(line)},
- Empty: true,
- })
- }
- startPos = t.pos()
- }
t.skip(uint(len(delimiterRuneList)))
t.skipBlank()
delimiterStart := t.pos()
diff --git a/backend/plugin/parser/tokenizer/tokenizer_test.go b/backend/plugin/parser/tokenizer/tokenizer_test.go
deleted file mode 100644
index 18bd88d81e..0000000000
--- a/backend/plugin/parser/tokenizer/tokenizer_test.go
+++ /dev/null
@@ -1,59 +0,0 @@
-package tokenizer
-
-import (
- "io"
- "os"
- "testing"
-
- "github.com/stretchr/testify/require"
- "gopkg.in/yaml.v3"
-
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
-)
-
-func TestSplitTiDBMultiSQL(t *testing.T) {
- type testCase struct {
- Description string
- Statement string
- Expected []base.SingleSQL
- }
-
- const (
- fp = "testdata/test_split_tidb_multi_sql.yaml"
- record = false
- )
-
- a := require.New(t)
-
- f, err := os.Open(fp)
- a.NoError(err)
- content, err := io.ReadAll(f)
- a.NoError(err)
- err = f.Close()
- a.NoError(err)
-
- var testCases []testCase
- err = yaml.Unmarshal(content, &testCases)
- a.NoError(err)
-
- for idx, tc := range testCases {
- tokenizer := NewTokenizer(tc.Statement, KeepEmptyBlocks(), SplitCommentBeforeDelimiter())
- list, err := tokenizer.SplitTiDBMultiSQL()
- a.NoError(err)
-
- if record {
- testCases[idx].Expected = list
- } else {
- a.Equal(tc.Expected, list, tc.Description)
- }
- }
-
- if record {
- content, err := yaml.Marshal(testCases)
- a.NoError(err)
- f, err := os.OpenFile(fp, os.O_WRONLY|os.O_TRUNC, 0644)
- a.NoError(err)
- _, err = f.Write(content)
- a.NoError(err)
- }
-}