Skip to content

Commit 17d4eaf

Browse files
committed
added Connection.Now()
1 parent 869a555 commit 17d4eaf

File tree

10 files changed

+60
-15
lines changed

10 files changed

+60
-15
lines changed

connection.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ type Connection interface {
4545
// to create this connection.
4646
Config() *Config
4747

48+
// Now returns the result of the SQL now()
49+
// function for the current connection.
50+
// Useful for getting the timestamp of a
51+
// SQL transaction for use in Go code.
52+
Now() (time.Time, error)
53+
4854
// Exec executes a query with optional args.
4955
Exec(query string, args ...any) error
5056

errors.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ func (e connectionWithError) Config() *Config {
100100
return &Config{Driver: "ConnectionWithError"}
101101
}
102102

103+
func (e connectionWithError) Now() (time.Time, error) {
104+
return time.Time{}, e.err
105+
}
106+
103107
func (e connectionWithError) Exec(query string, args ...any) error {
104108
return e.err
105109
}

go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ module github.com/domonda/go-sqldb
33
go 1.18
44

55
require (
6-
github.com/domonda/go-errs v0.0.0-20220317124005-ae77873647f5
7-
github.com/domonda/go-types v0.0.0-20220406183531-66c6125b4053
6+
github.com/domonda/go-errs v0.0.0-20220429185920-b85918b964f9
7+
github.com/domonda/go-types v0.0.0-20220513100758-7818d7fcb7bb
88
github.com/go-sql-driver/mysql v1.6.0
9-
github.com/lib/pq v1.10.4
9+
github.com/lib/pq v1.10.6
1010
github.com/stretchr/testify v1.7.1
1111
)
1212

@@ -17,7 +17,7 @@ require (
1717
github.com/kr/pretty v0.1.0 // indirect
1818
github.com/pmezard/go-difflib v1.0.0 // indirect
1919
github.com/ungerik/go-reflection v0.0.0-20220113085621-6c5fc1f2694a // indirect
20-
golang.org/x/exp v0.0.0-20220407100705-7b9b53b0aca4 // indirect
20+
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf // indirect
2121
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
22-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
22+
gopkg.in/yaml.v3 v3.0.0 // indirect
2323
)

go.sum

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4-
github.com/domonda/go-errs v0.0.0-20220317124005-ae77873647f5 h1:XkFYt/h2t9NeI04R463HihLj3YzP/PDCGMmane++6dc=
5-
github.com/domonda/go-errs v0.0.0-20220317124005-ae77873647f5/go.mod h1:Gd1uZZ8DXmCjmqwcCmxjThzCzVvCXJ3lWka2ND/kKss=
4+
github.com/domonda/go-errs v0.0.0-20220429185920-b85918b964f9 h1:2Me+4z758BNweppw7TjZG69BUBTsiPDgTrxgMH90TBE=
5+
github.com/domonda/go-errs v0.0.0-20220429185920-b85918b964f9/go.mod h1:Gd1uZZ8DXmCjmqwcCmxjThzCzVvCXJ3lWka2ND/kKss=
66
github.com/domonda/go-pretty v0.0.0-20220317123925-dd9e6bef129a h1:6/Is0KGl5Ot3E8ZLAgAFWYiSRdU+3t3jL38+5yIlCV4=
77
github.com/domonda/go-pretty v0.0.0-20220317123925-dd9e6bef129a/go.mod h1:3QkM8UJdyJMeKZiIo7hYzSkQBpRS3k0gOHw4ysyEIB4=
8-
github.com/domonda/go-types v0.0.0-20220406183531-66c6125b4053 h1:5wUPa/dTeZY6BBRCGoKZuEPgnIZWnZB0lx+IWYCbWJc=
9-
github.com/domonda/go-types v0.0.0-20220406183531-66c6125b4053/go.mod h1:9ua3vAmznw3YURkKRv5ZrLJPSpzIvB65tQHDST4b7O8=
8+
github.com/domonda/go-types v0.0.0-20220513100758-7818d7fcb7bb h1:/+ltgjE+3/CrcxBxij/4sJgg9EXjGb733wqkTGifhDY=
9+
github.com/domonda/go-types v0.0.0-20220513100758-7818d7fcb7bb/go.mod h1:9+VXi/vJHmCp2Nl/hQB+UtJbPA8aUWAfAulGX86i1EA=
1010
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
1111
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
1212
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
@@ -16,20 +16,20 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
1616
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1717
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1818
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
19-
github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk=
20-
github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
19+
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
20+
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
2121
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2222
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2323
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2424
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
2525
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2626
github.com/ungerik/go-reflection v0.0.0-20220113085621-6c5fc1f2694a h1:9vfYtqoyrPw08TbSLxkSXEflp6iXa3RL86Qjs+DrVas=
2727
github.com/ungerik/go-reflection v0.0.0-20220113085621-6c5fc1f2694a/go.mod h1:6Hnd2/4g3Tpt6TjvxHx8wXOZziwApVxRdIGkr7vNpXs=
28-
golang.org/x/exp v0.0.0-20220407100705-7b9b53b0aca4 h1:K3x+yU+fbot38x5bQbU2QqUAVyYLEktdNH2GxZLnM3U=
29-
golang.org/x/exp v0.0.0-20220407100705-7b9b53b0aca4/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
28+
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf h1:oXVg4h2qJDd9htKxb5SCpFBHLipW6hXmL3qpUixS2jw=
29+
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys=
3030
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3131
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
3232
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3333
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
34-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
35-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
34+
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
35+
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

impl/connection.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ func (conn *connection) Config() *sqldb.Config {
7575
return conn.config
7676
}
7777

78+
func (conn *connection) Now() (time.Time, error) {
79+
return Now(conn)
80+
}
81+
7882
func (conn *connection) Exec(query string, args ...any) error {
7983
_, err := conn.db.ExecContext(conn.ctx, query, args...)
8084
return WrapNonNilErrorWithQuery(err, query, conn.argFmt, args)

impl/now.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package impl
2+
3+
import (
4+
"time"
5+
6+
"github.com/domonda/go-sqldb"
7+
)
8+
9+
func Now(conn sqldb.Connection) (now time.Time, err error) {
10+
err = conn.QueryRow(`select now()`).Scan(&now)
11+
if err != nil {
12+
return time.Time{}, err
13+
}
14+
return now, nil
15+
}

impl/transaction.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func (conn *transaction) Ping(timeout time.Duration) error { return conn.parent.
5757
func (conn *transaction) Stats() sql.DBStats { return conn.parent.Stats() }
5858
func (conn *transaction) Config() *sqldb.Config { return conn.parent.Config() }
5959

60+
func (conn *transaction) Now() (time.Time, error) {
61+
return Now(conn)
62+
}
63+
6064
func (conn *transaction) Exec(query string, args ...any) error {
6165
_, err := conn.tx.Exec(query, args...)
6266
return WrapNonNilErrorWithQuery(err, query, conn.parent.argFmt, args)

mockconn/connection.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ func (conn *connection) Ping(time.Duration) error {
7676
return nil
7777
}
7878

79+
func (conn *connection) Now() (time.Time, error) {
80+
return time.Now(), nil
81+
}
82+
7983
func (conn *connection) Exec(query string, args ...any) error {
8084
if conn.queryWriter != nil {
8185
fmt.Fprint(conn.queryWriter, query)

pqconn/connection.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ func (conn *connection) Config() *sqldb.Config {
9898
return conn.config
9999
}
100100

101+
func (conn *connection) Now() (time.Time, error) {
102+
return impl.Now(conn)
103+
}
104+
101105
func (conn *connection) Exec(query string, args ...any) error {
102106
_, err := conn.db.ExecContext(conn.ctx, query, args...)
103107
return impl.WrapNonNilErrorWithQuery(err, query, argFmt, args)

pqconn/transaction.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func (conn *transaction) Ping(timeout time.Duration) error { return conn.parent.
5757
func (conn *transaction) Stats() sql.DBStats { return conn.parent.Stats() }
5858
func (conn *transaction) Config() *sqldb.Config { return conn.parent.Config() }
5959

60+
func (conn *transaction) Now() (time.Time, error) {
61+
return impl.Now(conn)
62+
}
63+
6064
func (conn *transaction) Exec(query string, args ...any) error {
6165
_, err := conn.tx.Exec(query, args...)
6266
return impl.WrapNonNilErrorWithQuery(err, query, argFmt, args)

0 commit comments

Comments
 (0)