<commit_before>(ns clojurewerkz.cassaforte.cql
  (:import [com.datastax.driver.core Session])
  (:require
   [qbits.hayt.cql :as cql]
   [clojurewerkz.cassaforte.query :as query]
   [clojurewerkz.cassaforte.client :as client]
   [clojurewerkz.cassaforte.debug-utils :as debug-utils]))

(def ^:dynamic *debug* false)

(defmacro with-debug
  "Executes query with debug output"
  [& body]
  `(binding [*debug* true]
     (debug-utils/catch-exceptions ~@body)))

;; connect! can be replaced by client/set-session!

;; Ability to turn on and off prepared statements by default? Turn on prepared statements on per-query basis
;; (macro+binding)

(defmacro prepared
  "Helper macro to execute prepared statement"
  [& body]
  `(binding [cql/*prepared-statement* true
             cql/*param-stack*        (atom [])]
     (do ~@body)))

(defn render-query
  [query-params]
  (let [renderer (if cql/*prepared-statement* query/->prepared query/->raw)]
    (renderer query-params)))

(defn compile-query
  [query-params builder]
  (apply builder (flatten query-params)))

(defn execute
  "Executes built query"
  ([query]
     (execute client/*default-session* query))
  ([session query]
     (client/with-session session
       (into []  ;; alia prefers not to return vectors
             (if cql/*prepared-statement*
               (client/execute (client/prepare (first query))
                               :values (second query))
               (client/execute query))))))

(defn ^:private execute-
  [query-params builder]
  (let [rendered-query (render-query (compile-query query-params builder))]
    (when *debug*
      (debug-utils/output-debug rendered-query))
    (execute rendered-query)))

;;
;; Schema operations
;;

(defn drop-keyspace
  [ks]
  (execute- [ks] query/drop-keyspace-query))

(defn create-keyspace
  [& query-params]
  (execute- query-params query/create-keyspace-query))

(defn create-table
  [& query-params]
  (execute- query-params query/create-table-query))

(def create-column-family create-table)

(defn drop-table<commit_msg>Preparing for async operations<commit_after>(ns clojurewerkz.cassaforte.cql
  (:import [com.datastax.driver.core Session Query
            ResultSet ResultSetFuture]
           [com.google.common.util.concurrent Futures FutureCallback])
  (:require
   [qbits.hayt.cql :as cql]
   [qbits.alia.codec :as codec]
   [clojurewerkz.cassaforte.query :as query]
   [clojurewerkz.cassaforte.client :as client]
   [clojurewerkz.cassaforte.debug-utils :as debug-utils]))

(def ^:dynamic *debug* false)
(def ^:dynamic *async* false)

(defmacro with-debug
  "Executes query with debug output"
  [& body]
  `(binding [*debug* true]
     (debug-utils/catch-exceptions ~@body)))

(defmacro async
  "Executes query with debug output"
  [& body]
  `(binding [*async* true]
     ~@body))

;; connect! can be replaced by client/set-session!

;; Ability to turn on and off prepared statements by default? Turn on prepared statements on per-query basis
;; (macro+binding)

(defmacro prepared
  "Helper macro to execute prepared statement"
  [& body]
  `(binding [cql/*prepared-statement* true
             cql/*param-stack*        (atom [])]
     (do ~@body)))

(defn render-query
  [query-params]
  (let [renderer (if cql/*prepared-statement* query/->prepared query/->raw)]
    (renderer query-params)))

(defn compile-query
  [query-params builder]
  (apply builder (flatten query-params)))

(defn execute
  "Executes built query"
  ([query]
     (execute client/*default-session* query))
  ([session query & {:keys [success failure]}]
     (client/with-session session
       (let [^Query statement (if cql/*prepared-statement*
                                (client/query->statement (client/prepare (first query))
                                                         (second query))
                                (client/query->statement query nil))
             ^ResultSetFuture future (.executeAsync session statement)]
         (into []  ;; alia prefers not to return vectors
               (if *async*
                 (Futures/addCallback
                  future
                  (reify FutureCallback
                    (onSuccess [_ result]
                      (success (codec/result-set->maps (.get future) true)))
                    (onFailure [_ result]
                      (failure result))))
                 (codec/result-set->maps  (.getUninterruptibly future) true)))))))

(defn ^:private execute-
  [query-params builder]
  (let [rendered-query (render-query (compile-query query-params builder))]
    (when *debug*
      (debug-utils/output-debug rendered-query))
    (execute rendered-query)))

;;
;; Schema operations
;;

(defn drop-keyspace
  [ks]
  (execute- [ks] query/drop-keyspace-query))

(defn create-keyspace
  [& query-params]
  (execute- query-params query/create-keyspace-query))

(defn create-table
  [& query-params]
  (execute- query-params query/create-table-query))

(def create-column-family create-table)

(defn drop-table
{'fraction_non_alphanumeric': 0.13059105431309903, 'fraction_numerical': 0.0, 'mean_word_length': 2.835375191424196, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1550672', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1533, 'n_words': 393}