-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
88 lines (75 loc) · 2.2 KB
/
helper.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package sqlite
import (
"context"
"database/sql"
"time"
"github.com/zalgonoise/x/errors"
"golang.org/x/exp/constraints"
)
// Scanner describes an object that scans values into destination pointers
type Scanner interface {
Scan(dest ...interface{}) error
}
// RowQuerier describes an object that queries a single row in a SQL database
type RowQuerier interface {
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}
// RowsQuerier describes an object that queries multiple rows in a SQL database
type RowsQuerier interface {
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}
// Executer describes an object that executes a mutation in a SQL database
type Executer interface {
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}
// Querier encapsulates a RowQuerier, RowsQuerier and Executer implementations
type Querier interface {
RowQuerier
RowsQuerier
Executer
}
// ToSQLString converts the input string into a sql.NullString
func ToSQLString(s string) sql.NullString {
return sql.NullString{String: s, Valid: s != ""}
}
// ToSQLInt64 converts the generic integer into a sql.NullInt64
func ToSQLInt64[T constraints.Integer](v T) sql.NullInt64 {
return sql.NullInt64{Int64: int64(v), Valid: v >= 0}
}
// ToSQLTime converts the input time into a sql.NullTime
func ToSQLTime(t time.Time) sql.NullTime {
return sql.NullTime{Time: t, Valid: t != time.Time{} && t.Unix() != 0}
}
// IsUserFound returns an error if the entity is not found
func IsUserFound(res sql.Result) error {
n, err := res.RowsAffected()
if err != nil {
return errors.Join(ErrDBError, err)
}
if n == 0 {
return ErrNotFoundUser
}
return nil
}
// IsSecretFound returns an error if the entity is not found
func IsSecretFound(res sql.Result) error {
n, err := res.RowsAffected()
if err != nil {
return errors.Join(ErrDBError, err)
}
if n == 0 {
return ErrNotFoundSecret
}
return nil
}
// IsShareFound returns an error if the entity is not found
func IsShareFound(res sql.Result) error {
n, err := res.RowsAffected()
if err != nil {
return errors.Join(ErrDBError, err)
}
if n == 0 {
return ErrNotFoundShare
}
return nil
}