Skip to content

Commit 3a6b4d0

Browse files
returned Datetime type for proton datetime and datetime64, implement Marshaler and Unmarshaler interface of json and yaml for types.Date and types.Datetime
1 parent 47c96de commit 3a6b4d0

17 files changed

+307
-136
lines changed

lib/column/column_gen.go

+4-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/column/datetime.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ func (dt *DateTime) Row(i int, ptr bool) interface{} {
7373
func (dt *DateTime) ScanRow(dest interface{}, row int) error {
7474
switch d := dest.(type) {
7575
case *time.Time:
76-
*d = dt.row(row)
76+
*d = dt.row(row).Time
7777
case **time.Time:
7878
*d = new(time.Time)
79-
**d = dt.row(row)
79+
**d = dt.row(row).Time
8080
case *types.Datetime:
81-
*d = types.Datetime{dt.row(row)}
81+
*d = dt.row(row)
8282
case **types.Datetime:
8383
*d = new(types.Datetime)
84-
**d = types.Datetime{dt.row(row)}
84+
**d = dt.row(row)
8585
default:
8686
return &ColumnConverterError{
8787
Op: "ScanRow",
@@ -195,12 +195,12 @@ func (dt *DateTime) Encode(encoder *binary.Encoder) error {
195195
return dt.values.Encode(encoder)
196196
}
197197

198-
func (dt *DateTime) row(i int) time.Time {
198+
func (dt *DateTime) row(i int) types.Datetime {
199199
v := time.Unix(int64(dt.values[i]), 0)
200200
if dt.timezone != nil {
201201
v = v.In(dt.timezone)
202202
}
203-
return v
203+
return types.Datetime{Time: v}
204204
}
205205

206206
var _ Interface = (*DateTime)(nil)

lib/column/datetime64.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ func (dt *DateTime64) Row(i int, ptr bool) interface{} {
8787
func (dt *DateTime64) ScanRow(dest interface{}, row int) error {
8888
switch d := dest.(type) {
8989
case *time.Time:
90-
*d = dt.row(row)
90+
*d = dt.row(row).Time
9191
case **time.Time:
9292
*d = new(time.Time)
93-
**d = dt.row(row)
93+
**d = dt.row(row).Time
9494
case *types.Datetime:
95-
*d = types.Datetime{dt.row(row)}
95+
*d = dt.row(row)
9696
case **types.Datetime:
9797
*d = new(types.Datetime)
98-
**d = types.Datetime{dt.row(row)}
98+
**d = dt.row(row)
9999
default:
100100
return &ColumnConverterError{
101101
Op: "ScanRow",
@@ -213,7 +213,7 @@ func (dt *DateTime64) Encode(encoder *binary.Encoder) error {
213213
return dt.values.Encode(encoder)
214214
}
215215

216-
func (dt *DateTime64) row(i int) time.Time {
216+
func (dt *DateTime64) row(i int) types.Datetime {
217217
var nano int64
218218
if dt.precision < 19 {
219219
nano = dt.values[i] * int64(math.Pow10(9-dt.precision))
@@ -226,7 +226,7 @@ func (dt *DateTime64) row(i int) time.Time {
226226
if dt.timezone != nil {
227227
time = time.In(dt.timezone)
228228
}
229-
return time
229+
return types.Datetime{Time: time}
230230
}
231231

232232
func (dt *DateTime64) timeToInt64(t time.Time) int64 {

tests/array_test.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package tests
1919

2020
import (
2121
"context"
22+
"github.com/timeplus-io/proton-go-driver/v2/types"
2223
"testing"
2324
"time"
2425

@@ -56,14 +57,14 @@ func TestArray(t *testing.T) {
5657
if err := conn.Exec(ctx, ddl); assert.NoError(t, err) {
5758
if batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_array (* except _tp_time)"); assert.NoError(t, err) {
5859
var (
59-
timestamp = time.Now().Truncate(time.Second)
60+
timestamp = types.Datetime{Time: time.Now().Truncate(time.Second)}
6061
col1Data = []string{"A", "b", "c"}
6162
col2Data = [][]uint32{
6263
{1, 2},
6364
{3, 87},
6465
{33, 3, 847},
6566
}
66-
col3Data = [][][]time.Time{
67+
col3Data = [][][]types.Datetime{
6768
{
6869
{
6970
timestamp,
@@ -96,7 +97,7 @@ func TestArray(t *testing.T) {
9697
var (
9798
col1 []string
9899
col2 [][]uint32
99-
col3 [][][]time.Time
100+
col3 [][][]types.Datetime
100101
)
101102
if err := rows.Scan(&col1, &col2, &col3); assert.NoError(t, err) {
102103
assert.Equal(t, col1Data, col1)
@@ -143,14 +144,14 @@ func TestColumnarArray(t *testing.T) {
143144
}()
144145
if err := conn.Exec(ctx, ddl); assert.NoError(t, err) {
145146
var (
146-
timestamp = time.Now().Truncate(time.Second)
147+
timestamp = types.Datetime{Time: time.Now().Truncate(time.Second)}
147148
col1Data = []string{"A", "b", "c"}
148149
col2Data = [][]uint32{
149150
{1, 2},
150151
{3, 87},
151152
{33, 3, 847},
152153
}
153-
col3Data = [][][]time.Time{
154+
col3Data = [][][]types.Datetime{
154155
{
155156
{
156157
timestamp,
@@ -174,7 +175,7 @@ func TestColumnarArray(t *testing.T) {
174175

175176
col1DataColArr [][]string
176177
col2DataColArr [][][]uint32
177-
col3DataColArr [][][][]time.Time
178+
col3DataColArr [][][][]types.Datetime
178179
)
179180

180181
for i := 0; i < 10; i++ {
@@ -199,7 +200,7 @@ func TestColumnarArray(t *testing.T) {
199200
var (
200201
col1 []string
201202
col2 [][]uint32
202-
col3 [][][]time.Time
203+
col3 [][][]types.Datetime
203204
)
204205
if err := rows.Scan(&col1, &col2, &col3); assert.NoError(t, err) {
205206
assert.Equal(t, col1Data, col1)

tests/datetime64_test.go

+38-37
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package tests
1919

2020
import (
2121
"context"
22+
"github.com/timeplus-io/proton-go-driver/v2/types"
2223
"testing"
2324
"time"
2425

@@ -63,26 +64,26 @@ func TestDateTime64(t *testing.T) {
6364
if err := conn.Exec(ctx, ddl); assert.NoError(t, err) {
6465
if batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_datetime64 (* except _tp_time)"); assert.NoError(t, err) {
6566
var (
66-
datetime1 = time.Now().Truncate(time.Millisecond)
67-
datetime2 = time.Now().Truncate(time.Nanosecond)
68-
datetime3 = time.Now().Truncate(time.Second)
67+
datetime1 = types.Datetime{time.Now().Truncate(time.Millisecond)}
68+
datetime2 = types.Datetime{time.Now().Truncate(time.Nanosecond)}
69+
datetime3 = types.Datetime{time.Now().Truncate(time.Second)}
6970
)
7071
if err := batch.Append(
7172
datetime1,
7273
datetime2,
7374
datetime3,
7475
&datetime1,
75-
[]time.Time{datetime1, datetime1},
76-
[]*time.Time{&datetime3, nil, &datetime3},
76+
[]types.Datetime{datetime1, datetime1},
77+
[]*types.Datetime{&datetime3, nil, &datetime3},
7778
); assert.NoError(t, err) {
7879
if err := batch.Send(); assert.NoError(t, err) {
7980
var (
80-
col1 time.Time
81-
col2 time.Time
82-
col3 time.Time
83-
col4 *time.Time
84-
col5 []time.Time
85-
col6 []*time.Time
81+
col1 types.Datetime
82+
col2 types.Datetime
83+
col3 types.Datetime
84+
col4 *types.Datetime
85+
col5 []types.Datetime
86+
col6 []*types.Datetime
8687
)
8788
if err := conn.QueryRow(ctx, "SELECT (* except _tp_time) FROM test_datetime64 WHERE _tp_time > earliest_ts() LIMIT 1").Scan(&col1, &col2, &col3, &col4, &col5, &col6); assert.NoError(t, err) {
8889
assert.Equal(t, datetime1, col1)
@@ -154,12 +155,12 @@ func TestNullableDateTime64(t *testing.T) {
154155
if err := batch.Append(datetime1, datetime1, datetime2, datetime2, datetime3, datetime3); assert.NoError(t, err) {
155156
if err := batch.Send(); assert.NoError(t, err) {
156157
var (
157-
col1 time.Time
158-
col1Null *time.Time
159-
col2 time.Time
160-
col2Null *time.Time
161-
col3 time.Time
162-
col3Null *time.Time
158+
col1 types.Datetime
159+
col1Null *types.Datetime
160+
col2 types.Datetime
161+
col2Null *types.Datetime
162+
col3 types.Datetime
163+
col3Null *types.Datetime
163164
)
164165
if err := conn.QueryRow(ctx, "SELECT (* except _tp_time) FROM test_datetime64 WHERE _tp_time > earliest_ts() LIMIT 1").Scan(
165166
&col1, &col1Null,
@@ -188,12 +189,12 @@ func TestNullableDateTime64(t *testing.T) {
188189
if err := batch.Append(datetime1, nil, datetime2, nil, datetime3, nil); assert.NoError(t, err) {
189190
if err := batch.Send(); assert.NoError(t, err) {
190191
var (
191-
col1 time.Time
192-
col1Null *time.Time
193-
col2 time.Time
194-
col2Null *time.Time
195-
col3 time.Time
196-
col3Null *time.Time
192+
col1 types.Datetime
193+
col1Null *types.Datetime
194+
col2 types.Datetime
195+
col2Null *types.Datetime
196+
col3 types.Datetime
197+
col3Null *types.Datetime
197198
)
198199
if err := conn.QueryRow(ctx, "SELECT (* except _tp_time) FROM test_datetime64 WHERE _tp_time > earliest_ts() LIMIT 1").Scan(
199200
&col1, &col1Null,
@@ -259,14 +260,14 @@ func TestColumnarDateTime64(t *testing.T) {
259260
if batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_datetime64 (* except _tp_time)"); assert.NoError(t, err) {
260261
var (
261262
id []uint64
262-
col1Data []time.Time
263-
col2Data []*time.Time
264-
col3Data [][]time.Time
265-
col4Data [][]*time.Time
263+
col1Data []types.Datetime
264+
col2Data []*types.Datetime
265+
col3Data [][]types.Datetime
266+
col4Data [][]*types.Datetime
266267
)
267268
var (
268-
datetime1 = time.Now().Truncate(time.Millisecond)
269-
datetime2 = time.Now().Truncate(time.Second)
269+
datetime1 = types.Datetime{Time: time.Now().Truncate(time.Millisecond)}
270+
datetime2 = types.Datetime{Time: time.Now().Truncate(time.Second)}
270271
)
271272
for i := 0; i < 1000; i++ {
272273
id = append(id, uint64(i))
@@ -276,10 +277,10 @@ func TestColumnarDateTime64(t *testing.T) {
276277
} else {
277278
col2Data = append(col2Data, nil)
278279
}
279-
col3Data = append(col3Data, []time.Time{
280+
col3Data = append(col3Data, []types.Datetime{
280281
datetime1, datetime2, datetime1,
281282
})
282-
col4Data = append(col4Data, []*time.Time{
283+
col4Data = append(col4Data, []*types.Datetime{
283284
&datetime2, nil, &datetime1,
284285
})
285286
}
@@ -302,16 +303,16 @@ func TestColumnarDateTime64(t *testing.T) {
302303
}
303304
if assert.NoError(t, batch.Send()) {
304305
var result struct {
305-
Col1 time.Time
306-
Col2 *time.Time
307-
Col3 []time.Time
308-
Col4 []*time.Time
306+
Col1 types.Datetime
307+
Col2 *types.Datetime
308+
Col3 []types.Datetime
309+
Col4 []*types.Datetime
309310
}
310311
if err := conn.QueryRow(ctx, "SELECT Col1, Col2, Col3, Col4 FROM test_datetime64 WHERE ID = $1 AND _tp_time > earliest_ts() LIMIT 1", 11).ScanStruct(&result); assert.NoError(t, err) {
311312
if assert.Nil(t, result.Col2) {
312313
assert.Equal(t, datetime1, result.Col1)
313-
assert.Equal(t, []time.Time{datetime1, datetime2, datetime1}, result.Col3)
314-
assert.Equal(t, []*time.Time{&datetime2, nil, &datetime1}, result.Col4)
314+
assert.Equal(t, []types.Datetime{datetime1, datetime2, datetime1}, result.Col3)
315+
assert.Equal(t, []*types.Datetime{&datetime2, nil, &datetime1}, result.Col4)
315316
}
316317
}
317318
}

0 commit comments

Comments
 (0)