Skip to content

Commit

Permalink
Update xsql supports "time.Time", "go_ora.TimeStamp", "*timestamppb.T…
Browse files Browse the repository at this point in the history
…imestamp"
  • Loading branch information
onanying committed May 15, 2024
1 parent e5fe8b4 commit 976f208
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
39 changes: 39 additions & 0 deletions src/xsql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(&timestamppb.Timestamp{}, test2.Bar)
a.Equal(test2.Bar.Seconds, now.Seconds)
}
4 changes: 2 additions & 2 deletions src/xsql/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 976f208

Please sign in to comment.