Documentation
¶
Overview ¶
Package sql helps to create SQL squirrel filters using the TSL package.
Example ¶
Example for the tsl package.
// Set a TSL input string. input := "name = 'joe' and city != 'rome'" // Parse input string into a TSL tree. tree, _ := tsl.ParseTSL(input) // Set filter filter, _ := Walk(tree) // Convert TSL tree into SQL string using squirrel sql builder. sql, args, _ := sq.Select("name, city, state"). From("users"). Where(filter). ToSql() fmt.Printf("SQL : %s\n", sql) fmt.Printf("Args: %v\n", args)
Output: SQL : SELECT name, city, state FROM users WHERE (name = ? AND city != ?) Args: [joe rome]
Example (Arithmetic) ¶
input := "(base_salary + bonus) * tax_rate > 20000" tree, _ := tsl.ParseTSL(input) filter, _ := Walk(tree) sql, args, _ := sq.Select("*").From("salaries").Where(filter).ToSql() fmt.Printf("SQL : %s\n", sql) fmt.Printf("Args: %v\n", args)
Output: SQL : SELECT * FROM salaries WHERE ((base_salary + bonus) * tax_rate) > ? Args: [20000]
Example (Complex) ¶
// Complex query with multiple conditions input := ` (salary * 12) > 50000 AND department IN ['IT', 'HR'] AND hire_date BETWEEN 2020-01-01T00:00:00Z and 2023-12-31T23:59:59Z AND (manager IS NULL OR title LIKE '%Senior%') ` // Parse input string into a TSL tree tree, _ := tsl.ParseTSL(input) // Convert to SQL filter, _ := Walk(tree) sql, args, _ := sq.Select("name, department, salary"). From("employees"). Where(filter). ToSql() fmt.Printf("SQL : %s\n", sql) fmt.Printf("Args: %v\n", args)
Output: SQL : SELECT name, department, salary FROM employees WHERE ((((salary * ?) > ? AND department IN (?,?)) AND hire_date BETWEEN ? AND ?) AND (manager IS NULL OR title LIKE ?)) Args: [12 50000 IT HR 2020-01-01 00:00:00 2023-12-31 23:59:59 %Senior%]
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Walk ¶
Walk travel the TSL tree to create squirrel SQL select operators.
Users can call the Walk method inside a squirrel Where to add the query.
filter, _ := sql.Walk(tree) sql, args, _ := sq.Select("name, city, state"). From("users"). Where(filter). ToSql()
Squirrel: https://github.com/Masterminds/squirrel
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.