-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnosql.go
More file actions
141 lines (125 loc) · 4.33 KB
/
nosql.go
File metadata and controls
141 lines (125 loc) · 4.33 KB
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
package harper
type AffectedResponse struct {
MessageResponse
SkippedHashes []interface{} `json:"skipped_hashes"`
InsertedHashes []interface{} `json:"inserted_hashes"`
UpdatedHashes []interface{} `json:"update_hashes"` // (sic) not updated_hashes
DeletedHashes []interface{} `json:"deleted_hashes"`
UpsertedHashes []interface{} `json:"upserted_hashes"`
// returned hashes can be of any JSON primitive
}
type SearchByConditionsOptions struct {
Operator string `json:"operator,omitempty"`
Offset int `json:"offset,omitempty"`
Limit int `json:"limit,omitempty"`
Sort Sort `json:"sort,omitempty"`
}
type SearchCondition struct {
Attribute string `json:"attribute,omitempty"`
Comparator string `json:"comparator,omitempty"`
Value interface{} `json:"value,omitempty"`
Operator string `json:"operator,omitempty"`
Conditions []*SearchCondition `json:"conditions,omitempty"`
}
type SearchConditions []SearchCondition
type Sort struct {
Attribute string `json:"attribute,omitempty"`
Descending bool `json:"descending,omitempty"`
Next *Sort `json:"next,omitempty"`
}
// Insert inserts one or more JSON objects into a table
// Hash value of the inserted JSON record MUST be present.
func (c *Client) Insert(schema, table string, records interface{}) (*AffectedResponse, error) {
result := AffectedResponse{}
err := c.opRequest(operation{
Operation: OP_INSERT,
Schema: schema,
Table: table,
Records: records,
}, &result)
return &result, err
}
// Update updates one or more JSON objects in a table.
// Hash value of the inserted JSON record MUST be present.
func (c *Client) Update(schema, table string, records interface{}) (*AffectedResponse, error) {
result := AffectedResponse{}
err := c.opRequest(operation{
Operation: OP_UPDATE,
Schema: schema,
Table: table,
Records: records,
}, &result)
return &result, err
}
// Update updates one or more JSON objects in a table.
// Hash value of the inserted JSON record MUST be present.
func (c *Client) Upsert(database, table string, records interface{}) (*AffectedResponse, error) {
result := AffectedResponse{}
err := c.opRequest(operation{
Operation: OP_UPSERT,
Database: database,
Table: table,
Records: records,
}, &result)
return &result, err
}
// Delete delete one or more JSON objects from a table.
// hashValues must be an array of slice
func (c *Client) Delete(schema, table string, hashValues AttributeList) (*AffectedResponse, error) {
result := AffectedResponse{}
err := c.opRequest(operation{
Operation: OP_DELETE,
Schema: schema,
Table: table,
HashValues: hashValues,
}, &result)
return &result, err
}
// SearchByHash fetches records based on the table's hash field
// (i.e. primary key).
func (c *Client) SearchByHash(schema, table string, v interface{}, hashValues AttributeList, getAttributes AttributeList) error {
return c.opRequest(operation{
Operation: OP_SEARCH_BY_HASH,
Schema: schema,
Table: table,
HashValues: hashValues,
GetAttributes: getAttributes,
}, &v)
}
func (c *Client) SearchById(database, table string, v interface{}, ids interface{}, getAttributes AttributeList) error {
return c.opRequest(operation{
Operation: OP_SEARCH_BY_ID,
Database: database,
Table: table,
IDs: ids,
GetAttributes: getAttributes,
}, &v)
}
// SearchByValue fetches records based on the value of an attribute
// Wilcards are allowed in `searchValue`
func (c *Client) SearchByValue(schema, table string, v interface{}, attribute Attribute, value interface{}, getAttributes AttributeList) error {
op := operation{
Operation: OP_SEARCH_BY_VALUE,
Schema: schema,
Table: table,
Attribute: attribute,
Value: value,
GetAttributes: getAttributes,
}
return c.opRequest(op, &v)
}
func (c *Client) SearchByConditions(database, table string, v interface{}, conditions []SearchCondition, getAttributes AttributeList, options SearchByConditionsOptions) error {
op := operation{
Operation: OP_SEARCH_BY_CONDITIONS,
Database: database,
Table: table,
Conditions: conditions,
Offset: options.Offset,
Limit: options.Limit,
GetAttributes: getAttributes,
}
if (options.Sort != Sort{}) {
op.Sort = &options.Sort
}
return c.opRequest(op, &v)
}