WIP: Refactor Exec implementations to be chainable
This commit is contained in:
+138
@@ -0,0 +1,138 @@
|
||||
package exec
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"reflect"
|
||||
|
||||
"git.gdulai.com/gdulai/simpleorm"
|
||||
"git.gdulai.com/gdulai/simpleorm/schema"
|
||||
log "gitlab.com/gdulai/simpleloglvl"
|
||||
)
|
||||
|
||||
// Select execution which is translated to a select sql query.
|
||||
type Select[T any] struct {
|
||||
target schema.Table
|
||||
whereStmt string
|
||||
args []any
|
||||
limit int64
|
||||
offset int64
|
||||
ordering []OrderBy
|
||||
results []T
|
||||
}
|
||||
|
||||
// Creates the select query builder
|
||||
func CreateSelect[T any](orm *simpleorm.ORM) (Select[T], error) {
|
||||
table, ok := orm.Cache().Get(reflect.TypeFor[T]().Name())
|
||||
if !ok {
|
||||
return Select[T]{}, errors.New("Failed to get table from schema cache")
|
||||
}
|
||||
|
||||
return Select[T]{target: *table, limit: -1, offset: -1}, nil
|
||||
}
|
||||
|
||||
// Sets the where part of the select query and the arguments
|
||||
// Returns the pointer of the Select instance
|
||||
func (s *Select[T]) Where(whereStmt string, args ...any) *Select[T] {
|
||||
s.whereStmt = whereStmt
|
||||
s.args = args
|
||||
return s
|
||||
}
|
||||
|
||||
// Sets the limit part of the select query
|
||||
// Returns the pointer of the Select instance
|
||||
func (s *Select[T]) Limit(limit int64) *Select[T] {
|
||||
s.limit = limit
|
||||
return s
|
||||
}
|
||||
|
||||
// Sets the offset part of the select query
|
||||
// Returns the pointer of the Select instance
|
||||
func (s *Select[T]) Offset(offset int64) *Select[T] {
|
||||
s.offset = offset
|
||||
return s
|
||||
}
|
||||
|
||||
// Sets the order by part of the select query
|
||||
// Returns the pointer of the Select instance
|
||||
func (s *Select[T]) OrderBy(ordering ...OrderBy) *Select[T] {
|
||||
s.ordering = ordering
|
||||
return s
|
||||
}
|
||||
|
||||
// Returns the result of the select
|
||||
func (s *Select[T]) Results() []T {
|
||||
return s.results
|
||||
}
|
||||
|
||||
func (s *Select[T]) Execute(conn *simpleorm.DBConnection) error {
|
||||
return s.execute(conn, nil)
|
||||
}
|
||||
|
||||
func (s *Select[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
|
||||
// Reinit the results, new execution
|
||||
s.results = []T{}
|
||||
dml, err := s.target.GetSelectDML()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if s.whereStmt != "" {
|
||||
dml += " WHERE " + s.whereStmt
|
||||
}
|
||||
|
||||
var effectiveArgs []any = s.args
|
||||
if s.limit != -1 {
|
||||
dml += " LIMIT ?"
|
||||
effectiveArgs = append(effectiveArgs, s.limit)
|
||||
}
|
||||
|
||||
if s.offset != -1 {
|
||||
dml += " OFFSET ?"
|
||||
effectiveArgs = append(effectiveArgs, s.offset)
|
||||
}
|
||||
|
||||
log.LogDebug("Preparing sql: %s, with args: %s", dml, effectiveArgs)
|
||||
|
||||
var stmt *sql.Stmt
|
||||
if tx != nil {
|
||||
stmt, err = tx.Prepare(dml)
|
||||
} else {
|
||||
stmt, err = conn.Prepare(dml)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
log.LogDebug("Executing statement: %s", stmt)
|
||||
|
||||
var rows *sql.Rows
|
||||
if len(effectiveArgs) == 0 {
|
||||
rows, err = stmt.Query()
|
||||
} else {
|
||||
// Flattent args to make sure it can be parsed correctly
|
||||
var flatArgs []any
|
||||
for _, a := range effectiveArgs {
|
||||
if s, ok := a.([]any); ok {
|
||||
flatArgs = append(flatArgs, s...)
|
||||
} else {
|
||||
flatArgs = append(flatArgs, a)
|
||||
}
|
||||
}
|
||||
rows, err = stmt.Query(flatArgs...)
|
||||
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.results, err = readRows[T](s.target, rows)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user