-
Notifications
You must be signed in to change notification settings - Fork 1
/
relationref.go
74 lines (63 loc) · 1.88 KB
/
relationref.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
package sqlconnect
import (
"encoding/json"
"fmt"
)
func NewRelationRef(name string, options ...Option) RelationRef {
var o RelationRefOption
for _, option := range options {
option(&o)
}
relationType := TableRelation
if o.Type != "" {
relationType = o.Type
}
return RelationRef{
Name: name,
Schema: o.Schema,
Catalog: o.Catalog,
Type: relationType,
}
}
// NewSchemaTableRef creates a new RelationRef with a schema and a table
func NewSchemaTableRef(schema, table string) RelationRef {
return NewRelationRef(table, WithSchema(schema))
}
type RelationType string
const (
TableRelation RelationType = "table"
ViewRelation RelationType = "view"
)
// RelationRef provides a reference to a database table
type RelationRef struct {
Name string `json:"name"` // the relation's name
Schema string `json:"schema,omitempty"` // the relation's schema
Catalog string `json:"catalog,omitempty"` // the relation's catalog
Type RelationType `json:"type,omitempty"` // the relation's type
}
func (t RelationRef) String() string {
if t.Catalog != "" && t.Schema != "" {
return fmt.Sprintf("%s.%s.%s", t.Catalog, t.Schema, t.Name)
}
if t.Schema != "" {
return fmt.Sprintf("%s.%s", t.Schema, t.Name)
}
return t.Name
}
func (r *RelationRef) UnmarshalJSON(data []byte) error {
var rawRelationRef struct {
Name string `json:"name"`
Schema string `json:"schema,omitempty"`
Catalog string `json:"catalog,omitempty"`
Type RelationType `json:"type"`
}
err := json.Unmarshal(data, &rawRelationRef)
if err != nil {
return fmt.Errorf("failed to unmarshal RelationRef: %w", err)
}
if rawRelationRef.Type == "" {
rawRelationRef.Type = TableRelation
}
*r = NewRelationRef(rawRelationRef.Name, WithSchema(rawRelationRef.Schema), WithCatalog(rawRelationRef.Catalog), WithRelationType(rawRelationRef.Type))
return nil
}