Skip to content

Commit

Permalink
Moves PostgreSQL DSL extensions to dsl/pgsql
Browse files Browse the repository at this point in the history
  • Loading branch information
solatis committed Jun 1, 2016
1 parent 8799014 commit c4ae4a0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
7 changes: 0 additions & 7 deletions src/suricatta/dsl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
org.jooq.impl.DefaultConfiguration
org.jooq.impl.DefaultDataType
org.jooq.impl.SQLDataType
org.jooq.util.postgres.PostgresDSL
org.jooq.util.postgres.PostgresDataType
org.jooq.util.mariadb.MariaDBDataType
org.jooq.util.mysql.MySQLDataType
Expand Down Expand Up @@ -481,12 +480,6 @@
(into-array org.jooq.Condition)
(.where @q))))

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

(defn exists
"Create an exists condition."
[q]
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)))

3 changes: 2 additions & 1 deletion 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 @@ -257,7 +258,7 @@
(let [sq (-> (dsl/select (dsl/field :id))
(dsl/from :book)
(dsl/where "book.authorid = author.id"))
q (-> (dsl/select :fullname, (dsl/field (dsl/array sq) "books"))
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"))))
Expand Down

0 comments on commit c4ae4a0

Please sign in to comment.