Skip to content

Commit

Permalink
added truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Aug 10, 2024
1 parent 5b3b5b7 commit f9596b1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 16 deletions.
36 changes: 23 additions & 13 deletions kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import (
)

const (
KindUnknown = "unknown"
KindSelect = "select"
KindLoad = "load"
KindInsert = "insert"
KindUpdate = "update"
KindMerge = "merge"
KindDelete = "delete"
KindRegisterType = "register type"
KindRegisterSet = "register set"
KindCreateTable = "create table"
KindDropTable = "drop table"
KindCreateIndex = "create index"
KindDropIndex = "drop index"
KindUnknown = "unknown"
KindSelect = "select"
KindLoad = "load"
KindInsert = "insert"
KindUpdate = "update"
KindMerge = "merge"
KindDelete = "delete"
KindRegisterType = "register type"
KindRegisterSet = "register set"
KindCreateTable = "create table"
KindDropTable = "drop table"
KindCreateIndex = "create index"
KindDropIndex = "drop index"
KindTruncateTable = "truncate table"
)

// Kind represents the type of SQL statement.
Expand Down Expand Up @@ -139,6 +140,15 @@ func ParseKind(SQL string) Kind {
return KindDropIndex
}
}
case 't':
if len(secondToken) == 0 {
return KindUnknown
}
switch secondToken[0] {
case 't': //truncate
return KindTruncateTable
}

case 'r': //register
if len(secondToken) == 0 {
return KindUnknown
Expand Down
2 changes: 2 additions & 0 deletions lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const (
registerKeyword
typeKeyword
ttlKeyword
truncateTableKeyword
)

var whitespaceMatcher = parsly.NewToken(whitespaceCode, "whitespace", matcher.NewWhiteSpace())
Expand All @@ -96,6 +97,7 @@ var inlineCommentMatcher = parsly.NewToken(commentBlock, "--", matcher.NewSeqBlo
var selectKeywordMatcher = parsly.NewToken(selectKeyword, "SELECT", matcher.NewKeyword("select", &option.Case{}))
var exceptKeywordMatcher = parsly.NewToken(exceptKeyword, "EXCEPT", matcher.NewKeyword("except", &option.Case{}))
var betweenKeywordMatcher = parsly.NewToken(betweenToken, "BETWEEN", matcher.NewKeyword("between", &option.Case{}))
var truncateKeywordMatcher = parsly.NewToken(truncateTableKeyword, "TRUNCATE", matcher.NewSpacedFragment("truncate table", &option.Case{}))

var fromKeywordMatcher = parsly.NewToken(fromKeyword, "FROM", matcher.NewKeyword("from", &option.Case{}))
var joinMatcher = parsly.NewToken(joinToken, "LEFT OUTER JOIN|LEFT JOIN|JOIN", matcher.NewSpacedSet([]string{
Expand Down
34 changes: 31 additions & 3 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/viant/sqlparser/update"
)

//TableName returns main table name
// TableName returns main table name
func TableName(node node.Node) string {
switch actual := node.(type) {
case *query.Select:
Expand Down Expand Up @@ -73,7 +73,35 @@ func trimEnclosure(node node.Node) string {
return name
}

//ParseDropTable parses drop table
// ParseTruncateTable parses truncate table
func ParseTruncateTable(SQL string) (*table.Truncate, error) {
result := &table.Truncate{}
SQL = removeSQLComments(SQL)
cursor := parsly.NewCursor("", []byte(SQL), 0)
err := parseTruncateTable(cursor, result)
if err != nil {
return result, fmt.Errorf("%s", SQL)
}
return result, err
}

func parseTruncateTable(cursor *parsly.Cursor, result *table.Truncate) error {
match := cursor.MatchAfterOptional(whitespaceMatcher, truncateKeywordMatcher)
if match.Code != truncateTableKeyword {
return cursor.NewError(truncateKeywordMatcher)
}
if match = cursor.MatchOne(whitespaceMatcher); match.Code != whitespaceCode {
return cursor.NewError(whitespaceMatcher)
}
match = cursor.MatchAfterOptional(whitespaceMatcher, identifierMatcher)
if match.Code != identifierCode {
return cursor.NewError(identifierMatcher)
}
result.Table = match.Text(cursor)
return nil
}

// ParseDropTable parses drop table
func ParseDropTable(SQL string) (*table.Drop, error) {
result := &table.Drop{}
SQL = removeSQLComments(SQL)
Expand Down Expand Up @@ -104,7 +132,7 @@ func parseDropTable(cursor *parsly.Cursor, dest *table.Drop) error {
return nil
}

//ParseCreateTable parses create table
// ParseCreateTable parses create table
func ParseCreateTable(SQL string) (*table.Create, error) {
result := &table.Create{}
SQL = removeSQLComments(SQL)
Expand Down
6 changes: 6 additions & 0 deletions table/truncate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package table

// Truncate represents a truncate table statement
type Truncate struct {
Table string
}

0 comments on commit f9596b1

Please sign in to comment.