-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulk.r
2840 lines (2420 loc) · 70.3 KB
/
bulk.r
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
REBOL [
; -- Core Header attributes --
title: {BULK | record based, table engine using a flat block.}
file: %bulk.r
version: 1.0.1
date: lib-bulk
author: "Maxim Olivier-Adlhoch"
purpose: "A data-exchange format for use in any REBOL script"
web: http://www.revault.org/modules/bulk.rmrk
source-encoding: "Windows-1252"
note: {slim Library Manager is Required to use this module.}
; -- slim - Library Manager --
slim-name: 'bulk
slim-version: 1.2.1
slim-prefix: none
slim-update: http://www.revault.org/downloads/modules/bulk.r
; -- Licensing details --
copyright: "Copyright © 2013 Maxim Olivier-Adlhoch"
license-type: "Apache License v2.0"
license: {Copyright © 2013 Maxim Olivier-Adlhoch
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.}
;- / history
history: {
all history lost :-( bulk is used in a few projects.
v1.0.0 - 2013-09-12
-changed bulk model so it's using proper, strong modularisation properties of slim.
-license changed to Apache v2}
;- \ history
;- / documentation
documentation: {
-BULK will is used extensively by GLASS and its various APIs.
-There is automated unit launch included at the end, remove if you want.
-Change the unit testing code and data in the header to try out stuff.
-Bulk was not upgraded to using the slut engine yet...
---------
BULK
---------
What is bulk?
---
a flat table data structure, which allows hierarchical data via the use of bulk data within the
table.
why?
---
to promote and sponsor an open data exchange format which takes up the least amount of space for
large data sets, yet provides flexibility for nested data sets.
the specification includes guidelines about every aspect of the the datastructure. If you find
an undefined issue, reach me on altme and I'll be sure to discuss it and add it to this
specification.
overall specification
---
A bulk is a block which contains a single header and 0 or more fixed length rows of items.
an item is a single rebol value as undertood by LOAD/NEXT, which must be serializable using mold/all.
at its most basic, a BULK is defined like so:
bulk: [
[columns: #[integer!]]
...
]
where:
... is an undefined number of rows of data which have 'columns number of items each.
properties in the header are specified using 'SET-WORD notation followed by ZERO OR ONE item
of data. when a set-word follows, its considered undefined.
ex: [name: sex: "m"] here, name is considered undefined, so will return NONE!, not "m"
(note this might change to reflect object spec notation where name: might return "m")
the header MUST inlude the 'COLUMNS: property. It defines how many items per row.
header may include any number of other properties and expects to be usable as a 'CONSTRUCT
or map! spec directly.
any WORD! or LIT-WORD! value in the header is expected to be used litteraly, not as a bound,
evaluatable word. Because of this, PRINTing the header might provoke errors, you should
always PROBE it. In Red, the header will be explicitely unbound.
You can obviously have BULK items in a BULK, hence its nested aspect.
so what's the big deal?
---
-Anyone can understand and quickly build bulk supporting tools.
-The fact that its a flat table means its extremely memory efficient (REBOL wise) and VERY easy
to manipulate, join, extract, etc.
-ONLY the columns property will ever be required (so far) for the low-level tools to be happy
because bulk is a simple format to exchange data between arbitrary tools, some include databases
and huge but simple datasets.
-Generic tools for conversion, disk & networking can safely convert any bulk with minimal fuss,
including the header.
-Programmatically, the header is easily avoided by simply skipping it, and can then be used as a
negative skip, if you want to refer to it later. When skipped, the BULK looks like an ordinary
block. Be carefull though, cause some functions like INDEX? calculate from the head, so will
consider the header in their results.
-MANY liquid plugs will play around with BULK data. It is very
useful, cause it reduces the number of dependencies to make when data is expected to live alongside
each other. there will be generic bulk creation and extraction tools, reducing the number of plug
types to build in some situations.
Bulk properties
----
you may add as many properties as you wish in the bulk header as long as you follow (very)
simple guidelines.
namely:
-properties should be type constrained, so their handling remains simple and usefull.
-when words are used to define a property, they are intended to be literal, not evaluatable
words. You should use LIT-WORD!s, when possible.
-an unspecified property (property is not in header) is not an error, it returns none,
and your code should adapt with a default value if possible.
some "standard" properties are defined but will never be required for the most basic bulk support
this being said, if you don't specify them, bulk will not be able to react as automatically as it could.
the specification will only require that their use conforms to their intended goals.
example of possible (eventual) new standard headers:
type: [word!]
identifies this bulk as belonging to a specific class of datasets.
there will be official types.
date: [date!]
when was this bulk created
source: [file! url! string! word!]
where does the source data come from.
doc: [string! url!]
documentation about this bulk data be included or refered to via an url
label-column: [word! integer]
will indicate what column should be used as a "label" for each row.
standard properties:
----
labels: [word! string! integer!]
field names given to your columns.
-should be equal in number to number of columns
-trailing undefined columns are simply unlabeled.
label-column: [word! integer]
will indicate what column should be used as a "label" for each row.
sort-column: [word! integer]
will indicate what column should be used for sorting by default.
NOTES:
-the header IS NOT an object on purpose.
-in Red it will become a map!
}
;- \ documentation
bulk-test-data: {
good-bulk: [
[columns: 3 type: labels: [first second third] date:]
1 2 3
4 5 6
7 8 9
]
good-header-bulk: [
[columns: 3]
1 2 3
4 5 6
7 8 9
]
bad-bulk: [
[columns: one]
33 33
]
}
bulk-unit-tests: {
print ""
print "------------"
print "Performing unit tests:"
print "------------"
print "is-bulk? good-bulk"
prin ">> "
probe is-bulk? good-bulk
print ""
print "is-bulk?/header good-header-bulk"
prin ">> "
probe is-bulk?/header good-header-bulk
print ""
print "is-bulk? bad-bulk"
prin ">> "
probe is-bulk? bad-bulk
print ""
print "get-bulk-property good-bulk 'labels"
prin ">> "
probe get-bulk-property good-bulk 'labels
print "get-bulk-property good-bulk 'date"
prin ">> "
probe get-bulk-property good-bulk 'date
print "get-bulk-property good-bulk 'type"
prin ">> "
probe get-bulk-property good-bulk 'type
print ""
print "set-bulk-property good-bulk 'type 'number-grid"
prin ">> "
probe set-bulk-property good-bulk 'type 'number-grid
print ""
print "set-bulk-property good-bulk 'date now"
prin ">> "
probe set-bulk-property good-bulk 'date now
print "get-bulk-property good-bulk 'type"
prin ">> "
probe get-bulk-property good-bulk 'type
print ""
print "insert-bulk-records good-bulk [11 22 33] 2 "
prin ">> "
probe insert-bulk-records good-bulk [11 22 33] 2
print ""
print "insert-bulk-records good-bulk [111 222 333] none "
prin ">> "
probe insert-bulk-records good-bulk [111 222 333] none
print ""
print "get-bulk-row good-bulk 2"
prin ">> "
probe get-bulk-row good-bulk 2
print ""
print "probe good-bulk"
probe good-bulk
print ""
print "------------"
ask "Press Enter to quit"
}
]
;--------------------------------------
; unit testing setup
;--------------------------------------
;
; test-enter-slim 'bulk
;
;--------------------------------------
slim/register [
;- .
;-----------------------------------------------------------------------------------------------------------
;
;- LIBS
;
;-----------------------------------------------------------------------------------------------------------
xmlb: slim/open/expose 'xmlb none [load-xml mold-xml xml-attr-grid]
slim/open/expose 'utils-encoding none [utf8-win1252 strip-bom]
;- .
;-----------------------------------------------------------------------------------------------------------
;
;- GLOBALS
;
;-----------------------------------------------------------------------------------------------------------
;--------------------------
;- default-null:
;
;--------------------------
default-null: "#[NULL]"
;- .
;-----------------------------------------------------------------------------------------------------------
;
;- BINDINGS
;
;-----------------------------------------------------------------------------------------------------------
; declare words to bind locally to context
;--------------------------
;- CSV-CTX: [...]
;
; Used to store the transient values of the csv file parser
;--------------------------
csv-ctx: context [
;--------------------------
;- .table:
;
;--------------------------
.table: []
;--------------------------
;- .row:
;
;--------------------------
.row: []
;--------------------------
;- .columns-ctx:
;
; used whenever we are given a set of columns to use in select & where clauses.
;--------------------------
.columns-ctx: none
;--------------------------
;- tag-row-end:
;
; Will add ___ROW-END___ at the end of each rows in the table result
;--------------------------
tag-row-end: none
;--------------------------
;- select/where filtering:
;
;--------------------------
.column-names: .select-clause: .where-clause: none
;--------------------------
;- line-expressions:
;
;--------------------------
.do-each: .do-every: none
;--------------------------
;- .output-columns:
;
;--------------------------
.output-columns: .extra-columns: .orig-row-columns: none
;--------------------------
;- .value:
;
;--------------------------
.value: ""
;--------------------------
;- .here:
;
;--------------------------
.here: none
.txt: none
;--------------------------
;- .column-count:
;
;--------------------------
.column-count: none
.old-column-count: none
;--------------------------
;- .line-count:
;
;--------------------------
.line-count: 0
;--------------------------
;- flow rules:
;
;--------------------------
=ok=: none
=fail=: [end skip]
;--------------------------
;- whitespaces:
;
;--------------------------
=space=: charset [ #" " #"^(A0)" #"^(8D)" #"^(8F)" #"^(90)" ]
=tab=: charset "^-"
=spacer=: union =space= =tab=
=spacers=: [some =spacer=]
=nl=: [ opt =cr= =lf= (++ .line-count)]
=cr=: #"^M"
=lf=: #"^/"
;--------------------------
;- collectors:
;
;--------------------------
!collect!: [(append .value .txt)]
!collect-value!: [(either .value = default-null [append .row none] [append .row copy .value clear .value])]
;--------------------------
;- =separator=:
;
;--------------------------
=separator=: charset ","
;--------------------------
;- =quoted-chars=:
;
;--------------------------
=quoted-chars=: complement charset {"^/^M} ; note we consider the ^/ directly in the rule, to increment line number
;--------------------------
;- =unquoted-chars=:
;
;--------------------------
=unquoted-chars=: complement union =separator= charset {^/^M}
;--------------------------
;- =qvalue=:
;
;--------------------------
=qvalue=: [
{"}
;(print {"})
any [
[{""} (append .value {"})]
| [
copy .txt some [
=quoted-chars=
| =nl= ;(++ .line-count)
]
!collect!
]
]
{"}
]
;--------------------------
;- =uqvalue=:
;
;--------------------------
=uqvalue=: [
copy .txt some =unquoted-chars= !collect!
]
;--------------------------
;- =value=:
;
;--------------------------
=value=: [
[
=qvalue= ;(print "Q - ")
| =uqvalue= ;(print "UQ - ")
| [
empty-ptr: [
=separator=; (print "!!")
| =lf= ;(print "<<")
| end
]
(.txt: copy "")
:empty-ptr
]
]
]
;--------------------------
;- =row=:
;
;--------------------------
=row=: [
=value= !collect-value!
any [
=separator= ;(print "separator")
=value= !collect-value!
]
;(print "=row=")
]
;--------------------------
;- =csv=:
;
;--------------------------
=csv=: [
(=not-eof=: =ok=)
some [
.here:
[
; note that we do not accumulate blank lines in the result dataset.
any =spacers= =nl= (
)
| [
=not-eof= =row=
;(print "============")
[
=nl= end (=not-eof=: =fail= )
| (-- .line-count) =nl=
| end (=not-eof=: =fail=)
]
;(print "``````````````````")
(
if tag-row-end [
append .row '___ROW-END___
]
.old-column-count: .column-count
.column-count: length? .row
all [
.old-column-count
.column-count <> .old-column-count
to-error rejoin ["column count mismatch! (line ".line-count ") was : " .old-column-count " found : " .column-count ]
]
.qualified?: true
;----------------
; rebuild expression ctx to run any of the clauses.
; this is reset at each init.
;
; we want to run this only once every csv file.
;----------------
if all [
.column-names
not .columns-ctx
][
;vprint "====================================="
;vprint " COMPILING RUN-TIME QUERY"
;vprint "====================================="
;----------------
; we need to build an object version of the .column-names
; this is to bind the where clause to it.
;
; note that we add the full row block which you CAN
; manipulate BEFORE running the WHERE clause
;----------------
.columns-ctx: copy [.row: .line-count: .table: ]
.output-columns: copy [] ; columns after select-clause if any.
.orig-row-columns: copy []
foreach word .column-names [
append .orig-row-columns to-word word ; makes sure all column names are words (not string!)
append .columns-ctx to-set-word word
append .output-columns to-word word
]
foreach word [.do-each .do-every .where-clause][
;--------
; add any set words from the expressions within the ctx
; this way it isolates the expression from the calling code
; and we can add completely new column values in the result!
;--------
append .columns-ctx extract-set-words/only any [get word []]
]
append .columns-ctx none
.columns-ctx: context .columns-ctx
bind .orig-row-columns .columns-ctx
if .select-clause [
if find .select-clause '* [
; we must fill-in the columns with all columns not yet
; listed in the select clause
.extra-columns: exclude .output-columns .select-clause
;v?? .extra-columns
replace .select-clause '* .extra-columns
]
;v?? .select-clause
.output-columns: .select-clause
]
foreach word [.do-each .do-every .select-clause .where-clause .output-columns][
words: get word
;v?? words
if words [
bind get word .columns-ctx
]
]
; if .where-clause [
; bind .where-clause .columns-ctx
; ]
; if .do-each [
; bind .do-each .columns-ctx
; ]
; if .do-every[
; bind .do-every .columns-ctx
; ]
; if .select-clause [
; bind .select-clause .columns-ctx
; ]
;v?? .output-columns
;v?? .select-clause
;vprint "====================================="
;vprint " DONE COMPILING RUN-TIME QUERY"
;vprint "====================================="
]
; note that extra columns will be set to none,
; before all processing is done, which is very useful.
;v?? .orig-row-columns
;v?? .row
if .orig-row-columns [
set .orig-row-columns .row
]
if .columns-ctx [
.columns-ctx/.row: .row
.columns-ctx/.table: .table
.columns-ctx/.line-count: .line-count
]
if .do-every [
; be careful, you can destroy the .row for all other phases,
; including data accumulation
do .do-every
]
;---------------------------
; APPLY WHERE CLAUSE FILTER
;---------------------------
if .where-clause [
;v?? .where-clause
.qualified?: do .where-clause
;if .qualified? [
; ask "found one"
;]
]
if .qualified? [
;vprint "-----------------------"
;vprint "ADDING DATA"
;vprint "-----------------------"
;v?? .output-columns
;v?? .do-each
; setting here allows us to load it back on result to generate the labels properly in the bulk.
unless .output-columns [
.output-columns: copy .row
]
result-row: reduce .output-columns
;v?? result-row
if .columns-ctx [
.columns-ctx/.row: result-row
]
if .do-each [
do .do-each
]
append .table result-row
]
clear .row
)
]
;---
]
]
]
;--------------------------
;- init()
;--------------------------
; purpose: initialise or reset the parse-ctx
;
; inputs:
;
; returns:
;
; notes:
;
; to do:
;
; tests:
;--------------------------
init: func [][
vin "bulk.r/csv-ctx/init()"
.table: copy []
.row: copy []
.value: copy ""
.here: none
.txt: none
.column-count: none
.old-column-count: none
.line-count: 0
.select-clause: .column-names: .where-clause: none
.do-each: .do-every: none
.columns-ctx: none
.output-columns: none
vout
]
]
;- .
;-----------------------------------------------------------------------------------------------------------
;
;- UTILITY CODE
;
;-----------------------------------------------------------------------------------------------------------
;-----------------
;- find-same()
;-----------------
find-same: func [
series [block!] "note this is not a bulk input but an arbitrary series type"
item [series! none! ]
/local s
][
unless none? item [
while [s: find series item] [
if same? first s item [return s]
series: next s
]
]
none
]
;- .
;-----------------------------------------------------------------------------------------------------------
;
;- INSPECTION AND CORE CREATION
;
;-----------------------------------------------------------------------------------------------------------
;-----------------
;- make-bulk()
;
; create a new bulk
;-----------------
make-bulk: funcl [
columns [integer! block!] "when a block! is given, it's the names of the columns, columns count header is then set automatically.^/Names can be given in string! or word! type."
/records data [block!]
/properties props [block! none!]
][
vin "make-bulk()"
either block? columns [
bulk: compose/deep [[columns: (length? columns)]]
column-labels/set bulk columns
][
; integer! given
bulk: compose/deep [[columns: (columns)]]
]
if records [
insert-bulk-records bulk data none
]
if props [
set-bulk-properties bulk props
]
vout
bulk
]
;-----------------
;- is-bulk?
;
; returns true if data complies to all required bulk prerequisites (including type)
;-----------------
is-bulk?: func [
blk
/header "Only verify header, content might not match columns number"
/local cols
][
all [
block? blk
integer? cols: get-bulk-property blk 'columns
any [
header
0 = mod ((length? blk) - 1) cols
]
]
]
;-----------------
;- symmetric-bulks?()
;
; returns true if both bulks are of same shape.
;
; currently this only makes sure both bulks have the same number of columns.
; eventually, if the bulks have column labels, they should be in the same order.
;-----------------
symmetric-bulks?: func [
blk [block!]
blk2 [block!]
/strict "this will trigger a stricter verification (undefined for now)"
][
(bulk-columns blk) = (bulk-columns blk2)
]
;- .
;-----------------------------------------------------------------------------------------------------------
;
;- FILE i/o FUNCTIONS
;
;-----------------------------------------------------------------------------------------------------------
;--------------------------
;- read-data()
;--------------------------
; purpose: Get content string
;
; inputs:
;
; returns:
;
; notes: Used by all <FORMAT>-to-bulk() functions
;
; to do:
;
; tests:
;--------------------------
read-data: funcl [
data [string! file! binary!] "Path of the datafile, or its binary|string content"
/utf8 "Set if the file is in UTF-8 and needs to be converted to ANSI"
][
vin "read-data()"
data: switch type?/word data [
file! [
;---
; If given a file, read the file to get its content to parse
;
; /binary is important because many text formats are actually binary in specification
; ex: XML and CSV are NOT text formats, but binary. there are subtle
; details like newlines and encodings which depend of very specific
; characters which cannot be mangled.
;---
as-string read/binary data
]
binary! [
; If given a binary, convert it to a string for parsing
as-string data
]
string! [
; If given a string, take it as is
data
]
]
if utf8 [ data: utf8-win1252/transcode strip-bom data ]
;probe stats
vout
data
]
;--------------------------
;- parse-csv()
;--------------------------
; purpose: Parses a csv string and returns its data in a block
;
; inputs: a binary loaded string of text
;
; returns: just the table data, not yet in bulk format.
;
; notes: - beware CRLF vs LF they are not treated the same by CSV parser.
; - any none! parameter is simply ignored.
; - /columns is required when using any of the other refinements.
;
; to do:
;
; tests:
;--------------------------
parse-csv: funcl [
data [string!] "The data to parse"
/columns
column-names [block! none!] "set the column names for expression part of select/where/every/each expressions."
/select
s-clause [block! none!] "must reduce to a list of words which columns in column-names."
/where
w-clause [block! none!] "Expression to run on each line to qualify in output"
/every
do-every [block! none!] "do this for each qualified line"
/each
do-each [block! none!] "do this for each qualified line"
][
vin "parse-csv()"
;---
; We want to reinitialize context between each call
;v?? data
;ask "..."
csv-ctx/init
;v?? w-columns
;v?? w-clause
csv-ctx/.column-names: column-names
csv-ctx/.select-clause: s-clause
csv-ctx/.where-clause: w-clause
csv-ctx/.do-every: do-every
csv-ctx/.do-each: do-each
unless parse/all data csv-ctx/=csv= [
to-error rejoin ["bulk/parse-csv : CSV format error here: " copy/part csv-ctx/.here 50 ]
]
;v?? csv-ctx/.table
vout
first reduce [ csv-ctx/.table csv-ctx/.table: none ]
]
;--------------------------
;- csv-to-bulk()
;--------------------------
; purpose: Converts a csv string or file to a bulk
;
; inputs:
;
; returns:
;
; notes: - we use the csv-ctx values after the call to 'PARSE-CSV
;
; to do: /auto-fill (use the ___ROW-END___ feature of csv parser to detect non symmetric rows)
;
; tests:
;--------------------------
csv-to-bulk: funcl [
csv-data [string! file! binary!] "Path of the csv file, or its binary|string content"
/no-header "Will store the first CSV row as a normal bulk row instead of in the labels property"
/quiet "do not show warnings"
/utf8 "Set if the file is in UTF-8 and needs to be converted to ANSI"
/null
null-value [string!] "The value to convert to none in the bulk, default is #[NULL]"
/select
select-clause [block!] "chose which columns to return. If not used, returns all columns"
/where
where-clause [block!]"provide a where clause to filter lines AS WE LOAD them. this may greatly reduce the memory consumption. uses the same mechanism as select-bulk"
/types
type-clause [block!] "Column name and type to convert to"
/every
do-every [block!] "do this block for every line in source file, filtered or not"
/each
do-each [block!]"do this block for each row in final bulk (after where clause)"
;/auto-fill "will fill rows missing data (at end)"
][
vin "csv-to-bulk()"
csv-data: either utf8 [read-data/utf8 csv-data][read-data csv-data]
; - Data integrity verification
; If the data has more than one row, it should contain at least one crlf
; The absence of clrf might indicate a wrong file reading => WARNING
unless quiet [
unless find csv-data crlf [
vprint "==============================================================="
vprint "WARNING! (csv-to-bulk): No crlf found in data"
vprint {Please use [to-string read/binary <file-path>] to read the file}