@@ -4,9 +4,105 @@ import (
44 "fmt"
55 "github.com/viant/parsly"
66 "github.com/viant/sqlparser/column"
7+ del "github.com/viant/sqlparser/delete"
8+ "github.com/viant/sqlparser/expr"
9+ "github.com/viant/sqlparser/insert"
10+ "github.com/viant/sqlparser/node"
11+ "github.com/viant/sqlparser/query"
712 "github.com/viant/sqlparser/table"
13+ "github.com/viant/sqlparser/update"
814)
915
16+ //TableName returns main table name
17+ func TableName (node node.Node ) string {
18+ switch actual := node .(type ) {
19+ case * query.Select :
20+ return queryTableName (actual )
21+ case * insert.Statement :
22+ return trimEnclosure (actual .Target .X )
23+ case * update.Statement :
24+ return trimEnclosure (actual .Target .X )
25+ case * del.Statement :
26+ return trimEnclosure (actual .Target .X )
27+ case * table.Create :
28+ return trimEnclosure (actual .Spec .Name )
29+ case * table.Drop :
30+ return trimEnclosure (actual .Name )
31+ }
32+ return ""
33+ }
34+
35+ func queryTableName (sel * query.Select ) string {
36+ if sel .From .X == nil {
37+ return ""
38+ }
39+ switch actual := sel .From .X .(type ) {
40+ case * expr.Ident :
41+ return actual .Name
42+ case * expr.Selector :
43+ return trimEnclosure (actual )
44+ case * expr.Parenthesis :
45+ raw := trimEnclosure (actual .Raw )
46+ if sel , _ := ParseQuery (raw ); sel != nil {
47+ actual .X = sel
48+ return TableName (sel )
49+ }
50+ case * expr.Raw :
51+ if actual .X != nil {
52+ return TableName (actual .X )
53+ }
54+ default :
55+ panic (fmt .Sprintf ("not supported:%T " , actual ))
56+ }
57+ return ""
58+ }
59+
60+ func trimEnclosure (node node.Node ) string {
61+ if node == nil {
62+ return ""
63+ }
64+ var name string
65+ var ok bool
66+ if name , ok = node .(string ); ! ok {
67+ name = Stringify (node )
68+ }
69+ switch name [0 ] {
70+ case '`' , '"' , '[' , '\'' , '(' :
71+ name = name [1 : len (name )- 1 ]
72+ }
73+ return name
74+ }
75+
76+ func ParseDropTable (SQL string ) (* table.Drop , error ) {
77+ result := & table.Drop {}
78+ SQL = removeSQLComments (SQL )
79+ cursor := parsly .NewCursor ("" , []byte (SQL ), 0 )
80+ err := parseDropTable (cursor , result )
81+ if err != nil {
82+ return result , fmt .Errorf ("%s" , SQL )
83+ }
84+ return result , err
85+ }
86+
87+ func parseDropTable (cursor * parsly.Cursor , dest * table.Drop ) error {
88+ match := cursor .MatchAfterOptional (whitespaceMatcher , dropTableMatcher )
89+ if match .Code != dropTableToken {
90+ return cursor .NewError (createTableMatcher )
91+ }
92+ if match = cursor .MatchOne (whitespaceMatcher ); match .Code != whitespaceCode {
93+ return cursor .NewError (whitespaceMatcher )
94+ }
95+ if match = cursor .MatchOne (ifExistsMatcher ); match .Code == ifExistsToken {
96+ dest .IfExists = true
97+ }
98+ match = cursor .MatchAfterOptional (whitespaceMatcher , identifierMatcher )
99+ if match .Code != identifierCode {
100+ return cursor .NewError (ifNotExistsMatcher )
101+ }
102+ dest .Name = match .Text (cursor )
103+ return nil
104+ }
105+
10106func ParseCreateTable (SQL string ) (* table.Create , error ) {
11107 result := & table.Create {}
12108 SQL = removeSQLComments (SQL )
0 commit comments