How do you obtain the actual value, from the result of an exec-raw statement?

35 views
Skip to first unread message

Dan Campbell

unread,
Apr 23, 2015, 3:15:00 PM4/23/15
to sqlk...@googlegroups.com

Sorry, but I'm drawing a blank on this, and it should be obvious.  The result of using exec-raw, to call a stored procedure which returns a single value, is a sequence of a map with a single key-value pair.  That's fine, but I can't figure out how to get the single value from this map, because of the second keyword which gets inserted into the key.


In other words, 


( defn get-id
  [ n_account ]
  ( let [ seq_id ( exec-raw [ "SELECT OURPACKAGE.get_id( ? ) FROM DUAL" [ n_account ] ] :results )
         map_id ( into {} seq_id ) ]
         map_id ))



If you call this and try to use the map, there are 2 keywords referenced in the key, which makes it difficult to get any information from:


( def map_id ( get-id 12345 ) )


map_id is now a map with a single key-value pair, which is almost what I want.
{:OURPACKAGE.get_id(:1) "678910"}



So if we try to get the value from this, it returns an error because of the :1 keyword that represents the parameter:

( map_id OURPACKAGE.get_id(:1) )


IllegalArgumentException Wrong number of args passed to keyword: :1  clojure.lang.Keyword.throwArity (Keyword.java:92)


Should I just flatten the map, and get the (1)th element?  Or can the original sequence of the map be used directly?







Lars Nilsson

unread,
Apr 23, 2015, 3:34:47 PM4/23/15
to sqlk...@googlegroups.com
On Thu, Apr 23, 2015 at 3:15 PM, Dan Campbell <dcwh...@gmail.com> wrote:
> ( defn get-id
> [ n_account ]
> ( let [ seq_id ( exec-raw [ "SELECT OURPACKAGE.get_id( ? ) FROM DUAL" [
> n_account ] ] :results )
> map_id ( into {} seq_id ) ]
> map_id ))
...
> map_id is now a map with a single key-value pair, which is almost what I
> want.
> {:OURPACKAGE.get_id(:1) "678910"}
...
> ( map_id OURPACKAGE.get_id(:1) )
>
>
> IllegalArgumentException Wrong number of args passed to keyword: :1
> clojure.lang.Keyword.throwArity (Keyword.java:92)
>
>
> Should I just flatten the map, and get the (1)th element? Or can the
> original sequence of the map be used directly?

How about adding a column name for OURPACKAGE.get_id(?) in the query,
perhaps named id?

"SELECT OURPACKAGE.get_id( ?) id FROM DUAL"

Should result in {:id "678910"} which should be easier to use in expressions.

Lars Nilsson

Dennis Roberts

unread,
Apr 23, 2015, 4:22:32 PM4/23/15
to sqlk...@googlegroups.com
The only way I could find to use that keyword as-is was to call (keyword) on its name:

((keyword "OURPACKAGE.get_id(:1)") map_id)

You may want to use an alias instead in this case, though. Here's a contrived example:

metadactyl.core=> (:id (first (exec-raw ["SELECT abs(?) AS id FROM apps LIMIT 1" [-42]] :results)))

42

Dennis

--
You received this message because you are subscribed to the Google Groups "Korma" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlkorma+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dan Campbell

unread,
Apr 24, 2015, 7:46:39 AM4/24/15
to sqlk...@googlegroups.com

Of course.  Very simple.  exec-raw processes a sql statement directly from the underlying engine, therefore aliases will work there, just as they do in plsql.

Ok, thanks Dennis & Lars, everything's good.
Reply all
Reply to author
Forward
0 new messages