r0man/sqlingvo

Insert values that use SQL functions

ninjudd opened this issue · 14 comments

Is there a way to insert values that depend on SQL?

For example, I'd like to do something like:

(insert :tokens []
  {:value 123, :expires-at `(+ (now) ~interval)})

Hi Justin,
this is not possible at the moment.
Roman

Thanks @r0man. I figured out a better way to do what I need in this case (store an expiry column as a postgres interval instead of expires_at). If I need SQL insert values in the future, I'll dive into the code and see if I can figure out how to add them.

By the way, this is a great library! Thanks. I spent yesterday converting my app to SQLingvo, and it is an absolute pleasure to use.

Hi Justin,

I'm glad you found a solution for that. I already thought about
interpreting symbols in the value position of a map as SQL
expressions for the UPDATE statement. The same strategy should be
applied to the INSERTs as well. I'll take a look at it, if I have
time. Just curious, what did you use before SQLingvo?

Roman

On Fri, Feb 21, 2014 at 6:51 PM, Justin Balthrop
notifications@github.comwrote:

Thanks @r0man https://github.com/r0man. I figured out a better way to
do what I need in this case (store an expiry column as a postgres
interval instead of expired_at). If I need SQL insert values in the
future, I'll dive into the code and see if I can figure out how to add them.

By the way, this is a great library! Thanks. I spent yesterday converting
my app to SQLingvo, and it is an absolute pleasure to use.

Reply to this email directly or view it on GitHubhttps://github.com//issues/3#issuecomment-35755009
.

I was using Korma.

On previous projects, I have also used ClojureQL v2 (I helped Lau with some of the API) and java.jdbc directly.

@r0man looks like I'm at a point where I need this for UPDATE. SQLingvo still does not support this right?

Hi Justin, no it's still not implemented. I'm also on holiday the next
weeks, so I will not have time to look into this myself.
On 19 Aug 2014 19:22, "Justin Balthrop" notifications@github.com wrote:

@r0man https://github.com/r0man looks like I'm at a point where I need
this for UPDATE. SQLingvo still does not support this right?


Reply to this email directly or view it on GitHub
#3 (comment).

@r0man any tips on where to start with this? I've put it off as long as possible, but now I need to be able to update a row to append an element to a postgres array. Something like:

(update :item `[(= :tags (|| :tags ~tag))]
  (where `(= :id ~id)))

Or even better:

(update :item `[(=|| :tags ~tag))]
  (where `(= :id ~id)))

Hey Justin,

I had something like this in mind for insert statement

(insert :films [:name]
  (values {:name '(lower "X")}))

and this for update statements

(update :films {:name '(lower "X")}
  (where `(= :id 1)))

To get started I think we would need to make sure the values of the
hash map passed to inser/update are parsed via parse-expr to convert
them to the proper AST structures used elsewhere.

What do you think?

Roman

That sounds good. Does this mean the first update statement I posted above will work now?

Hi Justin,

I have a branch over here where I played around a bit.

https://github.com/r0man/sqlingvo/tree/insert-update-fn

Unforntunatly it's not ready yet. Lot's of things are breaking
with these changes. I'm traveling/working at the moment, so I
can't promise to work on that feature yet. If you want to take a
look into this yourself and need any help, ping me.

The compiler got also a bit out of control I fear :)

Roman

Interesting. I got to the point of adding parse-map-expr and calling it in values, but that wasn't enough, obviously. Seems like calling parse-map-expr on row in update may work though.

Nope. Getting the same type of error I got with insert...

@ninjudd Can you give the latest changes in the insert-update-fn branch a try. The statements mentioned above should compile now ...

r0man commented

Closing this issue, since this has been merged a while ago ...