-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathds.lasso
1416 lines (1143 loc) · 40.7 KB
/
ds.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?lassoscript
//=======================================================================================
//
// DS Suite for Lasso 9 — Free to use, license TBD
//
//..All rights reserved — K Carlton 2013.................................................
//---------------------------------------------------------------------------------------
//
// Connections stored on thread level
//
//---------------------------------------------------------------------------------------
define ds_connections => {
if(var(__ds__connections__)->isnota(::map)) => {
$__ds__connections__ = map
// Queue closure of connections
web_request ? define_atend({ds_close_connections})
}
return $__ds__connections__
}
define ds_close_connections => {
// Marked connections closing (this prevents connections being stored / they close after execution)
ds_connections_closing = 1
// Marked connections closed (this prevents connections being stored / they close after execution)
handle => {
ds_connections_closed = 1
}
ds_connections->foreach => {
#1->close
}
}
define ds_connections_closing => var(__ds_connections_closing) || 0
define ds_connections_closing = (p::integer) => var(__ds_connections_closing) := #p
define ds_connections_closed => var(__ds_connections_closed) || 0
define ds_connections_closed = (p::integer) => var(__ds_connections_closed) := #p
//---------------------------------------------------------------------------------------
//
// datasource — english relay
//
//---------------------------------------------------------------------------------------
define datasource(...) => ds(:#rest || staticarray)
//---------------------------------------------------------------------------------------
//
// ds — defaults
//
//---------------------------------------------------------------------------------------
define ds_default_silent => false
define ds_default_maxrows => 50
define ds_retry_lost_connections => false
define ds_max_retry_attempts => 1
define ds_retry_error_strings => (:
'Lost connection',
'Can\'t connect to MySQL'
)
//---------------------------------------------------------------------------------------
//
// ds — main type
//
//---------------------------------------------------------------------------------------
define ds => type{
data
public dsinfo::dsinfo,
public key::string = '',
public silent::boolean,
public attempts::integer = 0,
private keycolumn::string = 'id',
private results = staticarray,
// Legacy: support action_params
public actionparams = staticarray,
// Connector
public capi
// Thread safe copy
public ascopy => {
local(ds) = ds
#ds->dsinfo = .dsinfo->makeinheritedcopy
#ds->dsinfo->keycolumns = .dsinfo->keycolumns
#ds->key = .key
#ds->capi = .capi
return #ds
}
public ascopydeep => .ascopy
public silent => (.'silent'->isnota(::void) ? .'silent' | ds_default_silent)
//---------------------------------------------------------------------------------------
//
// Oncreate
//
//---------------------------------------------------------------------------------------
// Look up details from lasso admin (overhead)
public oncreate(
database::string,
table::string = ''
) => .oncreate(
-database = #database,
-table = #table
) => givenblock
// Slick db.table accessor ds(::database.table)
public oncreate(databasetable::tag) => {
local(s) = #databasetable->asstring->splitextension('.')
.oncreate(
-database = #s->first,
-table = #s->second || ''
) => givenblock
}
// Fast host accessor — ds(::mysqlds,'127.0.0.1',::product.list)
public oncreate(
datasource::tag,
host::string,
databasetable::tag,
username::string='',
password::string='',
port::integer=3306
) => {
local(s) = #databasetable->asstring->splitextension('.')
.oncreate(
-datasource = #datasource->asstring,
-database = #s->first,
-table = #s->second || '',
-host = #host,
-username = #username,
-password = #password,
-port = #port
) => givenblock
}
// Fast host accessor — ds(::mysqlds,'127.0.0.1','product','list')
public oncreate(
datasource::tag,
host::string,
database::string,
table::string,
username::string='',
password::string='',
port::integer=3306
) => .oncreate(
-datasource = #datasource->asstring,
-database = #database,
-table = #table,
-host = #host,
-username = #username,
-password = #password,
-port = #port
) => givenblock
//------------------------------------------------------------------------
//
// Duplicate a connection (legacy inline support bundled here)
//
//------------------------------------------------------------------------
public oncreate(dsinfo::dsinfo,
useinfo::boolean=false,
params::staticarray=staticarray
) => {
// Legacy: store action params
.actionparams = #params
#useinfo ? handle => {.keycolumn = ''}
return .oncreate(
-datasource = #dsinfo->hostdatasource || 'mysqlds',
-database = #dsinfo->databasename,
-table = #dsinfo->tablename,
-host = #dsinfo->hostname,
-hostschema = #dsinfo->hostschema,
-username = #dsinfo->hostusername,
-password = #dsinfo->hostpassword,
-port = integer(#dsinfo->hostport),
-encoding = #dsinfo->hosttableencoding || 'UTF-8',
-maxrows = #dsinfo->maxrows || ds_default_maxrows,
-dsinfo = #dsinfo, // Legacy: leverage dsinfo constructor
-useinfo = #useinfo // Legacy: switch to trigger legacy mode
) => givenblock
}
//------------------------------------------------------------------------
//
// Support core inline params
//
//------------------------------------------------------------------------
public oncreate(
-datasource::string='mysqlds',
-database::string='',
-table::string='',
-keycolumn::string='',
-maxrows::integer=ds_default_maxrows,
// Host params
-host::string='',
-port::integer=3306,
-username::string='',
-password::string='',
-schema::string='',
-encoding::string='UTF-8',
// Perhaps this should be culled (legacy support for old version of ds)
-sql::string='',
// Legacy: support classic inline
-dsinfo::dsinfo=dsinfo,
-useinfo::boolean=false,
// Allow key override
-key::string = #host + #database + #username + #port
) => {
// Work round oncreate givenblock bug.
.'dsinfo' = #dsinfo
local(
dsinfo = .'dsinfo',
hostinfo,
store = true,
gb = givenblock
)
// Store key
.key = #key
handle => {
// Set keycolumn info
.keycolumn = #keycolumn || .keycolumn
#store ? .store // Details only (connection unlikely)
}
if(.primed) => {
// Reusing details + connection (if isactive)
#store = false
else(#host)
// Host specified, skip look up — fast
#dsinfo->hostdatasource = #datasource
#dsinfo->hostid = 0
#dsinfo->hostname = #host
#dsinfo->hostport = #port->asstring
#dsinfo->hostusername = #username
#dsinfo->hostpassword = #password
#dsinfo->hosttableencoding = #encoding
#dsinfo->hostschema = #schema
.'capi' = \#datasource
else(#database)
// Look up database info (slow due to extra db call)
#table // if table specified check for specific encoding
? #hostinfo = .get_dsinfo(#database,#table)
| #hostinfo = .get_dsinfo(#database)
fail_if(!#hostinfo,'Unable to determine host for: '+#database+'.'+#table)
// Set properties from found info
#dsinfo->hostdatasource = #hostinfo->get(1)
#dsinfo->hostid = #hostinfo->get(2)
#dsinfo->hostname = #hostinfo->get(3)
#dsinfo->hostport = #hostinfo->get(4)
#dsinfo->hostusername = #hostinfo->get(5)
#dsinfo->hostpassword = #hostinfo->get(6)
#dsinfo->hostschema = #hostinfo->get(7)
#dsinfo->hosttableencoding = #hostinfo->get(8)||#encoding
.'capi' = \#datasource
else
#store = false
}
// Replace database and table (most likely the same unless key)
#dsinfo->databasename = #database
#dsinfo->tablename = #table
#dsinfo->maxrows = #maxrows
// Legacy: leverage clasic inlie constructor
if(#useinfo) => {
.'dsinfo'->action = #dsinfo->action
.'dsinfo'->statement = #dsinfo->statement
.'dsinfo'->statementonly = #dsinfo->statementonly
.'dsinfo'->skiprows = #dsinfo->skiprows
#dsinfo->keycolumns ? .'dsinfo'->keycolumns = #dsinfo->keycolumns
#dsinfo->inputcolumns ? .'dsinfo'->inputcolumns = #dsinfo->inputcolumns
#dsinfo->returncolumns ? .'dsinfo'->returncolumns = #dsinfo->returncolumns
#dsinfo->sortColumns ? .'dsinfo'->sortColumns = #dsinfo->sortColumns
}
// Cheeky sql short cut — should be killed.
if(#sql) => {
#dsinfo->action = lcapi_datasourceExecSQL
#dsinfo->statement = #sql
}
#gb ? return .invoke => #gb
}
//---------------------------------------------------------------------------
//
// Bypass Lasso's database_registry for performance gains (350ms vs 5000ms)
//
//---------------------------------------------------------------------------
// Cache database_registry registry
private reg => {
var(::__ds_reg__)->isnota(::sqlite_db)
? $__ds_reg__ = sqlite_db(database_database)
return $__ds_reg__
}
// Faster lookup than database_registry
private get_dsinfo(
database::string
) => .reg->executeLazy(
'SELECT
ds.name AS datasource,
h.id AS id,
h.name AS host,
h.port AS port,
h.username,
h.password,
h.schema,
"" as encoding
FROM datasources AS ds,
datasource_hosts AS h,
datasource_databases AS db
WHERE h.id = db.id_host
AND db.id_datasource = ds.id
AND db.alias = ' + database_qs(#database) + '
LIMIT 0,1'
)->foreach => {return #1}
// Faster lookup table specific
private get_dsinfo(
database::string,
table::string
) => .reg->executeLazy(
'SELECT
ds.name AS datasource,
h.id AS id,
h.name AS host,
h.port AS port,
h.username,
h.password,
h.schema,
tb.encoding
FROM datasources AS ds,
datasource_hosts AS h,
datasource_databases AS db,
database_tables AS tb
WHERE h.id = db.id_host
AND db.id_datasource = ds.id
AND tb.id_database = db.id
AND db.alias = ' + database_qs(#database) + '
AND tb.alias = ' + database_qs(#table) + '
LIMIT 0,1'
)->foreach => {return #1}
//-----------------------------------------------------------
//
// Issue SQL command
//
//-----------------------------------------------------------
public sql(
statement::string, maxrows::integer = .dsinfo->maxrows || ds_default_maxrows
) => {
// Clear old results
.removeall
local(dsinfo) =.'dsinfo'
// Set to execute SQL
#dsinfo->maxrows = #maxrows
#dsinfo->action = lcapi_datasourceExecSQL
#dsinfo->statement = #statement
return .invoke => givenblock
}
public lazysql(statement::string) => {
.sql = #statement
return self
}
public sqlcomment => {
// No comment if a comment is already present
.'dsinfo' && .'dsinfo'->statement && .'dsinfo'->statement->asstring->beginswith('/*')
? return ''
// Otherwise check for given block
local(gb) = givenblock
if(#gb) => {
local(comments) = array
#gb->self->isA(::void)
? #comments->insert('method: ' + #gb->methodname)
| #gb->self->isNotA(::sourcefile)
? #comments->insert('method: ' + #gb->self->type->asString + '.' + #gb->methodname)
#comments->insert('file: ' + #gb->callsite_file)
#comments->insert('date: ' + date)
return '/* ' + #comments->join(', ') + ' */\n'
}
// Otherwise no comment
return ''
}
public sql=(statement::string) => {
local(dsinfo) =.'dsinfo'
#dsinfo->action = lcapi_datasourceExecSQL
#dsinfo->statement = #statement
}
private store => {
// Don't store connections when the thread has been closed off
.storeconnection
? ds_connections->insert(.key = self)
}
private store(dsinfo::dsinfo) => {
// Deal with multiple unopened connections
.dsinfo->connection = #dsinfo->connection
.dsinfo->prepared = #dsinfo->prepared
.dsinfo->refobj = #dsinfo->refobj
.store
}
private active => ds_connections->find(.key)
private primed => {
// Connection could be stored but not active — primed deals with this
// We still want to use the details (dsinfo) and connection if active
// On invoke we must ensure we use an active connection via .active
local(
dsinfo = .dsinfo,
active = .active,
d
)
if(#active) => {
#d = #active->dsinfo
// Check for existing connection
.'capi' = #active->capi
// Ensure thread safe
#dsinfo->hostdatasource = #d->hostdatasource
#dsinfo->hostid = #d->hostid
#dsinfo->hostname = #d->hostname
#dsinfo->hostport = #d->hostport
#dsinfo->hostusername = #d->hostusername
#dsinfo->hostpassword = #d->hostpassword
#dsinfo->hosttableencoding = #d->hosttableencoding
#dsinfo->hostschema = #d->hostschema
#dsinfo->connection = #d->connection
#dsinfo->prepared = #d->prepared
#dsinfo->refobj = #d->refobj
return true
}
return false
}
private storeconnection => !ds_connections_closed && web_request
private isactive => {
// Previously this did nothing if had a .dsinfo->connection
// This caused a problem if another ds had closed the connection
// takes 5 micros vs 1 micros
! .storeconnection
? return 0
local(
dsinfo = .dsinfo,
active = .active,
d
)
if(#active && #active->dsinfo->connection) => {
#d = #active->dsinfo
// Re use existing connection
.'capi' = #active->capi
#dsinfo->connection = #d->connection
#dsinfo->prepared = #d->prepared
#dsinfo->refobj = #d->refobj
return 1
}
// If the connection connection is not active then open and store a new one
#dsinfo->connection = 0
return 0
}
public close(dsinfo::dsinfo = .dsinfo) => {
if(#dsinfo->connection) => {
#dsinfo->action = lcapi_datasourcetickle
.'capi'->invoke(#dsinfo)
#dsinfo->action = lcapi_datasourceCloseConnection
.'capi'->invoke(#dsinfo)
// Ensure both supplied connection and own connection are cleared
#dsinfo->connection = 0
.dsinfo->connection = 0
// Deal with descendants
local(active) = ds_connections->find(.key)
#active ? #active->dsinfo->connection = 0
// This is needed for thread support
.dsinfo = #dsinfo->makeinheritedcopy
}
}
public notyet => {
local(w) = (givenblock ? givenblock->methodname->asstring)
not #w ? #w = 'this'
fail('Oops — '+#w+' still needs to be implemented')
}
public shouldretry(error_msg::string, attempts::integer) => {
if(ds_retry_lost_connections && #attempts < ds_max_retry_attempts) => {
with key in ds_retry_error_strings
where #error_msg >> #key
do {
return 1
}
}
return 0
}
//-----------------------------------------------------------
//
// Invoke connector
//
//-----------------------------------------------------------
public invoke(dsinfo::dsinfo = .'dsinfo') => {
// Remove old results
.removeall
local(
gb = givenblock,
capi = .'capi',
results = array,
s = 1,
set,
result,
error,
affected,
keycolumns,
index,
cols,
col
)
// Automatically include SQL comments
if(#dsinfo->action == lcapi_datasourceExecSQL && #dsinfo->hostdatasource == 'mysqlds') => {
#dsinfo->statement = (.sqlcomment => #gb) + #dsinfo->statement
}
fail_if(not #capi, 'No datasource: check -database, -table or -datasource')
// Close connection if thread or if new connection is after connections closed
! .storeconnection
? handle => {
.close(#dsinfo)
}
.storeconnection && not .isactive
? handle => {
.store
}
protect => {
// Ensure error stack is set
handle => {
// Shared error per request
#error = (: error_code, error_msg, error_stack)
// Restore keycolumn info
#keycolumns
? #dsinfo->keycolumns = #keycolumns
// Output errors
if(error_code) => {
protect => {
debug(#dsinfo->statement)
}
stdoutnl('\nds error: ' + error_msg + '\n' + error_stack)
else
// Clear attempts on success
.attempts = 0
}
}
// Store keycolumns for restore
#keycolumns = #dsinfo->keycolumns
// Searches can not contain keycolumns (remove when null)
#dsinfo->action == lcapi_datasourcesearch && #keycolumns->size && #keycolumns->get(1)->get(3)->isa(::null)
? #dsinfo->keycolumns = staticarray
// Key values should not be used on add (::mysqlds returns random rows)
// | #dsinfo->action == lcapi_datasourceadd && #keycolumns->size && #keycolumns->get(1)->get(3)->isa(::null)
// ? #dsinfo->keycolumns = staticarray
#result = #capi->invoke(#dsinfo)
#result ? return #result
}
// Attempt on lost connection
if(.shouldretry(#error->get(2)->asstring, .attempts++)) => {
stdoutnl('ds retrying: ' + #error->get(2)->asstring + '\n' + error_stack)
// Pause for a moment
sleep(500 * .attempts)
// Clear connection (since lost)
#dsinfo->connection = 0
// Return with given block
return (
.invoke(#dsinfo) => #gb
)
}
if(.silent) => {
error_code = #error->get(1)
error_msg = #error->get(2)
error_stack = #error->get(3) || ''
else
#error->get(1)
? fail(#error->get(1),#error->get(2),#error->get(3))
}
{
#set = #dsinfo->getset(#s)
#affected = integer(var(::__updated_count__))
#set
? #result = ds_result(self,#set,#dsinfo,0,#error,#s->ascopy)
| #result = ds_result(indextable,staticarray,staticarray,staticarray,0,#affected,#error,#s->ascopy)
// Set result number
#result->num = #s->ascopy
#results->insert(#result)
#s++ < #dsinfo->numsets
? currentcapture->restart
}()
#gb ? .push(#results->asstaticarray)
#gb ? handle => {
.pop
}
.'results' = #results->asstaticarray
return (#gb ? #gb(#results->first)) or self
}
public results => .'results'
public removeall => { .'results' = staticarray }
public first => .'results'->first || ds_result
public last => .'results'->last || ds_result
//---------------------------------------------------------------------------------------
//
// legacy method support, perhaps should just be dropped.
// supports all except: action_params and keycolumn_name (due to 'workingkeyfield_name' accessor)
//
//---------------------------------------------------------------------------------------
public push(results::staticarray) => {
local(scope) = map(
::currentinline = self,
::currentset = ((#results->size ? #results->first->set) || (:(:),(:),0))
)
inline_scopepush(#scope)
result_push(#results)
}
public pop => {
inline_scopepop
result_pop
}
//---------------------------------------------------------------------------------------
//
// Result iterator
//
//---------------------------------------------------------------------------------------
public foreach => {
local(gb) = givenblock
.'results'->foreach => {#gb(#1)}
}
public do(gb::capture) => {
.results->foreach => {
#gb(#1)
}
}
//---------------------------------------------------------------------------------------
//
// inline support
//
//---------------------------------------------------------------------------------------
public workingkeyfield_name => .keycolumn
//---------------------------------------------------------------------------------------
//
// key column setter
//
//---------------------------------------------------------------------------------------
public keycolumn => {
.'dsinfo'->keycolumns->size
? return .'dsinfo'->keycolumns->get(1)->get(1)
| return .'keycolumn'
}
public keycolumn=(col::string) => {
#col
? .'dsinfo'->keycolumns = (:(:#col, lcapi_datasourceopeq, null))
| .'dsinfo'->keycolumns = (:)
return #col
}
public keycolumn(col::tag) => .keycolumn(#col->asstring)
public keycolumn(col::string) => {
.keycolumn = #col
return self
}
//---------------------------------------------------------------------------------------
//
// Multiple key column support
//
//---------------------------------------------------------------------------------------
public keycolumns => {
.'dsinfo'->keycolumns->size
? return (with key in .'dsinfo'->keycolumns select #key->get(1))->asstaticarray
| return (:.'keycolumn')
}
public keycolumns=(keycolumns::trait_positionallykeyed) => {
.'dsinfo'->keycolumns = (
with col in #keycolumns
select .keyvalue(#col = null)
)->asstaticarray
return #keycolumns->asstaticarray
}
public keycolumns(key::string,...) => .keycolumns((with p in params select #p)->asstaticarray)
public keycolumns(key::tag,...) => .keycolumns((with p in params select #p->asstring)->asstaticarray)
public keycolumns(keycolumns::trait_positionallykeyed) => {
.keycolumns = #keycolumns
return self
}
//---------------------------------------------------------------------------------------
//
// key value wrappers
//
//---------------------------------------------------------------------------------------
private keyvalues => .keyvalues(.keycolumns)
private keyvalues(p::pair,...) => .keyvalues(params)
private keyvalues(p::integer) => .keyvalues(.keycolumn = #p)
private keyvalues(p::string) => .keyvalues(.keycolumn = #p)
private keyvalues(keys::trait_keyedforeach) => {
return (with p in #keys->eachpair select .keyvalue(#p))->asstaticarray
}
private keyvalues(keys::trait_positionallykeyed) => {
return (with p in #keys select .keyvalue(#p))->asstaticarray
}
private keyvalue(p::string) => (:#p, lcapi_datasourceopeq,null)
private keyvalue(p::tag) => (:#p->asstring, lcapi_datasourceopeq,null)
private keyvalue(p::pair) => (:#p->name, lcapi_datasourceopeq,.filterinput(#p->value))
//---------------------------------------------------------------------------------------
//
// input column wrapper
//
//---------------------------------------------------------------------------------------
private inputcolumns(p::trait_positionallykeyed) => {
local(input) = array
#p->foreach => {
#input->insert((:#1->first, lcapi_datasourceopeq, .filterinput(#1->second)))
}
return #input->asstaticarray
}
private filterinput(p::integer) => #p
private filterinput(p::decimal) => #p
private filterinput(p::string) => #p
private filterinput(p::bytes) => #p
private filterinput(p::null) => #p
private filterinput(p::void) => null
private filterinput(p::any) => #p->asstring
//---------------------------------------------------------------------------------------
//
// Table operators
//
//---------------------------------------------------------------------------------------
public tickle => {
.dsinfo->action = lcapi_datasourcetickle
return .invoke
}
public info => {
.dsinfo->maxrows = -1
.dsinfo->action = lcapi_datasourceinfo
return .invoke->last
}
public statement => .dsinfo->statement
public datasource => .dsinfo->hostdatasource
public database => .dsinfo->databasename
public table => .dsinfo->tablename
public columns => .info->columns
public database(name::tag) => .database(#name->asstring)
public database(name::string) => {
.dsinfo->databasename = #name->asstring
return self
}
public table(name::tag) => .table(#name->asstring)
public table(name::string) => {
.dsinfo->tablename = #name->asstring
return self
}
public silent(shouldprotect::boolean) => {
.silent = #shouldprotect
return self
}
public maxrows => .dsinfo->maxrows
public maxrows(max::integer) => {
.dsinfo->maxrows = #max
return self
}
public all => .maxrows(-1)
public affected => .'results'->size ? .'results'->last->affected | 0
public found => .'results'->size ? .'results'->last->found | 0
//---------------------------------------------------------------------------------------
//
// Execute
//
//---------------------------------------------------------------------------------------
public execute(
action::tag,
table::string,
keyvalues::staticarray,
values::staticarray,
firstrow::boolean=false
) => {
// New dsinfo
local(
d = .'dsinfo',
dsinfo = dsinfo,
)
// Ensure current DS is primed
.primed
#dsinfo->databasename = #d->databasename
#dsinfo->tablename = #d->tablename
#dsinfo->maxrows = #d->maxrows
#dsinfo->hostdatasource = #d->hostdatasource
#dsinfo->hostid = #d->hostid
#dsinfo->hostname = #d->hostname
#dsinfo->hostport = #d->hostport
#dsinfo->hostusername = #d->hostusername
#dsinfo->hostpassword = #d->hostpassword
#dsinfo->hosttableencoding = #d->hosttableencoding
#dsinfo->hostschema = #d->hostschema
#dsinfo->connection = #d->connection
#dsinfo->prepared = #d->prepared
#dsinfo->refobj = #d->refobj
/*
handle => {
#d->tablename = #org_table
#d->keycolumns = #org_keycolumns
#d->inputcolumns = (:)
}*/
// Set values
#dsinfo->tablename = #table
#dsinfo->keycolumns = (#keyvalues->size ? #keyvalues | .keyvalues)
#dsinfo->inputcolumns = .inputcolumns(#values)
// Determine action
match(#action) => {
case(::add) #dsinfo->action = lcapi_datasourceadd
case(::update) #dsinfo->action = lcapi_datasourceupdate
case(::search) #dsinfo->action = lcapi_datasourcesearch
case(::delete) #dsinfo->action = lcapi_datasourcedelete
case return self
}
handle => {
if(!#d->connection && #dsinfo->connection) => {
#d->connection = #dsinfo->connection
#d->prepared = #dsinfo->prepared
#d->refobj = #dsinfo->refobj
}
#d->statement = #dsinfo->statement
}
local(out) = .invoke(#dsinfo) => givenblock
return #firstrow ? .firstrow | #out
}