forked from cockroachdb/cockroach
/
pgtypes.go
73 lines (66 loc) · 2.56 KB
/
pgtypes.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
// Copyright 2016 The Cockroach Authors.
//
// 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.
//
// Author: Nathan VanBenschoten (nvanbenschoten@gmail.com)
package sql
import (
"reflect"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/lib/pq/oid"
)
var oidToDatum = map[oid.Oid]parser.Type{
oid.T_anyelement: parser.TypeAny,
oid.T_bool: parser.TypeBool,
oid.T_bytea: parser.TypeBytes,
oid.T_date: parser.TypeDate,
oid.T_float4: parser.TypeFloat,
oid.T_float8: parser.TypeFloat,
oid.T_int2: parser.TypeInt,
oid.T_int4: parser.TypeInt,
oid.T_int8: parser.TypeInt,
oid.T_interval: parser.TypeInterval,
oid.T_numeric: parser.TypeDecimal,
oid.T_text: parser.TypeString,
oid.T__text: parser.TypeArray,
oid.T_timestamp: parser.TypeTimestamp,
oid.T_timestamptz: parser.TypeTimestampTZ,
oid.T_varchar: parser.TypeString,
}
var datumToOid = map[reflect.Type]oid.Oid{
reflect.TypeOf(parser.TypeAny): oid.T_anyelement,
// Currently, only text arrays are supported.
reflect.TypeOf(parser.TypeArray): oid.T__text,
reflect.TypeOf(parser.TypeBool): oid.T_bool,
reflect.TypeOf(parser.TypeBytes): oid.T_bytea,
reflect.TypeOf(parser.TypeDate): oid.T_date,
reflect.TypeOf(parser.TypeFloat): oid.T_float8,
reflect.TypeOf(parser.TypeInt): oid.T_int8,
reflect.TypeOf(parser.TypeInterval): oid.T_interval,
reflect.TypeOf(parser.TypeDecimal): oid.T_numeric,
reflect.TypeOf(parser.TypeString): oid.T_text,
reflect.TypeOf(parser.TypeTimestamp): oid.T_timestamp,
reflect.TypeOf(parser.TypeTimestampTZ): oid.T_timestamptz,
reflect.TypeOf(parser.TypeTuple): oid.T_record,
}
// OidToDatum maps Postgres object IDs to CockroachDB types.
func OidToDatum(oid oid.Oid) (parser.Type, bool) {
t, ok := oidToDatum[oid]
return t, ok
}
// DatumToOid maps CockroachDB types to Postgres object IDs, using reflection
// to support unhashable types.
func DatumToOid(typ parser.Type) (oid.Oid, bool) {
oid, ok := datumToOid[reflect.TypeOf(typ)]
return oid, ok
}