Created
March 6, 2013 20:55
-
-
Save charles-dyfis-net/5102970 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns xqj | |
(:require [clojure.data.xml :as xml] | |
[clojure.zip :as zip] | |
[clojure.data.zip :as zf] | |
[clojure.data.zip.xml :as zx] | |
[clojure.data.xml]) | |
(:import [clojure.lang Keyword] | |
[java.util Date] | |
[javax.xml.namespace QName] | |
[javax.xml.xquery XQConnection XQDataSource XQDynamicContext XQItemType XQConstants XQSequence] | |
[javax.xml.stream XMLInputFactory] | |
[java.io StringWriter] | |
[net.xqj.basex BaseXXQDataSource])) | |
(defn connect | |
"Sample use: | |
(connect :serverName \"localhost\" :port 1984 :user \"admin\" :password \"admin\")" | |
[& args] | |
(let [source (BaseXXQDataSource.)] | |
(doseq [[^Keyword key, val] (partition 2 args)] | |
(.setProperty source (.getName key) (str val))) | |
(.getConnection source))) | |
(defn result-as-string [^XQSequence result] | |
(.getSequenceAsString result (java.util.Properties.))) | |
(def pull-seq- @(clojure.lang.RT/var "clojure.data.xml" "pull-seq")) | |
(defn result-as-xml [^XQSequence result] | |
(xml/event-tree (pull-seq- (.getSequenceAsStream result)))) | |
(defprotocol QNameAble | |
(^QName as-qname [v])) | |
(extend-protocol QNameAble | |
Keyword (as-qname [n] | |
(if (= ::context-item n) | |
XQConstants/CONTEXT_ITEM | |
(QName. (.getName n)))) | |
String (as-qname [n] (QName. n)) | |
QName (as-qname [n] n)) | |
(defprotocol XQueryBind | |
(xq-bind [value name ^XQDynamicContext context])) | |
(extend-protocol XQueryBind | |
Boolean (xq-bind [v n ^XQDynamicContext ctx] (.bindByte ctx (as-qname n) v XQItemType/XQBASETYPE_BOOLEAN)) | |
String (xq-bind [v n ^XQDynamicContext ctx] (.bindString ctx (as-qname n) v XQItemType/XQBASETYPE_STRING)) | |
Short (xq-bind [v n ^XQDynamicContext ctx] (.bindShort ctx (as-qname n) v XQItemType/XQBASETYPE_SHORT)) | |
Long (xq-bind [v n ^XQDynamicContext ctx] (.bindLong ctx (as-qname n) v XQItemType/XQBASETYPE_LONG)) | |
Integer (xq-bind [v n ^XQDynamicContext ctx] (.bindInt ctx (as-qname n) v XQItemType/XQBASETYPE_INTEGER)) | |
Float (xq-bind [v n ^XQDynamicContext ctx] (.bindFloat ctx (as-qname n) v XQItemType/XQBASETYPE_FLOAT)) | |
Double (xq-bind [v n ^XQDynamicContext ctx] (.bindDouble ctx (as-qname n) v XQItemType/XQBASETYPE_DOUBLE)) | |
Date (xq-bind [v n ^XQDynamicContext ctx] (.bindObject ctx (as-qname n) v XQItemType/XQBASETYPE_DATETIME)) | |
XQSequence (xq-bind [v n ^XQDynamicContext ctx] (.bindSequence ctx (as-qname n) v)) | |
) | |
(defn run | |
([^XQConnection connection, ^String query] | |
(run connection query {})) | |
([^XQConnection connection, ^String query, vars] | |
(let [expression (.createExpression connection)] | |
(doseq [[k v] vars] | |
(xq-bind v k expression)) | |
(.executeQuery expression query)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment