Skip to content

Instantly share code, notes, and snippets.

@sw1nn
Created May 7, 2014 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sw1nn/6193e97b421e8d317e72 to your computer and use it in GitHub Desktop.
Save sw1nn/6193e97b421e8d317e72 to your computer and use it in GitHub Desktop.
(require '[qbits.hayt :refer :all])
(require '[clj-time.core :refer (date-time)])
;; Assume this column family definition
;;
;; create table meta_data (
;; "a-b" text,
;; "b-c" timestamp,
;; primary key ("a-b"))
;;
;; create index on meta_data("a-b")
(->raw (select :meta-data (columns :a-b :b-c) (where [[= :a-b "foo"]])))
;; Invalid CQL:
;;=> SELECT a-b, b-c FROM meta-data WHERE a-b = 'foo';
;;
;; + meta-data can't be a column family name
;; + '-' needs to be quoted in column names
(require '[qbits.hayt.cql :refer (CQLEntities dquote-string)])
;; Can we tweak keyword handling? i.e. copy String handling.
(extend-protocol CQLEntities
clojure.lang.Keyword
(cql-identifier [x] (dquote-string (name x)))
(cql-value [x] (dquote-string (str x))))
(->raw (select :meta_data (columns :a-b :b-c) (where [[= :a-b "foo"]])))
;; Seems valid.
;;=> SELECT "a-b", "b-c" FROM "meta_data" WHERE "a-b" = 'foo';
;; joda-time codec
(->raw (select :meta_data (columns :a-b :b-c) (where [[= :b-c (date-time 2014 5 5)]])))
;; Seems valid
;;=> SELECT "a-b", "b-c" FROM "meta_data" WHERE "b-c" = 1399248000000;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment