|
1 | 1 | (ns yesql.generate |
2 | 2 | (:require [clojure.java.jdbc :as jdbc] |
3 | 3 | [clojure.core.typed :as t :refer [ann HMap tc-ignore Any IFn]] |
| 4 | + [clojure.string :refer [join]] |
4 | 5 | [yesql.util :refer [distinct-except create-root-var]] |
5 | 6 | [yesql.types :refer [map->Query]] |
6 | | - [yesql.statement-parser :refer [split-at-parameters reassemble-query]]) |
| 7 | + [yesql.statement-parser :refer [expected-parameter-list rewrite-query-for-jdbc]]) |
7 | 8 | (:import [yesql.types Query])) |
8 | 9 |
|
9 | 10 | ;; (ann replace-question-mark-with-gensym |
|
59 | 60 | query-options] |
60 | 61 | (assert name "Query name is mandatory.") |
61 | 62 | (assert statement "Query statement is mandatory.") |
62 | | - (let [split-query (split-at-parameters statement) |
63 | | - {:keys [query-args display-args function-args]} (split-query->args split-query) |
64 | | - jdbc-fn (cond |
| 63 | + (let [jdbc-fn (cond |
65 | 64 | (= (take-last 2 name) [\< \!]) insert-handler |
66 | 65 | (= (last name) \!) execute-handler |
67 | 66 | :else jdbc/query) |
| 67 | + required-args (expected-parameter-list statement) |
| 68 | + global-connection (:connection query-options) |
68 | 69 | real-fn (fn [args call-options] |
69 | | - (let [connection (:connection (merge query-options |
70 | | - call-options))] |
| 70 | + (let [connection (or (:connection call-options) |
| 71 | + global-connection)] |
71 | 72 | (assert connection |
72 | | - (format "No database connection supplied to function '%s',\nCheck the docs, and supply {:connection ...} as an option to the function call, or globally to the defquery declaration." |
| 73 | + (format (join "\n" |
| 74 | + ["No database connection supplied to function '%s'," |
| 75 | + "Check the docs, and supply {:connection ...} as an option to the function call, or globally to the defquery declaration."]) |
73 | 76 | name)) |
74 | | - (jdbc-fn (:connection (merge query-options |
75 | | - call-options)) |
76 | | - (reassemble-query split-query args)))) ] |
77 | | - (with-meta |
78 | | -;;; TODO The next step is to get the query args generated. |
79 | | - #_(cond |
80 | | - (and (:connection query-options) |
81 | | - (empty? expected-args)) |
82 | | - anonymous-version |
83 | | - |
84 | | - (:connection query-options) |
85 | | - single-arg-version |
86 | | - |
87 | | - :else double-arg-version) |
88 | | - (fn foo |
89 | | - ([] |
90 | | - (foo {})) |
91 | | - ([args] |
92 | | - (foo args {})) |
93 | | - ([args call-options] |
94 | | - (real-fn args call-options))) |
| 77 | + (jdbc-fn connection |
| 78 | + (rewrite-query-for-jdbc statement args)))) |
| 79 | + [display-args generated-function] (let [named-args (if-let [as-vec (seq (mapv (comp symbol clojure.core/name) |
| 80 | + required-args))] |
| 81 | + {:keys as-vec} |
| 82 | + {}) |
| 83 | + global-args {:keys ['connection]}] |
| 84 | + (if global-connection |
| 85 | + (if (empty? required-args) |
| 86 | + [(list [] |
| 87 | + [named-args global-args]) |
| 88 | + (fn foo |
| 89 | + ([] (foo {} {})) |
| 90 | + ([args call-options] (real-fn args call-options)))] |
| 91 | + [(list [named-args] |
| 92 | + [named-args global-args]) |
| 93 | + (fn foo |
| 94 | + ([args] (foo args {})) |
| 95 | + ([args call-options] (real-fn args call-options)))]) |
| 96 | + [(list [named-args global-args]) |
| 97 | + (fn foo |
| 98 | + ([args call-options] (real-fn args call-options)))]))] |
| 99 | + (with-meta generated-function |
95 | 100 | (merge {:name name |
96 | | - :arglists (list [display-args] |
97 | | - [display-args {:connection '...}]) |
| 101 | + :arglists display-args |
98 | 102 | ::source (str statement)} |
99 | 103 | (when docstring |
100 | 104 | {:doc docstring}))))) |
101 | 105 |
|
102 | | -(generate-query-fn (yesql.types/map->Query {:name "fetch" |
103 | | - :statement "SELECT * FROM users WHERE user_id = ?"}) |
104 | | - {:dofault-db {:subprotocol "derby" |
105 | | - :subname (gensym "memory:") |
106 | | - :create true}}) |
107 | | - |
108 | 106 | (defprotocol FunctionGenerator |
109 | 107 | (generate-fn [this options])) |
110 | 108 |
|
|
0 commit comments