Skip to content

Commit a5bb5f7

Browse files
author
Qi Xiao
committed
Fit for CovenantSQL use
1 parent 2f0401b commit a5bb5f7

30 files changed

Lines changed: 2037 additions & 8895 deletions

analyzer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"strings"
2626
"unicode"
2727

28-
"github.com/xwb1989/sqlparser/dependency/sqltypes"
28+
"github.com/CovenantSQL/sqlparser/dependency/sqltypes"
2929
)
3030

3131
// These constants are used to identify the SQL statement type.

analyzer_test.go

Lines changed: 1 addition & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"strings"
2222
"testing"
2323

24-
"github.com/xwb1989/sqlparser/dependency/sqltypes"
24+
"github.com/CovenantSQL/sqlparser/dependency/sqltypes"
2525
)
2626

2727
func TestPreview(t *testing.T) {
@@ -360,140 +360,6 @@ func TestStringIn(t *testing.T) {
360360
}
361361
}
362362

363-
func TestExtractSetValues(t *testing.T) {
364-
testcases := []struct {
365-
sql string
366-
out map[SetKey]interface{}
367-
scope string
368-
err string
369-
}{{
370-
sql: "invalid",
371-
err: "syntax error at position 8 near 'invalid'",
372-
}, {
373-
sql: "select * from t",
374-
err: "ast did not yield *sqlparser.Set: *sqlparser.Select",
375-
}, {
376-
sql: "set autocommit=1+1",
377-
err: "invalid syntax: 1 + 1",
378-
}, {
379-
sql: "set transaction_mode='single'",
380-
out: map[SetKey]interface{}{{Key: "transaction_mode", Scope: "session"}: "single"},
381-
}, {
382-
sql: "set autocommit=1",
383-
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(1)},
384-
}, {
385-
sql: "set autocommit=true",
386-
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(1)},
387-
}, {
388-
sql: "set autocommit=false",
389-
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(0)},
390-
}, {
391-
sql: "set autocommit=on",
392-
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: "on"},
393-
}, {
394-
sql: "set autocommit=off",
395-
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: "off"},
396-
}, {
397-
sql: "set @@global.autocommit=1",
398-
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "global"}: int64(1)},
399-
}, {
400-
sql: "set @@global.autocommit=1",
401-
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "global"}: int64(1)},
402-
}, {
403-
sql: "set @@session.autocommit=1",
404-
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(1)},
405-
}, {
406-
sql: "set @@session.`autocommit`=1",
407-
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(1)},
408-
}, {
409-
sql: "set @@session.'autocommit'=1",
410-
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(1)},
411-
}, {
412-
sql: "set @@session.\"autocommit\"=1",
413-
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(1)},
414-
}, {
415-
sql: "set @@session.'\"autocommit'=1",
416-
out: map[SetKey]interface{}{{Key: "\"autocommit", Scope: "session"}: int64(1)},
417-
}, {
418-
sql: "set @@session.`autocommit'`=1",
419-
out: map[SetKey]interface{}{{Key: "autocommit'", Scope: "session"}: int64(1)},
420-
}, {
421-
sql: "set AUTOCOMMIT=1",
422-
out: map[SetKey]interface{}{{Key: "autocommit", Scope: "session"}: int64(1)},
423-
}, {
424-
sql: "SET character_set_results = NULL",
425-
out: map[SetKey]interface{}{{Key: "character_set_results", Scope: "session"}: nil},
426-
}, {
427-
sql: "SET foo = 0x1234",
428-
err: "invalid value type: 0x1234",
429-
}, {
430-
sql: "SET names utf8",
431-
out: map[SetKey]interface{}{{Key: "names", Scope: "session"}: "utf8"},
432-
}, {
433-
sql: "SET names ascii collate ascii_bin",
434-
out: map[SetKey]interface{}{{Key: "names", Scope: "session"}: "ascii"},
435-
}, {
436-
sql: "SET charset default",
437-
out: map[SetKey]interface{}{{Key: "charset", Scope: "session"}: "default"},
438-
}, {
439-
sql: "SET character set ascii",
440-
out: map[SetKey]interface{}{{Key: "charset", Scope: "session"}: "ascii"},
441-
}, {
442-
sql: "SET SESSION wait_timeout = 3600",
443-
out: map[SetKey]interface{}{{Key: "wait_timeout", Scope: "session"}: int64(3600)},
444-
scope: "session",
445-
}, {
446-
sql: "SET GLOBAL wait_timeout = 3600",
447-
out: map[SetKey]interface{}{{Key: "wait_timeout", Scope: "session"}: int64(3600)},
448-
scope: "global",
449-
}, {
450-
sql: "set session transaction isolation level repeatable read",
451-
out: map[SetKey]interface{}{{Key: "tx_isolation", Scope: "session"}: "repeatable read"},
452-
scope: "session",
453-
}, {
454-
sql: "set session transaction isolation level read committed",
455-
out: map[SetKey]interface{}{{Key: "tx_isolation", Scope: "session"}: "read committed"},
456-
scope: "session",
457-
}, {
458-
sql: "set session transaction isolation level read uncommitted",
459-
out: map[SetKey]interface{}{{Key: "tx_isolation", Scope: "session"}: "read uncommitted"},
460-
scope: "session",
461-
}, {
462-
sql: "set session transaction isolation level serializable",
463-
out: map[SetKey]interface{}{{Key: "tx_isolation", Scope: "session"}: "serializable"},
464-
scope: "session",
465-
}, {
466-
sql: "set session tx_read_only = 0",
467-
out: map[SetKey]interface{}{{Key: "tx_read_only", Scope: "session"}: int64(0)},
468-
scope: "session",
469-
}, {
470-
sql: "set session tx_read_only = 1",
471-
out: map[SetKey]interface{}{{Key: "tx_read_only", Scope: "session"}: int64(1)},
472-
scope: "session",
473-
}, {
474-
sql: "set session sql_safe_updates = 0",
475-
out: map[SetKey]interface{}{{Key: "sql_safe_updates", Scope: "session"}: int64(0)},
476-
scope: "session",
477-
}, {
478-
sql: "set session sql_safe_updates = 1",
479-
out: map[SetKey]interface{}{{Key: "sql_safe_updates", Scope: "session"}: int64(1)},
480-
scope: "session",
481-
}}
482-
for _, tcase := range testcases {
483-
out, _, err := ExtractSetValues(tcase.sql)
484-
if tcase.err != "" {
485-
if err == nil || err.Error() != tcase.err {
486-
t.Errorf("ExtractSetValues(%s): %v, want '%s'", tcase.sql, err, tcase.err)
487-
}
488-
} else if err != nil {
489-
t.Errorf("ExtractSetValues(%s): %v, want no error", tcase.sql, err)
490-
}
491-
if !reflect.DeepEqual(out, tcase.out) {
492-
t.Errorf("ExtractSetValues(%s): %v, want '%v'", tcase.sql, out, tcase.out)
493-
}
494-
}
495-
}
496-
497363
func newStrVal(in string) *SQLVal {
498364
return NewStrVal([]byte(in))
499365
}

