Last active
April 14, 2016 12:37
-
-
Save danielneal/fd9a1e52fa223a5d859aae01360c094b to your computer and use it in GitHub Desktop.
Clojure-SQL relational algebra / query composition
This file contains hidden or 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 clojure-sql-demo | |
(:require [clojure-sql.core :as sql]) ;;[clojure-sql "0.2.0-SNAPSHOT"] | |
(def user-emails | |
(-> (sql/table :user_email_history) | |
(sql/project {:email :user_email_history.email | |
:user_id :user_email_history.user_id | |
:email_request_id :user_email_history.email_request_id}))) | |
(def users | |
(-> (sql/table :users) | |
(sql/project {:user_id :users.user_id | |
:email_request_id :users.email_request_id}))) | |
(def users-with-email | |
(-> users | |
(sql/join user-emails :on `(and (= :user_email_history.email_request_id :users.email_request_id) | |
(= :user_email_history.user_id :users.user_id))) | |
(sql/project {:users.user_id :user_id | |
:user_email_history.email :email}))) | |
(defn users-by-email [email] | |
(-> users-with-email | |
(sql/select `(= :email ~email)))) | |
;; (users-by-email "[email protected]") => | |
;; | |
;; ["SELECT \"table20393\".\"email\" AS \"email\", | |
;; \"users20330\".\"user_id\" AS \"user_id\" | |
;; FROM (\"users\" AS \"users20330\" INNER JOIN | |
;; \"user_email_history\" AS \"table20393\" | |
;; ON ((\"table20393\".\"email_request_id\" = \"users20330\".\"email_request_id\") | |
;; AND (\"table20393\".\"user_id\" = \"users20330\".\"user_id\"))) | |
;; WHERE (\"table20393\".\"email\" = ?)" | |
;; | |
;; "[email protected]"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment