From 5ccaca496f4babb4fef2d34e272b8772e077fa25 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Thu, 23 May 2013 18:26:46 +1000 Subject: Simplify the compiler, better subquery support The compiler's been simplified a bit by breaking out the `writer` stuff into its own namespace and by generally making the monadic stuff better. It's all hidden behind a nice, simple, `clojure-sql.compiler/compile` function now. Call that and you'll get back what you need. (Internally it's a writer monad which is really modelled as a state monad with the only operation able to be performed on the state being `tell`.) Subqueries are now handled by the DSL in such a way as to not blow up everything. Subqueries have no support for referencing values in the superquery, though, so their utility is quite limited at present. Thinking about how to do subqueries properly may be difficult. --- test/clojure_sql/core_test.clj | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'test/clojure_sql') diff --git a/test/clojure_sql/core_test.clj b/test/clojure_sql/core_test.clj index d6654cf..b5d65e7 100644 --- a/test/clojure_sql/core_test.clj +++ b/test/clojure_sql/core_test.clj @@ -3,9 +3,23 @@ (:require [clojure-sql.core :refer :all] [midje.sweet :refer :all])) -#_(fact - (compile-query nil (table :user)) +(fact + @(table :user) => ["SELECT * FROM \"user\""] + + @(-> (table :user) (select '(= :username "george"))) + => ["SELECT * FROM \"user\" WHERE (\"user\".\"username\" = ?)" "george"] - (compile-query nil (-> (table :user) (select '(= :username "george")))) - => ["SELECT * FROM \"user\" WHERE (\"user\".\"username\" = ?)" "george"]) + @(-> (table :user) (project {:username :u})) + => ["SELECT \"user\".\"username\" AS \"u\" FROM \"user\""] + + @(-> (table :user) (project {'(+ :username :password) :u})) + => ["SELECT (\"user\".\"username\" + \"user\".\"password\") AS \"u\" FROM \"user\""]) + +(into {} (-> (table :user) (project '{(+ :username :password) :u}))) + +(-> (table {(-> (table :users) + (project '[:username (+ 1 2 3)]) + (rename '{(+ 1 2 3) :x}) + (select `(exists ~(table :users)))) :u}) + println) -- cgit v1.2.3