Skip to content

Commit 1e4e0a6

Browse files
authored
Merge pull request #28 from pablo-abc/master
Add ILIKE handler
2 parents 3a8968f + c0ccfae commit 1e4e0a6

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff 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
165186
The following are the SQL functions added in `honeysql-postgres`
166187
- not

src/honeysql_postgres/format.cljc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,20 @@
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)))

test/honeysql_postgres/postgres_test.cljc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,19 @@
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)))))

0 commit comments

Comments
 (0)