Skip to content

Commit

Permalink
allow header lines in CSV import
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkoSchuetz committed Mar 13, 2017
1 parent 2dc0747 commit 628d97a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
18 changes: 11 additions & 7 deletions csv.ur
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,30 @@ fun csvFold [fs] [acc] (f : $fs -> acc -> acc)
f acc' acc
end

fun loop input acc =
fun loop (header : int) input acc =
case String.split input #"\n" of
None =>
(case input of
"" => acc
| _ => doLine input acc)
| _ => if header = 0
then acc
else doLine input acc)
| Some (line, input) =>
loop input (doLine line acc)
if header = 0
then loop 0 input (doLine line acc)
else loop (header-1) input acc
in
loop
end


fun parse [fs] (injs : $(map sql_injectable fs)) (reads : $(map read fs)) (fl : folder fs)
(input : string) =
@csvFold (fn r acc => r :: acc) injs reads fl input []
(header : int) (input : string) =
@csvFold (fn r acc => r :: acc) injs reads fl header input []

fun importTable [fs] [cs] (injs : $(map sql_injectable fs)) (reads : $(map read fs)) (fl : folder fs)
(tab : sql_table fs cs) (input : string) =
List.app (@Sql.easy_insert injs fl tab) (@parse injs reads fl input)
(tab : sql_table fs cs) (header : int) (input : string) =
List.app (@Sql.easy_insert injs fl tab) (@parse injs reads fl header input)

open Bootstrap3

Expand Down
2 changes: 2 additions & 0 deletions csv.urs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
val importTable : fs ::: {Type} -> cs ::: {{Unit}}
-> $(map sql_injectable fs) -> $(map read fs) -> folder fs
-> sql_table fs cs
-> int
-> string
-> transaction unit

val parse : fs ::: {Type}
-> $(map sql_injectable fs) -> $(map read fs) -> folder fs
-> int
-> string
-> list $fs

Expand Down
2 changes: 1 addition & 1 deletion examples/mg.ur
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ task initialize = fn () =>
dml (INSERT INTO time (Hour, Minute, Description) VALUES (12, 00, 'noon'))


val importHomes = Csv.importTable h
val importHomes = Csv.importTable h 0

open Bootstrap3

Expand Down

0 comments on commit 628d97a

Please sign in to comment.