Skip to content

Commit 88f1b05

Browse files
committed
1.0.0
1 parent 0e3b148 commit 88f1b05

File tree

12 files changed

+95
-53
lines changed

12 files changed

+95
-53
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# WIP
1+
# 1.0.0
22

3-
- Support `:postgresql`, `:h2`
3+
- Support `:h2`, `:mysql`, `:postgresql` dbtypes
44

55
# 0.1.0
66

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ See [datascript/doc/storage.md](https://github.com/tonsky/datascript/blob/master
77
Add this to `deps.edn`:
88

99
```clj
10-
io.github.tonsky/datascript-storage-sql {:mvn/version "0.1.0"}
10+
io.github.tonsky/datascript-storage-sql {:mvn/version "1.0.0"}
1111
```
1212

1313
Create storage by passing in `java.sql.Connection` and `:dbtype` option:
@@ -65,9 +65,10 @@ or
6565

6666
Currently supported `:dbtype`-s:
6767

68-
- `:sqlite`
6968
- `:h2`
69+
- `:mysql`
7070
- `:postgresql`
71+
- `:sqlite`
7172

7273
If needed, you can close connection through storage:
7374

deps.edn

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
datascript/datascript {:mvn/version "1.5.2"}}
44
:aliases
55
{:dev
6-
{:extra-paths
7-
["dev"]
6+
{:extra-paths ["dev"]
87
:extra-deps
98
{org.clojure/tools.namespace {:mvn/version "1.4.4"}}}
10-
:sqlite
11-
{:extra-deps
12-
{org.xerial/sqlite-jdbc {:mvn/version "3.42.0.0"}}}
13-
:h2
14-
{:extra-deps
15-
{com.h2database/h2 {:mvn/version "2.2.220"}}}
16-
:postgresql
17-
{:extra-deps
18-
{org.postgresql/postgresql {:mvn/version "42.6.0"}}}}}
19-
20-
9+
:test
10+
{:extra-paths ["test"]
11+
:extra-deps
12+
{com.h2database/h2 {:mvn/version "2.2.220"}
13+
com.mysql/mysql-connector-j {:mvn/version "8.1.0"}
14+
org.postgresql/postgresql {:mvn/version "42.6.0"}
15+
org.xerial/sqlite-jdbc {:mvn/version "3.42.0.0"}}}}}

