Delete
SQL DELETE statements can be executed using the genorm.Delete
function.
It must start with a genorm.Delete
function and end with either Do
or DoCtx
, but the rest of the order can be swapped. However, if the same method other than OrderBy
is executed twice in the same method chain, Do
and DoCtx
will return an error without querying at runtime.
Example
// DELETE FROM `users`
affectedRows, err = genorm.
Delete(orm.User()).
Do(db)
// DELETE FROM `users`
affectedRows, err = genorm.
Delete(orm.User()).
DoCtx(context.Background(), db)
// DELETE FROM `users` WHERE `id`={{uuid.New()}}
affectedRows, err = genorm.
Delete(orm.User()).
Where(genorm.EqLit(user.IDExpr, uuid.New())).
Do(db)
// DELETE FROM `users` ORDER BY `created_at` LIMIT 1
affectedRows, err = genorm.
Delete(orm.User()).
OrderBy(genorm.Desc, user.CreatedAt).
Limit(1).
Do(db)
Delete
Function to start a chain of methods to issue a DELETE
statement. It takes a table generated by the CLI as its first argument. The DELETE
statement cannot be executed on a joined table, so a joined table is not given.
Where(optional)
Sets the WHERE
clause in the DELETE
statement. The first argument takes an expression corresponding to bool
in Go language.
Do
Executes the query and exits the method chain. The argument is a value satisfying the genorm.DB
interface including *sql.DB
/*sql.Tx
It takes this and uses it to execute the query.
DoCtx
Executes the query and exits the method chain. The second argument is a value satisfying the genorm.DB
interface including *sql.DB
/*sql.Tx
It takes this and uses it to execute the query. It also receives context.Context
as its first argument. When context is canceled, the connection to the database is released, preventing unnecessary use of connections.