190 lines
3.7 KiB
Go

package q
import (
"encoding/json"
"strings"
)
const NONE = 0
type QueryType int
const (
SELECT QueryType = iota + 1
UPDATE
INSERT
DELETE
)
// QueryTypeString converts a QueryType integer to its string representation.
func QueryTypeString(t QueryType) string {
switch t {
case SELECT:
return "SELECT"
case UPDATE:
return "UPDATE"
case INSERT:
return "INSERT"
case DELETE:
return "DELETE"
default:
return "UNKNOWN"
}
}
type Table struct {
Name string `json:"name"`
Alias string `json:"alias"`
}
type Conditional struct {
Key string `json:"key"`
Operator string `json:"operator"`
Value string `json:"value"`
Extension string `json:"extension"`
}
type AggregateFunctionType int
const (
MIN AggregateFunctionType = iota + 1
MAX
COUNT
SUM
AVG
)
// AggregateFunctionTypeString converts an AggregateFunctionType integer to its string representation.
func AggregateFunctionTypeString(t AggregateFunctionType) string {
switch t {
case MIN:
return "MIN"
case MAX:
return "MAX"
case COUNT:
return "COUNT"
case SUM:
return "SUM"
case AVG:
return "AVG"
default:
return "UNKNOWN"
}
}
// Converts the string name of AggregateFunctionType into it original int
func AggregateFunctionTypeByName(name string) AggregateFunctionType {
var functionType AggregateFunctionType
switch strings.ToUpper(name) {
case "MIN":
functionType = MIN
case "MAX":
functionType = MAX
case "COUNT":
functionType = COUNT
case "SUM":
functionType = SUM
case "AVG":
functionType = AVG
default:
functionType = 0
}
return functionType
}
type JoinType int
const (
INNER JoinType = iota
LEFT
RIGHT
FULL
SELF
)
// JoinTypeString converts a JoinType integer to its string representation.
func JoinTypeString(t JoinType) string {
switch t {
case INNER:
return "INNER"
case LEFT:
return "LEFT"
case RIGHT:
return "RIGHT"
case FULL:
return "FULL"
case SELF:
return "SELF"
default:
return "UNKNOWN"
}
}
type Column struct {
Name string `json:"name"`
Alias string `json:"alias"`
AggregateFunction AggregateFunctionType `json:"aggregateFunction"`
}
type Join struct {
Type JoinType `json:"type"`
MainTable Table `json:"mainTable"`
JoiningTable Table `json:"joiningTable"`
Ons []Conditional `json:"ons"`
}
type OrderBy struct {
Key string `json:"key"`
IsDescend bool `json:"isDescend"`
}
type Select struct {
Type QueryType `json:"type"`
Table Table `json:"table"`
Columns []Column `json:"columns"`
Conditionals []Conditional `json:"conditionals"`
OrderBys []OrderBy `json:"orderBys"`
Joins []Join `json:"joins"`
IsWildcard bool `json:"isWildcard"`
IsDistinct bool `json:"is_distinct"`
}
func MarshalSelect(selectStatement Select) ([]byte, error) {
jsonData, err := json.MarshalIndent(selectStatement, "", " ")
return jsonData, err
}
//func test() {
//selectStatement := Select{
// Table: "users",
// Columns: []Column{
// {Name: "id", Alias: "user_id", AggregateFunction: COUNT},
// },
// Conditionals: []Conditional{
// {Key: "age", Operator: ">", Value: "18"},
// },
// OrderBys: []OrderBy{
// {Key: "name", IsDescend: false},
// },
// Joins: []Join{
// {
// Type: LEFT,
// Table: Table{Name: "orders"},
// Ons: []Conditional{{Key: "user_id", Operator: "=", Value: "users.id"}},
// }},
//}
//
//jsonData, err := json.MarshalIndent(selectStatement, "", " ")
//if err != nil {
// fmt.Println("Error marshaling JSON:", err)
// return
//}
//
//fmt.Println(string(jsonData))
//
//fmt.Println(QueryTypeString(SELECT))
//fmt.Println(AggregateFunctionTypeString(MAX))
//fmt.Println(JoinTypeString(INNER))
//}