-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.go
170 lines (128 loc) · 3.5 KB
/
views.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
package metadata
import (
"github.com/pkg/errors"
"github.com/unhandled-exception/sophiadb/internal/pkg/records"
"github.com/unhandled-exception/sophiadb/internal/pkg/scan"
)
const (
MaxViewNameLength = MaxTableNameLength
MaxViewsDefLength = 200
viewsCatalogTableName = "sdb_views"
VcatViewNameField = "viewname"
VcatViewDefField = "viewdef"
)
type Views struct {
VcatTableName string
VcatLayout records.Layout
tables TablesManager
}
func NewViews(tables TablesManager, isNew bool, trx scan.TRXInt) (*Views, error) {
v := &Views{
VcatTableName: viewsCatalogTableName,
VcatLayout: newViewsCatalogLayout(),
tables: tables,
}
if isNew {
if err := tables.CreateTable(v.VcatTableName, v.VcatLayout.Schema, trx); err != nil {
return nil, errors.WithMessage(ErrViewsMetadata, err.Error())
}
}
return v, nil
}
func newViewsCatalogLayout() records.Layout {
schema := records.NewSchema()
schema.AddStringField(VcatViewNameField, MaxViewNameLength)
schema.AddStringField(VcatViewDefField, MaxViewsDefLength)
return records.NewLayout(schema)
}
func (v *Views) ViewExists(viewName string, trx scan.TRXInt) (stop bool, err error) {
found := false
vcat, err := v.newViewCatalogTableScan(trx)
if err != nil {
return false, v.wrapError(err, viewName, nil)
}
defer vcat.Close()
if err := scan.ForEach(vcat, func() (stop bool, err error) {
name, err := vcat.GetString(VcatViewNameField)
if err != nil {
return true, err
}
found = (name == viewName)
return found, nil
}); err != nil {
return false, v.wrapError(err, viewName, nil)
}
return found, nil
}
func (v *Views) CreateView(viewName string, viewDef string, trx scan.TRXInt) error {
exists, err := v.ViewExists(viewName, trx)
if err != nil {
return v.wrapError(err, viewName, nil)
}
if exists {
return ErrViewExists
}
vcat, err := v.newViewCatalogTableScan(trx)
if err != nil {
return v.wrapError(err, viewName, nil)
}
defer vcat.Close()
if err := vcat.Insert(); err != nil {
return v.wrapError(err, viewName, nil)
}
if err := scan.ForEachField(vcat, func(name string, fieldType records.FieldType) (stop bool, err error) {
switch name {
case VcatViewNameField:
err = vcat.SetString(VcatViewNameField, viewName)
case VcatViewDefField:
err = vcat.SetString(VcatViewDefField, viewDef)
}
return false, err
}); err != nil {
return v.wrapError(err, viewName, nil)
}
return nil
}
func (v *Views) ViewDef(viewName string, trx scan.TRXInt) (string, error) {
var viewDef string
found := false
vcat, err := v.newViewCatalogTableScan(trx)
if err != nil {
return "", v.wrapError(err, viewName, nil)
}
defer vcat.Close()
if err := scan.ForEach(vcat, func() (stop bool, err error) {
name, err := vcat.GetString(VcatViewNameField)
if err != nil {
return true, err
}
if name == viewName {
viewDef, err = vcat.GetString(VcatViewDefField)
if err != nil {
return true, err
}
found = true
return true, nil
}
return false, nil
}); err != nil {
return "", v.wrapError(err, viewName, nil)
}
if !found {
return "", ErrViewNotFound
}
return viewDef, nil
}
func (v *Views) newViewCatalogTableScan(trx scan.TRXInt) (*scan.TableScan, error) {
vcat, err := scan.NewTableScan(trx, v.VcatTableName, v.VcatLayout)
if err != nil {
return nil, err
}
return vcat, nil
}
func (v *Views) wrapError(err error, viewName string, baseError error) error {
if baseError == nil {
baseError = ErrTablesMetadata
}
return errors.WithMessagef(baseError, "view %s: %s", viewName, err)
}