<commit_before>(ns clojurewerkz.cassaforte.new-query-api
  "Functions for building dynamic CQL queries, in case you feel
   that `cql` namespace is too limiting for you."
  (:import [com.datastax.driver.core.querybuilder QueryBuilder
            Select$Selection Select Select$Where

            Clause]
           [com.datastax.driver.core.querybuilder ]))


(def select-command-order
  [QueryBuilder Select$Selection Select Select$Where])

(defn from
  [^String table-name]
  [2 (fn from-query [^Select$Selection query-builder]
       (.from query-builder (name table-name))
       )])

(defn all
  []
  [1 (fn all-query [^Select$Selection query-builder]
       (.all query-builder))])

(defn column
  [column]
  [1 (fn column-query [^Select$Selection query-builder]
       (.column query-builder column))])

(defn columns
  [columns]
  [1 (fn [^Select$Selection query-builder]
       (reduce (fn [^Select$Selection builder column]
                 (.column builder column))
               query-builder
               columns))])

;; Clause eq(String name, Object value)
(defn- ^Clause eq
  [^String column ^Object value]
  (QueryBuilder/eq column value))

(defn- ^Clause in
  [^String column ^java.util.List values]
  (QueryBuilder/in column values))

(defn- ^Clause lt
  [^String column ^Object value]
  (QueryBuilder/lt column value))

(defn- ^Clause gt
  [^String column ^Object value]
  (QueryBuilder/gt column value))

(defn- ^Clause lte
  [^String column ^Object value]
  (QueryBuilder/lte column value))

(defn- ^Clause gte
  [^String column ^Object value]
  (QueryBuilder/gte column value))

(def ^:private query-type-map
  {:in in
   :=  eq
   =   eq
   :>  gt
   >   gt
   :>= gte
   :<  lt
   <   lt
   :<= lte})

(defprotocol WhereBuilder
  (build-where [construct query-builder]))

(extend-protocol WhereBuilder
  clojure.lang.IPersistentVector
  (build-where [construct ^Select$Where query-builder]
    (reduce
     (fn [^Select$Where builder [query-type column value]]
       (.and builder ((query-type-map query-type) (name column) value)))
     query-builder
     construct))
  clojure.lang.IPersistentMap
  (build-where [construct ^Select$Where query-builder]
    (reduce
     (fn [^Select$Where builder [column value]]
       (.and builder (eq (name column) value)))
     query-builder
     construct)))

(defn where
  [m]
  [3 (fn where-query [^Select query-builder]
       (build-where m (.where query-builder)))])

(defn asc
  [^String column-name]
  (QueryBuilder/asc column-name))

(defn desc
  [^String column-name]
  (QueryBuilder/desc column-name))

(defn order-by
  [& orderings]
  [3 (fn order-by-query [^Select query-builder]
       (.orderBy query-builder orderings))])

(defn limit
  [m]
  [3 (fn order-by-query [^Select  query-builder]
       )])

(defn allow-filtering
  [m]
  [3 (fn order-by-query [^Select  query-builder]
       )])
   ;; public Select orderBy(Ordering... orderings) {
   ;;  public Select limit(int limit) {
   ;;  public Select limit(BindMarker marker) {
   ;;  public Select allowFiltering() {

(defn select
  [table-name & statements]
  (->> (conj statements (from (name table-name)))
       (sort-by first)
       ;; (map println)
       (map second)
       (reduce (fn [builder statement]
                 ;; (println builder statement)
                 (statement builder))
               (QueryBuilder/select)
               )
       (.toString)<commit_msg>Implement ordering, limit and filtering<commit_after>(ns clojurewerkz.cassaforte.new-query-api
  "Functions for building dynamic CQL queries, in case you feel
   that `cql` namespace is too limiting for you."
  (:import [com.datastax.driver.core.querybuilder QueryBuilder
            Select$Selection Select Select$Where

            Clause]
           [com.datastax.driver.core.querybuilder ]))


(def select-command-order
  [QueryBuilder Select$Selection Select Select$Where])

(defn from
  [^String table-name]
  [2 (fn from-query [^Select$Selection query-builder]
       (.from query-builder (name table-name))
       )])

(defn all
  []
  [1 (fn all-query [^Select$Selection query-builder]
       (.all query-builder))])

(defn column
  [column]
  [1 (fn column-query [^Select$Selection query-builder]
       (.column query-builder column))])

(defn columns
  [columns]
  [1 (fn [^Select$Selection query-builder]
       (reduce (fn [^Select$Selection builder column]
                 (.column builder column))
               query-builder
               columns))])

;; Clause eq(String name, Object value)
(defn- ^Clause eq
  [^String column ^Object value]
  (QueryBuilder/eq column value))

(defn- ^Clause in
  [^String column ^java.util.List values]
  (QueryBuilder/in column values))

(defn- ^Clause lt
  [^String column ^Object value]
  (QueryBuilder/lt column value))

(defn- ^Clause gt
  [^String column ^Object value]
  (QueryBuilder/gt column value))

(defn- ^Clause lte
  [^String column ^Object value]
  (QueryBuilder/lte column value))

(defn- ^Clause gte
  [^String column ^Object value]
  (QueryBuilder/gte column value))

(def ^:private query-type-map
  {:in in
   :=  eq
   =   eq
   :>  gt
   >   gt
   :>= gte
   :<  lt
   <   lt
   :<= lte})

(defprotocol WhereBuilder
  (build-where [construct query-builder]))

(extend-protocol WhereBuilder
  clojure.lang.IPersistentVector
  (build-where [construct ^Select$Where query-builder]
    (reduce
     (fn [^Select$Where builder [query-type column value]]
       (.and builder ((query-type-map query-type) (name column) value)))
     query-builder
     construct))
  clojure.lang.IPersistentMap
  (build-where [construct ^Select$Where query-builder]
    (reduce
     (fn [^Select$Where builder [column value]]
       (.and builder (eq (name column) value)))
     query-builder
     construct)))

(defn where
  [m]
  [3 (fn where-query [^Select query-builder]
       (build-where m (.where query-builder)))])

(defn asc
  [^String column-name]
  (QueryBuilder/asc (name column-name)))

(defn desc
  [^String column-name]
  (QueryBuilder/desc column-name))

(defn order-by
  [orderings]
  [2 (fn order-by-query [^Select query-builder]
       (.orderBy query-builder (into-array orderings)))])

(defn limit
  [lim]
  [2 (fn order-by-query [^Select  query-builder]
       (.limit query-builder (int lim)))])

(defn allow-filtering
  []
  [2 (fn order-by-query [^Select  query-builder]
       (.allowFiltering query-builder))])
   ;; public Select orderBy(Ordering... orderings) {
   ;;  public Select limit(int limit) {
   ;;  public Select limit(BindMarker marker) {
   ;;  public Select allowFiltering() {

(defn select
  [table-name & statements]
  (->> (conj statements (from (name table-name)))
       (sort-by first)
       ;; (map println)
       (map second)
       (reduce (fn [builder statement]
                 (println builder statement)
                 (statement builder))
               (QueryBuilder/select)
               )
       (.toString)
{'fraction_non_alphanumeric': 0.1310979997121888, 'fraction_numerical': 0.00230248956684415, 'mean_word_length': 2.891377379619261, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '411632', 'n_tokens_mistral': 2257, 'n_tokens_neox': 2182, 'n_words': 612}