@@ -13,7 +13,7 @@ import (
1313 "github.com/viant/sqlparser/update"
1414)
1515
16- //TableName returns main table name
16+ // TableName returns main table name
1717func TableName (node node.Node ) string {
1818 switch actual := node .(type ) {
1919 case * query.Select :
@@ -73,7 +73,35 @@ func trimEnclosure(node node.Node) string {
7373 return name
7474}
7575
76- //ParseDropTable parses drop table
76+ // ParseTruncateTable parses truncate table
77+ func ParseTruncateTable (SQL string ) (* table.Truncate , error ) {
78+ result := & table.Truncate {}
79+ SQL = removeSQLComments (SQL )
80+ cursor := parsly .NewCursor ("" , []byte (SQL ), 0 )
81+ err := parseTruncateTable (cursor , result )
82+ if err != nil {
83+ return result , fmt .Errorf ("%s" , SQL )
84+ }
85+ return result , err
86+ }
87+
88+ func parseTruncateTable (cursor * parsly.Cursor , result * table.Truncate ) error {
89+ match := cursor .MatchAfterOptional (whitespaceMatcher , truncateKeywordMatcher )
90+ if match .Code != truncateTableKeyword {
91+ return cursor .NewError (truncateKeywordMatcher )
92+ }
93+ if match = cursor .MatchOne (whitespaceMatcher ); match .Code != whitespaceCode {
94+ return cursor .NewError (whitespaceMatcher )
95+ }
96+ match = cursor .MatchAfterOptional (whitespaceMatcher , identifierMatcher )
97+ if match .Code != identifierCode {
98+ return cursor .NewError (identifierMatcher )
99+ }
100+ result .Table = match .Text (cursor )
101+ return nil
102+ }
103+
104+ // ParseDropTable parses drop table
77105func ParseDropTable (SQL string ) (* table.Drop , error ) {
78106 result := & table.Drop {}
79107 SQL = removeSQLComments (SQL )
@@ -104,7 +132,7 @@ func parseDropTable(cursor *parsly.Cursor, dest *table.Drop) error {
104132 return nil
105133}
106134
107- //ParseCreateTable parses create table
135+ // ParseCreateTable parses create table
108136func ParseCreateTable (SQL string ) (* table.Create , error ) {
109137 result := & table.Create {}
110138 SQL = removeSQLComments (SQL )
0 commit comments