| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package dpsapi
- import (
- "github.com/auxten/postgresql-parser/pkg/sql/parser"
- "github.com/auxten/postgresql-parser/pkg/walk"
- "strings"
- )
- type insertClause struct {
- into string
- }
- type deleteClause struct {
- }
- type updateClause struct {
- }
- type selectClause struct {
- selectExpr []string
- from string
- where []string
- limit int
- offset int
- }
- func parseSql(sqlStr string) ([]any, error) {
- sqls := strings.Split(sqlStr, ";")
- sqlClauses := make([]any, 0)
- for _, sql := range sqls {
- stmts, err := parser.Parse(sql)
- if err != nil {
- return nil, err
- }
- trimSQL := strings.TrimSpace(sql)
- upperTrimSQL := strings.ToUpper(trimSQL)
- var clause any
- w := new(walk.AstWalker)
- if strings.HasPrefix(upperTrimSQL, "INSERT") {
- c := new(insertClause)
- clause = c
- w.Fn = insertWalkFunc(c)
- } else if strings.HasPrefix(upperTrimSQL, "DELETE") {
- c := new(deleteClause)
- clause = c
- w.Fn = deleteWalkFunc(c)
- } else if strings.HasPrefix(upperTrimSQL, "UPDATE") {
- c := new(updateClause)
- clause = c
- w.Fn = updateWalkFunc(c)
- } else if strings.HasPrefix(upperTrimSQL, "SELECT") {
- c := new(selectClause)
- clause = c
- w.Fn = selectWalkFunc(c)
- }
- _, err = w.Walk(stmts, nil)
- if err != nil {
- return nil, err
- }
- sqlClauses = append(sqlClauses, clause)
- }
- return sqlClauses, nil
- }
- func insertWalkFunc(clause *insertClause) func(ctx interface{}, node interface{}) (stop bool) {
- return func(ctx interface{}, node interface{}) (stop bool) {
- return false
- }
- }
- func deleteWalkFunc(clause *deleteClause) func(ctx interface{}, node interface{}) (stop bool) {
- return func(ctx interface{}, node interface{}) (stop bool) {
- return false
- }
- }
- func updateWalkFunc(clause *updateClause) func(ctx interface{}, node interface{}) (stop bool) {
- return func(ctx interface{}, node interface{}) (stop bool) {
- return false
- }
- }
- func selectWalkFunc(clause *selectClause) func(ctx interface{}, node interface{}) (stop bool) {
- return func(ctx interface{}, node interface{}) (stop bool) {
- return false
- }
- }
|