Skip to content

Commit

Permalink
use SQL standard CURRENT_TIMESTAMP instead of connection type specifi…
Browse files Browse the repository at this point in the history
…c Now() method
  • Loading branch information
ungerik committed Dec 1, 2024
1 parent 74b63c7 commit 91125a0
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 38 deletions.
6 changes: 0 additions & 6 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ type Connection interface {
// column of the connection's database.
ValidateColumnName(name string) error

// Now returns the result of the SQL now()
// function for the current connection.
// Useful for getting the timestamp of a
// SQL transaction for use in Go code.
Now() (time.Time, error)

// Exec executes a query with optional args.
Exec(query string, args ...any) error

Expand Down
15 changes: 9 additions & 6 deletions db/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ import (
"github.com/domonda/go-sqldb"
)

// Now returns the result of the SQL now()
// function for the current connection.
// CurrentTimestamp returns the SQL CURRENT_TIMESTAMP
// for the connection added to the context
// or else the default connection.
//
// Returns time.Now() in case of any error.
//
// Useful for getting the timestamp of a
// SQL transaction for use in Go code.
// Returns time.Now() in case of an error.
func Now(ctx context.Context) time.Time {
now, err := Conn(ctx).Now()
func CurrentTimestamp(ctx context.Context) time.Time {
t, err := QueryValue[time.Time](ctx, "SELECT CURRENT_TIMESTAMP")
if err != nil {
return time.Now()
}
return now
return t
}

// Exec executes a query with optional args.
Expand Down
6 changes: 4 additions & 2 deletions db/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"os"
"time"
"unicode/utf8"

"github.com/domonda/go-sqldb"
Expand Down Expand Up @@ -37,9 +38,10 @@ func DebugPrintConn(ctx context.Context, args ...any) {
args = append(args, "Isolation", optsStr)
}
}
now, err := Conn(ctx).Now()
var t time.Time
err := Conn(ctx).QueryRow("SELECT CURRENT_TIMESTAMP").Scan(&t)
if err == nil {
args = append(args, "NOW():", now)
args = append(args, "CURRENT_TIMESTAMP:", t)
} else {
args = append(args, "ERROR:", err)
}
Expand Down
4 changes: 0 additions & 4 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,6 @@ func (e connectionWithError) ValidateColumnName(name string) error {
return e.err
}

func (e connectionWithError) Now() (time.Time, error) {
return time.Time{}, e.err
}

func (e connectionWithError) Exec(query string, args ...any) error {
return e.err
}
Expand Down
4 changes: 0 additions & 4 deletions impl/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ func (conn *connection) ValidateColumnName(name string) error {
return conn.validateColumnName(name)
}

func (conn *connection) Now() (time.Time, error) {
return Now(conn)
}

func (conn *connection) Exec(query string, args ...any) error {
_, err := conn.db.ExecContext(conn.ctx, query, args...)
return WrapNonNilErrorWithQuery(err, query, conn.argFmt, args)
Expand Down
4 changes: 0 additions & 4 deletions impl/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ func (conn *transaction) ValidateColumnName(name string) error {
return conn.parent.validateColumnName(name)
}

func (conn *transaction) Now() (time.Time, error) {
return Now(conn)
}

func (conn *transaction) Exec(query string, args ...any) error {
_, err := conn.tx.Exec(query, args...)
return WrapNonNilErrorWithQuery(err, query, conn.parent.argFmt, args)
Expand Down
4 changes: 0 additions & 4 deletions mockconn/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ func (conn *connection) Ping(time.Duration) error {
return nil
}

func (conn *connection) Now() (time.Time, error) {
return time.Now(), nil
}

func (conn *connection) Exec(query string, args ...any) error {
if conn.queryWriter != nil {
fmt.Fprint(conn.queryWriter, query)
Expand Down
4 changes: 0 additions & 4 deletions pqconn/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ func (conn *connection) ValidateColumnName(name string) error {
return validateColumnName(name)
}

func (conn *connection) Now() (time.Time, error) {
return impl.Now(conn)
}

func (conn *connection) Exec(query string, args ...any) error {
impl.WrapArrayArgs(args)
_, err := conn.db.ExecContext(conn.ctx, query, args...)
Expand Down
4 changes: 0 additions & 4 deletions pqconn/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ func (conn *transaction) ValidateColumnName(name string) error {
return validateColumnName(name)
}

func (conn *transaction) Now() (time.Time, error) {
return impl.Now(conn)
}

func (conn *transaction) Exec(query string, args ...any) error {
impl.WrapArrayArgs(args)
_, err := conn.tx.Exec(query, args...)
Expand Down

0 comments on commit 91125a0

Please sign in to comment.