ast.go

Lines changed: 51 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
"log"
2626
"strings"
2727

28-
"github.com/xwb1989/sqlparser/dependency/querypb"
29-
"github.com/xwb1989/sqlparser/dependency/sqltypes"
28+
"github.com/CovenantSQL/sqlparser/dependency/querypb"
29+
"github.com/CovenantSQL/sqlparser/dependency/sqltypes"
3030
)
3131

3232
// Instructions for creating new types: If a type
@@ -673,6 +673,7 @@ const (
673673
DropStr = "drop"
674674
RenameStr = "rename"
675675
TruncateStr = "truncate"
676+
DropIndexStr = "drop index"
676677
CreateVindexStr = "create vindex"
677678
AddColVindexStr = "add vindex"
678679
DropColVindexStr = "drop vindex"
@@ -696,8 +697,14 @@ func (node *DDL) Format(buf *TrackedBuffer) {
696697
exists = " if exists"
697698
}
698699
buf.Myprintf("%s table%s %v", node.Action, exists, node.Table)
700+
case DropIndexStr:
701+
exists := ""
702+
if node.IfExists {
703+
exists = " if exists"
704+
}
705+
buf.Myprintf("%s%s %v", node.Action, exists, node.Table)
699706
case RenameStr:
700-
buf.Myprintf("%s table %v to %v", node.Action, node.Table, node.NewName)
707+
buf.Myprintf("alter table %v %s to %v", node.Table, node.Action, node.NewName)
701708
case AlterStr:
702709
if node.PartitionSpec != nil {
703710
buf.Myprintf("%s table %v %v", node.Action, node.Table, node.PartitionSpec)
@@ -935,12 +942,6 @@ func (ct *ColumnType) Format(buf *TrackedBuffer) {
935942
if ct.Zerofill {
936943
opts = append(opts, keywordStrings[ZEROFILL])
937944
}
938-
if ct.Charset != "" {
939-
opts = append(opts, keywordStrings[CHARACTER], keywordStrings[SET], ct.Charset)
940-
}
941-
if ct.Collate != "" {
942-
opts = append(opts, keywordStrings[COLLATE], ct.Collate)
943-
}
944945
if ct.NotNull {
945946
opts = append(opts, keywordStrings[NOT], keywordStrings[NULL])
946947
}
@@ -953,9 +954,6 @@ func (ct *ColumnType) Format(buf *TrackedBuffer) {
953954
if ct.Autoincrement {
954955
opts = append(opts, keywordStrings[AUTO_INCREMENT])
955956
}
956-
if ct.Comment != nil {
957-
opts = append(opts, keywordStrings[COMMENT_KEYWORD], String(ct.Comment))
958-
}
959957
if ct.KeyOpt == colKeyPrimary {
960958
opts = append(opts, keywordStrings[PRIMARY], keywordStrings[KEY])
961959
}
@@ -965,9 +963,6 @@ func (ct *ColumnType) Format(buf *TrackedBuffer) {
965963
if ct.KeyOpt == colKeyUniqueKey {
966964
opts = append(opts, keywordStrings[UNIQUE], keywordStrings[KEY])
967965
}
968-
if ct.KeyOpt == colKeySpatialKey {
969-
opts = append(opts, keywordStrings[SPATIAL], keywordStrings[KEY])
970-
}
971966
if ct.KeyOpt == colKey {
972967
opts = append(opts, keywordStrings[KEY])
973968
}
@@ -1051,10 +1046,6 @@ func (ct *ColumnType) SQLType() querypb.Type {
10511046
return sqltypes.Char
10521047
case keywordStrings[VARCHAR]:
10531048
return sqltypes.VarChar
1054-
case keywordStrings[BINARY]:
1055-
return sqltypes.Binary
1056-
case keywordStrings[VARBINARY]:
1057-
return sqltypes.VarBinary
10581049
case keywordStrings[DATE]:
10591050
return sqltypes.Date
10601051
case keywordStrings[TIME]:
@@ -1071,30 +1062,8 @@ func (ct *ColumnType) SQLType() querypb.Type {
10711062
return sqltypes.Float64
10721063
case keywordStrings[DECIMAL]:
10731064
return sqltypes.Decimal
1074-
case keywordStrings[BIT]:
1075-
return sqltypes.Bit
1076-
case keywordStrings[ENUM]:
1077-
return sqltypes.Enum
10781065
case keywordStrings[SET]:
10791066
return sqltypes.Set
1080-
case keywordStrings[JSON]:
1081-
return sqltypes.TypeJSON
1082-
case keywordStrings[GEOMETRY]:
1083-
return sqltypes.Geometry
1084-
case keywordStrings[POINT]:
1085-
return sqltypes.Geometry
1086-
case keywordStrings[LINESTRING]:
1087-
return sqltypes.Geometry
1088-
case keywordStrings[POLYGON]:
1089-
return sqltypes.Geometry
1090-
case keywordStrings[GEOMETRYCOLLECTION]:
1091-
return sqltypes.Geometry
1092-
case keywordStrings[MULTIPOINT]:
1093-
return sqltypes.Geometry
1094-
case keywordStrings[MULTILINESTRING]:
1095-
return sqltypes.Geometry
1096-
case keywordStrings[MULTIPOLYGON]:
1097-
return sqltypes.Geometry
10981067
}
10991068
panic("unimplemented type " + ct.Type)
11001069
}
@@ -1805,15 +1774,13 @@ type JoinTableExpr struct {
18051774

18061775
// JoinTableExpr.Join
18071776
const (
1808-
JoinStr = "join"
1809-
InnerJoinStr = "inner join"
1810-
CrossJoinStr = "cross join"
1811-
StraightJoinStr = "straight_join"
1812-
LeftJoinStr = "left join"
1813-
RightJoinStr = "right join"
1814-
NaturalJoinStr = "natural join"
1815-
NaturalLeftJoinStr = "natural left join"
1816-
NaturalRightJoinStr = "natural right join"
1777+
JoinStr = "join"
1778+
InnerJoinStr = "inner join"
1779+
CrossJoinStr = "cross join"
1780+
StraightJoinStr = "straight_join"
1781+
LeftJoinStr = "left join"
1782+
NaturalJoinStr = "natural join"
1783+
NaturalLeftJoinStr = "natural left join"
18171784
)
18181785

18191786
// Format formats the node.
@@ -1946,6 +1913,7 @@ func (*ConvertUsingExpr) iExpr() {}
19461913
func (*MatchExpr) iExpr() {}
19471914
func (*GroupConcatExpr) iExpr() {}
19481915
func (*Default) iExpr() {}
1916+
func (*TimeExpr) iExpr() {}
19491917

19501918
// ReplaceExpr finds the from expression from root
19511919
// and replaces it with to. If from matches root,
@@ -2105,22 +2073,20 @@ type ComparisonExpr struct {
21052073

21062074
// ComparisonExpr.Operator
21072075
const (
2108-
EqualStr = "="
2109-
LessThanStr = "<"
2110-
GreaterThanStr = ">"
2111-
LessEqualStr = "<="
2112-
GreaterEqualStr = ">="
2113-
NotEqualStr = "!="
2114-
NullSafeEqualStr = "<=>"
2115-
NullSafeNotEqualStr = "<>"
2116-
InStr = "in"
2117-
NotInStr = "not in"
2118-
LikeStr = "like"
2119-
NotLikeStr = "not like"
2120-
RegexpStr = "regexp"
2121-
NotRegexpStr = "not regexp"
2122-
JSONExtractOp = "->"
2123-
JSONUnquoteExtractOp = "->>"
2076+
EqualStr = "="
2077+
LessThanStr = "<"
2078+
GreaterThanStr = ">"
2079+
LessEqualStr = "<="
2080+
GreaterEqualStr = ">="
2081+
NotEqualStr = "!="
2082+
NullSafeEqualStr = "<=>"
2083+
NullSafeNotEqualStr = "<>"
2084+
InStr = "in"
2085+
NotInStr = "not in"
2086+
LikeStr = "like"
2087+
NotLikeStr = "not like"
2088+
RegexpStr = "regexp"
2089+
NotRegexpStr = "not regexp"
21242090
)
21252091

21262092
// Format formats the node.
@@ -2634,6 +2600,24 @@ func (node *CollateExpr) replace(from, to Expr) bool {
26342600
return replaceExprs(from, to, &node.Expr)
26352601
}
26362602

2603+
// TimeExpr represents a time expression.
2604+
type TimeExpr struct {
2605+
Expr ColIdent
2606+
}
2607+
2608+
// Format formats the node.
2609+
func (node *TimeExpr) Format(buf *TrackedBuffer) {
2610+
buf.Myprintf(node.Expr.Lowered())
2611+
}
2612+
2613+
func (node *TimeExpr) walkSubtree(visit Visit) error {
2614+
return nil
2615+
}
2616+
2617+
func (node *TimeExpr) replace(from, to Expr) bool {
2618+
return false
2619+
}
2620+
26372621
// FuncExpr represents a function call.
26382622
type FuncExpr struct {
26392623
Qualifier TableIdent
@@ -2816,7 +2800,7 @@ type ConvertExpr struct {
28162800

28172801
// Format formats the node.
28182802
func (node *ConvertExpr) Format(buf *TrackedBuffer) {
2819-
buf.Myprintf("convert(%v, %v)", node.Expr, node.Type)
2803+
buf.Myprintf("cast(%v as %v)", node.Expr, node.Type)
28202804
}
28212805

28222806
func (node *ConvertExpr) walkSubtree(visit Visit) error {

0 commit comments

Comments
 (0)