-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
119 lines (100 loc) · 2.72 KB
/
types.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
package dal
import (
"database/sql"
"fmt"
)
// Field represents a field on a table
type Field struct {
Name string
Type string
DefaultValue string
}
// NewTable creates a new table object
func NewTable(name string) *Table {
t := new(Table)
t.Name = name
t.Fields = map[string]*Field{}
return t
}
// Table represents a database table
type Table struct {
Name string
Fields map[string]*Field
Alias string
FieldKeys []string
}
// AddFields adds a collection of fields
func (t *Table) AddFields(fieldNames []string) {
for _, fieldName := range fieldNames {
t.AddField(fieldName)
}
}
// AddField adds a new field to the database object
func (t *Table) AddField(fieldName string) (e error) {
var ok bool
if _, ok = t.Fields[fieldName]; ok {
e = fmt.Errorf("the field `%s`.`%s` has already been defined", t.Name, fieldName)
return
}
newField := new(Field)
newField.Name = fieldName
t.Fields[fieldName] = newField
t.FieldKeys = append(t.FieldKeys, fieldName)
return
}
// Field gets a field from the table by its name
func (t *Table) Field(fieldName string) *Field {
var field *Field
var ok bool
if field, ok = t.Fields[fieldName]; !ok {
panic(fmt.Sprintf("Field `%s`.`%s` not found", t.Name, fieldName))
}
return field
}
// ValueField is a name/value pair used to setting data on insert or update queries
type ValueField struct {
Name string
Value interface{}
}
// JoinField represents a part of a join clause
type JoinField struct {
FieldName string
Value interface{}
JoinTable string
JoinField string
}
// Join represents a join clause
type Join struct {
Table *Table
Fields []JoinField
}
// IQuery outlines the methods on build a sql query and interacting with the database
type IQuery interface {
Query() (*sql.Rows, error)
Exec() (result sql.Result, e error)
And() IQuery
Or() IQuery
Where(name string, value interface{}) IQuery
Set(fieldName string, value interface{}) IQuery
Join(tableName string) IQuery
OnValue(tableName string, value interface{}) IQuery
OnField(fieldName string, joinTable string, joinField string) IQuery
Limit(limit int) IQuery
Offset(offset int) IQuery
Order(field string, direction string) IQuery
ToSQL() string
GetValues() []interface{}
SelectJoinField(tableName string, fieldName string, as string) IQuery
}
// ISchema represents DAL schema methods
type ISchema interface {
Select(tableName string) IQuery
Update(tableName string) IQuery
Delete(tableName string) IQuery
Insert(tableName string) IQuery
AddTable(name string, fields []string) error
Table(name string) (t *Table)
Exec(query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (result *sql.Rows, e error)
GetTables() map[string]*Table
}