Skip to content

Commit

Permalink
Merge pull request #22 from ontodev/sort-export
Browse files Browse the repository at this point in the history
Add sorting option for `export`
  • Loading branch information
jamesaoverton committed May 2, 2024
2 parents 93ff6f2 + 22d0e68 commit 99ed71b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
9 changes: 7 additions & 2 deletions src/ldtab/cli.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
["-f" "--format FORMAT" "Output format"
:parse-fn #(identity %)]
["-c" "--connection" "Database connection uri"]
["-s" "--streaming"]])
["-s" "--streaming"]
["-l" "--sort" "Sort output in lexicographical order"]])

(defn get-file-extension
[path]
Expand Down Expand Up @@ -212,6 +213,9 @@
(not (contains? #{"ttl" "tsv"} (get-file-extension (nth arguments 2))))
{:exit-message (str "Invalid output format: " (get-file-extension (nth arguments 2)))}

(and (:streaming options) (:sort options))
{:exit-message "Invalid input: --sort and --streaming are mutually exclusive."}

:else
{:action command})))

Expand Down Expand Up @@ -292,6 +296,7 @@
db (second arguments)
output (nth arguments 2)
streaming (:streaming options) ;TODO: should we always write with streams?
sorting (:sort options)
table (get options :table "statement")
database-connection (:connection options)
extension (get-file-extension output);TODO: add options for output format
Expand All @@ -307,7 +312,7 @@
"tsv")]
(if streaming
(export-db/export-stream db-con-uri table extension output)
(export-db/export db-con-uri table extension output))))
(export-db/export db-con-uri table extension sorting output))))

;TODO handle options for subcommand
;TODO validate tsv file
Expand Down
25 changes: 17 additions & 8 deletions src/ldtab/export.clj
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,28 @@
write ThickTriples in a TSV file to output."
([dp-path output]
(export-tsv dp-path "statement" output))
([db-connection table output]
([db-connection table sorting-flag output]
(let [data (jdbc/query db-connection [(str "SELECT * FROM " table)])
header (get-tsv-header data)
data (map #(triple-2-tsv %) data)
data (if sorting-flag
(sort data)
data)
output-path (io/as-relative-path output)]
(with-open [w (io/writer output-path :append true)]
(.write w (str (get-tsv-header data) "\n"))
(.write w (str header "\n"))
(doseq [row data]
(.write w (str (triple-2-tsv row) "\n")))))))
(.write w (str row "\n")))))))

(defn export-turtle
([db-connection output]
(export-turtle db-connection "statement" output))
([db-connection table output]
(let [data (jdbc/query db-connection [(str "SELECT * FROM " table)])
([db-connection table sorting-flag output]
(let [data (if sorting-flag
(jdbc/query db-connection [(str "SELECT * FROM " table)]
{:result-set-fn (fn [results] (sort-by triple-2-tsv results))})
(jdbc/query db-connection [(str "SELECT * FROM " table)]))

prefix (jdbc/query db-connection [(str "SELECT * FROM prefix")])
output-path (io/as-relative-path output)]
(thick-2-rdf/triples-2-rdf-model-stream data prefix output-path))))
Expand Down Expand Up @@ -100,13 +109,13 @@
(.finish writer-stream))))

(defn export
[db-connection table file-format output]
[db-connection table file-format sorting-flag output]
(let [db {:connection-uri db-connection}]
(cond
(= file-format "tsv")
(export-tsv db table output)
(export-tsv db table sorting-flag output)
(= file-format "ttl")
(export-turtle db table output))))
(export-turtle db table sorting-flag output))))

(defn export-stream
[db-connection table file-format output]
Expand Down

0 comments on commit 99ed71b

Please sign in to comment.