dev/data_readers.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{p user/p}

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject io.github.tonsky/datascript-storage-sql "0.1.0"
1+
(defproject io.github.tonsky/datascript-storage-sql "0.0.0"
22
:description "SQL Storage implementation for DataScript"
33
:license {:name "MIT" :url "https://github.com/tonsky/datascript-storage-sql/blob/master/LICENSE"}
44
:url "https://github.com/tonsky/datascript-storage-sql"

script/repl.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
set -o errexit -o nounset -o pipefail
33
cd "`dirname $0`/.."
44

5-
clj -A:dev:test:sqlite:h2:postgresql -M -m user
5+
clj -A:dev:test -M -m user

script/test_all.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
set -o errexit -o nounset -o pipefail
3+
cd "`dirname $0`/.."
4+
5+
clj -A:test -M -m datascript.storage.sql.test-main

src/datascript/storage/sql/core.clj

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,31 @@
1919
(finally
2020
(.setAutoCommit conn# true)))))
2121

22-
(defn execute! [conn sql]
22+
(defn execute! [^Connection conn sql]
2323
(with-open [stmt (.createStatement conn)]
2424
(.execute stmt sql)))
2525

26-
(defmulti store-impl
27-
(fn [conn opts addr+data-seq]
28-
(:dbtype opts)))
26+
(defmulti upsert-dml :dbtype)
2927

30-
(defmethod store-impl :h2 [^Connection conn opts addr+data-seq]
31-
(let [{:keys [table binary? freeze-str freeze-bytes batch-size]} opts
32-
sql (str "merge into " table " key (addr) values (?, ?)")]
33-
(with-tx conn
34-
(with-open [stmt (.prepareStatement conn sql)]
35-
(doseq [part (partition-all batch-size addr+data-seq)]
36-
(doseq [[addr data] part]
37-
(.setLong stmt 1 addr)
38-
(if binary?
39-
(let [content ^bytes (freeze-bytes data)]
40-
(.setBytes stmt 2 content))
41-
(let [content ^String (freeze-str data)]
42-
(.setString stmt 2 content)))
43-
(.addBatch stmt))
44-
(.executeBatch stmt))))))
28+
(defmethod upsert-dml :h2 [opts]
29+
(str "merge into " (:table opts) " key (addr) values (?, ?)"))
30+
31+
(defmethod upsert-dml :mysql [opts]
32+
(str
33+
"insert into " (:table opts) " (addr, content) "
34+
"values (?, ?) "
35+
"ON DUPLICATE KEY UPDATE content = ?"))
36+
37+
(defmethod upsert-dml :default [opts]
38+
(str
39+
"insert into " (:table opts) " (addr, content) "
40+
"values (?, ?) "
41+
"on conflict(addr) do update set content = ?"))
4542

46-
(defmethod store-impl :default [^Connection conn opts addr+data-seq]
43+
(defn store-impl [^Connection conn opts addr+data-seq]
4744
(let [{:keys [table binary? freeze-str freeze-bytes batch-size]} opts
48-
sql (str
49-
"insert into " table " (addr, content) "
50-
"values (?, ?) "
51-
"on conflict(addr) do update set content = ?")]
45+
sql (upsert-dml opts)
46+
cnt (count (re-seq #"\?" sql))]
5247
(with-tx conn
5348
(with-open [stmt (.prepareStatement conn sql)]
5449
(doseq [part (partition-all batch-size addr+data-seq)]
@@ -57,10 +52,12 @@
5752
(if binary?
5853
(let [content ^bytes (freeze-bytes data)]
5954
(.setBytes stmt 2 content)
60-
(.setBytes stmt 3 content))
55+
(when (= 3 cnt)
56+
(.setBytes stmt 3 content)))
6157
(let [content ^String (freeze-str data)]
6258
(.setString stmt 2 content)
63-
(.setString stmt 3 content)))
59+
(when (= 3 cnt)
60+
(.setString stmt 3 content))))
6461
(.addBatch stmt))
6562
(.executeBatch stmt))))))
6663

@@ -108,6 +105,12 @@
108105
" (addr BIGINT primary key, "
109106
" content " (if binary? "BINARY VARYING" "CHARACTER VARYING") ")"))
110107

108+
(defmethod ddl :mysql [{:keys [table binary?]}]
109+
(str
110+
"create table if not exists " table
111+
" (addr BIGINT primary key, "
112+
" content " (if binary? "LONGBLOB" "LONGTEXT") ")"))
113+
111114
(defmethod ddl :postgresql [{:keys [table binary?]}]
112115
(str
113116
"create table if not exists " table

test/datascript/storage/sql/test_core.clj

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
[clojure.edn :as edn]
44
[clojure.test :as t :refer [deftest is are testing]]
55
[datascript.core :as d]
6-
[datascript.storage.sql.core :as storage-sql]))
6+
[datascript.storage.sql.core :as storage-sql])
7+
(:import
8+
[java.sql Connection]))
79

810
(defn test-storage [{:keys [dbtype connect-fn reset-fn]}]
911
(let [schema {:id {:db/unique :db.unique/identity}}
@@ -13,7 +15,7 @@
1315
:age 38}]]
1416
(reset-fn)
1517
(testing "Read back same conn"
16-
(with-open [conn (connect-fn)]
18+
(with-open [conn ^Connection (connect-fn)]
1719
(let [db (d/db-with (d/empty-db schema) tx)
1820
storage (storage-sql/make conn
1921
{:dbtype dbtype})
@@ -22,7 +24,7 @@
2224
(is (= db db')))))
2325

2426
(testing "Read back new conn"
25-
(with-open [conn (connect-fn)]
27+
(with-open [conn ^Connection (connect-fn)]
2628
(let [db (d/db-with (d/empty-db schema) tx)
2729
storage (storage-sql/make conn
2830
{:dbtype dbtype})
@@ -31,7 +33,7 @@
3133

3234
(reset-fn)
3335
(testing "Rountrip binary"
34-
(with-open [conn (connect-fn)]
36+
(with-open [conn ^Connection (connect-fn)]
3537
(let [db (d/db-with (d/empty-db schema) tx)
3638
storage (storage-sql/make conn
3739
{:dbtype dbtype
@@ -40,6 +42,3 @@
4042
_ (d/store db storage)
4143
db' (d/restore storage)]
4244
(is (= db db')))))))
43-
44-
(comment
45-
(t/run-all-tests))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(ns datascript.storage.sql.test-main
2+
(:require
3+
[clojure.test :as t]
4+
[datascript.storage.sql.test-core]
5+
[datascript.storage.sql.test-h2]
6+
[datascript.storage.sql.test-mysql]
7+
[datascript.storage.sql.test-postgresql]
8+
[datascript.storage.sql.test-sqlite]))
9+
10+
(defn -main [& args]
11+
(t/run-all-tests #"datascript\.storage\.sql\..*"))
12+
13+
(comment
14+
(-main))

0 commit comments

Comments
 (0)