Skip to content

Commit

Permalink
fixes #547 by adding examples
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Corfield <[email protected]>
  • Loading branch information
seancorfield committed Oct 6, 2024
1 parent e2dc330 commit b55eb23
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changes

* 2.6.next in progress
* Address [#547](https://github.com/seancorfield/honeysql/issues/547) by adding examples of conditional SQL building with the helpers to the README and the `honey.sql.helpers` ns docstring.
* Performance optimizations via PRs [#545](https://github.com/seancorfield/honeysql/pull/545) and [#546](https://github.com/seancorfield/honeysql/pull/546) [@alexander-yakushev](https://github.com/alexander-yakushev).
* Address [#544](https://github.com/seancorfield/honeysql/issues/544) by adding support for MySQL's `VALUES ROW(..)` syntax.
* Fix [#543](https://github.com/seancorfield/honeysql/issues/543) by supporting both symbols and keywords in named parameters.
Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,24 @@ If you want to replace a clause, you can `dissoc` the existing clause first, sin
=> ["SELECT * FROM foo WHERE (a = ?) AND (b < ?)" 1 100]
```

The power of this approach comes from the abiliity to programmatically and
conditionally build up queries:

<!-- :test-doc-blocks/skip -->
```clojure
(defn fetch-user [& {:keys [id name]}]
(-> (select :*)
(from :users)
(cond->
id (where [:= :id id])
name (where [:= :name name]))
sql/format))
```

You can call `fetch-user` with either `:id` or `:name` _or both_ and get back
a query with the appropriate `WHERE` clause, since the helpers will merge the
conditions into the query DSL.

Column and table names may be aliased by using a vector pair of the original
name and the desired alias:

Expand All @@ -226,6 +244,21 @@ name and the desired alias:
=> ["SELECT a, b AS bar, c, d AS x FROM foo AS quux WHERE (quux.a = ?) AND (bar < ?)" 1 100]
```

or conditionally:

<!-- :test-doc-blocks/skip -->
```clojure
(-> (select :a [:b :bar])
(cond->
need-c (select :c)
x-val (select [:d :x]))
(from [:foo :quux])
(where [:= :quux.a 1] [:< :bar 100])
(cond->
x-val (where [:> :x x-val]))
sql/format)
```

In particular, note that `(select [:a :b])` means `SELECT a AS b` rather than
`SELECT a, b` -- helpers like `select` are generally variadic and do not take
a collection of column names.
Expand Down
10 changes: 10 additions & 0 deletions src/honey/sql/helpers.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
(sql/format))
```
or conditionally like this:
```
(-> (select :a :b :c)
(from :table)
(cond->
id (where [:= :id id]))
(sql/format))
```
Therefore all helpers can take an existing DSL expression
as their first argument or, if the first argument is not
a hash map, an empty DSL is assumed -- an empty hash map.
Expand Down

0 comments on commit b55eb23

Please sign in to comment.