Finish off the PostgreSQL comparison docs

This commit is contained in:
Sean Corfield 2021-04-09 17:48:13 -07:00
parent 2da32c70f1
commit c2e8bb9193
3 changed files with 31 additions and 5 deletions

View file

@ -629,14 +629,24 @@ user=> (sql/format {:select [:id
:MaxSalary]]]] :MaxSalary]]]]
:from [:employee] :from [:employee]
:window [:w {:partition-by [:department]}]}) :window [:w {:partition-by [:department]}]})
["SELECT id, AVG(salary) OVER (PARTITION BY department ORDER BY designation ASC) AS Average, MAX(salary) OVER w AS MaxSalary FROM employee WINDOW w AS (PARTITION BY department)"] ;; newlines inserted for readability:
["SELECT id,
AVG(salary) OVER (PARTITION BY department ORDER BY designation ASC) AS Average,
MAX(salary) OVER w AS MaxSalary
FROM employee
WINDOW w AS (PARTITION BY department)"]
;; easier to write with helpers (and easier to read!): ;; easier to write with helpers (and easier to read!):
user=> (sql/format (-> (select :id user=> (sql/format (-> (select :id
(over [[:avg :salary] (-> (partition-by :department) (order-by :designation)) :Average] (over [[:avg :salary] (-> (partition-by :department) (order-by :designation)) :Average]
[[:max :salary] :w :MaxSalary])) [[:max :salary] :w :MaxSalary]))
(from :employee) (from :employee)
(window :w (partition-by :department)))) (window :w (partition-by :department))))
["SELECT id, AVG(salary) OVER (PARTITION BY department ORDER BY designation ASC) AS Average, MAX(salary) OVER w AS MaxSalary FROM employee WINDOW w AS (PARTITION BY department)"] ;; newlines inserted for readability:
["SELECT id,
AVG(salary) OVER (PARTITION BY department ORDER BY designation ASC) AS Average,
MAX(salary) OVER w AS MaxSalary
FROM employee
WINDOW w AS (PARTITION BY department)"]
``` ```
The window function in the `:over` expression may be `{}` or `nil`: The window function in the `:over` expression may be `{}` or `nil`:

View file

@ -190,7 +190,7 @@ There is a helper function for every single clause that HoneySQL
supports out of the box. In addition, there are helpers for supports out of the box. In addition, there are helpers for
`composite`, `lateral`, `over`, and `upsert` that make it easier to construct those `composite`, `lateral`, `over`, and `upsert` that make it easier to construct those
parts of the SQL DSL (examples of `composite` appear in the [README](README.md), parts of the SQL DSL (examples of `composite` appear in the [README](README.md),
examples of `over` appear in the [Clause Reference](docs/clause-reference.md)) examples of `over` appear in the [Clause Reference](clause-reference.md))
In addition to being variadic -- which often lets you omit one In addition to being variadic -- which often lets you omit one
level of `[`..`]` -- the helper functions merge clauses, which level of `[`..`]` -- the helper functions merge clauses, which

View file

@ -5,7 +5,7 @@ features that HoneySQL supports out of the box
for which you previously needed the for which you previously needed the
[nilenso/honeysql-postgres library](https://github.com/nilenso/honeysql-postgres). [nilenso/honeysql-postgres library](https://github.com/nilenso/honeysql-postgres).
Everything that the nilenso library provided is implemented Everything that the nilenso library provided (in 0.3.104) is implemented
directly in HoneySQL 2.x although a few things have a directly in HoneySQL 2.x although a few things have a
slightly different syntax. slightly different syntax.
@ -79,7 +79,7 @@ user=> (-> (insert-into :distributors)
``` ```
`ON CONSTRAINT` is handled slightly differently to the nilenso library, `ON CONSTRAINT` is handled slightly differently to the nilenso library,
with provided a single `on-conflict-constraint` helper (and clause): which provided a single `on-conflict-constraint` helper (and clause):
```clojure ```clojure
user=> (-> (insert-into :distributors) user=> (-> (insert-into :distributors)
@ -153,6 +153,18 @@ By comparison, this is the DSL structure that nilenso would have required:
## INSERT INTO AS ## INSERT INTO AS
HoneySQL supports aliases directly in `:insert-into` so no special
clause is needed for this any more:
```clojure
user=> (sql/format (-> (insert-into :table :alias)
(values [[1 2 3] [4 5 6]])))
["INSERT INTO table AS alias VALUES (?, ?, ?), (?, ?, ?)" 1 2 3 4 5 6]
user=> (sql/format {:insert-into [:table :alias],
:values [[1 2 3] [4 5 6]]})
["INSERT INTO table AS alias VALUES (?, ?, ?), (?, ?, ?)" 1 2 3 4 5 6]
```
## Returning ## Returning
The `RETURNING` clause is supported identically to the nilenso library: The `RETURNING` clause is supported identically to the nilenso library:
@ -325,3 +337,7 @@ user=> (sql/format (alter-table :fruit
``` ```
## Window / Partition Support ## Window / Partition Support
HoneySQL supports `:window`, `:partition-by`, and `:over`
directly now.
See the Clause Reference for examples of [WINDOW, PARTITION BY, and OVER](clause-reference.md#window-partition-by-and-over).