first commit
This commit is contained in:
74
qry_delete.go
Normal file
74
qry_delete.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package pgm
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type (
|
||||
deleteQry struct {
|
||||
table string
|
||||
condition []Conditioner
|
||||
args []any
|
||||
debug bool
|
||||
}
|
||||
)
|
||||
|
||||
func (t *Table) Delete() WhereOrExec {
|
||||
qb := &deleteQry{
|
||||
table: t.Name,
|
||||
debug: t.debug,
|
||||
}
|
||||
|
||||
return qb
|
||||
}
|
||||
|
||||
func (q *deleteQry) Where(cond ...Conditioner) WhereOrExec {
|
||||
q.condition = append(q.condition, cond...)
|
||||
return q
|
||||
}
|
||||
|
||||
func (q *deleteQry) Exec(ctx context.Context) error {
|
||||
_, err := poolPGX.Load().Exec(ctx, q.String(), q.args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *deleteQry) ExecTx(ctx context.Context, tx pgx.Tx) error {
|
||||
_, err := tx.Exec(ctx, q.String(), q.args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *deleteQry) String() string {
|
||||
sb := getSB()
|
||||
defer putSB(sb)
|
||||
|
||||
n := len("DELETE FROM ") + len(q.table) + 20
|
||||
sb.Grow(n)
|
||||
|
||||
// DELETE
|
||||
sb.WriteString("DELETE FROM ")
|
||||
sb.WriteString(q.table)
|
||||
|
||||
// WHERE
|
||||
if len(q.condition) > 0 {
|
||||
sb.WriteString(" WHERE ")
|
||||
var argIdx int
|
||||
for i, c := range q.condition {
|
||||
argIdx = len(q.args)
|
||||
if i > 0 {
|
||||
sb.WriteString(" AND ")
|
||||
}
|
||||
sb.WriteString(c.Condition(&q.args, argIdx))
|
||||
}
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
Reference in New Issue
Block a user