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

Enums and Multiple Insert #121

Open
Folcon opened this issue Mar 30, 2020 · 0 comments
Open

Enums and Multiple Insert #121

Folcon opened this issue Mar 30, 2020 · 0 comments

Comments

@Folcon
Copy link

Folcon commented Mar 30, 2020

Firstly thanks for the lib =)...

Two quick questions, if I define an enum as:

(sql/create-type sql-db :gender
  (sql/enum ["male" "female"]))

Is there no better api than:

(sql/insert sql-db :peep []
   (sql/values [{:gender `(raw "'male'")}]))
#_=> ["INSERT INTO \"peep\" (\"gender\") VALUES ('male')"]

I don't see how else to insert enums, as they always appear to be inserted as strings, IE:

(sql/sql
    (sql/insert sql-db :peep []
        (sql/values [{:gender :male}])))
#_=> ["INSERT INTO \"peep\" (\"gender\") VALUES (\"male\")"]

Also if I wanted to use a sql statement inside an insert:

(sql/with sql-db
    [:person (sql/insert sql-db :peep []
               (sql/values [{:name "Peep 1"}])
               (sql/returning :*))]
    (sql/insert sql-db :peep []
      (sql/values [{:name "Peep 2" :friend-id (sql/select sql-db [:id] (sql/from :person))}])))
#_=>
["WITH \"person\" AS (INSERT INTO \"peep\" (\"name\") VALUES (?) RETURNING *) INSERT INTO \"peep\" (\"friend-id\", \"name\") VALUES (SELECT \"id\" FROM \"person\", ?)"
 "Peep 1"
 "Peep 2"]

As you can see SELECT \"id\" FROM \"person\" is not inside it's own expr ie: (SELECT \"id\" FROM \"person\"), so it's not valid, the only way I can see to do this is:

(sql/with sql-db
    [:person (sql/insert sql-db :peep []
               (sql/values [{:name "Peep 1"}])
               (sql/returning :*))]
    (sql/insert sql-db :peep []
      (sql/values [{:name "Peep 2" :friend-id `(raw ~(str "(" (first (sql/sql (sql/select sql-db [:id] (sql/from :person)))) ")"))}])))
#_=>
["WITH \"person\" AS (INSERT INTO \"peep\" (\"name\") VALUES (?) RETURNING *) INSERT INTO \"peep\" (\"friend-id\", \"name\") VALUES ((SELECT \"id\" FROM \"person\"), ?)"
 "Peep 1"
 "Peep 2"]

Which is pretty horrible, is there a better way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant