-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
52 lines (43 loc) · 1.47 KB
/
data.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
package data
import (
"errors"
"github.com/PacktPublishing/Hands-On-Dependency-Injection-in-Go/ch12/04_new_service/01_data_with_cache/internal/logging"
_ "github.com/go-sql-driver/mysql"
)
const (
// SQL statements as constants (to reduce duplication and maintenance in tests)
sqlAllColumns = "id, fullname, phone, currency, price"
sqlLoadByID = "SELECT " + sqlAllColumns + " FROM person WHERE id = ? LIMIT 1"
)
var (
// ErrNotFound is returned when the no records where matched by the query
ErrNotFound = errors.New("not found")
)
// Config is the configuration for the data package
type Config interface {
// Logger returns a reference to the logger
Logger() logging.Logger
// DataDSN returns the data source name
DataDSN() string
}
// Person is the data transfer object (DTO) for this package
type Person struct {
// ID is the unique ID for this person
ID int
// FullName is the name of this person
FullName string
// Phone is the phone for this person
Phone string
// Currency is the currency this person has paid in
Currency string
// Price is the amount (in the above currency) paid by this person
Price float64
}
// custom type so we can convert sql results to easily
type scanner func(dest ...interface{}) error
// reduce the duplication (and maintenance) between sql.Row and sql.Rows usage
func populatePerson(scanner scanner) (*Person, error) {
out := &Person{}
err := scanner(&out.ID, &out.FullName, &out.Phone, &out.Currency, &out.Price)
return out, err
}