Skip to content

Commit

Permalink
Merge pull request #14 from solatis/master
Browse files Browse the repository at this point in the history
Implements support for ARRAY()
  • Loading branch information
niwinz committed Jun 2, 2016
2 parents f29d1aa + c4ae4a0 commit 52affd2
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/suricatta/dsl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,11 @@
params (rest v)]
(->> (map -unwrap params)
(into-array Object)
(DSL/condition sql)))))
(DSL/condition sql))))

suricatta.types.Deferred
(-condition [s]
(-condition @s)))

(extend-protocol IVal
Object
Expand Down Expand Up @@ -478,9 +482,9 @@

(defn exists
"Create an exists condition."
[select']
[q]
(defer
(DSL/exists select')))
(DSL/exists @q)))

(defn group-by
[q & fields]
Expand Down
60 changes: 60 additions & 0 deletions src/suricatta/dsl/pgsql.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
;; Copyright (c) 2014-2015, Andrey Antukh <[email protected]>
;; All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions are met:
;;
;; * Redistributions of source code must retain the above copyright notice, this
;; list of conditions and the following disclaimer.
;;
;; * Redistributions in binary form must reproduce the above copyright notice,
;; this list of conditions and the following disclaimer in the documentation
;; and/or other materials provided with the distribution.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
;; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
;; CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
;; OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

(ns suricatta.dsl.pgsql
"PostgreSQL-specific DSL extensions"
(:require [suricatta.proto :as proto]
[suricatta.types :as types :refer [defer]])
(:import org.jooq.util.postgres.PostgresDSL))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Protocol Implementations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(extend-protocol proto/ISQLType
(Class/forName "[Ljava.lang.String;")
(-convert [self]
(into [] self)))

(extend-protocol proto/ISQLType
(Class/forName "[Ljava.lang.Long;")
(-convert [self]
(into [] self)))

(extend-protocol proto/ISQLType
java.sql.Array
(-convert [self]
(into [] (.getArray self))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Common DSL functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn array
"Convert an expression to an array."
[q]
(defer
(PostgresDSL/array @q)))

19 changes: 19 additions & 0 deletions test/suricatta/dsl_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [clojure.test :refer :all]
[suricatta.core :refer :all]
[suricatta.dsl :as dsl]
[suricatta.dsl.pgsql :as pgsql]
[suricatta.format :as fmt]))

(def dbspec {:subprotocol "h2"
Expand Down Expand Up @@ -252,6 +253,24 @@
(dsl/from :author))]
(is (= (fmt/sql q)
"select fullname, (select count(*) from book where (book.authorid = author.id)) \"books\" from author"))))

(testing "Nested select returned as array"
(let [sq (-> (dsl/select (dsl/field :id))
(dsl/from :book)
(dsl/where "book.authorid = author.id"))
q (-> (dsl/select :fullname, (dsl/field (pgsql/array sq) "books"))
(dsl/from :author))]
(is (= (fmt/sql q)
"select fullname, array(select id from book where (book.authorid = author.id)) \"books\" from author"))))

(testing "Nested select in where clause using exists"
(let [q (-> (dsl/select :fullname)
(dsl/from :author)
(dsl/where (dsl/exists (-> (dsl/select :id)
(dsl/from :table)
(dsl/where ["table.author_id = author.id"])))))]
(is (= (fmt/sql q)
"select fullname from author where exists (select id from table where (table.author_id = author.id))"))))
)

(deftest dsl-insert
Expand Down

0 comments on commit 52affd2

Please sign in to comment.