summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/clojure_sql/compiler.clj23
-rw-r--r--src/clojure_sql/core.clj22
2 files changed, 29 insertions, 16 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 [])]
diff --git a/src/clojure_sql/core.clj b/src/clojure_sql/core.clj
index f10d968..e77eeb0 100644
--- a/src/clojure_sql/core.clj
+++ b/src/clojure_sql/core.clj
@@ -58,11 +58,19 @@
(q/set-query-deref-behaviour! run-query)
-(let [users (-> (table :users)
- (project [:id :username :password-hash]))]
- (insert! (-> users
- (project [:username]))
- {:username "john"}
- {:username "not-john"}
- {:username "bleep bloop"}))
+
+
+(try
+ (require '[clojure.java.jdbc :as jdbc])
+ (defn use-jdbc! [connection-string]
+ (set-query-executor! (fn [type query]
+ (jdbc/with-connection connection-string
+ (case type
+ :query (jdbc/with-query-results results query
+ (vec results))
+ :insert (jdbc/do-prepared-return-keys (first query) (next query))
+ (jdbc/do-prepared (first query) (next query)))))))
+
+ (catch Exception e
+ ))