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
(sequel/filter (and (< :table1/a 5) (= :table1/b "x")) | |
(sequel/collect :table1)) |
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
(defn collect* | |
[table & xs] | |
(if (vector? (last xs)) | |
(let [tbls (interpose ", " (map #(sqlize %) (conj (butlast xs) table))) | |
cols (str-utils/str-join ", " (map #(sqlize %) (last xs)))] | |
(apply str "select " cols " from " tbls)) | |
(let [tbls (interpose ", " (map #(sqlize %) (conj xs table)))] | |
(apply str "select * from " tbls)))) | |
(defmacro collect |
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
sequel> (collect :projects) | |
"select * from projects" | |
sequel> (collect :projects :invoices) | |
"select * from projects, invoices" | |
sequel> (collect :projects :invoices [:projects/id :projects/name (as :inv_id :invoices/id) :invoices/date]) | |
"select projects.id, projects.name, invoices.id AS inv_id, invoices.date from projects, invoices" |
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
(defmacro filter | |
[pred query] | |
`(let [~'and (fn[& xs#] (apply str (interpose " AND " xs#))) | |
~'or (fn[& xs#] (apply str (interpose " OR " xs#))) | |
~'= (fn[x# y#] (sql-exp "=" x# y#)) | |
~'> (fn[x# y#] (sql-exp ">" x# y#)) | |
~'< (fn[x# y#] (sql-exp "<" x# y#)) | |
~'like (fn[x# y#] (sql-exp "like" x# y#)) | |
~'in (fn[x# xs#] | |
(str (sqlize x#) " IN (" (apply str (interpose ", " xs#)) ")"))] |
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
sequel> (filter (> :projects/id 5) (collect :projects)) | |
"select * from projects where projects.id > 5" | |
sequel> (filter (and (> :projects/id 2) (= :projects/name "sequel")) (collect :projects)) | |
"select * from projects where projects.id > 2 AND projects.name = 'sequel'" | |
sequel> (filter (and (in :projects/id [1 2 3 4 5 6 7 8 9 10]) (= :projects/name "sequel")) (collect :projects)) | |
"select * from projects where projects.id IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) AND projects.name = 'sequel'" | |
sequel> (filter (and (in :projects/id [1 2 3 4 5 6 7 8 9 10]) (like :projects/name "seq%")) (collect :projects)) | |
"select * from projects where projects.id IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) AND projects.name like 'seq%'" | |
sequel> (filter (or (and (> :project/id 5) (= :project/name "sequel")) (= :project/id 15)) (collect :projects)) | |
"select * from projects where project.id > 5 AND project.name = 'sequel' OR project.id = 15" |
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 net.groppe.sequel.core | |
(:require [clojure.contrib.str-utils :as str-utils])) | |
(defn- sqlize | |
[x] | |
(cond | |
(keyword? x) (str-utils/re-sub #"\/" "." (str-utils/re-sub #"^:" "" (str x))) | |
(string? x) (str "'" x "'") | |
:else x)) |
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
"select * from products where id > 5" |
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
"select * from products where id > ?" |
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
(defn- stmt | |
[& stmts] | |
(if (vector? (last stmts)) | |
{:stmt (reduce #(str %1 %2) "" (butlast stmts)) :vars (last stmts)} | |
{:stmt (reduce #(str %1 %2) "" stmts) :vars []})) |
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
(defn- paramatize | |
[x] | |
(cond | |
(keyword? x) (stmt (sqlize x)) | |
:else (stmt "?" [x]))) |