-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
62 lines (49 loc) · 1.4 KB
/
session.go
File metadata and controls
62 lines (49 loc) · 1.4 KB
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
package pg
import (
"database/sql"
"errors"
_ "github.com/lib/pq"
)
var (
ERecordNotFound = errors.New("Record not found")
EMultipleResults = errors.New("Unexpected multiple results from query")
)
// Session implements the EntityHandler and the ResultHandler interface
type Session struct {
DB *sql.DB
}
func NewSession(connectionString string) (*Session, error) {
db, err := sql.Open("postgres", connectionString)
if err != nil {
return nil, err
}
return &Session{db}, nil
}
func (s *Session) Create(entity Entity) error {
return create(s.DB, entity)
}
func (s *Session) FindOne(entity Entity, where string, whereParams ...interface{}) (Entity, error) {
return findOne(s.DB, entity, where, whereParams...)
}
func (s *Session) FindAll(entity Entity, where string, whereParams ...interface{}) ([]Entity, error) {
return findAll(s.DB, entity, where, whereParams...)
}
func (s *Session) Update(entity Entity) error {
return update(s.DB, entity)
}
func (s *Session) Delete(entity Entity) error {
return delete(s.DB, entity)
}
func (s *Session) Query(result Result, sql string, params ...interface{}) ([]Result, error) {
return query(s.DB, result, sql, params...)
}
func (s *Session) Exec(query string, args ...interface{}) (sql.Result, error) {
return exec(s.DB, query, args...)
}
func (s *Session) Tx() (*Tx, error) {
tx, err := s.DB.Begin()
if err != nil {
return nil, err
}
return &Tx{tx}, nil
}