Comments (4)
I'm running into this issue as well. I thought I might be able to solve this by using the :modifiers
key, but the current behavior simply uppercases the keys within the value, which is embedded in the format-clause
implementation of :select
. I chose to create a new implementation :selectd
with added behavior to support DISTINCT ON (...)
:
(require '[honeysql.format :as f])
(require '[clojure.string :as string])
(defmethod f/format-clause :selectd [[_ fields] sql-map]
(str "SELECT "
(when (:distinct-on sql-map)
(str "DISTINCT ON ("
(f/comma-join (map name (:distinct-on sql-map)))
") "))
(f/comma-join (map f/to-sql fields))))
(f/register-clause! :selectd 50)
It's used like so:
{:selectd [:foo]
:from [:bar]
:distinct-on [:baz]
This workaround has its limits as we lose the composability of HoneySQL with :selectd
(the helper methods like merge-select
no longer work here).
I think there are two ways to solve this:
- The
:select
clause could be augmented to handle a:distinct-on
top-level clause, which I believe would violate the general design decision of the library to be database agnostic (DISTINCT ON
is specific to Postgres). - Open up the
:modifiers
to multi-methods, and allow users to extend the space betweenSELECT
and its clauses as needed.
The second option seems intuitive to me. Something like this might work:
{:select [:foo]
:from [:bar]
:modifiers [[:distinct-on :baz]]}
from honeysql.
It seems like this should be pushed to honeysql-postgres
?
from honeysql.
I think so, it's just unclear if it's possible to make it work, because in our codebase we're doing this: {:select [(sql/raw "distinct on (id) id") :other_field]}
.
from honeysql.
Given HoneySQL's position of trying to be vendor-neutral and the presence of reasonable approaches to make this work (without changing HoneySQL), I'm closing this out.
from honeysql.
Related Issues (20)
- `(where false)` is not equivalent to `{:where false}` HOT 1
- Allow `with expr AS ident` syntax in WITH clause HOT 1
- pg array constructor (:'ARRAY) gets quoted when :quoted is true HOT 3
- Document that create clauses can take a sequence of keywords
- Support embedded user and password in JDBC URLs HOT 1
- Producing PostgreSQL's `ON DISTINCT CASCADE` DDL for foreign keys HOT 2
- Support full TRUNCATE syntax (cascade, identity) HOT 2
- Improve column specification support in a generic way HOT 1
- Support for multiple tables in `truncate`
- First call to format with :dialect may fail HOT 4
- values helper should accept one or more hash maps HOT 1
- Make it clearer how to declare primary key etc HOT 1
- Several GH Actions are outdated/deprecated -- update
- Cannot set a ON CONFLICT clause on INSERT INTO (SQLite) HOT 7
- Differences from Honey SQL 1 dox should mention things like `:desc-nulls-last` (2.x) vs `:desc :nulls-last` (1.x) in `:order-by` HOT 1
- Is there any way to do `IN (...)` with columns inside the list? Should a `:list` primitive be available? HOT 2
- `:union`/`:union-all` can fail if subqueries are not wrapped in parentheses HOT 4
- Cannot create compound keys in table constraint with :unique HOT 3
- Support postgres `:-` for arbitrary number of key values
- Parameters are lost in `is null` expression HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from honeysql.