diff --git a/exec/count.go b/exec/count.go index 6f39da2..3628236 100644 --- a/exec/count.go +++ b/exec/count.go @@ -10,6 +10,7 @@ import ( log "gitlab.com/gdulai/simpleloglvl" ) +// Count represents a count query. type Count[T any] struct { target schema.Table whereStmt string @@ -17,6 +18,9 @@ type Count[T any] struct { 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) { table, ok := orm.Cache().Get(reflect.TypeFor[T]().Name()) 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 } +// Result returns the result of the count query. func (c *Count[T]) Result() int64 { 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 { 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 { // Reinit the results, new execution c.result = -1 diff --git a/exec/insert.go b/exec/insert.go index 41fd5c3..0575066 100644 --- a/exec/insert.go +++ b/exec/insert.go @@ -10,12 +10,23 @@ import ( log "gitlab.com/gdulai/simpleloglvl" ) +// Insert represents an insert query operation. type Insert[T any] struct { target schema.Table toInsert []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) { table, ok := orm.Cache().Get(reflect.TypeFor[T]().Name()) 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 } +// Returns the results of the insert operation. +// +// Returns: +// - []T: A slice of model instances representing the inserted rows + func (ins *Insert[T]) Results() []T { 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 { + 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 { dml, err := ins.target.GetInsertDML(len(ins.toInsert)) if err != nil { diff --git a/orm.go b/orm.go index ebc66c8..3450fe5 100644 --- a/orm.go +++ b/orm.go @@ -17,8 +17,9 @@ type ORM struct { cache *cache.SchemaCache } -// Inits the ORM library. -// Param objs is an array which should be an array of the types which describe the tables. +// NewOrm inits and creates an instance ORM library. +// +// obj is an array which should be an array of the types which describe the tables. func NewORM(objs ...any) *ORM { typeParsers := make(map[string]*schema.Parser) @@ -57,7 +58,7 @@ func NewORM(objs ...any) *ORM { 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) { var ddl strings.Builder