forked from chrobson/RedisCache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.go
More file actions
43 lines (33 loc) · 841 Bytes
/
database.go
File metadata and controls
43 lines (33 loc) · 841 Bytes
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
package main
import (
"database/sql"
_ "github.com/lib/pq"
)
type Database interface {
GetUserById(string) (*Person, error)
}
type PostgresDatabase struct {
db *sql.DB
}
func NewPostgresDatabase() (*PostgresDatabase, error) {
connStr := "user=postgres dbname=people password=postgrespw host=127.0.0.1 port=49153 sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
return nil, err
}
if err := db.Ping(); err != nil {
return nil, err
}
return &PostgresDatabase{
db: db,
}, nil
}
func (pd *PostgresDatabase) GetUserById(id string) (*Person, error) {
row := pd.db.QueryRow("SELECT * FROM people WHERE id=$1", id)
person := new(Person)
err := row.Scan(&person.Id, &person.FirstName, &person.Secondname, &person.Mail, &person.Gender)
if err != nil {
return nil, err
}
return person, err
}