-
Notifications
You must be signed in to change notification settings - Fork 1
/
crud-csv.red
203 lines (155 loc) · 4.56 KB
/
crud-csv.red
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
Red [
Title: "crud-csv.red"
Alias: [
crud-csv
]
Build: 1.0.1.1
History-Latest: [
1.0.0.1 {First version}
1.0.0.2 {Optionally return a header for read-csv}
1.0.0.3 {Optionally return flat records with header for read-csv}
1.0.0.5 {Start fixing save-csv when header arg is a block instead of a delimited comma string}
1.0.0.8 {End fixing save-csv when header arg is a block instead of a delimited comma string}
1.0.0.9 {add-csv : +case not block? +records-or-file}
1.0.1.1 {fix block-to-csv-line does not allow string! for its .block argument}
]
]
.read-csv: function[data-file /header /flat /delimiter +delimiter][
either delimiter [
-delimiter: +delimiter
][
-delimiter: ","
]
if not exists? data-file [
print rejoin [data-file " doesn't exist."]
return false
]
lines: skip (all-lines: Read/lines data-file) 1 ; skip first csv header line
records: copy []
forall lines [
append/only records split lines/1 -delimiter
]
either not header [
return records
]
;--------------------- for header ------------------------------
[
header: split (first all-lines) -delimiter
unless flat [
return append [] compose/only [ (header) (records)]
]
return append [] compose [ (header) (records)]
]
]
read-csv: :.read-csv
.save-csv: function[>data-file +records /header +header][
if block? >data-file [
file: +records
+records: >data-file
>data-file: file
]
-whole-records: copy []
if header [
either block? +header [
-header: block-to-csv-line +header
][
-header: +header
]
append -whole-records -header
]
forall +records [
append -whole-records block-to-csv-line +records/1
]
write/lines >data-file -whole-records
]
save-csv: :.save-csv
.add-csv-record: function[+records [block!] +record [block! string!]][
either not block? +record [
-record: split +record ","
][
-record: +record
]
append/only +records +record
return +records
]
add-csv-record: :.add-csv-record
.add-csv: function[+records-or-file [block! file!] record [block! string!]][
either not block? +records-or-file [
;file case
-data-file: +records-or-file
-records: read-csv -data-file
][
-records: +records-or-file
]
if not block? record [
record: split record ","
]
append/only -records record
if not block? +records-or-file [
;file case
save-csv -data-file records
]
return records
]
add-csv: :.add-csv
add-csv-file: :.add-csv
search-csv: function[records searched-value][
records-numbers: copy []
forall records [
record: records/1
i: index? records
forall record [
field-value: record/1
if not none? find field-value searched-value [
append records-numbers i
break
]
]
]
return records-numbers
]
update-csv: function[records record-number record][
if (record-number > 1) [
repeat i (record-number - 1) [
records: next records
]
]
change/only records record
records: head records
return records
]
delete-csv: function[records record-number-or-search-string][
record-number: record-number-or-search-string
if string? record-number-or-search-string [
records-numbers: search-csv records record-number-or-search-string
record-number: records-numbers/1
]
if (record-number > 1) [
repeat i (record-number - 1) [
records: next records
]
]
remove records
records: head records
return records
]
;============================= utilities functions ====================================
.block-to-comma-delimited-string: function[.block [block!] /delimiter .delimiter][
delimited-string: copy ""
n: length? .block
unless delimiter [
.delimiter: ","
]
forall .block [
element: .block/1
i: index? .block
either i < n [
delimited-string: rejoin [delimited-string element .delimiter]
][
delimited-string: rejoin [delimited-string element]
]
]
]
block-to-comma-delimited-string: :.block-to-comma-delimited-string
.block-to-csv-line: :.block-to-comma-delimited-string
block-to-csv-line: :.block-to-comma-delimited-string