-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
resolve_oid.go
117 lines (108 loc) · 3.68 KB
/
resolve_oid.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
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package sql
import (
"context"
"fmt"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/errors"
"github.com/lib/pq/oid"
)
// ResolveOIDFromString is part of tree.TypeResolver.
func (p *planner) ResolveOIDFromString(
ctx context.Context, resultType *types.T, toResolve *tree.DString,
) (*tree.DOid, error) {
ie := p.ExecCfg().InternalExecutorFactory(ctx, p.SessionData())
return resolveOID(
ctx, p.Txn(),
ie,
resultType, toResolve,
)
}
// ResolveOIDFromOID is part of tree.TypeResolver.
func (p *planner) ResolveOIDFromOID(
ctx context.Context, resultType *types.T, toResolve *tree.DOid,
) (*tree.DOid, error) {
ie := p.ExecCfg().InternalExecutorFactory(ctx, p.SessionData())
return resolveOID(
ctx, p.Txn(),
ie,
resultType, toResolve,
)
}
func resolveOID(
ctx context.Context,
txn *kv.Txn,
ie sqlutil.InternalExecutor,
resultType *types.T,
toResolve tree.Datum,
) (*tree.DOid, error) {
info, ok := regTypeInfos[resultType.Oid()]
if !ok {
return nil, pgerror.Newf(
pgcode.InvalidTextRepresentation,
"invalid input syntax for type %s: %q",
resultType,
tree.AsStringWithFlags(toResolve, tree.FmtBareStrings),
)
}
queryCol := info.nameCol
if _, isOid := toResolve.(*tree.DOid); isOid {
queryCol = "oid"
}
q := fmt.Sprintf(
"SELECT %s.oid, %s FROM pg_catalog.%s WHERE %s = $1",
info.tableName, info.nameCol, info.tableName, queryCol,
)
results, err := ie.QueryRowEx(ctx, "queryOid", txn,
sessiondata.NoSessionDataOverride, q, toResolve)
if err != nil {
if errors.HasType(err, (*tree.MultipleResultsError)(nil)) {
return nil, pgerror.Newf(pgcode.AmbiguousAlias,
"more than one %s named %s", info.objName, toResolve)
}
return nil, err
}
if results.Len() == 0 {
return nil, pgerror.Newf(info.errType,
"%s %s does not exist", info.objName, toResolve)
}
return tree.NewDOidWithName(
results[0].(*tree.DOid).DInt,
resultType,
tree.AsStringWithFlags(results[1], tree.FmtBareStrings),
), nil
}
// regTypeInfo contains details on a pg_catalog table that has a reg* type.
type regTypeInfo struct {
tableName string
// nameCol is the name of the column that contains the table's entity name.
nameCol string
// objName is a human-readable name describing the objects in the table.
objName string
// errType is the pg error code in case the object does not exist.
errType pgcode.Code
}
// regTypeInfos maps an oid.Oid to a regTypeInfo that describes the pg_catalog
// table that contains the entities of the type of the key.
var regTypeInfos = map[oid.Oid]regTypeInfo{
oid.T_regclass: {"pg_class", "relname", "relation", pgcode.UndefinedTable},
oid.T_regnamespace: {"pg_namespace", "nspname", "namespace", pgcode.UndefinedObject},
oid.T_regproc: {"pg_proc", "proname", "function", pgcode.UndefinedFunction},
oid.T_regprocedure: {"pg_proc", "proname", "function", pgcode.UndefinedFunction},
oid.T_regrole: {"pg_authid", "rolname", "role", pgcode.UndefinedObject},
oid.T_regtype: {"pg_type", "typname", "type", pgcode.UndefinedObject},
}