154 lines
3.4 KiB
Go
154 lines
3.4 KiB
Go
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 []schema.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 ...schema.OrderBy) *Select[T] {
|
|
s.ordering = ordering
|
|
return s
|
|
}
|
|
|
|
// Returns the result of the select
|
|
func (s *Select[T]) Results() []T {
|
|
return s.results
|
|
}
|
|
|
|
// Executes the select query based on the Select exec instance
|
|
// Returns an error if theres any
|
|
func (s *Select[T]) Execute(conn *simpleorm.DBConnection) error {
|
|
return s.execute(conn, nil)
|
|
}
|
|
|
|
// Executes the select query based on the Select exec instance
|
|
// If tx is given, the transaction is used instead of the conn
|
|
// Returns an error if theres any
|
|
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)
|
|
}
|
|
|
|
if len(s.ordering) > 0 {
|
|
orderBy, err := s.target.GetOrderByDML(s.ordering...)
|
|
if err != nil {
|
|
log.LogError("Failed to create ORDER BY part: %s", err)
|
|
} else {
|
|
dml += orderBy
|
|
}
|
|
}
|
|
|
|
log.LogInfo("Preparing sql: %s", dml)
|
|
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
|
|
}
|