From 148f752b5f48707dc3d7fe448d1faf33d5cd0228 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Thu, 13 Jun 2013 18:24:05 +1000 Subject: Starting a re-write of the DSL, to be followed by the compiler. Flip around field/table aliases, do joins a bit differently. They're my main aims at the moment! I'll also add a preprocessor for the compiler to massage it into a nicer form there. I discovered that joins can be done with a pretty sweet syntax in SQL: (tableA LEFT JOIN tableB) RIGHT JOIN tableC This is pretty much perfect for my purposes. Flipping alias maps just makes more sense and removes a whole bunch of `flip-map` calls that would be unnecessary if the aliases were the other way around. The user-facing side of the DSL will be left unchanged, though. The user provides an `{old-name new-name}` map and internally we convert that into `{new-name old-name}`. Like magic. I'm also adding a bunch more tests. Hopefully that will make things more likely to work for long periods of time. Peace out! --- test/clojure_sql/core_test.clj | 63 +++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 25 deletions(-) (limited to 'test/clojure_sql/core_test.clj') diff --git a/test/clojure_sql/core_test.clj b/test/clojure_sql/core_test.clj index 236e421..6a12698 100644 --- a/test/clojure_sql/core_test.clj +++ b/test/clojure_sql/core_test.clj @@ -3,28 +3,41 @@ (:require [clojure-sql.core :refer :all] [midje.sweet :refer :all])) -(fact - @(table :user) - => ["SELECT * FROM \"user\""] - - @(-> (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 :users) - (project '[:username (+ 1 2 3)]) - (rename '{(+ 1 2 3) :x}) - (select `(exists ~(-> (table :users) - (select '(= 10 :username))))) - println) - -(-> (table :users) - (project {:username :un}) - (select '(= :username 10))) +(comment + + (fact + @(table :user) + => ["SELECT * FROM \"user\""] + + @(-> (table :user) (project [:username])) + => ["SELECT \"user\".\"username\" AS \"username\" FROM \"user\""] + + @(-> (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 {'(+ :age :modifier) :u})) + => ["SELECT (\"user\".\"age\" + \"user\".\"modifier\") AS \"u\" FROM \"user\""] + + @(-> (table :user) + (project [:id]) + (join (-> (table :x) + (project [:id]) + (join (-> (table :y) + (project [:id])) + :type :left)))) + => ["SELECT \"user\".\"id\" AS \"id\" FROM \"user\" INNER JOIN \"x\" ON ((\"user\".\"id\" = \"x\".\"id\"))"]) + + (into {} (-> (table :user) (project '{(+ :username :password) :u}))) + + (-> (table :users) + (project '[:username (+ 1 2 3)]) + (rename '{(+ 1 2 3) :x}) + (select `(exists ~(-> (table :users) + (select '(= 10 :username)))))) + + (-> (table :users) + (project {:username :un}) + (select '(= :username 10)))) -- cgit v1.2.3