66 korma.db
77 korma.core
88 hiccup.core
9- hiccup.util)
9+ hiccup.util
10+ hiccup.page)
1011 (:require [compojure.handler :as handler]
1112 [compojure.route :as route]
1213 [clojure.java.jdbc :as jdbc]
13- [clojure.java.jdbc.sql :as sql]
14- [net.cgrand.enlive-html :as html]))
14+ [clojure.java.jdbc.sql :as sql]))
1515
1616; Database connection
1717(defdb db (mysql {:subname " //localhost:3306/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts&cacheRSMetadata=true"
3838
3939; Run the specified number of queries, return the results
4040(defn run-queries [queries]
41- (vec ; Return as a vector
4241 (flatten ; Make it a list of maps
4342 (take
4443 queries ; Number of queries to run
45- (repeatedly get-world)))))
44+ (repeatedly get-world))))
4645
4746; Database connection for java.jdbc "raw"
4847; https://github.com/clojure/java.jdbc/blob/master/doc/clojure/java/jdbc/ConnectionPooling.md
7473(defn get-world-raw []
7574 (let [id (inc (rand-int 9999 ))] ; Num between 1 and 10,000
7675 (jdbc/with-connection (db-raw )
77- (jdbc/with-query-results rs [(str " select * from world where id = ?" ) id]
78- (doall rs)))))
76+ ; Set a naming strategy to preserve column name case
77+ (jdbc/with-naming-strategy {:keyword identity}
78+ (jdbc/with-query-results rs [(str " select * from world where id = ?" ) id]
79+ (doall rs))))))
7980
8081; Run the specified number of queries, return the results
8182(defn run-queries-raw [queries]
82- (vec ; Return as a vector
8383 (flatten ; Make it a list of maps
8484 (take
8585 queries ; Number of queries to run
86- (repeatedly get-world-raw)))))
86+ (repeatedly get-world-raw))))
8787
8888(defn get-query-count [queries]
8989 " Parse provided string value of query count, clamping values to between 1 and 500."
9595 1 ; clamp to 1 min
9696 q)))) ; otherwise use provided value
9797
98-
9998; Set up entity World and the database representation
10099(defentity fortune
101100 (pk :id )
@@ -116,7 +115,7 @@ message text, and then return the results."
116115
117116(defn fortunes-hiccup [fortunes]
118117 " Render the given fortunes to simple HTML using Hiccup."
119- (html
118+ (html5
120119 [:head
121120 [:title " Fortunes" ]]
122121 [:body
@@ -142,7 +141,9 @@ message text, and then return the results."
142141 :headers {" Content-Type" " text/plain; charset=utf-8" }
143142 :body " Hello, World!" })
144143 (GET " /json" [] (response {:message " Hello, World!" }))
144+ (GET " /db" [] (response (first (run-queries 1 ))))
145145 (GET " /db/:queries" [queries] (response (run-queries (get-query-count queries))))
146+ (GET " /dbraw" [] (response (first (run-queries-raw 1 ))))
146147 (GET " /dbraw/:queries" [queries] (response (run-queries-raw (get-query-count queries))))
147148 (GET " /fortune" [] (response (get-fortunes )))
148149 (GET " /fortune-hiccup" [] (fortunes-hiccup (get-fortunes )))
0 commit comments