Documentation

This commit is contained in:
2026-07-28 22:04:34 +02:00
parent 8d91fc9aab
commit 7553b57ec9
3 changed files with 53 additions and 3 deletions
+12
View File
@@ -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