Skip to content

Commit 91125a0

Browse files
committed
use SQL standard CURRENT_TIMESTAMP instead of connection type specific Now() method
1 parent 74b63c7 commit 91125a0

File tree

9 files changed

+13
-38
lines changed

9 files changed

+13
-38
lines changed

connection.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ type Connection interface {
5050
// column of the connection's database.
5151
ValidateColumnName(name string) error
5252

53-
// Now returns the result of the SQL now()
54-
// function for the current connection.
55-
// Useful for getting the timestamp of a
56-
// SQL transaction for use in Go code.
57-
Now() (time.Time, error)
58-
5953
// Exec executes a query with optional args.
6054
Exec(query string, args ...any) error
6155

db/query.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@ import (
1212
"github.com/domonda/go-sqldb"
1313
)
1414

15-
// Now returns the result of the SQL now()
16-
// function for the current connection.
15+
// CurrentTimestamp returns the SQL CURRENT_TIMESTAMP
16+
// for the connection added to the context
17+
// or else the default connection.
18+
//
19+
// Returns time.Now() in case of any error.
20+
//
1721
// Useful for getting the timestamp of a
1822
// SQL transaction for use in Go code.
19-
// Returns time.Now() in case of an error.
20-
func Now(ctx context.Context) time.Time {
21-
now, err := Conn(ctx).Now()
23+
func CurrentTimestamp(ctx context.Context) time.Time {
24+
t, err := QueryValue[time.Time](ctx, "SELECT CURRENT_TIMESTAMP")
2225
if err != nil {
2326
return time.Now()
2427
}
25-
return now
28+
return t
2629
}
2730

2831
// Exec executes a query with optional args.

db/utils.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"os"
10+
"time"
1011
"unicode/utf8"
1112

1213
"github.com/domonda/go-sqldb"
@@ -37,9 +38,10 @@ func DebugPrintConn(ctx context.Context, args ...any) {
3738
args = append(args, "Isolation", optsStr)
3839
}
3940
}
40-
now, err := Conn(ctx).Now()
41+
var t time.Time
42+
err := Conn(ctx).QueryRow("SELECT CURRENT_TIMESTAMP").Scan(&t)
4143
if err == nil {
42-
args = append(args, "NOW():", now)
44+
args = append(args, "CURRENT_TIMESTAMP:", t)
4345
} else {
4446
args = append(args, "ERROR:", err)
4547
}

errors.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,6 @@ func (e connectionWithError) ValidateColumnName(name string) error {
212212
return e.err
213213
}
214214

215-
func (e connectionWithError) Now() (time.Time, error) {
216-
return time.Time{}, e.err
217-
}
218-
219215
func (e connectionWithError) Exec(query string, args ...any) error {
220216
return e.err
221217
}

impl/connection.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ func (conn *connection) ValidateColumnName(name string) error {
8282
return conn.validateColumnName(name)
8383
}
8484

85-
func (conn *connection) Now() (time.Time, error) {
86-
return Now(conn)
87-
}
88-
8985
func (conn *connection) Exec(query string, args ...any) error {
9086
_, err := conn.db.ExecContext(conn.ctx, query, args...)
9187
return WrapNonNilErrorWithQuery(err, query, conn.argFmt, args)

impl/transaction.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ func (conn *transaction) ValidateColumnName(name string) error {
6464
return conn.parent.validateColumnName(name)
6565
}
6666

67-
func (conn *transaction) Now() (time.Time, error) {
68-
return Now(conn)
69-
}
70-
7167
func (conn *transaction) Exec(query string, args ...any) error {
7268
_, err := conn.tx.Exec(query, args...)
7369
return WrapNonNilErrorWithQuery(err, query, conn.parent.argFmt, args)

mockconn/connection.go

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

83-
func (conn *connection) Now() (time.Time, error) {
84-
return time.Now(), nil
85-
}
86-
8783
func (conn *connection) Exec(query string, args ...any) error {
8884
if conn.queryWriter != nil {
8985
fmt.Fprint(conn.queryWriter, query)

pqconn/connection.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ func (conn *connection) ValidateColumnName(name string) error {
106106
return validateColumnName(name)
107107
}
108108

109-
func (conn *connection) Now() (time.Time, error) {
110-
return impl.Now(conn)
111-
}
112-
113109
func (conn *connection) Exec(query string, args ...any) error {
114110
impl.WrapArrayArgs(args)
115111
_, err := conn.db.ExecContext(conn.ctx, query, args...)

pqconn/transaction.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ func (conn *transaction) ValidateColumnName(name string) error {
6363
return validateColumnName(name)
6464
}
6565

66-
func (conn *transaction) Now() (time.Time, error) {
67-
return impl.Now(conn)
68-
}
69-
7066
func (conn *transaction) Exec(query string, args ...any) error {
7167
impl.WrapArrayArgs(args)
7268
_, err := conn.tx.Exec(query, args...)

0 commit comments

Comments
 (0)