Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ONLY keyword (Inheritance) #389

Closed
rafaeldelboni opened this issue Feb 24, 2022 · 1 comment
Closed

Support ONLY keyword (Inheritance) #389

rafaeldelboni opened this issue Feb 24, 2022 · 1 comment
Assignees
Labels
documentation I need to write something up!

Comments

@rafaeldelboni
Copy link

rafaeldelboni commented Feb 24, 2022

Hey we are using postgresql's Inheritance and we need to extend honey to add the keyword ONLY to make selects like SELECT * FROM ONLY table_name needed to get data from the top level of the tables that have Inheritance.

This is what we did currently:

(def format-selects-common #'sql/format-selects-common)

(defn- format-selects [k xs]
  (format-selects-common
   (sql/sql-kw k)
   'from
   xs))

(sql/register-clause! :from-only format-selects :from)

(sql/format {:select [:*], :from-only [[:table :t]]})

ps: The example is not complete, since you can add ONLY in the front of the table name and use in JOINs as well
for example:

select * from ONLY countries join ONLY capitals on countries.id = capitals.country_id;

Docs:
SQL SELECT / ONLY and Inheritance
DDL Inheritance
Some usage example

@rafaeldelboni rafaeldelboni changed the title Support FROM ONLY keyword (Inheritance) Support ONLY keyword (Inheritance) Feb 24, 2022
@seancorfield seancorfield self-assigned this Mar 25, 2022
@seancorfield seancorfield added enhancement needs analysis I need to think about this! labels Mar 25, 2022
@seancorfield
Copy link
Owner

I realized you do not need any extensions for this. Although PostgreSQL considers them optional, the ANSI SQL syntax is more function-like:

  (sql/format {:select [:*], :from [[[:only :table] :t]]})
;;=> ["SELECT * FROM ONLY(table) AS t"]
  (sql/format {:select [:*]
               :from [[[:only :countries]]]
               :join [[[:only :capitals]] [:= :countries.id :capitals.country_id]]})
;;=> ["SELECT * FROM ONLY(countries) INNER JOIN ONLY(capitals) ON countries.id = capitals.country_id"]

That should work out of the box with PG.

I'll add some notes to the appropriate clauses.

@seancorfield seancorfield added documentation I need to write something up! and removed enhancement needs analysis I need to think about this! labels Aug 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation I need to write something up!
Projects
None yet
Development

No branches or pull requests

2 participants