What they show
(ns cassaforte.docs.examples
(:require [clojurewerkz.cassaforte.client :as cc]))
;; Will connect to localhost
(cc/connect ["127.0.0.1"])
(def cconnection (atom nil))
(def current-keyspace (atom nil))
(defn rebuild-connection []
(let [{:keys [seed-list cassandra-user cassandra-pass]} (conf/get-cassandra-conf)]
(reset! cconnection
(apply cc/connect
(if cassandra-user
[seed-list {:credentials
{:username cassandra-user :password cassandra-pass}}]
[seed-list])))
(reset! current-keyspace nil)))
(defn get-connection
"Gets existing connection or creates a new one."
[& [optional-keyspace & _]]
(when-not @cconnection
(locking cconnection
(when-not @cconnection
(rebuild-connection))))
(when optional-keyspace
(when (not= @current-keyspace optional-keyspace)
(cql/use-keyspace @cconnection :my_data)))
@cconnection)
What to do with state? This works but it's ugly
Way better ways?
(:require '[clojurewerkz.cassaforte.client :as cc]
(:require '[clojurewerkz.cassaforte.cql :as cql])
(def conn (cc/connect ["127.0.0.1"])
(cql/insert conn "test_data" {:data_id (int 42) :data_item "The Ultimate Answer"}))
(cql/insert conn "test_data" {:data_id (int 42) :data_item "The Ultimate Answer"}))
(use '[clojurewerkz.cassaforte.query])
(cql/select conn "test_data" (where [[= :data_id (int 42)]]))