summaryrefslogtreecommitdiff
path: root/src/utils.clj
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.clj')
-rwxr-xr-xsrc/utils.clj85
1 files changed, 70 insertions, 15 deletions
diff --git a/src/utils.clj b/src/utils.clj
index 44131df..6d0a4ed 100755
--- a/src/utils.clj
+++ b/src/utils.clj
@@ -11,6 +11,7 @@
org.antlr.stringtemplate.StringTemplateGroup)
(:use clojure.contrib.json.write
clojure.contrib.sql
+ clojure.contrib.def
clojure.contrib.duck-streams
clojure.contrib.str-utils
compojure
@@ -34,6 +35,9 @@
;; Misc
+(defn except! [& more]
+ (throw (Exception. (apply str more))))
+
(defn download-http-url [u]
(let [url (URL. u)]
(if (= (.getProtocol url) "http")
@@ -45,8 +49,11 @@
ip (if ip ip (:remote-addr request))] (str ip)) ; deployed locally
)
-(defn append [& vecs]
- (reduce into vecs))
+(defn append [& seqs]
+ (reduce into (map vector seqs)))
+
+(defn transpose [lsts]
+ (apply (partial map vector) lsts))
(declare stringify-and-escape)
(defn escape-html-deep [o]
@@ -114,24 +121,21 @@
;; Formatters
-(defn- comma-helper [s]
- (let [f (take 3 s)
- r (drop 3 s)]
- (if (= (count r) 0)
- f
- (append (comma-helper r) [\,] (reverse f)))))
(defn comma-format [i]
- (apply str
- (reverse (comma-helper (reverse (str i))))))
-
+ (.format (java.text.DecimalFormat. "#,###") i))
+
+(def yyyy-formatter (new SimpleDateFormat "yyyy"))
+(def yyyy-mm-formatter (new SimpleDateFormat "yyyy-MM"))
(def yyyy-mm-dd-formatter (new SimpleDateFormat "yyyy-MM-dd"))
+(def yymmdd-formatter (new SimpleDateFormat "yyyyMMdd"))
+
+(doseq [f [yyyy-formatter yyyy-mm-formatter yyyy-mm-dd-formatter yymmdd-formatter]]
+ (.setLenient f false))
(defn format-yyyy-mm-dd [d]
(.format yyyy-mm-dd-formatter d))
-(def yymmdd-formatter (new SimpleDateFormat "yyyyMMdd"))
-
(defn format-yyyymmdd [d]
(.format yymmdd-formatter d))
@@ -159,7 +163,7 @@
labels ["year" "month" "day" "hour" "minute"]
arr (into [] (for [[l v] (map vector labels vals) :when (> v 0)]
(str v " " (pluralize l v))))]
- (join arr ", ")))
+ (str-join ", " arr)))
(defn apply-formats [formats d]
(into {} (for [[k v] d]
@@ -188,6 +192,44 @@
;; Database
+(defn to-sql-date [dt]
+ (java.sql.Date. (.getTime dt)))
+
+(defn join-clauses [clauses]
+ (let [clause-func (fn [c]
+ (cond (string? c) [c []]
+ (vector? c) [(first c) (rest c)]
+ :else (except! "Invalid query-clause: " c)))
+ pairs (for [c clauses :when c]
+ (clause-func c))
+ [clauses vars] (transpose pairs)]
+ [clauses
+ (apply concat vars)]))
+
+(defnk build-query [:select nil :from nil :where nil
+ :ljoin nil
+ :order nil :limit nil :indent " "]
+ (cond
+ (not select) (except! "Invalid query missing SELECT")
+ (not from) (except! "Invalid query missing FROM")
+ (not where) (except! "Invalid query missing WHERE")
+ :else (let [[sel-cls sel-var] (join-clauses select)
+ [from-cls from-var] (join-clauses from)
+ [where-cls where-var] (join-clauses where)]
+ (vec
+ (concat
+ [(str "SELECT\n" indent
+ (str-join (str ",\n" indent) sel-cls)
+ "\nFROM\n" indent
+ (str-join (str ",\n" indent) from-cls)
+ (if ljoin
+ (str "\nLEFT JOIN " ljoin) "")
+ "\nWHERE\n" indent
+ (str-join (str " AND\n" indent) where-cls)
+ (if order (str "\nORDER BY " order) "")
+ (if limit (str "\nLIMIT " limit) ""))]
+ sel-var from-var where-var)))))
+
(defn do-cmds [query]
(with-connection *db*
(do-commands query)))
@@ -232,7 +274,7 @@
(let [stmt (.prepareStatement (connection) query)]
(doseq [[i o] (map vector (iterate inc 1) objects)]
(.setObject stmt i o))
- (println "update: " (.executeQuery stmt)))))
+ (.executeQuery stmt))))
;; Parsing
@@ -246,6 +288,19 @@
(defn maybe-parse-long [s f]
(if s (Long/parseLong s) f))
+(defn parse-yyyy-mm-dd-date [s]
+ (try (.parse yyyy-mm-dd-formatter s)
+ (catch java.text.ParseException _ nil)))
+
+(defn parse-flexi-date
+ "Accepts date strings as YYYY, YYYY-MM, or YYYY-MM-DD."
+ [s]
+ (let [parse-f (fn [f l] (try [(.parse f s) l]
+ (catch java.text.ParseException _ nil)))]
+ (or (parse-f yyyy-mm-dd-formatter :day)
+ (parse-f yyyy-mm-formatter :month)
+ (parse-f yyyy-formatter :year))))
+
(defn url-decode [text]
(URLDecoder/decode text "UTF-8"))