-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathds_result.lasso
335 lines (281 loc) · 7.05 KB
/
ds_result.lasso
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?lassoscript
//============================================================================
//
// result handles
//
//............................................................................
define result => result(1)
define result(setnum::integer) => {
local(results) = results
if(#results->isa(::staticarray)) => {
#setnum > #results->size || #setnum < 1
? fail('Invalid setnum: ' + #setnum)
return #results->get(#setnum)
else
return ds_result(#setnum)
}
}
define results => thread_var_get(::__ds_results)
define result_push(p::any) => thread_var_push(::__ds_results,#p)
define result_pop => thread_var_pop(::__ds_results)
//============================================================================
//
// ds_result
//
//............................................................................
define ds_result => type {
data
public index::trait_searchable,
public cols::trait_positionallykeyed,
public types::trait_positionallykeyed,
public rows::staticarray,
public set::staticarray,
public found::integer=0,
public affected::integer=0,
public num::integer=0,
private error::staticarray=(:0,'',''),
private ds,
private dsinfo,
private dsrows
//---------------------------------------------------------------------------------------
//
// oncreate sigs
//
//---------------------------------------------------------------------------------------
public oncreate(
set::staticarray,
dsinfo::dsinfo,
affected::integer,
error::staticarray,
num::integer
) => {
.dsinfo = #dsinfo
.affected = #affected
.oncreate(#set,#error,#num)
}
public oncreate(
ds::ds,
set::staticarray,
dsinfo::dsinfo,
affected::integer,
error::staticarray,
num::integer
) => {
.ds = #ds
.dsinfo = #dsinfo
.affected = #affected
.oncreate(#set,#error,#num)
}
public oncreate(
set::staticarray,
error::staticarray,
num::integer
) => {
local(
index = hashtable,
rows = #set->get(INLINE_RESULTROWS_POS),
found = #set->get(INLINE_FOUNDCOUNT_POS),
affected = 0,
cols = array,
i = 1,
col
)
#set->get(INLINE_COLUMNINFO_POS)->foreach => {
#cols->insert(#col := #1->get(INLINE_COLINFO_NAME_POS))
#index->insert(#col = #i++)
}
.'cols' = #cols->asstaticarray
.'index' = #index
.'rows' = #rows->asstaticarray
.'set' = #set
.'found' = #found
.'affected' = #affected
.'error' = #error
.'num' = #num
}
public oncreate(
index::trait_searchable,
cols::trait_positionallykeyed,
rows::staticarray,
set::staticarray,
found::integer=0,
affected::integer=0,
error::staticarray=.error_current,
num::integer=0
) => {
.'cols' = #cols
.'index' = #index
.'rows' = #rows
.'set' = #set
.'found' = #found
.'affected' = #affected
.'error' = #error
.'num' = #num
}
public oncreate => {
// Support standard inlines
local(set) = (inline_scopeGet ? inline_scopeGet->find(::currentset))
#set->size ? .oncreate(#set,.error_current)
.'error' = .error_current
}
public oncreate(num::integer) => {
local(
scope = inline_scopeget,
set = #scope ? #scope->find(::currentinline)->dsinfo->getset(#num)
)
#set->size ? .oncreate(#set,.error_current,#num)
.'error' = .error_current
.'num' = #num
}
//---------------------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------------------
public error_code => .'error'->get(1)
public error_msg => .'error'->get(2)
public error_stack => .'error'->get(3)
public error_current => (:error_code,error_msg,error_stack)
public columns => .cols->asstaticarray
public found_count => .found
public columntype(i::integer)::tag => {
match(#i) => {
case(lcapi_datasourcetypestring)
return ::string
case(lcapi_datasourcetypeinteger)
return ::integer
case(lcapi_datasourcetypeboolean)
return ::boolean
case(lcapi_datasourcetypeblob)
return ::bytes
case(lcapi_datasourcetypedecimal)
return ::decimal
case(lcapi_datasourcetypedate)
return ::date
case
return
}
}
public columntypes => {
.'types' ? return .'types'
local(types) = map
.'set'->get(INLINE_COLUMNINFO_POS)->foreach => {
#types->insert(
#1->get(INLINE_COLINFO_NAME_POS) = .columntype(
#1->get(INLINE_COLINFO_TYPE_POS)
)
)
}
return .'types' := #types
}
public rows => {
local(
rows = .'dsrows'
)
if(not #rows) => {
#rows = array
.'rows'->foreach => {
#rows->insert(
ds_row(.'index',.'cols',#1,.'dsinfo',.'ds')
)
}
}
.'dsrows' = #rows
.gb(#rows) => givenblock
return #rows->asstaticarray
}
public rows(type::tag) => {
local(
out = array,
base = #type->gettype,
row
)
.rows->foreach => {
#row = #base->ascopy
#row->oncreate(#1)
#out->insert(#row)
}
#out = #out->asstaticarray
.gb(#out) => givenblock
return #out
}
public rows(creator::memberstream) => {
local(
out = array
)
.rows->foreach => {
#out->insert(
#creator(#1)
)
}
#out = #out->asstaticarray
.gb(#out) => givenblock
return #out
}
private gb(rows::trait_foreach) => {
local(gb) = givenblock
if(#gb) => {
result_push(self)
#rows->foreach => {
#gb(#1)
}
result_pop
}
}
public row(row::integer) => {
.'dsrows' ? return .'dsrows'->get(#row)
return ds_row(.'index', .'cols', .'rows'->get(#row), .'dsinfo', .'ds')
}
public asstring => {
return(
.type->asstring+'('+
.'cols'->size + ' columns, '+
.'rows'->size + ' rows, '+
.found +' found, '+
.affected +' affected)'
)
}
//---------------------------------------------------------------------------------------
//
// Result iterator
//
//---------------------------------------------------------------------------------------
public foreach => {
local(gb) = givenblock
.rows->foreach => {#gb(#1)}
}
public do(gb::capture) => {
.rows->foreach => {
#gb(#1)
}
}
//---------------------------------------------------------------------------------------
//
// Shortcuts
//
//---------------------------------------------------------------------------------------
public first => .rows->first
public firstrow(...) => .first(:#rest || staticarray)
public first(col::string) => .first->find(#col)
public first(col::tag) => .first->find(#col->asstring)
public last => .rows->last
public lastrow(...) => .last(:#rest || staticarray)
public last(col::tag) => .last->find(#col->asstring)
public last(col::string) => .last->find(#col)
//---------------------------------------------------------------------------------------
//
// Find rows within result set
//
//---------------------------------------------------------------------------------------
public find(p::pair) => (
with row in .rows
where #row->find(#p->name) == #p->value
select #row
)->asstaticarray
public find(p1::pair,p2::pair) => (
with row in .rows
where #row->find(#p1->name) == #p1->value && #row->find(#p2->name) == #p2->value
select #row
)->asstaticarray
}
?>