Skip to content

Commit

Permalink
[Issue #105] post-merge cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBaranosky committed Jan 6, 2013
1 parent b880d44 commit d8fdbe5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Fixed table-name delimiting, and at the same time, support Postgres' schema and
queries covering multiple databases.
See: https://github.com/korma/Korma/pull/105 (Tsutomu YANO)

0.3.0-beta13
------------

Expand Down
3 changes: 1 addition & 2 deletions src/korma/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,7 @@
:fields []
:rel {}})

(defn- simple-table-name
[ent]
(defn- simple-table-name [ent]
(last (string/split (:table ent) #"\.")))

(defn create-relation
Expand Down
28 changes: 13 additions & 15 deletions src/korma/sql/engine.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
(map str-value vs))

(defn comma-values [vs]
(utils/comma (str-values vs)))
(utils/comma-separated (str-values vs)))

(defn wrap-values [vs]
(if (seq vs)
Expand Down Expand Up @@ -62,9 +62,9 @@

(defn table-identifier [table-name]
(let [parts (string/split table-name #"\.")]
(if-not (next parts)
(delimit-str table-name)
(string/join "." (map delimit-str parts)))))
(if (next parts)
(string/join "." (map delimit-str parts))
(delimit-str table-name))))

(defn field-identifier [field]
(cond
Expand Down Expand Up @@ -172,7 +172,7 @@


(defn do-infix [k op v]
(utils/space [(str-value k) op (str-value v)]))
(string/join " " [(str-value k) op (str-value v)]))

(defn do-group [op vs]
(utils/wrap (string/join op (str-values vs))))
Expand All @@ -181,9 +181,7 @@
(str op (utils/wrap (str-value v))))

(defn do-trinary [k op v1 sep v2]
(-> [(str-value k) op (str-value v1) sep (str-value v2)]
utils/space
utils/wrap))
(utils/wrap (string/join " " [(str-value k) op (str-value v1) sep (str-value v2)])))

(defn trinary [k op v1 sep v2]
(utils/pred do-trinary [(try-prefix k) op (try-prefix v1) sep (try-prefix v2)]))
Expand Down Expand Up @@ -286,8 +284,8 @@
(let [clauses (map field-str (:fields query))
modifiers-clause (when (seq (:modifiers query))
(str (reduce str (:modifiers query)) " "))
clauses-str (utils/comma clauses)
tables (utils/comma (map from-table (:from query)))
clauses-str (utils/comma-separated clauses)
tables (utils/comma-separated (map from-table (:from query)))
neue-sql (str "SELECT " modifiers-clause clauses-str " FROM " tables)]
(assoc query :sql-str neue-sql)))

Expand All @@ -303,9 +301,9 @@

(defn sql-insert [query]
(let [ins-keys (keys (first (:values query)))
keys-clause (utils/comma (map field-identifier ins-keys))
keys-clause (utils/comma-separated (map field-identifier ins-keys))
ins-values (insert-values-clause ins-keys (:values query))
values-clause (utils/comma ins-values)
values-clause (utils/comma-separated ins-values)
neue-sql (if-not (empty? ins-keys)
(str "INSERT INTO " (table-str query) " " (utils/wrap keys-clause) " VALUES " values-clause)
noop-query)]
Expand All @@ -320,7 +318,7 @@
(let [fields (for [[k v] (:set-fields query)]
[(utils/generated (field-identifier k)) (utils/generated (str-value v))])
clauses (map set= fields)
clauses-str (utils/comma clauses)
clauses-str (utils/comma-separated clauses)
neue-sql (str " SET " clauses-str)]
(update-in query [:sql-str] str neue-sql))))

Expand Down Expand Up @@ -348,15 +346,15 @@
(if (seq (:order query))
(let [clauses (for [[k dir] (:order query)]
(str (str-value k) " " (string/upper-case (name dir))))
clauses-str (utils/comma clauses)
clauses-str (utils/comma-separated clauses)
neue-sql (str " ORDER BY " clauses-str)]
(update-in query [:sql-str] str neue-sql))
query))

(defn sql-group [query]
(if (seq (:group query))
(let [clauses (map field-str (:group query))
clauses-str (utils/comma clauses)
clauses-str (utils/comma-separated clauses)
neue-sql (str " GROUP BY " clauses-str)]
(update-in query [:sql-str] str neue-sql))
query))
Expand Down
7 changes: 2 additions & 5 deletions src/korma/sql/utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,12 @@
;; str-utils
;;*****************************************************

(defn space [vs]
(string/join " " vs))

(defn comma [vs]
(defn comma-separated [vs]
(string/join ", " vs))

(defn wrap [v]
(str "(" v ")"))

(defn wrap-all [vs]
(wrap (comma vs)))
(wrap (comma-separated vs)))

17 changes: 11 additions & 6 deletions test/korma/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@

(defentity blah (pk :cool) (has-many users {:fk :cool_id}))

(defentity book-with-db (table :korma.book))
(defentity author-with-db (table :other.author) (belongs-to book-with-db))

(defentity book-with-schema (table :korma.myschema.book))
(defentity author-with-schema (table :korma.otherschema.author) (belongs-to book-with-schema))

(deftest select-function
(is (= "SELECT \"users\".\"id\", \"users\".\"username\" FROM \"users\" WHERE (\"users\".\"username\" = ?) ORDER BY \"users\".\"created\" ASC LIMIT 5 OFFSET 3"
(-> (select* "users")
Expand Down Expand Up @@ -440,6 +434,15 @@
"SELECT \"test\".* FROM \"test\" WHERE (\"test\".\"id\" != ?)"
(select :test (where {:id [not= 1]})))))


;;; Supporting Postgres' schema and queries covering multiple databases

(defentity book-with-db (table :korma.book))
(defentity author-with-db (table :other.author) (belongs-to book-with-db))

(defentity book-with-schema (table :korma.myschema.book))
(defentity author-with-schema (table :korma.otherschema.author) (belongs-to book-with-schema))

(deftest dbname-on-tablename
(are [query result] (= query result)
(sql-only
Expand All @@ -452,3 +455,5 @@
(select author-with-schema (with book-with-schema)))
"SELECT \"korma\".\"otherschema\".\"author\".*, \"korma\".\"myschema\".\"book\".* FROM \"korma\".\"otherschema\".\"author\" LEFT JOIN \"korma\".\"myschema\".\"book\" ON \"korma\".\"myschema\".\"book\".\"id\" = \"korma\".\"otherschema\".\"author\".\"book_id\""))


;;;

0 comments on commit d8fdbe5

Please sign in to comment.