/
context.go
165 lines (131 loc) · 3.73 KB
/
context.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package rebecca
// This file contains thin exported methods related to Context only.
//
// For unexported functions see: helpers.go
import (
"fmt"
"github.com/waterlink/rebecca/context"
"github.com/waterlink/rebecca/driver"
)
// Context is for storing query context
type Context struct {
// Defines ordering of the query
Order string
// Defines grouping criteria of the query
Group string
// Defines maximum amount of records requested for the query
Limit int
// Defines starting record for the query
Skip int
Offset int // alias of Skip
tx interface{}
}
// GetOrder is for fetching context's Order. Used by drivers
func (c *Context) GetOrder() string {
return c.Order
}
// GetGroup is for fetching context's Group. Used by drivers
func (c *Context) GetGroup() string {
return c.Group
}
// GetLimit is for fetching context's Limit. Used by drivers
func (c *Context) GetLimit() int {
return c.Limit
}
// GetSkip is for fetching context's Skip. Also it fetches Offset if present,
// hence the alias. Used by drivers
func (c *Context) GetSkip() int {
if c.Offset > 0 {
return c.Offset
}
return c.Skip
}
// GetTx is for fetching context's driver transaction state. Used by drivers
func (c *Context) GetTx() interface{} {
return c.tx
}
// SetOrder is for setting context's Order, it creates new Context. Used by drivers
func (c *Context) SetOrder(order string) context.Context {
ctx := c.makeCopy()
ctx.Order = order
return &ctx
}
// SetGroup is for setting context's Group. Used by drivers
func (c *Context) SetGroup(group string) context.Context {
ctx := c.makeCopy()
ctx.Group = group
return &ctx
}
// SetLimit is for setting context's Limit. Used by drivers
func (c *Context) SetLimit(limit int) context.Context {
ctx := c.makeCopy()
ctx.Limit = limit
return &ctx
}
// SetSkip is for setting context's Skip. Used by drivers
func (c *Context) SetSkip(skip int) context.Context {
ctx := c.makeCopy()
ctx.Skip = skip
ctx.Offset = skip
return &ctx
}
// SetTx is for setting context's Tx. Used by drivers
func (c *Context) SetTx(tx interface{}) context.Context {
ctx := c.makeCopy()
ctx.tx = tx
return &ctx
}
// All is for fetching all records
func (c *Context) All(records interface{}) error {
d, lock := driver.Get()
defer lock.Unlock()
meta, err := getMetadata(records)
if err != nil {
return err
}
fieldss, err := d.All(meta.tablename, meta.fields, c)
if err != nil {
return fmt.Errorf("Unable to fetch all records - %s", err)
}
if err := populateRecordsFromFieldss(records, fieldss); err != nil {
return fmt.Errorf("Unable to fetch all records - %s", err)
}
return nil
}
// Where is for fetching specific records
func (c *Context) Where(records interface{}, query string, args ...interface{}) error {
d, lock := driver.Get()
defer lock.Unlock()
meta, err := getMetadata(records)
if err != nil {
return err
}
fieldss, err := d.Where(meta.tablename, meta.fields, c, query, args...)
if err != nil {
return fmt.Errorf("Unable to fetch specific records - %s", err)
}
if err := populateRecordsFromFieldss(records, fieldss); err != nil {
return fmt.Errorf("Unable to fetch specific records - %s", err)
}
return nil
}
// First is for fetching only one specific record
func (c *Context) First(record interface{}, query string, args ...interface{}) error {
d, lock := driver.Get()
defer lock.Unlock()
meta, err := getMetadata(record)
if err != nil {
return err
}
fields, err := d.First(meta.tablename, meta.fields, c, query, args...)
if err != nil {
return fmt.Errorf("Unable to fetch specific records - %s", err)
}
if err := setFields(record, fields); err != nil {
return fmt.Errorf("Unable to assign fields for the record - %s", err)
}
return nil
}
func (c Context) makeCopy() Context {
return c
}