File tree Expand file tree Collapse file tree 3 files changed +47
-2
lines changed
Expand file tree Collapse file tree 3 files changed +47
-2
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ Currently honeysql-postgres supports the following postgres specific clauses:
2222 - drop column
2323 - rename column
2424- insert-into-as
25+ - pattern matching (ILIKE and NOT ILIKE)
2526
2627## Index
2728
@@ -37,6 +38,7 @@ Currently honeysql-postgres supports the following postgres specific clauses:
3738 - [ create table] ( https://github.com/nilenso/honeysql-postgres#create-table )
3839 - [ drop table] ( https://github.com/nilenso/honeysql-postgres#drop-table )
3940 - [ alter table] ( https://github.com/nilenso/honeysql-postgres#alter-table )
41+ - [ pattern matching] ( https://github.com/nilenso/honeysql-postgres#pattern-matching )
4042 - [ SQL functions] ( https://github.com/nilenso/honeysql-postgres#sql-functions )
4143- [ License] ( https://github.com/nilenso/honeysql-postgres#license )
4244
@@ -161,6 +163,25 @@ use `alter-table` along with `add-column` & `drop-column` to modify table level
161163=> [" ALTER TABLE employees DROP COLUMN address" ]
162164```
163165
166+ ### pattern matching
167+ The ` ilike ` and ` not-ilike ` operators can be used to query data using a pattern matching technique.
168+ - like
169+ ``` clj
170+ (-> (select :name )
171+ (from :products )
172+ (where [:ilike :name " %name%" ])
173+ sql/format)
174+ => [" SELECT * FROM products WHERE name ILIKE ?" " %name%" ]
175+ ```
176+ - not-ilike
177+ ``` clj
178+ (-> (select :name )
179+ (from :products )
180+ (where [:not-ilike :name " %name%" ])
181+ sql/format)
182+ => [" SELECT * FROM products WHERE name NOT ILIKE ?" " %name%" ]
183+ ```
184+
164185### SQL functions
165186The following are the SQL functions added in ` honeysql-postgres `
166187- not
Original file line number Diff line number Diff line change 8585 preds)]
8686 (str " CHECK" pred-string)))
8787
88+ (defmethod fn-handler " ilike" [_ field value]
89+ (str (sqlf/to-sql field) " ILIKE "
90+ (sqlf/to-sql value)))
91+
92+ (defmethod fn-handler " not-ilike" [_ field value]
93+ (str (sqlf/to-sql field) " NOT ILIKE "
94+ (sqlf/to-sql value)))
95+
8896; ; format-clause multimethods used to format various sql clauses as defined
8997
9098(defmethod format-clause :on-conflict-constraint [[_ k] _]
9199 (str " ON CONFLICT ON CONSTRAINT " (-> k
92- util/get-first
93- sqlf/to-sql)))
100+ util/get-first
101+ sqlf/to-sql)))
94102
95103(defmethod format-clause :on-conflict [[_ ids] _]
96104 (str " ON CONFLICT " (util/comma-join-args ids)))
Original file line number Diff line number Diff line change 196196 (is (= [" DROP TABLE IF EXISTS t1, t2, t3" ]
197197 (-> (drop-table :if-exists :t1 :t2 :t3 )
198198 sql/format)))))
199+
200+ (deftest select-where-ilike
201+ (testing " select from table with ILIKE operator"
202+ (is (= [" SELECT * FROM products WHERE name ILIKE ?" " %name%" ]
203+ (-> (select :* )
204+ (from :products )
205+ (where [:ilike :name " %name%" ])
206+ sql/format)))))
207+
208+ (deftest select-where-not-ilike
209+ (testing " select from table with NOT ILIKE operator"
210+ (is (= [" SELECT * FROM products WHERE name NOT ILIKE ?" " %name%" ]
211+ (-> (select :* )
212+ (from :products )
213+ (where [:not-ilike :name " %name%" ])
214+ sql/format)))))
You can’t perform that action at this time.
0 commit comments