-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
date.go
242 lines (208 loc) · 6.36 KB
/
date.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2024 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package types
import (
"bytes"
"fmt"
"reflect"
"time"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/types"
"github.com/dolthub/vitess/go/sqltypes"
"github.com/dolthub/vitess/go/vt/proto/query"
"github.com/lib/pq/oid"
)
// Date is the day, month, and year.
var Date = DateType{}
// DateType is the extended type implementation of the PostgreSQL date.
type DateType struct{}
var _ DoltgresType = DateType{}
// BaseID implements the DoltgresType interface.
func (b DateType) BaseID() DoltgresTypeBaseID {
return DoltgresTypeBaseID_Date
}
// CollationCoercibility implements the DoltgresType interface.
func (b DateType) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 5
}
// Compare implements the DoltgresType interface.
func (b DateType) Compare(v1 any, v2 any) (int, error) {
if v1 == nil && v2 == nil {
return 0, nil
} else if v1 != nil && v2 == nil {
return 1, nil
} else if v1 == nil && v2 != nil {
return -1, nil
}
ac, _, err := b.Convert(v1)
if err != nil {
return 0, err
}
bc, _, err := b.Convert(v2)
if err != nil {
return 0, err
}
ab := ac.(time.Time)
bb := bc.(time.Time)
return ab.Compare(bb), nil
}
// Convert implements the DoltgresType interface.
func (b DateType) Convert(val any) (any, sql.ConvertInRange, error) {
if val == nil {
return nil, sql.InRange, nil
}
switch val := val.(type) {
case string:
if t, err := time.Parse("2006-01-02", val); err == nil {
return t.UTC(), sql.InRange, nil
} else if t, err = time.Parse("January 02, 2006", val); err == nil {
return t.UTC(), sql.InRange, nil
} else if t, err = time.Parse("2006-Jan-02", val); err == nil {
return t.UTC(), sql.InRange, nil
}
return nil, sql.OutOfRange, fmt.Errorf("invalid format for date")
case time.Time:
return val.UTC(), sql.InRange, nil
default:
return nil, sql.OutOfRange, sql.ErrInvalidType.New(b)
}
}
// Equals implements the DoltgresType interface.
func (b DateType) Equals(otherType sql.Type) bool {
if otherExtendedType, ok := otherType.(types.ExtendedType); ok {
return bytes.Equal(MustSerializeType(b), MustSerializeType(otherExtendedType))
}
return false
}
// FormatSerializedValue implements the DoltgresType interface.
func (b DateType) FormatSerializedValue(val []byte) (string, error) {
deserialized, err := b.DeserializeValue(val)
if err != nil {
return "", err
}
return b.FormatValue(deserialized)
}
// FormatValue implements the DoltgresType interface.
func (b DateType) FormatValue(val any) (string, error) {
if val == nil {
return "", nil
}
converted, _, err := b.Convert(val)
if err != nil {
return "", err
}
return converted.(time.Time).Format("2006-01-02"), nil
}
// GetSerializationID implements the DoltgresType interface.
func (b DateType) GetSerializationID() SerializationID {
return SerializationID_Date
}
// IsUnbounded implements the DoltgresType interface.
func (b DateType) IsUnbounded() bool {
return false
}
// MaxSerializedWidth implements the DoltgresType interface.
func (b DateType) MaxSerializedWidth() types.ExtendedTypeSerializedWidth {
return types.ExtendedTypeSerializedWidth_64K
}
// MaxTextResponseByteLength implements the DoltgresType interface.
func (b DateType) MaxTextResponseByteLength(ctx *sql.Context) uint32 {
return 32
}
// OID implements the DoltgresType interface.
func (b DateType) OID() uint32 {
return uint32(oid.T_date)
}
// Promote implements the DoltgresType interface.
func (b DateType) Promote() sql.Type {
return Date
}
// SerializedCompare implements the DoltgresType interface.
func (b DateType) SerializedCompare(v1 []byte, v2 []byte) (int, error) {
if len(v1) == 0 && len(v2) == 0 {
return 0, nil
} else if len(v1) > 0 && len(v2) == 0 {
return 1, nil
} else if len(v1) == 0 && len(v2) > 0 {
return -1, nil
}
// The marshalled time format is byte-comparable
return bytes.Compare(v1, v2), nil
}
// SQL implements the DoltgresType interface.
func (b DateType) SQL(ctx *sql.Context, dest []byte, v any) (sqltypes.Value, error) {
if v == nil {
return sqltypes.NULL, nil
}
value, err := b.FormatValue(v)
if err != nil {
return sqltypes.Value{}, err
}
return sqltypes.MakeTrusted(sqltypes.Text, types.AppendAndSliceBytes(dest, []byte(value))), nil
}
// String implements the DoltgresType interface.
func (b DateType) String() string {
return "date"
}
// ToArrayType implements the DoltgresType interface.
func (b DateType) ToArrayType() DoltgresArrayType {
return DateArray
}
// Type implements the DoltgresType interface.
func (b DateType) Type() query.Type {
return sqltypes.Text
}
// ValueType implements the DoltgresType interface.
func (b DateType) ValueType() reflect.Type {
return reflect.TypeOf(time.Time{})
}
// Zero implements the DoltgresType interface.
func (b DateType) Zero() any {
return time.Time{}
}
// SerializeType implements the DoltgresType interface.
func (b DateType) SerializeType() ([]byte, error) {
return SerializationID_Date.ToByteSlice(0), nil
}
// deserializeType implements the DoltgresType interface.
func (b DateType) deserializeType(version uint16, metadata []byte) (DoltgresType, error) {
switch version {
case 0:
return Date, nil
default:
return nil, fmt.Errorf("version %d is not yet supported for %s", version, b.String())
}
}
// SerializeValue implements the DoltgresType interface.
func (b DateType) SerializeValue(val any) ([]byte, error) {
if val == nil {
return nil, nil
}
converted, _, err := b.Convert(val)
if err != nil {
return nil, err
}
return converted.(time.Time).MarshalBinary()
}
// DeserializeValue implements the DoltgresType interface.
func (b DateType) DeserializeValue(val []byte) (any, error) {
if len(val) == 0 {
return nil, nil
}
t := time.Time{}
if err := t.UnmarshalBinary(val); err != nil {
return nil, err
}
return t, nil
}