forked from proofrock/ws4sqlite-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
205 lines (186 loc) · 5.83 KB
/
request.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
Copyright (c) 2022-, Germano Rizzo <oss /AT/ germanorizzo /DOT/ it>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package ws4sqlite_client
import "errors"
type credentials struct {
User string `json:"user"`
Password string `json:"password"`
}
type requestItemCrypto struct {
Password string `json:"password"`
Fields []string `json:"fields"`
CompressionLevel int `json:"compressionLevel,omitempty"`
}
type requestItem struct {
Query string `json:"query,omitempty"`
Statement string `json:"statement,omitempty"`
NoFail bool `json:"noFail,omitempty"`
Values map[string]interface{} `json:"values,omitempty"`
ValuesBatch []map[string]interface{} `json:"valuesBatch,omitempty"`
Encoder *requestItemCrypto `json:"encoder,omitempty"`
Decoder *requestItemCrypto `json:"decoder,omitempty"`
}
type request struct {
Credentials *credentials `json:"credentials,omitempty"`
Transaction []requestItem `json:"transaction"`
}
// A builder class to build a Request to send to the ws4sqlite server with the <Client>.Send(Request) method.
//
// If an error is encountered during built, it's returned at Build() time, to be
// able to chain.
type RequestBuilder struct {
err string
list request
temp *requestItem
}
// Container class for a request to ws4sqlite. Built with RequestBuilder.
type Request struct {
req request
}
// First step when building. Generates a new RequestBuilder instance.
func NewRequestBuilder() *RequestBuilder {
return &RequestBuilder{list: request{Transaction: make([]requestItem, 0)}}
}
// Adds a new request to the list, for a query. It must be configured later on with the
// proper methods.
func (rb *RequestBuilder) AddQuery(query string) *RequestBuilder {
if rb.err != "" {
return rb
}
if rb.temp != nil {
rb.list.Transaction = append(rb.list.Transaction, *rb.temp)
}
rb.temp = &requestItem{}
rb.temp.Query = query
return rb
}
// Adds a new request to the list, for a statement. It must be configured later on with the
// proper methods.
func (rb *RequestBuilder) AddStatement(statement string) *RequestBuilder {
if rb.err != "" {
return rb
}
if rb.temp != nil {
rb.list.Transaction = append(rb.list.Transaction, *rb.temp)
}
rb.temp = &requestItem{}
rb.temp.Statement = statement
return rb
}
// Specify that the request must not cause a general failure.
func (rb *RequestBuilder) WithNoFail() *RequestBuilder {
if rb.err != "" {
return rb
}
rb.temp.NoFail = true
return rb
}
// Adds a list of values (ok, amap) for the request. If there's already one,
// it creates a batch.
func (rb *RequestBuilder) WithValues(values map[string]interface{}) *RequestBuilder {
if rb.err != "" {
return rb
}
if values == nil {
rb.err = "cannot specify a nil argument"
return rb
}
if rb.temp.Query != "" && (rb.temp.Values != nil || rb.temp.ValuesBatch != nil) {
rb.err = "cannot specify a batch for a query"
return rb
}
if rb.temp.ValuesBatch != nil {
rb.temp.ValuesBatch = append(rb.temp.ValuesBatch, values)
} else if rb.temp.Values != nil {
rb.temp.ValuesBatch = []map[string]interface{}{rb.temp.Values, values}
rb.temp.Values = nil
} else {
rb.temp.Values = values
}
return rb
}
// Add an encoder to the request, with compression. Allowed only for statements.
func (rb *RequestBuilder) WithEncoderAndCompression(password string, compressionLevel int, fields ...string) *RequestBuilder {
if rb.err != "" {
return rb
}
if compressionLevel < 1 || compressionLevel > 19 {
rb.err = "compressionLevel must be between 1 and 19"
return rb
}
if len(fields) <= 0 {
rb.err = "cannot specify an empty fields list"
return rb
}
if rb.temp.Query != "" {
rb.err = "cannot specify an encoder for a query"
return rb
}
rb.temp.Encoder = &requestItemCrypto{
Password: password,
CompressionLevel: compressionLevel,
Fields: fields,
}
return rb
}
// Add an encoder to the request. Allowed only for statements.
func (rb *RequestBuilder) WithEncoder(password string, fields ...string) *RequestBuilder {
if rb.err != "" {
return rb
}
if len(fields) <= 0 {
rb.err = "cannot specify an empty fields list"
return rb
}
if rb.temp.Query != "" {
rb.err = "cannot specify an encoder for a query"
return rb
}
rb.temp.Encoder = &requestItemCrypto{
Password: password,
Fields: fields,
}
return rb
}
// Add a decoder to the request. Allowed only for queries.
func (rb *RequestBuilder) WithDecoder(password string, fields ...string) *RequestBuilder {
if rb.err != "" {
return rb
}
if len(fields) <= 0 {
rb.err = "cannot specify an empty fields list"
return rb
}
if rb.temp.Statement != "" {
rb.err = "cannot specify a decoder for a statement"
return rb
}
rb.temp.Decoder = &requestItemCrypto{
Password: password,
Fields: fields,
}
return rb
}
// Returns the Request that was built, returning also any error that was
// encountered during build.
func (rb *RequestBuilder) Build() (*Request, error) {
if rb.temp == nil {
rb.err = "There are no requests"
}
if rb.err != "" {
return nil, errors.New(rb.err)
}
rb.list.Transaction = append(rb.list.Transaction, *rb.temp)
return &Request{rb.list}, nil
}