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

Function main

examples/transform/main.go:33–116  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

31)
32
33func main() {
34 // Example 1: Multi-tenant row filtering
35 // Add a tenant_id filter to any query for row-level security
36 fmt.Println("=== Example 1: Multi-Tenant Row Filtering ===")
37 tree, err := transform.ParseSQL("SELECT id, name, email FROM users WHERE active = true")
38 if err != nil {
39 log.Fatal(err)
40 }
41 stmt := tree.Statements[0]
42 err = transform.Apply(stmt, transform.AddWhereFromSQL("tenant_id = 42"))
43 if err != nil {
44 log.Fatal(err)
45 }
46 fmt.Println(transform.FormatSQL(stmt))
47 fmt.Println()
48
49 // Example 2: Column projection - remove sensitive columns
50 fmt.Println("=== Example 2: Column Projection ===")
51 tree, err = transform.ParseSQL("SELECT id, name, ssn, email, password FROM users")
52 if err != nil {
53 log.Fatal(err)
54 }
55 stmt = tree.Statements[0]
56 err = transform.Apply(stmt,
57 transform.RemoveColumn("ssn"),
58 transform.RemoveColumn("password"),
59 )
60 if err != nil {
61 log.Fatal(err)
62 }
63 fmt.Println(transform.FormatSQL(stmt))
64 fmt.Println()
65
66 // Example 3: JOIN injection for data enrichment
67 fmt.Println("=== Example 3: JOIN Injection ===")
68 tree, err = transform.ParseSQL("SELECT users.id, users.name FROM users")
69 if err != nil {
70 log.Fatal(err)
71 }
72 stmt = tree.Statements[0]
73 err = transform.Apply(stmt,
74 transform.AddJoinFromSQL("LEFT JOIN orders ON orders.user_id = users.id"),
75 transform.AddColumn(&ast.FunctionCall{
76 Name: "COUNT",
77 Arguments: []ast.Expression{&ast.Identifier{Name: "id", Table: "orders"}},
78 }),
79 )
80 if err != nil {
81 log.Fatal(err)
82 }
83 fmt.Println(transform.FormatSQL(stmt))
84 fmt.Println()
85
86 // Example 4: Pagination - add LIMIT, OFFSET, ORDER BY
87 fmt.Println("=== Example 4: Pagination ===")
88 tree, err = transform.ParseSQL("SELECT * FROM products WHERE category = 'electronics'")
89 if err != nil {
90 log.Fatal(err)

Callers

nothing calls this directly

Calls 11

ParseSQLFunction · 0.92
ApplyFunction · 0.92
AddWhereFromSQLFunction · 0.92
FormatSQLFunction · 0.92
RemoveColumnFunction · 0.92
AddJoinFromSQLFunction · 0.92
AddColumnFunction · 0.92
AddOrderByFunction · 0.92
SetLimitFunction · 0.92
SetOffsetFunction · 0.92
ReplaceTableFunction · 0.92

Tested by

no test coverage detected