WIP: Refactor parse

This commit is contained in:
2026-06-05 15:54:47 +02:00
parent d20184a6f0
commit 146f07fa02
10 changed files with 255 additions and 180 deletions
+12 -13
View File
@@ -157,11 +157,6 @@ type Exec[T any] interface {
execute(conn *simpleorm.DBConnection, tx *sql.Tx) error
}
type OrderBy struct {
Column string
Direction string
}
type Count[T any] struct {
target schema.Table
whereStmt string
@@ -425,9 +420,10 @@ func (d *Delete[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
}
func createSelectResultContainer(t schema.Table) []any {
vals := make([]any, t.Type.NumField())
typ := t.Type()
vals := make([]any, typ.NumField())
for i := range vals {
switch t.Type.Field(i).Type.Kind().String() {
switch typ.Field(i).Type.Kind().String() {
case "string":
var fieldContainer string
vals[i] = &fieldContainer
@@ -443,13 +439,14 @@ func createSelectResultContainer(t schema.Table) []any {
}
func prepareParams(src any, t schema.Table) []any {
typ := t.Type()
var params []any
for _, col := range t.Columns {
for _, col := range t.Columns() {
_, ok := col.Modifiers["pk"]
if ok && t.IsPkAuto() {
continue
}
field, ok := t.Type.FieldByName(col.FieldName)
field, ok := typ.FieldByName(col.FieldName)
if !ok {
continue
}
@@ -461,11 +458,12 @@ func prepareParams(src any, t schema.Table) []any {
}
func getPk(src any, t schema.Table) ([]any, error) {
typ := t.Type()
var values []any
for _, constraint := range t.Constraints {
for _, constraint := range t.Constraints() {
if constraint.Type == "pk" {
for _, col := range constraint.Columns {
field, ok := t.Type.FieldByName(col.FieldName)
field, ok := typ.FieldByName(col.FieldName)
if !ok {
continue
@@ -493,10 +491,11 @@ func readRows[T any](table schema.Table, rows *sql.Rows) ([]T, error) {
return nil, err
}
targetType := table.Type
targetType := table.Type()
cols := table.Columns()
parsedResult := reflect.New(targetType)
for i, fieldVal := range rowContainer {
col := table.Columns[i]
col := cols[i]
targetField := parsedResult.Elem().Field(i)
rawValue := reflect.Indirect(reflect.ValueOf(fieldVal))
+16 -2
View File
@@ -17,7 +17,7 @@ type Select[T any] struct {
args []any
limit int64
offset int64
ordering []OrderBy
ordering []schema.OrderBy
results []T
}
@@ -55,7 +55,7 @@ func (s *Select[T]) Offset(offset int64) *Select[T] {
// 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] {
func (s *Select[T]) OrderBy(ordering ...schema.OrderBy) *Select[T] {
s.ordering = ordering
return s
}
@@ -65,10 +65,15 @@ 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{}
@@ -92,6 +97,15 @@ func (s *Select[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
effectiveArgs = append(effectiveArgs, s.offset)
}
if len(s.ordering) > 0 {
dml += " ORDER BY"
for _, orderBy := range s.ordering {
dml += " " + orderBy.Field + " " + orderBy.Direction
}
}
log.LogInfo("Preparing sql: %s", dml)
log.LogDebug("Preparing sql: %s, with args: %s", dml, effectiveArgs)
var stmt *sql.Stmt