From eb9af9e8830773b57ce35afcf43b80414c8e8c84 Mon Sep 17 00:00:00 2001 From: "Freeman, Danny (ARC-TH)[ASRC Federal Data Solutions, LLC]" Date: Thu, 31 Aug 2023 13:11:40 -0400 Subject: [PATCH 1/2] Add option to return query results w/ order matching SELECT bindings --- src/stardog/core.clj | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/stardog/core.clj b/src/stardog/core.clj index 290e6df..4c235bd 100644 --- a/src/stardog/core.clj +++ b/src/stardog/core.clj @@ -100,9 +100,10 @@ "Converts a Iteration of bindings into a seq of keymaps." [^IFn keyfn ^IFn valfn ^CloseableIterator results] (let [mapper (partial binding->map keyfn valfn) - realized-results (into [] (map mapper) (iterator-seq results))] + realized-results (into [] (map mapper) (iterator-seq results)) + variables (map keyfn (.variables results))] (.close results) - realized-results)) + (vary-meta realized-results assoc :variables variables))) (defn vector-map-results "Converts a Graph of statements into a seq of vectors." @@ -122,7 +123,7 @@ (let [namespaces (into {} (map (fn [^Namespace ns] [(.prefix ns) (.iri ns)])) (iterator-seq (.. results namespaces iterator)))] - (with-meta (vector-map-results valfn results) {:namespaces namespaces}))) + (vary-meta (vector-map-results valfn results) assoc :namespaces namespaces))) SelectQueryResult (clojure-data* [results keyfn valfn] (key-map-results keyfn valfn results)) nil @@ -191,6 +192,18 @@ (filter second) (into {}))))) +(defn- order-results [results] + (let [{:keys [variables] :as metadata} (meta results) + order-result-set (fn [result-set] + (into (sorted-map-by (fn [binding1 binding2] + (compare + (.indexOf variables binding1) + (.indexOf variables binding2)))) + result-set))] + (if (not-empty variables) + (-> (map order-result-set results) + (with-meta metadata)) + results))) (defn query "Executes a query and returns results. @@ -207,11 +220,13 @@ - converter: A function to convert returned values with (Function). - key-converter: A function to convert returned binding names with (Function). - limit: The limit for the result. Must be present to use offset (integer). - - offset: The offset to start the result (integer)." + - offset: The offset to start the result (integer). + - ordered?: Preserve the order of the variable bindings present in the SELECT clause" [^Connection connection ^String text & args] (let [args (convert-to-map args) q (create-query #(.select connection text %) #(.select connection text) args)] - (execute* q args))) + (cond-> (execute* q args) + (get-in args [:parameters :ordered?]) (order-results)))) (defn ask From d59217668e9719009f8b7034e6dd586164cea8ab Mon Sep 17 00:00:00 2001 From: "Freeman, Danny (ARC-TH)[ASRC Federal Data Solutions, LLC]" Date: Thu, 31 Aug 2023 13:53:15 -0400 Subject: [PATCH 2/2] Fix reflection warnings, unused imports --- src/stardog/core.clj | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/stardog/core.clj b/src/stardog/core.clj index 4c235bd..0082c0c 100644 --- a/src/stardog/core.clj +++ b/src/stardog/core.clj @@ -15,14 +15,12 @@ ;; limitations under the License. (ns stardog.core - (:require [clojure.string :as str] - [stardog.values :as values]) + (:require [stardog.values :as values]) (:import [com.complexible.stardog.api Connection ConnectionPool ConnectionPoolConfig ConnectionConfiguration Query ReadQuery] [clojure.lang IFn] - [java.util Map Iterator] - [com.complexible.stardog.reasoning.api ReasoningType] + [java.util Map List] [com.complexible.common.base CloseableIterator] [com.stardog.stark Values Namespace] [com.stardog.stark.query SelectQueryResult GraphQueryResult BindingSet Binding] @@ -98,7 +96,7 @@ (defn key-map-results "Converts a Iteration of bindings into a seq of keymaps." - [^IFn keyfn ^IFn valfn ^CloseableIterator results] + [^IFn keyfn ^IFn valfn ^SelectQueryResult results] (let [mapper (partial binding->map keyfn valfn) realized-results (into [] (map mapper) (iterator-seq results)) variables (map keyfn (.variables results))] @@ -193,7 +191,7 @@ (into {}))))) (defn- order-results [results] - (let [{:keys [variables] :as metadata} (meta results) + (let [{:keys [^List variables] :as metadata} (meta results) order-result-set (fn [result-set] (into (sorted-map-by (fn [binding1 binding2] (compare