Insert and Update statement improvements.
This commit is contained in:
parent
038a4b9dd0
commit
a4feb66692
22 changed files with 660 additions and 453 deletions
|
|
@ -4,6 +4,7 @@ import (
|
|||
"database/sql"
|
||||
"errors"
|
||||
"github.com/go-jet/jet/sqlbuilder/execution"
|
||||
"github.com/serenize/snaker"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
|
|
@ -83,7 +84,7 @@ func serializeProjectionList(statement statementType, projections []projection,
|
|||
for i, col := range projections {
|
||||
if i > 0 {
|
||||
out.writeString(",")
|
||||
out.nextLine()
|
||||
out.newLine()
|
||||
}
|
||||
|
||||
if col == nil {
|
||||
|
|
@ -98,14 +99,14 @@ func serializeProjectionList(statement statementType, projections []projection,
|
|||
return nil
|
||||
}
|
||||
|
||||
func serializeColumnList(statement statementType, columns []Column, out *queryData) error {
|
||||
func serializeColumnNames(columns []column, out *queryData) error {
|
||||
for i, col := range columns {
|
||||
if i > 0 {
|
||||
out.writeByte(',')
|
||||
out.writeString(", ")
|
||||
}
|
||||
|
||||
if col == nil {
|
||||
return errors.New("nil column in columns list.")
|
||||
return errors.New("nil column in columns list")
|
||||
}
|
||||
|
||||
out.writeString(col.Name())
|
||||
|
|
@ -118,6 +119,59 @@ func isNil(v interface{}) bool {
|
|||
return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil())
|
||||
}
|
||||
|
||||
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{}
|
||||
|
||||
if structValue.Kind() != reflect.Struct {
|
||||
return row
|
||||
}
|
||||
|
||||
for _, column := range columns {
|
||||
columnName := column.Name()
|
||||
structFieldName := snaker.SnakeToCamel(columnName)
|
||||
|
||||
structField := structValue.FieldByName(structFieldName)
|
||||
|
||||
if !structField.IsValid() {
|
||||
continue
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func columnListToProjectionList(columns []Column) []projection {
|
||||
var ret []projection
|
||||
|
||||
|
|
@ -138,7 +192,7 @@ func Query(statement Statement, db execution.Db, destination interface{}) error
|
|||
return execution.Query(db, query, args, destination)
|
||||
}
|
||||
|
||||
func Execute(statement Statement, db execution.Db) (res sql.Result, err error) {
|
||||
func Exec(statement Statement, db execution.Db) (res sql.Result, err error) {
|
||||
query, args, err := statement.Sql()
|
||||
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue