Created
December 26, 2011 14:07
-
-
Save jramb/1521214 to your computer and use it in GitHub Desktop.
Playing with Clojure Korma SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns kormatest.core | |
(:require [korma.core :as sql]) | |
(:require [korma.db :as db]) | |
(:require [clojure.contrib.sql :as jdbc])) | |
(def play-con | |
(db/mysql {:db "playground" | |
;; :host "localhost" ;; optional? | |
:port "3406" ;; normal: 3306 | |
:user "root" | |
:password "passwrd"})) | |
(db/defdb play play-con) | |
;; einfacher clojure.contrib.sql test | |
(jdbc/with-connection play-con ;;play ;;(db/get-connection @db/_default) | |
(jdbc/with-query-results rs | |
["select count(*) from t1"] | |
(doall rs))) | |
sql/select | |
(sql/defentity t1 | |
(sql/table :t1) | |
(sql/entity-fields :name :age :birth) | |
(sql/database play)) | |
(sql/select* tt) | |
(sql/defentity t1 | |
#_(sql/table :t1)) | |
(sql/sql-only (sql/select* tt | |
(sql/limit 1))) | |
(sql/sql-only) | |
(sql/select | |
t1 | |
(sql/fields [ "count(*)" :cnt2]) | |
(sql/aggregate (sum :age) :cnt) | |
#_(sql/where {:name [like "J%rg"]})) | |
(defmacro transaction [& body] | |
`(jdbc/with-connection (db/get-connection @db/_default) | |
(jdbc/transaction | |
~@body))) | |
(def x (sql/where {:name [like "J%rg"]})) | |
(defn count-rows [tab] | |
(:cnt (first | |
(sql/select tab | |
(sql/aggregate (count :*) :cnt))))) | |
; Transaktion funktioniert nicht in diesem Fall, keine Ahnung warum... | |
; Während des Updates (dauert ca. 16 Sekunden) kann man in einer separaten | |
; Session per count(*) sehen, wie die Anzahl nach oben zählt -> transaktion | |
; ist schon vor dem Commit sichtbar... :-( | |
(time (do | |
(transaction | |
(dorun (map #(sql/insert | |
t1 | |
(sql/values [{:name (str "Bertil" %) :age % :birth "1923-12-12"}])) | |
(range 2000)))) | |
(count-rows t1))) | |
(sql/delete | |
t1 | |
(sql/where {:name [like "Bertil%"]})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment