MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / ReplaceTable

Function ReplaceTable

pkg/transform/tables.go:39–69  ·  view source on GitHub ↗

ReplaceTable returns a Rule that replaces all occurrences of a table name throughout a SELECT, UPDATE, or DELETE statement. The replacement covers: - FROM clause table references - JOIN clause left and right table references - Column qualifiers (table.column identifiers in SELECT list, WHERE, ORDER

(oldName, newName string)

Source from the content-addressed store, hash-verified

37//
38// Returns ErrUnsupportedStatement for INSERT or DDL statements.
39func ReplaceTable(oldName, newName string) Rule {
40 return RuleFunc(func(stmt ast.Statement) error {
41 switch s := stmt.(type) {
42 case *ast.SelectStatement:
43 replaceTableInFrom(s.From, oldName, newName)
44 replaceTableInJoins(s.Joins, oldName, newName)
45 for i, col := range s.Columns {
46 s.Columns[i] = replaceTableInExpr(col, oldName, newName)
47 }
48 s.Where = replaceTableInExpr(s.Where, oldName, newName)
49 for i, ob := range s.OrderBy {
50 s.OrderBy[i].Expression = replaceTableInExpr(ob.Expression, oldName, newName)
51 }
52 return nil
53 case *ast.UpdateStatement:
54 if strings.EqualFold(s.TableName, oldName) {
55 s.TableName = newName
56 }
57 s.Where = replaceTableInExpr(s.Where, oldName, newName)
58 return nil
59 case *ast.DeleteStatement:
60 if strings.EqualFold(s.TableName, oldName) {
61 s.TableName = newName
62 }
63 s.Where = replaceTableInExpr(s.Where, oldName, newName)
64 return nil
65 default:
66 return &ErrUnsupportedStatement{Transform: "ReplaceTable", Got: stmtTypeName(stmt)}
67 }
68 })
69}
70
71// AddTableAlias returns a Rule that assigns an alias to a specific table in a
72// SELECT, UPDATE, or DELETE statement. For SELECT statements the alias is applied

Callers 7

mainFunction · 0.92
TestReplaceTableFunction · 0.85
TestReplaceTable_UpdateFunction · 0.85
TestReplaceTable_DeleteFunction · 0.85

Calls 5

RuleFuncFuncType · 0.85
replaceTableInFromFunction · 0.85
replaceTableInJoinsFunction · 0.85
replaceTableInExprFunction · 0.85
stmtTypeNameFunction · 0.70

Tested by 6

TestReplaceTableFunction · 0.68
TestReplaceTable_UpdateFunction · 0.68
TestReplaceTable_DeleteFunction · 0.68