-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql2expr.go
More file actions
46 lines (44 loc) · 1.5 KB
/
sql2expr.go
File metadata and controls
46 lines (44 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Package sql2expr provides a SQL to expr-lang transpiler.
//
// sql2expr allows you to write SQL queries and transpile them to expr-lang expressions
// that can be used to filter and transform in-memory data structures and database results.
//
// Example usage:
//
// expr, err := sql2expr.Transpile("SELECT * FROM items WHERE price > 100")
// if err != nil {
// log.Fatal(err)
// }
// // expr will be: "filter(items, {user.price > 100})"
//
// // With custom data source:
// expr, err := sql2expr.Transpile("SELECT id, name FROM users WHERE active = true")
// if err != nil {
// log.Fatal(err)
// }
// // expr will be: "map(filter(users, {user.active == true}), user)"
package sql2expr
import (
"github.com/h22rana/sql2expr/transpiler"
)
// Transpile converts a SQL SELECT statement to an expr-lang expression.
// The data source variable name is automatically extracted from the FROM clause.
// If no FROM clause is present, a default name "data" is used.
//
// Supported SQL features:
// - SELECT with field projections and aliasing
// - WHERE clause with comparisons, AND, OR, NOT
// - ORDER BY with ASC/DESC
// - LIMIT and OFFSET
// - Basic aggregation functions (COUNT, SUM, AVG)
// - GROUP BY
//
// Example:
//
// sql := "SELECT id, name FROM users WHERE age > 18 ORDER BY name LIMIT 10"
// expr, err := sql2expr.Transpile(sql)
// // Result: "sortBy(map(filter(users, {user.age > 18}), user), user.name)[:10]"
func Transpile(sql string) (string, error) {
t := transpiler.New("data")
return t.Transpile(sql)
}