diff --git a/connection.go b/connection.go index be4fbae..dde2108 100644 --- a/connection.go +++ b/connection.go @@ -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 diff --git a/db/query.go b/db/query.go index 2d5df95..0c008ab 100644 --- a/db/query.go +++ b/db/query.go @@ -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. diff --git a/db/utils.go b/db/utils.go index 10f59c4..3d2c210 100644 --- a/db/utils.go +++ b/db/utils.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "os" + "time" "unicode/utf8" "github.com/domonda/go-sqldb" @@ -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) } diff --git a/errors.go b/errors.go index 6bbdaf6..2564e83 100644 --- a/errors.go +++ b/errors.go @@ -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 } diff --git a/impl/connection.go b/impl/connection.go index e3ca281..5c61f6e 100644 --- a/impl/connection.go +++ b/impl/connection.go @@ -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) diff --git a/impl/transaction.go b/impl/transaction.go index 298c3fd..4756415 100644 --- a/impl/transaction.go +++ b/impl/transaction.go @@ -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) diff --git a/mockconn/connection.go b/mockconn/connection.go index 27ae29b..4dfedc1 100644 --- a/mockconn/connection.go +++ b/mockconn/connection.go @@ -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) diff --git a/pqconn/connection.go b/pqconn/connection.go index bc87dac..d936b06 100644 --- a/pqconn/connection.go +++ b/pqconn/connection.go @@ -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...) diff --git a/pqconn/transaction.go b/pqconn/transaction.go index 3254016..2d69184 100644 --- a/pqconn/transaction.go +++ b/pqconn/transaction.go @@ -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...)