-
Notifications
You must be signed in to change notification settings - Fork 2
/
table.go
143 lines (121 loc) · 3.86 KB
/
table.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
package sol
import (
"fmt"
"log"
"github.com/aodin/sol/dialect"
"github.com/aodin/sol/types"
)
// Tabular is the interface that all dialects of a SQL table must implement
type Tabular interface {
// Two methods for neutral SQL element interfaces:
// (1) Require the interface to return the neutral implementation
// (2) Enumerate all the methods an implmentation would require
// Columnar and Tabular both use method (1)
// Name has been left as a legacy shortcut but may be removed
Compiles
Selectable
Name() string
Table() *TableElem
}
// TableElem is a dialect neutral implementation of a SQL table
type TableElem struct {
name string
alias string
columns UniqueColumnSet
pk PKArray // Table's primary key
uniques []UniqueArray
fks []FKElem // This table's foreign keys
referencedBy []FKElem // Foreign keys that reference this table
creates []types.Type
}
var _ Tabular = &TableElem{}
// Column returns the column as a ColumnElem. If the column does not exist
// it will return the ColumnElem in an invalid state that will be used to
// construct an error message
func (table TableElem) Column(name string) ColumnElem {
col := table.columns.Get(name)
// If the column is invalid add the current table in order
// to construct a better error message
if col.IsInvalid() {
col.table = &table
}
return col
}
// C is an alias for Column
func (table TableElem) C(name string) ColumnElem {
return table.Column(name)
}
// Columns returns all the table columns in the original schema order
func (table TableElem) Columns() []ColumnElem {
return table.columns.All()
}
func (table TableElem) Compile(d dialect.Dialect, ps *Parameters) (string, error) {
return table.Name(), nil
}
// Create returns a CREATE statement for the table
func (table *TableElem) Create() CreateStmt {
return CreateStmt{table: table}
}
// Delete is an alias for Delete(table). It will generate a DELETE statement
// for the entire table
func (table *TableElem) Delete() DeleteStmt {
return Delete(table)
}
// Drop returns a DROP statement for the table
func (table *TableElem) Drop() DropStmt {
return DropStmt{table: table}
}
// ForeignKeys returns the table's foreign keys
func (table *TableElem) ForeignKeys() []FKElem {
return table.fks
}
// Has returns true if the column exists in this table
func (table *TableElem) Has(name string) bool {
return table.columns.Has(name)
}
// Insert is an alias for Insert(table). It will create an INSERT statement
// for the entire table. Specify the insert values with the method Values().
func (table *TableElem) Insert() InsertStmt {
return Insert(table)
}
// Name returns the table name without escaping
func (table *TableElem) Name() string {
return fmt.Sprintf(`%s`, table.name)
}
// PrimaryKey returns the primary key array
func (table TableElem) PrimaryKey() PKArray {
return table.pk
}
// ReferencedBy returns the foreign keys that reference this table
func (table *TableElem) ReferencedBy() []FKElem {
return table.referencedBy
}
// Select returns a SelectStmt for the entire table
func (table *TableElem) Select(selections ...Selectable) (stmt SelectStmt) {
return SelectTable(table, selections...)
}
// Table returns the table itself
func (table *TableElem) Table() *TableElem {
return table
}
// Update is an alias for Update(table). It will create an UPDATE statement
// for the entire table. Specify the update values with the method Values().
func (table *TableElem) Update() UpdateStmt {
return Update(table)
}
// Table creates a new dialect netural table. It will panic on any errors.
func Table(name string, modifiers ...Modifier) *TableElem {
if err := isValidTableName(name); err != nil {
log.Panic(err)
}
table := &TableElem{
name: name,
columns: UniqueColumns(),
}
for _, modifier := range modifiers {
if err := modifier.Modify(table); err != nil {
log.Panic(err)
}
}
return table
}