Skip to content

Commit

Permalink
Merge pull request #2 from hden/staging
Browse files Browse the repository at this point in the history
Follow v0.4.0
  • Loading branch information
hden authored Mar 2, 2019
2 parents 683833d + d2a6ec9 commit efd5849
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Forked from: https://github.com/duct-framework/handler.sql

To install, add the following to your project `:dependencies`:

[hden/handler.honeysql "0.3.1"]
[hden/handler.honeysql "0.4.0"]

## Usage

Expand Down Expand Up @@ -45,6 +45,14 @@ identifier for the handler.
The `:db` option should be a `duct.database.sql.Boundary` record, and
the `:sql` option should be a [clojure.java.jdbc][] query vector.

If you omit the `:db` option, the `ig/prep` stage will default it to
`#ig/ref :duct.database/sql`. This means you can write:

```edn
{[:duct.handler.honeysql/query :example.handler.product/list]
{:sql {:select [:*] :from [:products]}}}
```

If you want to change the query based on the request, you can
destructure the parameters you want in the `:request` option:

Expand Down
16 changes: 8 additions & 8 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
(defproject hden/handler.honeysql "0.3.1"
(defproject hden/handler.honeysql "0.4.0"
:description "Duct library for building simple database-driven handlers"
:url "https://github.com/duct-framework/handler.sql"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.10.0"]
[org.clojure/java.jdbc "0.7.3"]
[duct/core "0.6.1"]
[org.clojure/java.jdbc "0.7.8"]
[duct/core "0.7.0"]
[duct/database.sql "0.1.0"]
[integrant "0.6.1"]
[honeysql "0.9.4"]
[medley "1.0.0"]
[ring/ring-core "1.6.3"]
[uritemplate-clj "1.1.1"]]
[integrant "0.7.0"]
[medley "1.1.0"]
[ring/ring-core "1.7.1"]
[uritemplate-clj "1.2.1"]]
:profiles
{:dev {:dependencies [[org.xerial/sqlite-jdbc "3.21.0"]]}})
{:dev {:dependencies [[org.xerial/sqlite-jdbc "3.25.2"]]}})
5 changes: 5 additions & 0 deletions src/duct/handler/honeysql.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
(defn generated-uri [result {:keys [location request-vars] :as opts}]
(uri-template location (merge request-vars result)))

(defmethod ig/prep-key :duct.handler/honeysql [_ opts]
(if (:db opts)
opts
(assoc opts :db (ig/ref :duct.database/sql))))

(defmethod ig/init-key ::query
[_ {:as opts :keys [db request sql] :or {request '_}}]
(let [opts (transform-opts-expr opts)
Expand Down
10 changes: 5 additions & 5 deletions src/duct_hierarchy.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{:duct.handler/sql [:duct/handler]
:duct.handler.sql/query [:duct.handler/sql :duct.module.sql/requires-db]
:duct.handler.sql/query-one [:duct.handler/sql :duct.module.sql/requires-db]
:duct.handler.sql/insert [:duct.handler/sql :duct.module.sql/requires-db]
:duct.handler.sql/execute [:duct.handler/sql :duct.module.sql/requires-db]}
{:duct.handler/honeysql [:duct/handler]
:duct.handler.honeysql/query [:duct.handler/honeysql]
:duct.handler.honeysql/query-one [:duct.handler/honeysql]
:duct.handler.honeysql/insert [:duct.handler/honeysql]
:duct.handler.honeysql/execute [:duct.handler/honeysql]}
39 changes: 38 additions & 1 deletion test/duct/handler/honeysql_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,43 @@
(isa? ::sql/execute :duct.module.sql/requires-db)
(isa? ::sql/insert :duct.module.sql/requires-db))

(deftest prep-test
(testing "query"
(is (= (ig/prep {::sql/query
{:sql {:select [:*] :from [:comments]}}})
{::sql/query
{:db (ig/ref :duct.database/sql)
:sql {:select [:*] :from [:comments]}}})))

(testing "query-one"
(is (= (ig/prep {::sql/query
{:request '{{:keys [id]} :route-params}
:sql '{:select [:subject :body] :from [:posts] :where [:= :id id]}}})
{::sql/query
{:db (ig/ref :duct.database/sql)
:request '{{:keys [id]} :route-params}
:sql '{:select [:subject :body] :from [:posts] :where [:= :id id]}}})))

(testing "execute"
(is (= (ig/prep {::sql/query
{:request '{{:keys [id]} :route-params, {:strs [body]} :form-params}
:sql '{:update :comments :set {:body body} :where [:= :id id]}}})
{::sql/query
{:db (ig/ref :duct.database/sql)
:request '{{:keys [id]} :route-params, {:strs [body]} :form-params}
:sql '{:update :comments :set {:body body} :where [:= :id id]}}})))

(testing "insert"
(is (= (ig/prep {::sql/query
{:request '{{:keys [pid]} :route-params, {:strs [body]} :form-params}
:sql '{:insert-into :comments :columns [:post_id :body] :values [[pid body]]}
:location "/posts{/pid}/comments{/last_insert_rowid}"}})
{::sql/query
{:db (ig/ref :duct.database/sql)
:request '{{:keys [pid]} :route-params, {:strs [body]} :form-params}
:sql '{:insert-into :comments :columns [:post_id :body] :values [[pid body]]}
:location "/posts{/pid}/comments{/last_insert_rowid}"}}))))

(deftest query-test
(testing "with destructuring"
(let [config {::sql/query
Expand Down Expand Up @@ -148,7 +185,7 @@
config {::sql/execute
{:db (db/->Boundary db)
:request '{{:keys [id]} :route-params, {:strs [body]} :form-params}
:sql '{:update :comments :set {:body body} :where [:= :id id]}}}
:sql '{:update :comments :set {:body body} :where [:= :id id]}}}
handler (::sql/execute (ig/init config))]
(testing "valid update"
(is (= (handler {:route-params {:id "1"}, :form-params {"body" "Average"}})
Expand Down

0 comments on commit efd5849

Please sign in to comment.