-
Notifications
You must be signed in to change notification settings - Fork 8
/
schema.go
61 lines (51 loc) · 1.44 KB
/
schema.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
package pqt
// Schema describes database schema.
// It is a collection of tables, functions and types.
type Schema struct {
// Name is a schema name.
Name string
// IfNotExists if true means that schema should be created only if it does not exist.
// If true, creation process will not fail if schema already exists.
IfNotExists bool
// Tables is a collection of tables that schema contains.
Tables []*Table
// Functions is a collection of functions that schema contains.
Functions []*Function
// Types is a collection of types that schema contains.
Types []Type
}
// NewSchema initializes new instance of Schema for given name and options.
func NewSchema(name string, opts ...SchemaOption) *Schema {
s := &Schema{
Name: name,
}
for _, opt := range opts {
opt(s)
}
return s
}
// AddTable adds table to schema.
func (s *Schema) AddTable(t *Table) *Schema {
if s.Tables == nil {
s.Tables = make([]*Table, 0, 1)
}
t.Schema = s
s.Tables = append(s.Tables, t)
return s
}
// AddFunction adds function to schema.
func (s *Schema) AddFunction(f *Function) *Schema {
if s.Functions == nil {
s.Functions = make([]*Function, 0, 1)
}
s.Functions = append(s.Functions, f)
return s
}
// SchemaOption configures how we set up a schema.
type SchemaOption func(*Schema)
// WithSchemaIfNotExists is schema option that sets IfNotExists flag to true.
func WithSchemaIfNotExists() SchemaOption {
return func(s *Schema) {
s.IfNotExists = true
}
}