-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_delete.go
40 lines (33 loc) · 1.13 KB
/
sql_delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package ssql
import (
"context"
"database/sql"
"fmt"
)
func (c Client) DeleteOne(ctx context.Context, table string, idKey, idValue string) (*sql.Result, error) {
stmt, err := c.db.Prepare(fmt.Sprintf("DELETE FROM \"%s\" WHERE %s=$1", table, idKey))
if err != nil {
c.l.error(err, "Error preparing postgres deleting")
return nil, err
}
defer stmt.Close() // Prepared statements take up server resources and should be closed after use.
result, err := stmt.Exec(idValue)
if err != nil {
c.l.error(err, "Error deleting to postgres")
}
return &result, err
}
func (c Client) Delete(ctx context.Context, table string, conditions []*ConditionPair) (*sql.Result, error) {
whereStm, args := getSQLWhereClauseFromConditions(conditions, 0)
stmt, err := c.db.Prepare(fmt.Sprintf("DELETE FROM \"%s\" WHERE %s", table, whereStm))
if err != nil {
c.l.error(err, "Error preparing postgres deleting")
return nil, err
}
defer stmt.Close() // Prepared statements take up server resources and should be closed after use.
result, err := stmt.Exec(args...)
if err != nil {
c.l.error(err, "Error deleting to postgres")
}
return &result, err
}