On Sun, Dec 21, 2008 at 7:03 PM, Adam Harrison (Clojure)
<[email protected]> wrote:
[...]
> (defmacro where [& triples]
> `(let [encode# (fn [x#] (cond (and (symbol? x#) (= (first (name x#))
> \?)) (name x#)
> (integer? x#) (str "\"" x# "\"^^xsd:integer")
> (float? x#) (str "\"" x# "\"^^xsd:decimal")
> (string? x#) (str "\"" x# "\"")))]
> (apply str
> (interpose " .\n"
> (for [triple# '~triples]
> (apply str
> (interpose " "
> (map encode# triple#))))))))
>
> As you can see, so far it correctly encodes SPARQL capture variables,
> and literal strings, integers and floats:
>
> user=> (print (where (?a ?b 1) (?a ?b 2.0) (?a ?b "string")))
> ?a ?b "1"^^xsd:integer .
> ?a ?b "2.0"^^xsd:decimal .
> ?a ?b "string"
>
> I tried adding '(list? x#) (eval x#)' to the encode cond to make it cope
> with expressions like this:
>
> (where (?a ?b (+ 1 2)))
>
> Unfortunately that results in an unencoded literal '3' in the query
> string instead of the '"3"^^xsd:integer' I was looking for. I tried
Here's my poor excuse for a nudge in what may or may not be the right direction:
user=> (defn encode-symbol [x] (if (= (first (name x))) (name x)))
#'user/encode-symbol
user=> (defn encode-other [x]
(cond (integer? x) (str \" x \" "^^xsd:integer")
(float? x) (str \" x \" "^^xsd:decimal")
(string? x) (str \" x \")))
#'user/encode-other
user=> (defmacro encode [x]
(if (symbol? x)
(encode-symbol x)
`(encode-other ~x)))
nil
user=> (encode ?a)
"?a"
user=> (encode 1)
"\"1\"^^xsd:integer"
user=> (encode 2.0)
"\"2.0\"^^xsd:decimal"
user=> (encode "string")
"\"string\""
user=> (encode (+ 1 2))
"\"3\"^^xsd:integer"
user=>
(Disclaimer: I don't know what I am doing, but this at least seems to work.)
--
Michael Wood <[email protected]>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---