Newer
Older
"upper.io/db.v2/sqlbuilder/cache"
)
// Statement represents different kinds of SQL statements.
type Statement struct {
Type
Table Fragment
Database Fragment
Columns Fragment
Values Fragment
ColumnValues Fragment
OrderBy Fragment
GroupBy Fragment
Joins Fragment
Where Fragment
Returning Fragment
Limit
Offset
}
type statementT struct {
Table string
Database string
Columns string
Values string
ColumnValues string
OrderBy string
GroupBy string
Where string
Joins string
Returning string
Limit
Offset
}
func (layout *Template) doCompile(c Fragment) string {
if c != nil && !reflect.ValueOf(c).IsNil() {
return c.Compile(layout)
}
return ""
}
func getHash(h cache.Hashable) string {
if h != nil && !reflect.ValueOf(h).IsNil() {
return h.Hash()
}
return ""
}
// Hash returns a unique identifier for the struct.
func (s *Statement) Hash() string {
}
// Compile transforms the Statement into an equivalent SQL query.
func (s *Statement) Compile(layout *Template) (compiled string) {
// No need to hit the cache.
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
}
if z, ok := layout.Read(s); ok {
return z
}
data := statementT{
Table: layout.doCompile(s.Table),
Database: layout.doCompile(s.Database),
Limit: s.Limit,
Offset: s.Offset,
Columns: layout.doCompile(s.Columns),
Values: layout.doCompile(s.Values),
ColumnValues: layout.doCompile(s.ColumnValues),
OrderBy: layout.doCompile(s.OrderBy),
GroupBy: layout.doCompile(s.GroupBy),
Where: layout.doCompile(s.Where),
Returning: layout.doCompile(s.Returning),
Joins: layout.doCompile(s.Joins),
}
switch s.Type {
case Truncate:
compiled = mustParse(layout.TruncateLayout, data)
case DropTable:
compiled = mustParse(layout.DropTableLayout, data)
case DropDatabase:
compiled = mustParse(layout.DropDatabaseLayout, data)
case Count:
compiled = mustParse(layout.CountLayout, data)
case Select:
compiled = mustParse(layout.SelectLayout, data)
case Delete:
compiled = mustParse(layout.DeleteLayout, data)
case Update:
compiled = mustParse(layout.UpdateLayout, data)
case Insert:
compiled = mustParse(layout.InsertLayout, data)
default:
panic("Unknown template type.")
}
layout.Write(s, compiled)
return compiled
}
// RawSQL represents a raw SQL statement.
func RawSQL(s string) *Statement {
return &Statement{
Type: SQL,
SQL: s,