Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include namespace in bound keywords #125

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions modules/alia/src/qbits/alia.clj
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
(doseq [[k x] values]
(settable-by-name/set-named-parameter!
builder
(name k)
(if (keyword? k) (str (.-sym ^clojure.lang.Keyword k)) k)
(encoder x)))
(.build builder))
(.bind statement (to-array (map encoder values))))
Expand Down Expand Up @@ -118,7 +118,8 @@
(SimpleStatement/newInstance
q
^Map (reduce-kv (fn [m k v]
(assoc m (name k) (encode v)))
(assoc m (format "\"%s\"" (if (keyword? k) (.-sym ^clojure.lang.Keyword k) k))
(encode v)))
{}
values))

Expand Down
49 changes: 49 additions & 0 deletions test/qbits/alia_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@
PRIMARY KEY (id)
);")

(alia/execute session "CREATE TABLE IF NOT EXISTS namespaced (
\"namespaced/id\" int,
\"namespaced/text\" varchar,
PRIMARY KEY (\"namespaced/id\")
);")

(alia/execute session "CREATE TABLE IF NOT EXISTS reserved (
\"view\" int,
\"all\" varchar,
PRIMARY KEY (\"view\")
);")

(alia/execute session "CREATE TABLE IF NOT EXISTS blobs (id uuid, data blob, PRIMARY KEY (id));"))

(defn teardown-test-keyspace
Expand Down Expand Up @@ -488,6 +500,43 @@
(alia/execute *session* "SELECT * FROM simple WHERE id = :id;"
{:values {:id an-id}})))))

(testing "reserved-names"
(let [prep-write (alia/prepare *session* "INSERT INTO reserved (\"view\", \"all\") VALUES(:\"view\", :\"all\");")
prep-read (alia/prepare *session* "SELECT * FROM reserved WHERE \"view\" = :\"view\";")
an-id (int 100)]

(is (= nil
(alia/execute *session* prep-write {:values {:view an-id
:all "inserted via reserved bindings"}})))

(is (= [{:view an-id
:all "inserted via reserved bindings"}]
(alia/execute *session* prep-read {:values {:view an-id}})))

(is (= [{:view an-id
:all "inserted via reserved bindings"}]
(alia/execute *session* "SELECT * FROM reserved WHERE \"view\" = :\"view\";"
{:values {:view an-id}})))))

(testing "namespaced-named-bindings"
(let [prep-write (alia/prepare *session* "INSERT INTO namespaced (\"namespaced/id\", \"namespaced/text\") VALUES(:\"namespaced/id\", :\"namespaced/text\");")
prep-read (alia/prepare *session* "SELECT * FROM namespaced WHERE \"namespaced/id\" = :\"namespaced/id\";")
an-id (int 100)]


(is (= nil
(alia/execute *session* prep-write {:values {:namespaced/id an-id
:namespaced/text "inserted via namespaced named bindings"}})))

(is (= [{:namespaced/id an-id
:namespaced/text "inserted via namespaced named bindings"}]
(alia/execute *session* prep-read {:values {:namespaced/id an-id}})))

(is (= [{:namespaced/id an-id
:namespaced/text "inserted via namespaced named bindings"}]
(alia/execute *session* "SELECT * FROM namespaced WHERE \"namespaced/id\" = :\"namespaced/id\";"
{:values {:namespaced/id an-id}})))))

(testing "parameterized set"
(let [;; s-parameterized-set (prepare "select * from users where emails=?;")
]))
Expand Down