Separate exec types into files, document code #10
+21
-2
@@ -10,11 +10,21 @@ import (
|
|||||||
log "gitlab.com/gdulai/simpleloglvl"
|
log "gitlab.com/gdulai/simpleloglvl"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Delete represents a delete operation.
|
||||||
type Delete[T any] struct {
|
type Delete[T any] struct {
|
||||||
target schema.Table
|
target schema.Table // The table from which records will be deleted.
|
||||||
toDelete []T
|
toDelete []T // A slice of model instances to be deleted.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewDelete creates a new instance of Delete.
|
||||||
|
//
|
||||||
|
// Parameters:
|
||||||
|
// - orm: The ORM instance that manages the database connection and schema
|
||||||
|
// - toDelete: A slice of model instances to delete
|
||||||
|
//
|
||||||
|
// Returns:
|
||||||
|
// - Delete[T]: A new Delete operation instance
|
||||||
|
// - error: An error if the table could not be found in the schema cache
|
||||||
func NewDelete[T any](orm *simpleorm.ORM, toDelete []T) (Delete[T], error) {
|
func NewDelete[T any](orm *simpleorm.ORM, toDelete []T) (Delete[T], error) {
|
||||||
table, ok := orm.Cache().Get(reflect.TypeFor[T]().Name())
|
table, ok := orm.Cache().Get(reflect.TypeFor[T]().Name())
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -24,10 +34,19 @@ func NewDelete[T any](orm *simpleorm.ORM, toDelete []T) (Delete[T], error) {
|
|||||||
return Delete[T]{target: *table, toDelete: toDelete}, nil
|
return Delete[T]{target: *table, toDelete: toDelete}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Execute executes the delete operation.
|
||||||
func (d *Delete[T]) Execute(conn *simpleorm.DBConnection) error {
|
func (d *Delete[T]) Execute(conn *simpleorm.DBConnection) error {
|
||||||
return d.execute(conn, nil)
|
return d.execute(conn, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// execute constructs and executes the DELETE SQL statement.
|
||||||
|
//
|
||||||
|
// 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
|
||||||
func (d *Delete[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
|
func (d *Delete[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
|
||||||
count := len(d.toDelete)
|
count := len(d.toDelete)
|
||||||
dml, err := d.target.GetDeleteDML(count)
|
dml, err := d.target.GetDeleteDML(count)
|
||||||
|
|||||||
+21
-2
@@ -10,11 +10,21 @@ import (
|
|||||||
log "gitlab.com/gdulai/simpleloglvl"
|
log "gitlab.com/gdulai/simpleloglvl"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Update represents an update operation.
|
||||||
type Update[T any] struct {
|
type Update[T any] struct {
|
||||||
target schema.Table
|
target schema.Table // The table where records will be updated.
|
||||||
toUpdate T
|
toUpdate T // A model instance with the new values.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewUpdate creates a new instance of Update.
|
||||||
|
//
|
||||||
|
// Parameters:
|
||||||
|
// - orm: The ORM instance that manages the database connection and schema
|
||||||
|
// - toUpdate: A model instance with new values
|
||||||
|
//
|
||||||
|
// Returns:
|
||||||
|
// - Update[T]: A new Update operation instance
|
||||||
|
// - error: An error if the table could not be found in the schema cache
|
||||||
func NewUpdate[T any](orm *simpleorm.ORM, toUpdate T) (Update[T], error) {
|
func NewUpdate[T any](orm *simpleorm.ORM, toUpdate T) (Update[T], error) {
|
||||||
table, ok := orm.Cache().Get(reflect.TypeFor[T]().Name())
|
table, ok := orm.Cache().Get(reflect.TypeFor[T]().Name())
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -24,10 +34,19 @@ func NewUpdate[T any](orm *simpleorm.ORM, toUpdate T) (Update[T], error) {
|
|||||||
return Update[T]{target: *table, toUpdate: toUpdate}, nil
|
return Update[T]{target: *table, toUpdate: toUpdate}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Execute executes the update operation.
|
||||||
func (u Update[T]) Execute(conn *simpleorm.DBConnection) error {
|
func (u Update[T]) Execute(conn *simpleorm.DBConnection) error {
|
||||||
return u.execute(conn, nil)
|
return u.execute(conn, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// execute constructs and executes the UPDATE SQL statement.
|
||||||
|
//
|
||||||
|
// 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
|
||||||
func (u Update[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
|
func (u Update[T]) execute(conn *simpleorm.DBConnection, tx *sql.Tx) error {
|
||||||
dml, err := u.target.GetUpdateDML()
|
dml, err := u.target.GetUpdateDML()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user