Browse Source

Add CurrentUnixTimestamp func

Volodymyr Tkach 2 years ago
parent
commit
5f56c05bf9
3 changed files with 13 additions and 0 deletions
  1. 5 0
      gosql/common/common.go
  2. 4 0
      gosql/common/dbmethods.go
  3. 4 0
      gosql/common/tx.go

+ 5 - 0
gosql/common/common.go

@@ -22,6 +22,7 @@ import (
 type Engine interface {
 	Begin(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
 	Close() error
+	CurrentUnixTimestamp() int64
 	DeleteRowByID(ctx context.Context, id int64, row any) error
 	Each(ctx context.Context, query string, logic func(ctx context.Context, rows *Rows) error, args ...any) error
 	Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
@@ -150,6 +151,10 @@ func deleteRowByIDString(row any) string {
 	return `DELETE FROM ` + table + ` WHERE id = $1`
 }
 
+func currentUnixTimestamp() int64 {
+	return time.Now().UTC().Unix()
+}
+
 func ParseUrl(dbURL string) (*url.URL, error) {
 	databaseURL, err := url.Parse(dbURL)
 	if err != nil {

+ 4 - 0
gosql/common/dbmethods.go

@@ -42,6 +42,10 @@ func (d *DBMethods) Close() error {
 	return err
 }
 
+func (d *DBMethods) CurrentUnixTimestamp() int64 {
+	return currentUnixTimestamp()
+}
+
 func (d *DBMethods) DeleteRowByID(ctx context.Context, id int64, row any) error {
 	query := deleteRowByIDString(row)
 	_, err := d.Exec(ctx, query, id)

+ 4 - 0
gosql/common/tx.go

@@ -35,6 +35,10 @@ func (t *Tx) Commit() error {
 	return err
 }
 
+func (t *Tx) CurrentUnixTimestamp() int64 {
+	return currentUnixTimestamp()
+}
+
 func (t *Tx) DeleteRowByID(ctx context.Context, id int64, row any) error {
 	query := deleteRowByIDString(row)
 	_, err := t.Exec(ctx, query, id)