         [:td (escape-html (:message x))]])
      ]]))
 
+(defn update-and-persist
+  "Using Korma: Changes the :randomNumber of a number of world entities.
+  Persists the changes to sql then returns the updated entities"
+  [queries]
+  (let [results (map #(assoc % :randomNumber (inc (rand-int 9999))) (run-queries queries))]
+    (doseq [{:keys [id randomNumber]} results]
+      (update world
+              (set-fields {:randomNumber randomNumber})
+              (where {:id id})))
+    results))
+
+(defn json-serialization
+  "Test 1: JSON serialization"
+  [request]
+  (bootstrap/json-response {:message "Hello, World!"}))
+
+(defn single-query-test
+  "Test 2: Single database query"
+  [request]
+  (bootstrap/json-response (first (run-queries 1))))
+
+(defn multiple-queries-test
+  "Test 3: Multiple database queries"
+  [request]
+  (-> request
+      (sanitize-queries-param)
+      (run-queries)
+      (bootstrap/json-response)))
 
 (defn fortune-test
   "Test 4: Fortunes"

(defentity world
  (pk :id)
  (table :world)
  (entity-fields :id :randomNumber) ;; Default fields for select
  (database mysql-db))


(defn random-world
  "Query a random World record from the database"
  []
  (let [id (inc (rand-int 9999))] ; Num between 1 and 10,000
    (select world
            (where {:id id }))))


(defn run-queries
  "Run query repeatedly -- Always returns an array"
  [queries]
  (flatten (take queries (repeatedly random-world))))


(defn single-query-test
  "Test 2: Single database query"
  [request]
  (bootstrap/json-response (first (run-queries 1))))


(defn sanitizeQueriesParam
  "Sanitizes the `queries` parameter. Caps the value between 1 and 500.
  Invalid (stringy) values become 1"
  [request]
  (let [queries (-> request
                    :params
                    :queries)]
    (if-let [n (if (= (re-find #"\A-?\d+" queries) nil)
                   1
                   (Integer/parseInt queries))]
      (cond
        (< n 1) 1
        (> n 500) 500
        :else n))))


(defn multiple-query-test
  "Test 3: Multiple database queries"
  [request]
  (-> request
      (sanitizeQueriesParam)
      (run-queries)
      (bootstrap/json-response)))


; Set up entity Fortune and the database representation
(defentity fortune
  (pk :id)
  (table :fortune)
  (entity-fields :id :message)
  (database mysql-db))


(defn get-all-fortunes
  "Query all Fortune records from the database."
  []
  (select fortune
          (fields :id :message)))

            [clojure.data.json :as json]
            [clojure.java.jdbc :as jdbc]))

(defn sanitize-queries-param
  "Sanitizes the `queries` parameter. Clamps the value between 1 and 500.
  Invalid (string) values become 1."
  [request]
  (let [queries (-> request
                    :params
                    :queries)
        n (try (Integer/parseInt queries)
               (catch Exception e 1))] ; default to 1 on parse failure
    (cond
      (< n 1) 1
      (> n 500) 500
      :else n)))

;; MySQL database connection
(defdb db-mysql
  (mysql {
    :classname "com.mysql.jdbc.Driver"
    :subprotocol "mysql"
    :subname "//127.0.0.1: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"
    :user "benchmarkdbuser"
    :password "benchmarkdbpass"
    ;;OPTIONAL KEYS
