forked from henvic/pgtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
43 lines (36 loc) · 938 Bytes
/
example.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
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/partounian/pgtools/sqltest/example/internal/postgres"
"github.com/jackc/pgx/v4/pgxpool"
)
// Database layer for your application.
type Database struct {
// Postgres connection using the limited postgres.PGX interface,
// instead of *pgxpool.Pool.
// Used as a light way of disencouraging unwarranted access to low-level APIs.
Postgres postgres.PGX
}
// Now gets the current time in the database.
func (db *Database) Now(ctx context.Context) (time.Time, error) {
var now time.Time
err := db.Postgres.QueryRow(ctx, "SELECT NOW();").Scan(&now)
return now, err
}
func main() {
pool, err := pgxpool.Connect(context.Background(), "")
if err != nil {
log.Fatal(err)
}
db := &Database{
Postgres: pool,
}
now, err := db.Now(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("time from PostgreSQL: %s\n", now.Format(time.RubyDate))
}