Separate exec types into files, document code #10

Merged
gdulai merged 3 commits from cleanup into main 2026-07-28 20:16:23 +00:00
3 changed files with 53 additions and 3 deletions
Showing only changes of commit 7553b57ec9 - Show all commits
+12
View File
@@ -10,6 +10,7 @@ import (
log "gitlab.com/gdulai/simpleloglvl" log "gitlab.com/gdulai/simpleloglvl"
) )
// Count represents a count query.
type Count[T any] struct { type Count[T any] struct {
target schema.Table target schema.Table
whereStmt string whereStmt string
@@ -17,6 +18,9 @@ type Count[T any] struct {
result int64 result int64
} }
// CreateCount returns a new instance of Count.
//
// orm is the simpleorm.ORM instance which connects, caches and manages the relations.
func CreateCount[T any](orm *simpleorm.ORM, whereStmt string, args ...any) (Count[T], error) { func CreateCount[T any](orm *simpleorm.ORM, whereStmt string, args ...any) (Count[T], error) {
table, ok := orm.Cache().Get(reflect.TypeFor[T]().Name()) table, ok := orm.Cache().Get(reflect.TypeFor[T]().Name())
if !ok { if !ok {
@@ -26,14 +30,22 @@ func CreateCount[T any](orm *simpleorm.ORM, whereStmt string, args ...any) (Coun
return Count[T]{target: *table, whereStmt: whereStmt, args: args}, nil return Count[T]{target: *table, whereStmt: whereStmt, args: args}, nil
} }
// Result returns the result of the count query.
func (c *Count[T]) Result() int64 { func (c *Count[T]) Result() int64 {
return c.result return c.result
} }
// Execute executes the parsed count query against a connection and returns an error if there was any during the execution.
//
// conn is the simpleorm.DBConnection which handles the database connection.
func (c *Count[T]) Execute(conn *simpleorm.DBConnection) error { func (c *Count[T]) Execute(conn *simpleorm.DBConnection) error {
return c.execute(conn, nil) return c.execute(conn, nil)
} }
// execute executes the parsed count query against a connection and transaction and returns an error if there was any during the execution.
//
// conn is the simpleorm.DBConnection which handles the database connection.
// tx is the sql.Tx transaction which the count query will be run in.
func (c *Count[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error { func (c *Count[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
// Reinit the results, new execution // Reinit the results, new execution
c.result = -1 c.result = -1
+37
View File
@@ -10,12 +10,23 @@ import (
log "gitlab.com/gdulai/simpleloglvl" log "gitlab.com/gdulai/simpleloglvl"
) )
// Insert represents an insert query operation.
type Insert[T any] struct { type Insert[T any] struct {
target schema.Table target schema.Table
toInsert []T toInsert []T
results []T results []T
} }
// Creates a new insert operation for the given model type.
//
// Parameters:
// - orm: The ORM instance that manages the database connection and schema
// - toInsert: A variadic list of model instances to insert
//
// Returns:
// - Insert[T]: A new insert operation instance
// - error: An error if the table could not be found in the schema cache
func NewInsert[T any](orm *simpleorm.ORM, toInsert ...T) (Insert[T], error) { func NewInsert[T any](orm *simpleorm.ORM, toInsert ...T) (Insert[T], error) {
table, ok := orm.Cache().Get(reflect.TypeFor[T]().Name()) table, ok := orm.Cache().Get(reflect.TypeFor[T]().Name())
if !ok { if !ok {
@@ -25,14 +36,40 @@ func NewInsert[T any](orm *simpleorm.ORM, toInsert ...T) (Insert[T], error) {
return Insert[T]{target: *table, toInsert: toInsert}, nil return Insert[T]{target: *table, toInsert: toInsert}, nil
} }
// Returns the results of the insert operation.
//
// Returns:
// - []T: A slice of model instances representing the inserted rows
func (ins *Insert[T]) Results() []T { func (ins *Insert[T]) Results() []T {
return ins.results return ins.results
} }
// Executes the insert operation using the provided database connection.
//
// Parameters:
// - conn: The database connection to use for executing the query
//
// Returns:
// - error: An error if the operation fails
func (ins *Insert[T]) Execute(conn *simpleorm.DBConnection) error { func (ins *Insert[T]) Execute(conn *simpleorm.DBConnection) error {
return ins.execute(conn, nil) return ins.execute(conn, nil)
} }
// Executes the insert operation using the provided database connection or transaction.
//
// Parameters:
// - conn: The database connection to use for executing the query
// - tx: Optional transaction to use instead of a connection
//
// Returns:
// - error: An error if the operation fails
//
// Implementation Details:
// 1. Constructs the SQL INSERT statement using GetInsertDML
// 2. Prepares parameters by flattening nested any values
// 3. Executes the statement and maps results to model instances
func (ins *Insert[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error { func (ins *Insert[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
dml, err := ins.target.GetInsertDML(len(ins.toInsert)) dml, err := ins.target.GetInsertDML(len(ins.toInsert))
if err != nil { if err != nil {
+4 -3
View File
@@ -17,8 +17,9 @@ type ORM struct {
cache *cache.SchemaCache cache *cache.SchemaCache
} }
// Inits the ORM library. // NewOrm inits and creates an instance ORM library.
// Param objs is an array which should be an array of the types which describe the tables. //
// obj is an array which should be an array of the types which describe the tables.
func NewORM(objs ...any) *ORM { func NewORM(objs ...any) *ORM {
typeParsers := make(map[string]*schema.Parser) typeParsers := make(map[string]*schema.Parser)
@@ -57,7 +58,7 @@ func NewORM(objs ...any) *ORM {
return &ORM{cache: cache} return &ORM{cache: cache}
} }
// Builds the DDL and returns it as a string // CreateSchmea builds the DDL and returns it as a string
func (orm *ORM) CreateSchema() (string, error) { func (orm *ORM) CreateSchema() (string, error) {
var ddl strings.Builder var ddl strings.Builder