2019-03-31 14:07:58 +02:00
|
|
|
package sqlbuilder
|
2019-04-14 17:55:10 +02:00
|
|
|
|
2019-04-20 19:49:29 +02:00
|
|
|
import (
|
|
|
|
|
"database/sql"
|
2019-04-29 14:39:48 +02:00
|
|
|
"errors"
|
2019-06-05 17:56:24 +02:00
|
|
|
"github.com/go-jet/jet/sqlbuilder/execution"
|
2019-06-14 14:35:50 +02:00
|
|
|
"github.com/serenize/snaker"
|
2019-06-05 17:15:20 +02:00
|
|
|
"reflect"
|
2019-04-20 19:49:29 +02:00
|
|
|
)
|
2019-04-14 17:55:10 +02:00
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func serializeOrderByClauseList(statement statementType, orderByClauses []OrderByClause, out *queryData) error {
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
for i, value := range orderByClauses {
|
2019-04-14 17:55:10 +02:00
|
|
|
if i > 0 {
|
2019-05-08 13:47:01 +02:00
|
|
|
out.writeString(", ")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
err := value.serializeForOrderBy(statement, out)
|
2019-04-14 17:55:10 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
func serializeGroupByClauseList(statement statementType, clauses []groupByClause, out *queryData) (err error) {
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
for i, c := range clauses {
|
2019-04-14 17:55:10 +02:00
|
|
|
if i > 0 {
|
2019-05-08 13:47:01 +02:00
|
|
|
out.writeString(", ")
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
|
return errors.New("nil clause.")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
if err = c.serializeForGroupBy(statement, out); err != nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
func serializeClauseList(statement statementType, clauses []clause, out *queryData) (err error) {
|
2019-05-07 19:06:21 +02:00
|
|
|
|
|
|
|
|
for i, c := range clauses {
|
|
|
|
|
if i > 0 {
|
2019-05-08 13:47:01 +02:00
|
|
|
out.writeString(", ")
|
2019-05-07 19:06:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
|
return errors.New("nil clause.")
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
if err = c.serialize(statement, out); err != nil {
|
2019-05-07 19:06:21 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func serializeExpressionList(statement statementType, expressions []Expression, separator string, out *queryData) error {
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
for i, value := range expressions {
|
|
|
|
|
if i > 0 {
|
2019-05-08 13:47:01 +02:00
|
|
|
out.writeString(separator)
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
err := value.serialize(statement, out)
|
2019-04-14 17:55:10 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-04-20 19:49:29 +02:00
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
func serializeProjectionList(statement statementType, projections []projection, out *queryData) error {
|
2019-04-29 14:39:48 +02:00
|
|
|
for i, col := range projections {
|
|
|
|
|
if i > 0 {
|
2019-05-12 18:15:23 +02:00
|
|
|
out.writeString(",")
|
2019-06-14 14:35:50 +02:00
|
|
|
out.newLine()
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
2019-05-12 18:15:23 +02:00
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
if col == nil {
|
2019-06-15 13:58:45 +02:00
|
|
|
return errors.New("projection is nil")
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
if err := col.serializeForProjection(statement, out); err != nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
func serializeColumnNames(columns []column, out *queryData) error {
|
2019-04-29 14:39:48 +02:00
|
|
|
for i, col := range columns {
|
|
|
|
|
if i > 0 {
|
2019-06-14 14:35:50 +02:00
|
|
|
out.writeString(", ")
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if col == nil {
|
2019-06-14 14:35:50 +02:00
|
|
|
return errors.New("nil column in columns list")
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
out.writeString(col.Name())
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
func columnListToProjectionList(columns []Column) []projection {
|
|
|
|
|
var ret []projection
|
|
|
|
|
|
|
|
|
|
for _, column := range columns {
|
|
|
|
|
ret = append(ret, column)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 17:15:20 +02:00
|
|
|
func isNil(v interface{}) bool {
|
|
|
|
|
return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil())
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
func valueToClause(value interface{}) clause {
|
|
|
|
|
if clause, ok := value.(clause); ok {
|
|
|
|
|
return clause
|
|
|
|
|
} else {
|
|
|
|
|
return literal(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func unwindRowFromModel(columns []column, data interface{}) []clause {
|
|
|
|
|
structValue := reflect.Indirect(reflect.ValueOf(data))
|
|
|
|
|
|
|
|
|
|
row := []clause{}
|
|
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
mustBe(structValue, reflect.Struct)
|
2019-06-14 14:35:50 +02:00
|
|
|
|
|
|
|
|
for _, column := range columns {
|
|
|
|
|
columnName := column.Name()
|
|
|
|
|
structFieldName := snaker.SnakeToCamel(columnName)
|
|
|
|
|
|
|
|
|
|
structField := structValue.FieldByName(structFieldName)
|
|
|
|
|
|
|
|
|
|
if !structField.IsValid() {
|
2019-06-15 13:58:45 +02:00
|
|
|
panic("missing struct field for column : " + column.Name())
|
2019-06-14 14:35:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var field interface{}
|
|
|
|
|
|
|
|
|
|
if structField.Kind() == reflect.Ptr && structField.IsNil() {
|
|
|
|
|
field = nil
|
|
|
|
|
} else {
|
|
|
|
|
field = reflect.Indirect(structField).Interface()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
row = append(row, literal(field))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return row
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func unwindRowFromValues(value interface{}, values []interface{}) []clause {
|
|
|
|
|
row := []clause{}
|
|
|
|
|
|
|
|
|
|
allValues := append([]interface{}{value}, values...)
|
|
|
|
|
|
|
|
|
|
for _, val := range allValues {
|
|
|
|
|
row = append(row, valueToClause(val))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return row
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
func mustBe(v reflect.Value, expected reflect.Kind) {
|
|
|
|
|
if k := v.Kind(); k != expected {
|
|
|
|
|
panic("argument mismatch: expected " + expected.String() + ", got " + v.Type().String())
|
2019-06-09 11:06:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-06-02 12:45:46 +02:00
|
|
|
|
2019-05-27 13:11:15 +02:00
|
|
|
func Query(statement Statement, db execution.Db, destination interface{}) error {
|
2019-04-29 14:39:48 +02:00
|
|
|
query, args, err := statement.Sql()
|
2019-04-20 19:49:29 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
return execution.Query(db, query, args, destination)
|
2019-04-20 19:49:29 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
func Exec(statement Statement, db execution.Db) (res sql.Result, err error) {
|
2019-04-29 14:39:48 +02:00
|
|
|
query, args, err := statement.Sql()
|
2019-04-20 19:49:29 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
return db.Exec(query, args...)
|
2019-04-20 19:49:29 +02:00
|
|
|
}
|