From 039a83d907c162bfdf75b5d694c3fe102f22396b Mon Sep 17 00:00:00 2001 From: Bruno FS Date: Fri, 26 Jul 2019 19:14:31 -0300 Subject: [PATCH 1/8] except clause test --- test/honeysql_postgres/postgres_test.cljc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/honeysql_postgres/postgres_test.cljc b/test/honeysql_postgres/postgres_test.cljc index 321a79b..c3b14f3 100644 --- a/test/honeysql_postgres/postgres_test.cljc +++ b/test/honeysql_postgres/postgres_test.cljc @@ -10,6 +10,7 @@ [honeysql.helpers :as sqlh :refer [insert-into values where select columns from order-by update sset query-values]] [honeysql.core :as sql] + ; [honeysql.types :refer [array-map]] [clojure.test :as test :refer [deftest is testing]])) (deftest upsert-test @@ -219,3 +220,11 @@ (from :products) (where [:not-ilike :name "%name%"]) sql/format))))) + +(deftest values-except-select + (testing "select rows not present in table" + (is (= ["VALUES (4),(5),(6) EXCEPT SELECT id FROM images"] + (sql/format + {:except + [{:values [[4] [5] [6]]} + {:select [:id] :from [:images]}]}))))) \ No newline at end of file From d278a968203759604004c8a7a88677994a2e4b68 Mon Sep 17 00:00:00 2001 From: Bruno FS Date: Fri, 26 Jul 2019 20:41:04 -0300 Subject: [PATCH 2/8] implement except clause --- src/honeysql_postgres/format.cljc | 8 +++++++- test/honeysql_postgres/postgres_test.cljc | 8 ++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/honeysql_postgres/format.cljc b/src/honeysql_postgres/format.cljc index 3a278fb..142d20e 100644 --- a/src/honeysql_postgres/format.cljc +++ b/src/honeysql_postgres/format.cljc @@ -1,7 +1,8 @@ (ns ^{:doc "Extension of the honeysql format functions specifically for postgreSQL"} honeysql-postgres.format (:require [honeysql.format :as sqlf :refer [fn-handler format-clause]] ;; multi-methods - [honeysql-postgres.util :as util])) + [honeysql-postgres.util :as util] + [clojure.string :as string])) (def ^:private custom-additions {:create-table 10 @@ -26,6 +27,7 @@ "Determines the order that clauses will be placed within generated SQL" (merge {:with 30 :with-recursive 40 + :except 45 :select 50 :insert-into 60 :update 70 @@ -208,4 +210,8 @@ (defmethod format-clause :insert-into-as [[_ [table-name table-alias]] _] (str "INSERT INTO " (sqlf/to-sql table-name) " AS " (sqlf/to-sql table-alias))) +(defmethod format-clause :except [[_ maps] _] + (binding [sqlf/*subquery?* false] + (string/join " EXCEPT " (map sqlf/to-sql maps)))) + (override-default-clause-priority) diff --git a/test/honeysql_postgres/postgres_test.cljc b/test/honeysql_postgres/postgres_test.cljc index c3b14f3..09bacd1 100644 --- a/test/honeysql_postgres/postgres_test.cljc +++ b/test/honeysql_postgres/postgres_test.cljc @@ -223,8 +223,8 @@ (deftest values-except-select (testing "select rows not present in table" - (is (= ["VALUES (4),(5),(6) EXCEPT SELECT id FROM images"] + (is (= ["VALUES (?), (?), (?) EXCEPT SELECT id FROM images" 4 5 6] (sql/format - {:except - [{:values [[4] [5] [6]]} - {:select [:id] :from [:images]}]}))))) \ No newline at end of file + {:except + [{:values [[4] [5] [6]]} + {:select [:id] :from [:images]}]}))))) From 26d92a6414ececbe0a0108aea5bc73472d1cfae1 Mon Sep 17 00:00:00 2001 From: Bruno FS Date: Fri, 26 Jul 2019 21:00:23 -0300 Subject: [PATCH 3/8] except-all tests --- test/honeysql_postgres/postgres_test.cljc | 29 ++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/test/honeysql_postgres/postgres_test.cljc b/test/honeysql_postgres/postgres_test.cljc index 09bacd1..5b03430 100644 --- a/test/honeysql_postgres/postgres_test.cljc +++ b/test/honeysql_postgres/postgres_test.cljc @@ -222,9 +222,36 @@ sql/format))))) (deftest values-except-select - (testing "select rows not present in table" + (testing "select which values are not not present in a table" (is (= ["VALUES (?), (?), (?) EXCEPT SELECT id FROM images" 4 5 6] (sql/format {:except [{:values [[4] [5] [6]]} {:select [:id] :from [:images]}]}))))) + + +(deftest select-except-select + (testing "select which rows are not present in another table" + (is (= ["SELECT ip EXCEPT SELECT ip FROM ip_location"] + (sql/format + {:except + [{:select [:ip]} + {:select [:ip] :from [:ip_location]}]}))))) + + +(deftest values-except-all-select + (testing "select which values are not not present in a table" + (is (= ["VALUES (?), (?), (?) EXCEPT ALL SELECT id FROM images" 4 5 6] + (sql/format + {:except-all + [{:values [[4] [5] [6]]} + {:select [:id] :from [:images]}]}))))) + + +(deftest select-except-all-select + (testing "select which rows are not present in another table" + (is (= ["SELECT ip EXCEPT ALL SELECT ip FROM ip_location"] + (sql/format + {:except-all + [{:select [:ip]} + {:select [:ip] :from [:ip_location]}]}))))) From fdc4d026be1794dd82d3c7273220a8d6192dde1d Mon Sep 17 00:00:00 2001 From: Bruno FS Date: Mon, 29 Jul 2019 12:19:39 -0300 Subject: [PATCH 4/8] implement except-all --- src/honeysql_postgres/format.cljc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/honeysql_postgres/format.cljc b/src/honeysql_postgres/format.cljc index 142d20e..5949bac 100644 --- a/src/honeysql_postgres/format.cljc +++ b/src/honeysql_postgres/format.cljc @@ -28,6 +28,7 @@ (merge {:with 30 :with-recursive 40 :except 45 + :except-all 45 :select 50 :insert-into 60 :update 70 @@ -214,4 +215,8 @@ (binding [sqlf/*subquery?* false] (string/join " EXCEPT " (map sqlf/to-sql maps)))) +(defmethod format-clause :except-all [[_ maps] _] + (binding [sqlf/*subquery?* false] + (string/join " EXCEPT ALL " (map sqlf/to-sql maps)))) + (override-default-clause-priority) From c3374f59dff308f061b28e323a5291009cccf354 Mon Sep 17 00:00:00 2001 From: Bruno FS Date: Mon, 29 Jul 2019 12:34:58 -0300 Subject: [PATCH 5/8] add except usage on README --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 0bc72c1..d61f0b8 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Currently honeysql-postgres supports the following postgres specific clauses: - rename column - insert-into-as - pattern matching (ILIKE and NOT ILIKE) +- except (and except-all) ## Index @@ -39,6 +40,7 @@ Currently honeysql-postgres supports the following postgres specific clauses: - [drop table](https://github.com/nilenso/honeysql-postgres#drop-table) - [alter table](https://github.com/nilenso/honeysql-postgres#alter-table) - [pattern matching](https://github.com/nilenso/honeysql-postgres#pattern-matching) + - [except](#except) - [SQL functions](https://github.com/nilenso/honeysql-postgres#sql-functions) - [License](https://github.com/nilenso/honeysql-postgres#license) @@ -181,6 +183,17 @@ The `ilike` and `not-ilike` operators can be used to query data using a pattern sql/format) => ["SELECT * FROM products WHERE name NOT ILIKE ?" "%name%"] ``` +### except + +```clj + +(sql/format + {:except + [{:select [:ip]} + {:select [:ip] :from [:ip_location]}]}) +=> ["SELECT ip EXCEPT SELECT ip FROM ip_location"] +``` +`except-all` works the same way as `except`. ### SQL functions The following are the SQL functions added in `honeysql-postgres` From a1d161915fb5f6fbe91e766dd7e98b3118f996b9 Mon Sep 17 00:00:00 2001 From: Bruno FS Date: Mon, 29 Jul 2019 13:49:22 -0300 Subject: [PATCH 6/8] update README links --- README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index d61f0b8..92cda72 100644 --- a/README.md +++ b/README.md @@ -27,22 +27,22 @@ Currently honeysql-postgres supports the following postgres specific clauses: ## Index -- [Usage](https://github.com/nilenso/honeysql-postgres#usage) - - [Leiningen](https://github.com/nilenso/honeysql-postgres#leiningen) - - [Maven](https://github.com/nilenso/honeysql-postgres#maven) - - [repl](https://github.com/nilenso/honeysql-postgres#repl) - - [Breaking Change](https://github.com/nilenso/honeysql-postgres#breaking-change) - - [upsert](https://github.com/nilenso/honeysql-postgres#upsert) - - [insert into with alias](https://github.com/nilenso/honeysql-postgres#insert-into-with-alias) - - [over](https://github.com/nilenso/honeysql-postgres#over) - - [create view](https://github.com/nilenso/honeysql-postgres#create-view) - - [create table](https://github.com/nilenso/honeysql-postgres#create-table) - - [drop table](https://github.com/nilenso/honeysql-postgres#drop-table) - - [alter table](https://github.com/nilenso/honeysql-postgres#alter-table) - - [pattern matching](https://github.com/nilenso/honeysql-postgres#pattern-matching) +- [Usage](#usage) + - [Leiningen](#leiningen) + - [Maven](#maven) + - [repl](#repl) + - [Breaking Change](#breaking-change) + - [upsert](#upsert) + - [insert into with alias](#insert-into-with-alias) + - [over](#over) + - [create view](#create-view) + - [create table](#create-table) + - [drop table](#drop-table) + - [alter table](#alter-table) + - [pattern matching](#pattern-matching) - [except](#except) - - [SQL functions](https://github.com/nilenso/honeysql-postgres#sql-functions) -- [License](https://github.com/nilenso/honeysql-postgres#license) + - [SQL functions](#sql-functions) +- [License](#license) ## Usage From c7325a4f61509db274135a0da6b044bbc7524f48 Mon Sep 17 00:00:00 2001 From: Bruno FS Date: Mon, 29 Jul 2019 17:35:02 -0300 Subject: [PATCH 7/8] bump version --- project.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project.clj b/project.clj index e96ce32..0a5e4e2 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject nilenso/honeysql-postgres "0.2.5" +(defproject nilenso/honeysql-postgres "0.2.6" :description "PostgreSQL extension for honeysql" :url "https://github.com/nilenso/honeysql-postgres" :license {:name "Eclipse Public License" From b14a4f1eccfde52a6d09d34b8e52b4c19561f331 Mon Sep 17 00:00:00 2001 From: Bruno FS Date: Mon, 29 Jul 2019 17:55:33 -0300 Subject: [PATCH 8/8] remove typo --- test/honeysql_postgres/postgres_test.cljc | 1 - 1 file changed, 1 deletion(-) diff --git a/test/honeysql_postgres/postgres_test.cljc b/test/honeysql_postgres/postgres_test.cljc index 5b03430..7f201d3 100644 --- a/test/honeysql_postgres/postgres_test.cljc +++ b/test/honeysql_postgres/postgres_test.cljc @@ -10,7 +10,6 @@ [honeysql.helpers :as sqlh :refer [insert-into values where select columns from order-by update sset query-values]] [honeysql.core :as sql] - ; [honeysql.types :refer [array-map]] [clojure.test :as test :refer [deftest is testing]])) (deftest upsert-test