-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #123 by adding type-hinting functions
- Loading branch information
1 parent
85734ab
commit 0379230
Showing
9 changed files
with
110 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
;; copyright (c) 2018-2020 Sean Corfield, all rights reserved | ||
|
||
(ns next.jdbc.types | ||
"Provides convenience functions for wrapping values you pass into SQL | ||
operations that have per-instance implementations of `SettableParameter` | ||
so that `.setObject()` is called with the appropriate `java.sql.Types` value." | ||
(:require [clojure.string :as str] | ||
[next.jdbc.prepare :as prep]) | ||
(:import (java.lang.reflect Field Modifier) | ||
(java.sql PreparedStatement))) | ||
|
||
(set! *warn-on-reflection* true) | ||
|
||
(defmacro ^:private all-types | ||
[] | ||
(let [names | ||
(into [] | ||
(comp (filter #(Modifier/isStatic (.getModifiers ^Field %))) | ||
(map #(.getName ^Field %))) | ||
(.getDeclaredFields java.sql.Types))] | ||
`(do | ||
~@(for [n names] | ||
(let [as-n (symbol (str "as-" | ||
(-> n | ||
(str/lower-case) | ||
(str/replace "_" "-"))))] | ||
`(defn ~as-n | ||
~(str "Wrap a Clojure value in a vector with metadata to implement `set-parameter` | ||
so that `.setObject()` is called with the `java.sql.Types/" n "` SQL type.") | ||
[~'obj] | ||
(with-meta [~'obj] | ||
{'next.jdbc.prepare/set-parameter | ||
(fn [[v#] ^PreparedStatement s# ^long i#] | ||
(.setObject s# i# v# ~(symbol "java.sql.Types" n)))}))))))) | ||
|
||
(all-types) | ||
|
||
(comment | ||
(macroexpand '(all-types))) |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
;; copyright (c) 2020 Sean Corfield, all rights reserved | ||
|
||
(ns next.jdbc.types-test | ||
"Some tests for the type-assist functions." | ||
(:require [clojure.test :refer [deftest is testing]] | ||
[next.jdbc.types :refer [as-varchar]])) | ||
|
||
(set! *warn-on-reflection* true) | ||
|
||
(deftest as-varchar-test | ||
(let [v (as-varchar "Hello")] | ||
(is (= ["Hello"] v)) | ||
(is (contains? (meta v) 'next.jdbc.prepare/set-parameter)) | ||
(is (fn? (get (meta v) 'next.jdbc.prepare/set-parameter))))) |