-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
grafito.art
executable file
·1415 lines (1181 loc) · 43.4 KB
/
grafito.art
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
#!/usr/bin/env arturo
;========================================================
; Grafito
;
; SQLite-based Graph Database
; in Arturo
;--------------------------------------------------------
;; name: grafito
;; version: 0.2.12
;; author: drkameleon
;; website: « https://github.com/arturo-lang/grafito
;; category: database
;; embed: [
;; "ui" "sql"
;; ".art" ".sql" ".html" ".less" ".js"
;; ]
;========================================================
;--------------------------
; The main library
;--------------------------
Grafito: #[
; version
Version: 0.2.12
; configuration
Debug?: false
verbose?: true
caseSensitive?: true
; colors
Palette: ""
]
;
; Define custom objects
;--------------------------
define :edgeFilter [direction, content, properties]
;----------------------------------------------
graph: function [
dbpath :string :null
body :block
][
;; description: « initiate a new graph environment with given name and body
;; options: [
;; create: « recreate database from scratch, even if it exists
;; case: « queries should be case-sensitive
;; verbose: « show messages regarding database operations
;; palette: :string :literal « select color palette (current: default)
;; ]
;; returns: :any
;; example: {
;; graph "mydb" [
;; ; do sth with this graph database
;; ]
;; ..........
;; ; in memory database
;; graph ø [
;; ; do sth with this graph database
;; ]
;; }
;==========================================
; CONSTANTS
;==========================================
; Global SQL scripts
schemaSQL: split.by:";" read.file ./"sql/schema.sql"
createNodeSQL: read.file ./"sql/procs/createNode.sql"
updateNodeSQL: read.file ./"sql/procs/updateNode.sql"
updateNodePropertySQL: read.file ./"sql/procs/updateNodeProperty.sql"
updateNodePropertiesSQL: read.file ./"sql/procs/updateNodeProperties.sql"
deleteNodeSQL: read.file ./"sql/procs/deleteNode.sql"
deleteNodeEdgesSQL: read.file ./"sql/procs/deleteNodeEdges.sql"
getNodeSQL: read.file ./"sql/procs/getNode.sql"
getRelatedNodesSQL: read.file ./"sql/procs/getRelatedNodes.sql"
getMaxNodeIdSQL: read.file ./"sql/procs/getMaxNodeId.sql"
createEdgeSQL: read.file ./"sql/procs/createEdge.sql"
updateEdgeSQL: read.file ./"sql/procs/updateEdge.sql"
deleteEdgeSQL: read.file ./"sql/procs/deleteEdge.sql"
deleteEdgeByIdSQL: read.file ./"sql/procs/deleteEdgeById.sql"
fetchNodesSQL: read.file ./"sql/procs/fetchNodes.sql"
fetchNodesWithEdgesSQL: read.file ./"sql/procs/fetchNodes.withEdges.sql"
countNodesSQL: read.file ./"sql/procs/countNodes.sql"
countEdgesSQL: read.file ./"sql/procs/countEdges.sql"
; Global SQL filters
hasEdgeFilter: read.file ./"sql/filters/hasEdge.sql"
hasPropertyFilter: read.file ./"sql/filters/hasProperty.sql"
edgeWithTargetFilter: read.file ./"sql/filters/edgeWithTarget.sql"
edgeWithSourceFilter: read.file ./"sql/filters/edgeWithSource.sql"
edgeWithAnyFilter: read.file ./"sql/filters/edgeWithAny.sql"
edgeWithPropertiesFilter: read.file ./"sql/filters/edgeWithProperties.sql"
nodePropertyWithValueFilter: read.file ./"sql/filters/nodePropertyWithValue.sql"
edgePropertyWithValueFilter: read.file ./"sql/filters/edgePropertyWithValue.sql"
; Global SQL pragmas
caseInsensitiveLikePragma: read.file ./"sql/pragmas/caseInsensitiveLike.sql"
caseSensitiveLikePragma: read.file ./"sql/pragmas/caseSensitiveLike.sql"
; Global UI template
app: read.file ./"ui/index.html"
; Color palettes
; for graph nodes
colorPalettes: #.raw flatten.once map list ./"ui/palettes" 'pal [
@[extract.filename pal, as.data pal]
]
;==========================================
; HELPERS
;==========================================
;
; Perform query
;--------------------------
performQuery: function [ctx, qu, params][
if and? [Grafito\Debug?][null? attr 'noDebug] [
context: to :string ctx
prepend: " | "
print color.bold #gray ">: " ++ context
queryString: qu
if block? qu [
queryString: join.with:"\n" qu
]
print color #gray prepend ++ join.with:"\n"++prepend split.lines queryString
inspect params
print ""
]
(not? null? params)? [
query db .with: params qu
][
query db qu
]
]
;
; Print message
;--------------------------
printDebug: function [msg][
print color #cyan ":: " ++ msg
]
;
; Format size
;--------------------------
formatSize: function [sz][
if sz < 1024 -> return sz
if sz < 1024*1024 -> return sz --> 'kB
if sz < 1024*1024*1024 -> return sz --> 'MB
if sz < 1024*1024*1024*1024 -> return sz --> 'GB
return @[]
]
;
; Get db analytics
;--------------------------
analytics: function [][
nodeCount: to :integer first first performQuery 'analytics countNodesSQL ø
edgeCount: to :integer first first performQuery 'analytics countEdgesSQL ø
avgDegree: edgeCount // nodeCount
filename: dbPath
filesize: 0
if not? empty? filename [
prefilesize: formatSize volume filename
filesize: #[
number: to :string scalar prefilesize
units: to :string units prefilesize
]
]
prememory: formatSize process\memory\occupied
mem: #[
number: to :string scalar prememory
units: to :string units prememory
]
return #[
database: #[
nodes: nodeCount
edges: edgeCount
degree: to :floating to :string .format: ".2f" avgDegree
]
memory: mem
disk: #[
path: filename
size: filesize
]
]
]
;
; Get suitable fg color
; depending on node bg
; for better contrast
;--------------------------
idealForeground: function [back][
rgbs: values extract back
lrgbs: map rgbs 'rgb [
C: rgb // 255
(C =< 0.03928)? -> C: C // 12.92
-> C: ((C+0.055)//1.055) ^ 2.4
C
]
L: (lrgbs\0*0.2126) + (lrgbs\1*0.7152) + (lrgbs\2*0.0722)
if L > 0.189 [ return #black ]
return #white
]
;
; Safe SQLite-compatible
; value codification
; including Unicode strings
;--------------------------
codifySafe: function [val][
(string? val)? [
return {"} ++ (escape.json val) ++ {"}
][
return as.code val
]
]
;
; Get styled node
; ready for use in the
; graph view
;--------------------------
styleNode: function [nodeId, nodeTag, nodeProps][
if not? key? Grafito\nodeColors nodeTag [
cpal: Grafito\Palette
cpal: colorPalettes\[cpal]
nodeColor: sample cpal
existingColors: map values Grafito\nodeColors => first
while [contains? existingColors nodeColor] ->
nodeColor: sample cpal
Grafito\nodeColors\[nodeTag]: @[
nodeColor
idealForeground nodeColor
]
updateMeta 'colors Grafito\nodeColors
]
bg: to :string Grafito\nodeColors\[nodeTag]\0
fg: to :string Grafito\nodeColors\[nodeTag]\1
nodeLabel: first values nodeProps
highlightField: #[
border: as.code darken to :color bg 0.2
background: bg
]
return #[
id: to :integer nodeId
tag: nodeTag
properties: nodeProps
label: truncate nodeLabel 10
fullLabel: nodeLabel
color: #[
border: bg
background: bg
hover: bg
highlight: highlightField
]
font: #[
color: fg
]
]
]
;
; Create new edge
;--------------------------
edge: function [
src :integer :dictionary
name :literal :string
tgt :integer :dictionary
][
;; description: « create edge from source to target node with given name
;; returns: :dictionary
srcId: src
tgtId: tgt
props: (attr "") ?? #[]
unless dictionary? props -> props: # props
if dictionary? srcId -> srcId: src\id
if dictionary? tgtId -> tgtId: tgt\id
edgeId: performQuery.id 'edge createEdgeSQL @[name, write.compact.json ø props, srcId, tgtId]
#[
id: edgeId
tag: name
properties: props
source: src
target: tgt
]
]
;
; Delete existing edge
;--------------------------
unedge: function [
src :integer
name :literal :string
tgt :integer
][
;; description: « delete edge from source to target node with given name
performQuery 'unedge deleteEdgeSQL @[name, src, tgt]
]
;
; Delete existing edge
; by id
;--------------------------
unedgeById: function [
eid :integer
][
;; description: « delete edge with given id
performQuery 'unedgeById deleteEdgeByIdSQL @[eid]
]
;
; Update single property
; by id
;--------------------------
editProperty: function [
nid :integer
prop :literal :string
val :any
][
;; description: « set or update single node property
performQuery 'editProperty updateNodePropertySQL @[~"$.|prop|", write.compact.json ø val, nid]
]
;
; Update meta value
;--------------------------
updateMeta: function [
prop :literal :string
val :any
][
;; description: « update a single meta value
editProperty 1 prop val
]
;
; Re-tag existing edge
; by id
;--------------------------
retagEdge: function [
eid :integer
tag :literal :string
][
;; description: « update edge with given tag
performQuery 'retag updateEdgeSQL @[tag, eid]
]
;
; Get node with properties
; from ID
;--------------------------
nodeFromId: function [nid][
results: performQuery 'nodeFromId getNodeSQL @[nid]
if empty? results ->
return ø
return #[
id: nid
tag: first first results
properties: read.json last first results
]
]
;
; Get nodeset
; for graph visualization
;--------------------------
nodeFromIds: function [nodes][
nodeset: nodes
if not? block? nodeset ->
nodeset: @[nodeset]
nodeElements: map nodeset 'nd -> styleNode nd\id nd\tag nd\properties
edgeElements: new []
loop nodeset [nd][
rezu: performQuery 'nodeFromIds getRelatedNodesSQL @[nd\id, nd\id, nd\id]
loop rezu [rez][
'nodeElements ++ styleNode rez\0 rez\1 read.json rez\2
'edgeElements ++ #[
id: ~"|rez\3|+|rez\4|+|rez\5|"
dbId: rez\6
label: rez\5
properties: read.json rez\7
from: to :integer rez\3
to: to :integer rez\4
]
]
]
unique 'nodeElements
unique 'edgeElements
return #[
nodes: nodeElements
edges: edgeElements
]
]
;
; Generate access helper
;--------------------------
generateHelper: function [id,block][
(not? null? attr 'new)? [
put id block
][
fetch id block
]
]
;==========================================
; REPL
;==========================================
;
; Switch verbosity on/off
;--------------------------
verbose: function [] [
Grafito\verbose?: not? Grafito\verbose?
]
;==========================================
; METHODS
;==========================================
;
; Create new node
;--------------------------
put: function [
name :literal :string
attributes :string :block :dictionary
][
;; description: « insert new node(s) to graph with given name and attributes
;; options: [
;; unique: « add only if node doesn't already exist
;; many: « add multiple nodes at once
;; ]
;; returns: :dictionary :block
if attr "unique" [
found: fetch name attributes
if not? empty? found -> return found
]
result: new []
(not? null? attr 'many)? [
lastId: to :integer first first performQuery 'put getMaxNodeIdSQL ø
queries: new []
vals: new []
loop attributes 'nd [
lastId: lastId + 1
att: nd
(block? att)? -> att: # att [
(string? att)? -> att: #[
name: att
] []
]
'queries ++ createNodeSQL
'vals ++ @[name, write.compact.json ø att]
'result ++ #[
id: lastId
tag: name
properties: att
]
]
performQuery 'put queries vals
if Grafito\verbose? ->
printDebug ~{created |size attributes| nodes}
] [
att: attributes
(block? att)? -> att: # att [
(string? att)? -> att: #[
name: att
] []
]
nodeId: performQuery.id 'put createNodeSQL @[name, write.compact.json ø att]
result: #[
id: nodeId
tag: name
properties: att
]
if Grafito\verbose? ->
printDebug ~{created 1 node (@|nodeId|) with |size att| propert|(1 = size att)? -> "y" -> "ies"|}
]
return result
]
;
; Update existing node
;--------------------------
edit: function [
nd :dictionary :block
attributes :block :dictionary
][
;; description: « update given node(s) with attributes
;; options: [
;; tag: :literal :string « also set new tag
;; replace: « replace entire object properties with given ones
;; ]
;; returns: :nothing
toUpdate: ø
(dictionary? nd)? ->
toUpdate: @[nd]
->
toUpdate: new nd
doReplace?: not? null? attr 'replace
withTag: attr 'tag
additional: attributes
if block? additional ->
additional: # additional
loop toUpdate 'updateable [
newAttributes: ø
(doReplace?)? ->
newAttributes: additional
->
newAttributes: extend updateable\properties additional
updateable\properties: newAttributes
(null? withTag)? ->
performQuery 'edit updateNodePropertiesSQL @[write.compact.json ø newAttributes, updateable\id]
->
performQuery 'edit updateNodeSQL @[withTag, write.compact.json ø newAttributes, updateable\id]
]
]
;
; Delete existing node
;--------------------------
unput: function [
nd :integer :dictionary :block
][
;; description: « remove given node(s) from graph
;; returns: :nothing
toDelete: ø
(block? nd)? ->
toDelete: new nd
->
toDelete: @[nd]
loop toDelete 'deletable [
nodeId: (dictionary? deletable) ? -> deletable\id -> deletable
performQuery 'unput @[
deleteNodeSQL, deleteNodeEdgesSQL
] @[
nodeId, nodeId, nodeId
]
]
]
;
; Link nodes with edges
;--------------------------
link: function [
src :block :dictionary
name :literal :string
tgt :block :dictionary
]
[
;; description: « create a connection from source to target node with given name
;; returns: :block :dictionary
edgeId: 0
(:dictionary = type tgt)? [
edges: new []
if :dictionary = type src [
ret: edge src name tgt
return ret
]
sr: @ src
loop sr [s]->
'edges ++ edge s name tgt
if Grafito\verbose? -> printDebug ~"created |size edges| edges"
return edges
][
edges: new []
tg: @ tgt
(:dictionary = type src)? [
loop tg [t][
'edges ++ edge src name t
]
][
sr: @ src
loop sr [s] [
loop tg [t]->
'edges ++ edge s name t
]
]
if Grafito\verbose? -> printDebug ~"created |size edges| edges"
return edges
]
]
reverseLink: function [
tgt :block :dictionary
name :literal :string
src :block :dictionary
][
link src name tgt
]
reciprocalLink: function [
src :block :dictionary
name :literal :string
tgt :block :dictionary
][
link src name tgt
link tgt name src
]
;
; Delete node edges
;--------------------------
unlink: function [
src :integer :dictionary :block
name :literal :string
tgt :integer :dictionary :block
][
;; description: « remove connection from source to target node with given name
nodesFrom: new []
nodesTo: new []
;case [(type src)=]
(dictionary? src)? -> nodesFrom: @[src\id]
[
(integer? src)? -> nodesFrom: @[src]
[
nodesFrom: new map src 'n [
(dictionary? n)? -> n\id
-> n
]
]
]
; when? -> :dictionary -> nodesFrom: @[src\id]
; when? -> :integer -> nodesFrom: @[src]
; else -> nodesFrom: new map src 'n [
; if? dictionary? n -> n\id
; else -> n
; ]
(dictionary? tgt)? -> nodesFrom: @[tgt\id]
[
(integer? tgt)? -> nodesFrom: @[tgt]
[
nodesFrom: new map tgt 'n [
(dictionary? n)? -> n\id
-> n
]
]
]
; if?case [(type tgt)=]
; when? -> :dictionary -> nodesTo: @[tgt\id]
; when? -> :integer -> nodesTo: @[tgt]
; else -> nodesTo: new map tgt 'n [
; if? dictionary? n -> n\id
; else -> n
; ]
loop nodesFrom 'nf [
loop nodesTo 'nt [
unedge nf name nt
]
]
if Grafito\verbose? [
totalEdges: mul size nodesFrom size nodesTo
edgeLabel: (totalEdges = 1) ? -> "edge" -> "edges"
printDebug ~"removed |totalEdges| |edgeLabel|"
]
]
;
; Check node type
;--------------------------
isA?: function [ls,nd][
nd\tag = to :string ls
]
;
; Helpers
; for edge filters
;--------------------------
edgeFilterRight: function [cnt, props][
pp: props
if block? pp -> pp: # pp
to :edgeFilter @[1, cnt, pp]
]
edgeFilterLeft: function [cnt, props][
pp: props
if block? pp -> pp: # pp
to :edgeFilter @[2, cnt, pp]
]
edgeFilterAny: function [cnt, props][
pp: props
if block? pp -> pp: # pp
to :edgeFilter @[3, cnt, pp]
]
;
; Fetch all results for tag
; with given properties
; and edges
;--------------------------
fetch: function [
name :literal :string
attributes :null :string :block :dictionary
][
;; description: « retrieves nodes with name that match all given attributes
;; options: [
;; .any « try matching any of the attributes
;; ]
;; returns: :block
;; setup main variables
catchAny?: false
if not? null? attr "any" ->
catchAny?: true
collator: ""
if not? Grafito\caseSensitive? ->
collator: " COLLATE NOCASE"
propertyFilters: new []
edgeFilters: new []
qvals: new @[name]
qpropvals: new []
qedgevals: new []
att: new attributes
(string? att)? [
[collate,symb,val]: @[collator, "=", codifySafe att]
; add it once (to check for `name`)
'propertyFilters ++ ~nodePropertyWithValueFilter
'qpropvals ++ @[~"$.name"]
; add it once more (to check for `title`)
'propertyFilters ++ ~nodePropertyWithValueFilter
'qpropvals ++ @[~"$.title"]
; we won't it to be valid when any of the two criteria above is true
; so we'll have to forcefully set `catchAny?` to true
catchAny?: true
]
[
if and? not? dictionary? att
not? null? att ->
att: # att
; ; HACK - to solve
; remove.key 'att 'n
; remove.key 'att 'a
if not? empty? att [
loop att [k,v][
if null? v [
; it's an edge filter without criteria
'edgeFilters ++ hasEdgeFilter
'qedgevals ++ @[k]
continue
]
if dictionary? v [
; it's an edge filter
searchForProperties: ""
'edgeFilters ++ ~edgeWithTargetFilter
'qedgevals ++ @[k, v\id]
continue
]
if is? :edgeFilter v [
; it's a directed edge filter
if dictionary? v\content [
searchForProperties: ""
subfilters: []
subvals: []
if not? empty? v\properties [
collator: ""
if not? Grafito\caseSensitive? ->
collator: " COLLATE NOCASE"
subfilters: new []
loop v\properties [kk,vv][
[collate,symb,val]: @[collator, "=", codifySafe vv]
subvals: subvals ++ ~"$.|kk|"
subfilters: subfilters ++ ~edgePropertyWithValueFilter
]
subfilters: join.with:" AND " subfilters
searchForProperties: ~"AND (|subfilters|)"
]
(v\direction = 1)? [
'edgeFilters ++ ~edgeWithTargetFilter
'qedgevals ++ @[k, v\content\id] ++ subvals
][
(v\direction = 2)? [
'edgeFilters ++ ~edgeWithSourceFilter
'qedgevals ++ @[k, v\content\id] ++ subvals
][
(v\direction = 3)? [
'edgeFilters ++ ~edgeWithAnyFilter
'qedgevals ++ @[k, v\content\id, v\content\id] ++ subvals
][
print "Shouldn't have reached here!"
]
]
]
]
if all? @[
block? v\content
or?
and? [0 < size v\content]
[:dictionary = type first v\content]
not? empty? v\properties
][
searchForProperties: ""
subfilters: []
subvals: []
if not? empty? v\properties [
collator: ""
if not? Grafito\caseSensitive? ->
collator: " COLLATE NOCASE"
subfilters: new []
loop v\properties [kk,vv][
[collate,symb,val]: @[collator, "=", codifySafe vv]
subvals: subvals ++ ~"$.|kk|"
subfilters: subfilters ++ ~edgePropertyWithValueFilter
]
subfilters: join.with:" AND " subfilters
searchForProperties: ~"AND (|subfilters|)"
]
orCriteria: new []
(0 < size v\content)? [
loop v\content [edgef][
(v\direction = 1)? [
'orCriteria ++ ~edgeWithTargetFilter
'qedgevals ++ @[k, edgef\id] ++ subvals
][
(v\direction = 2)? [
'orCriteria ++ ~edgeWithSourceFilter
'qedgevals ++ @[k, edgef\id] ++ subvals
][
(v\direction = 3)? [
'orCriteria ++ ~edgeWithAnyFilter
'qedgevals ++ @[k, edgef\id, edgef\id] ++ subvals
][
print "Shouldn't have reached here!"
]
]
]
]
][
'orCriteria ++ ~edgeWithPropertiesFilter
'qedgevals ++ @[k] ++ subvals
]
'edgeFilters ++ "(" ++ (join.with:" OR " orCriteria) ++ ")"
]
continue
]
if block? v [
(and? 0 < size v
:dictionary = type first v)? [
; it's an array of edge filters
orCriteria: new []
loop v [edgef][
searchForProperties: ""
'orCriteria ++ ~edgeWithTargetFilter
'qedgevals ++ @[k, edgef\id]
]
'edgeFilters ++ "(" ++ (join.with:" OR " orCriteria) ++ ")"
]
[
; it's a complex property filter
loop # v [filt,rg][
[collate,symb,val]: @[collator, "=", v]
requiresCodification: true
(filt = "contains")? -> [collate,symb,val]: @["", "LIKE", ~"%|rg|%"]
[
(filt = "prefix")? -> [collate,symb,val]: @["", "LIKE", ~"|rg|%"]
[
(filt = "suffix")? -> [collate,symb,val]: @["", "LIKE", ~"%|rg|"]
[
(filt = "under")? -> [collate,symb,val]: @["", "<", rg]
[
(filt = "over")? -> [collate,symb,val]: @["", ">", rg]
[
(filt = "underOrEqual")? -> [collate,symb,val]: @["", "<=", rg]
[
(filt = "overOrEqual")? -> [collate,symb,val]: @["", ">=", rg]
[
(filt = "in")? [
[collate,symb,val]: @[collator, "IN", "(" ++ (join.with:", " map rg [x][codifySafe x]) ++ ")"]
requiresCodification: false
][
(filt = "not")? [
(block? rg)? [
[collate,symb,val]: @[collator, "NOT IN", "(" ++ (join.with:", " map rg [x][codifySafe x]) ++ ")"]
requiresCodification: false
]
-> [collate,symb,val]: @[collator, "!=", rg]
]
[
panic.code: 1 ~"filter: |filt| not recognized"
]
]
]
]
]
]