-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
135 lines (110 loc) · 2.72 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
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
package dal
import (
"database/sql"
"fmt"
"strconv"
)
// NewSchema defines a new schema
func NewSchema(dal *Dal, name string) *Schema {
s := new(Schema)
s.Name = name
s.Tables = map[string]*Table{}
s.Aliases = map[string]string{}
s.Dal = dal
return s
}
// Schema is a collection of tables
type Schema struct {
Name string
Tables map[string]*Table
Aliases map[string]string
Dal *Dal
}
// GetTables lists the available tables
func (s *Schema) GetTables() map[string]*Table {
return s.Tables
}
// AddTable adds a table to the schema
func (s *Schema) AddTable(name string, fields []string) error {
t := NewTable(name)
if len(fields) > 0 {
t.AddFields(fields)
}
return s.define(t)
}
// Table gets a table
func (s *Schema) Table(name string) (t *Table) {
var ok bool
if t, ok = s.Tables[name]; !ok {
panic(fmt.Sprintf("No table named `%s` has been defined", name))
}
return
}
// Select starts a select statement
func (s *Schema) Select(tableName string) IQuery {
return s.newQuery(tableName, "select")
}
// Update starts an update query
func (s *Schema) Update(tableName string) IQuery {
return s.newQuery(tableName, "update")
}
// Delete starts a delete query
func (s *Schema) Delete(tableName string) IQuery {
return s.newQuery(tableName, "delete")
}
// Insert starts an insert query
func (s *Schema) Insert(tableName string) IQuery {
return s.newQuery(tableName, "insert")
}
// Count starts a count query
func (s *Schema) Count(tableName string) IQuery {
return s.newQuery(tableName, "count")
}
// Exec prepares and executes the query
func (s *Schema) Exec(query string, args ...interface{}) (sql.Result, error) {
var e error
var stmt *sql.Stmt
stmt, e = s.Dal.Connection.Prepare(query)
// fmt.Printf("Stmt: %v\n", stmt)
if e != nil {
fmt.Printf("Error: %s", e.Error())
return nil, e
}
defer stmt.Close()
return stmt.Exec(args...)
}
// Query runs sql.Query and returns the results
func (s *Schema) Query(query string, args ...interface{}) (result *sql.Rows, e error) {
result, e = s.Dal.Connection.Query(query, args...)
return
}
func (s *Schema) newQuery(tableName string, queryType string) IQuery {
q := new(Query)
q.Dal = s.Dal
q.Table = s.Table(tableName)
q.QueryType = queryType
return q
}
// define adds a table to the set of tables in the schema
func (s *Schema) define(t *Table) (e error) {
var ok bool
if _, ok = s.Tables[t.Name]; ok {
e = fmt.Errorf("a table named `%s` has already been defined", t.Name)
return
}
alias := t.Name[0:1]
tryAlias := alias
tries := 0
for {
if _, ok = s.Aliases[tryAlias]; !ok {
alias = tryAlias
break
}
tries = tries + 1
tryAlias = alias + strconv.Itoa(tries)
}
t.Alias = alias
s.Tables[t.Name] = t
s.Aliases[alias] = t.Name
return
}