Skip to content

Commit

Permalink
Merge pull request #19 from ontodev/parametrize-init
Browse files Browse the repository at this point in the history
Table option for init
  • Loading branch information
jamesaoverton authored Dec 21, 2023
2 parents f248e20 + fe2bbe9 commit 7f589ba
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
21 changes: 14 additions & 7 deletions src/ldtab/cli.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

(def init-options
[["-h" "--help"]
["-t" "--table TABLE" "Table"
:parse-fn #(identity %)]
["-c" "--connection" "Database connection uri"]])

(def prefix-options
Expand Down Expand Up @@ -245,15 +247,22 @@
(println msg)
(System/exit status))

(defn file-exists?
[path]
(.exists (io/as-file path)))

(defn ldtab-init
[command]
(let [{:keys [options arguments errors summary]} (parse-opts command import-options)
db (second arguments)
database-connection (:connection options)]
database-connection (:connection options)
table (:table options)
table (if table table "statement")]
(if database-connection
(init-db/initialise-database db);expects a connection-uri
(init-db/create-sql-database db))));expects the name for the database
(init-db/initialise-database db table);expects a connection-uri
(if (file-exists? db)
(init-db/add-table db table)
(init-db/create-sql-database db table))))) ;expects the name for the database

;TODO handle options for subcommend
(defn ldtab-import
Expand All @@ -262,11 +271,10 @@
db (second arguments)
ontology (nth arguments 2)
streaming (:streaming options)
table (:table options)
table (get options :table "statement")
database-connection (:connection options)

;set defaults
table (if table table "statement")
db-con-uri (if database-connection
db ;db is connection-uri
(str "jdbc:sqlite:"
Expand All @@ -284,12 +292,11 @@
db (second arguments)
output (nth arguments 2)
streaming (:streaming options) ;TODO: should we always write with streams?
table (:table options)
table (get options :table "statement")
database-connection (:connection options)
extension (get-file-extension output);TODO: add options for output format

;set defaults
table (if table table "statement")
db-con-uri (if database-connection
db ;db is connection-uri
(str "jdbc:sqlite:"
Expand Down
42 changes: 28 additions & 14 deletions src/ldtab/init.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
(ns ldtab.init
(:require [clojure.java.jdbc :as jdbc]))

(defn get-ldtab-statement-schema
"Returns the schema of the ldtab database."
[]
[[:assertion :int "NOT NULL"]
[:retraction :int "NOT NULL DEFAULT 0"]
[:graph "TEXT" "NOT NULL"]
[:subject "TEXT" "NOT NULL"]
[:predicate "TEXT" "NOT NULL"]
[:object "TEXT" "NOT NULL"]
[:datatype "TEXT" "NOT NULL"]
[:annotation "TEXT"]])

(defn setup
[db-spec]
[db-spec table]
(let [metadata-table-ddl (jdbc/create-table-ddl :ldtab
[[:key "TEXT" "PRIMARY KEY"]
[:value "TEXT"]])
Expand All @@ -11,15 +23,8 @@
[[:prefix "TEXT" "PRIMARY KEY"]
[:base "TEXT" "NOT NULL"]])

statement-table-ddl (jdbc/create-table-ddl :statement
[[:assertion :int "NOT NULL"]
[:retraction :int "NOT NULL DEFAULT 0"]
[:graph "TEXT" "NOT NULL"]
[:subject "TEXT" "NOT NULL"]
[:predicate "TEXT" "NOT NULL"]
[:object "TEXT" "NOT NULL"]
[:datatype "TEXT" "NOT NULL"]
[:annotation "TEXT"]])]
statement-table-ddl (jdbc/create-table-ddl (keyword table)
(get-ldtab-statement-schema))]

;add tables to database
(jdbc/db-do-commands db-spec metadata-table-ddl)
Expand All @@ -31,18 +36,27 @@

;example: {:connection-uri "jdbc:postgresql://127.0.0.1:5432/kdb?user=knocean&password=knocean"}
(defn initialise-database
[connection]
[connection table]
(let [db-spec {:connection-uri connection}]
(setup db-spec)))
(setup db-spec table)))

(defn create-sql-database
"Creates an SQLite file with three tables:
1. 'ldtab' with columns: [key, value],
2. 'prefix' with columns: [prefix, base],
3. 'statement' with columns: [assertion, retraction, graph, subject, predicate, object, datatype, annotation]."
[dbname]
[dbname table]
(let [db-spec {:dbtype "sqlite"
:dbname (str dbname)
:user "myaccount"
:password "secret"}]
(setup db-spec table)))

(defn add-table
[dbname table]
(let [db-spec {:dbtype "sqlite"
:dbname (str dbname)
:user "myaccount"
:password "secret"}]
(setup db-spec)))
(jdbc/db-do-commands db-spec (jdbc/create-table-ddl (keyword table)
(get-ldtab-statement-schema)))))

0 comments on commit 7f589ba

Please sign in to comment.