summaryrefslogtreecommitdiff
path: root/src/clojure_sql/compiler.clj
diff options
context:
space:
mode:
Diffstat (limited to 'src/clojure_sql/compiler.clj')
-rw-r--r--src/clojure_sql/compiler.clj23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/clojure_sql/compiler.clj b/src/clojure_sql/compiler.clj
index a3666dd..881a794 100644
--- a/src/clojure_sql/compiler.clj
+++ b/src/clojure_sql/compiler.clj
@@ -1,6 +1,8 @@
(ns clojure-sql.compiler
(:refer-clojure :exclude [compile sequence])
- (:require [clojure.string :as string]
+ (:require [clojure.set :as set]
+ [clojure.string :as string]
+ [clojure.walk :as walk]
[clojure-sql.query :refer [query?]]
[clojure-sql.util :as u :refer [named?]]
[clojure-sql.writer :as w :refer [return lift p-lift sequence do-m tell >>]]))
@@ -275,21 +277,24 @@
(defn update! [db {:keys [tables fields where joins]} partial-record]
(assert (= (count tables) 1) "Cannot delete from a multiple-table query")
- (let [table-name (-> tables first val)
+ (assert (seq (set/intersection (keys partial-record)
+ (keys fields))) "At least one field must be being updated")
+ (let [fix-expr (partial walk/prewalk (fn [x]
+ (if (vector? x)
+ (second x)
+ x)))
where-expression (if where
($str (return " WHERE ")
- (compile-expression db where))
+ (compile-expression db (fix-expr where)))
(return nil))
updates (->> (for [[alias value] partial-record
:when (fields alias)]
- ($str (compile-expression db (fields alias))
+ ($str (compile-expression db (second (fields alias)))
(return " = ")
- (compile-expression db value)))
+ (compile-expression db (fix-expr value))))
(apply sequence))
- table (compile-tables db joins tables)
- combined ($str (return "UPDATE ")
- table
- (return " SET ")
+ table-name (-> tables first val name)
+ combined ($str (return (str "UPDATE " table-name " SET "))
((p-lift string/join ", ") updates)
where-expression)
[sql vars] (combined [])]