From 976f208fbcd6e93237cead765f07893bc1275dd6 Mon Sep 17 00:00:00 2001 From: liujian Date: Wed, 15 May 2024 16:04:40 +0800 Subject: [PATCH] Update xsql supports "time.Time", "go_ora.TimeStamp", "*timestamppb.Timestamp" --- src/xsql/db_test.go | 39 +++++++++++++++++++++++++++++++++++++++ src/xsql/options.go | 4 ++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/xsql/db_test.go b/src/xsql/db_test.go index acd5bb3..5c6d2ed 100644 --- a/src/xsql/db_test.go +++ b/src/xsql/db_test.go @@ -7,6 +7,7 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/mix-go/xsql" "github.com/stretchr/testify/assert" + "google.golang.org/protobuf/types/known/timestamppb" "log" "strings" "testing" @@ -45,6 +46,16 @@ type EmbeddingTest struct { Test2 } +type Test3 struct { + Id int `xsql:"id"` + Foo string `xsql:"foo"` + Bar *timestamppb.Timestamp `xsql:"bar"` +} + +func (t Test3) TableName() string { + return "xsql" +} + func newDB() *xsql.DB { db, err := sql.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=true&loc=UTC&multiStatements=true") if err != nil { @@ -469,3 +480,31 @@ func TestTxRollback(t *testing.T) { err = tx.Rollback() a.Empty(err) } + +func TestPbTimestamp(t *testing.T) { + a := assert.New(t) + + DB := newDB() + + // Insert + now := timestamppb.Now() + log.Println(now.AsTime().Format(time.RFC3339)) + test := Test3{ + Id: 0, + Foo: "test_pb_timestamp", + Bar: now, + } + res, err := DB.Insert(&test) + a.Empty(err) + insertId, _ := res.LastInsertId() + + // First + var test2 Test3 + err = DB.First(&test2, "SELECT * FROM xsql WHERE id = ?", insertId) + if err != nil { + log.Fatal(err) + } + // Timestamp + a.IsType(×tamppb.Timestamp{}, test2.Bar) + a.Equal(test2.Bar.Seconds, now.Seconds) +} diff --git a/src/xsql/options.go b/src/xsql/options.go index 55acdec..52b0b88 100644 --- a/src/xsql/options.go +++ b/src/xsql/options.go @@ -14,7 +14,7 @@ func newDefaultOptions() sqlOptions { TableKey: "${TABLE}", Placeholder: "?", ColumnQuotes: "`", - TimeLayout: "2006-01-02 15:04:05.000000", + TimeLayout: "2006-01-02 15:04:05", TimeLocation: time.Local, TimeFunc: func(placeholder string) string { return placeholder @@ -41,7 +41,7 @@ type sqlOptions struct { // For oracle, can be configured as " ColumnQuotes string - // Default: 2006-01-02 15:04:05.000000 + // Default: 2006-01-02 15:04:05 TimeLayout string // Default: time.Local