Welcome to a small package leveraging vitess that helps you list all the Joins, Scans and Aggregates within your SQL query.
It's an easy API which helps you utilize all the different elements of your queries without having to do the hardwork of parsing the query yourself and doing the hardwork of traversing the AST by yourself.
You can install this package using -
go install github.com/uds5501/parse-sql-elements
Demo program -
package main
import (
"fmt"
"github.com/uds5501/parse-sql-elements/parser"
)
func main() {
parser := parser.NewParser()
sql := `SELECT departments.department_name, AVG(employees.salary) AS average_salary
FROM employees JOIN departments ON employees.department_id = departments.department_id;`
parser.ParseQuery(sql)
fmt.Println("Joins: ", parser.GetJoins())
fmt.Println("Aggregates: ", parser.GetAggregates())
}
This should give you the following output (as per release >= v0.0.4
)
Joins: [{{department_id employees} {department_id departments}}]
Aggregates: [{ salary employees AVG}]
To parse a new query, just perform a parser.Reset()
and you are good to go!
Currently, the parser should be able to identify the joins, selects and aggregates from the following query styles:
Query Type | Aggregates Extracted | Joins extracted | Scans extracted |
---|---|---|---|
Select query | SUM, AVG, MIN, MAX, COUNT | INNER JOINS | ALL |
Subqueries | SUM, AVG, MIN, MAX, COUNT | INNER JOINS | ALL |
Derived tables | To support | To support | To support |
Update | N/A | N/A | N/A |
Insert | N/A | N/A | N/A |
Current stable published version is v0.0.4
, I'll be updating this as I keep supporting newer queries.
If you have a feature request and there is something I like to implement in future versions, I'd recommend you to
- email me the requirements and why do you need the said feature.
- Or you can raise an issue and send a PR to implement the same!