diff options
-rw-r--r-- | README.md | 27 |
1 files changed, 19 insertions, 8 deletions
@@ -17,6 +17,7 @@ Latest release: `0.1.0` ## Example Usage + :::clojure (require '[clojure-sql.core :as s]) (s/table :users) @@ -53,6 +54,7 @@ The primary operations available in `clojure-sql` are the following: * `select`ion: selecting rows which match a condition + :::clojure (s/select (s/table :users) `(= :username "mange")) The second argument is a `clojure-sql` expression. This is expressed @@ -64,22 +66,25 @@ The primary operations available in `clojure-sql` are the following: Some more examples of `clojure-sql` expressions and their "equivalent" SQL: - (let [user-id 5] `(= :user-id ~user-id)) - ;; => "(\"user-id\" = 5)" - `(and (= :user-id 5) (= :username "mange")) - ;; => "((\"user-id\" = 5) AND (\"username\" = 'mange'))" - `(or (= :user-id 10) (= :username "mange")) - ;; => "((\"user-id\" = 10) OR (\"username\" = 'mange'))" - `(and (= :name "Barry") (= (length :username) 10)) - ;; => "((\"name\" = 'Barry') AND (\"length\"(\"username\") = 10)) + :::clojure + (let [user-id 5] `(= :user-id ~user-id)) + ;; => "(\"user-id\" = 5)" + `(and (= :user-id 5) (= :username "mange")) + ;; => "((\"user-id\" = 5) AND (\"username\" = 'mange'))" + `(or (= :user-id 10) (= :username "mange")) + ;; => "((\"user-id\" = 10) OR (\"username\" = 'mange'))" + `(and (= :name "Barry") (= (length :username) 10)) + ;; => "((\"name\" = 'Barry') AND (\"length\"(\"username\") = 10)) * `project`ion: setting the fields which are exposed by the query + :::clojure (s/project (s/table :users) [:id :username]) (s/project (s/table :users) {:id :uid, :username :uname}) ;; project and rename in one operation * `rename`ing: giving an existing field (or fields) a new name + :::clojure (-> (s/table :users) (s/project [:id :username]) (s/rename {:id :uid})) @@ -88,6 +93,7 @@ The primary operations available in `clojure-sql` are the following: transformation. This can be particularly handy when combined with the helper functions `as-subobject` and `prefix-names`: + :::clojure (-> (s/table :users) (s/project [:id :username]) (s/rename (s/as-subobject :user))) @@ -97,10 +103,12 @@ The primary operations available in `clojure-sql` are the following: a specified key in the result map. As an example, the above query will result in a result map like: + :::clojure {:user {:id 5, :username "mange"}} * `join`ing: combine two queries into one query + :::clojure (s/join (-> (s/table :users) (s/project [:id :username :person-id])) (-> (s/table :people) @@ -111,6 +119,7 @@ The primary operations available in `clojure-sql` are the following: If a join between two queries has common fields then it is assumed to be a natural join (that is: an inner join on common fields). + :::clojure (s/join (-> (s/table :users) (s/project [:id :username :person,id])) (-> (s/table :people) @@ -120,6 +129,7 @@ The primary operations available in `clojure-sql` are the following: With joins the composability of `clojure-sql` becomes much more useful: + :::clojure (def users (-> (s/table :users) (s/project [:id :username :person]))) @@ -167,6 +177,7 @@ undertaken in future). [`clojure.java.jdbc`][4] support is provided, but must be included explicitly. `clojure-sql` does not depend on `clojure.java.jdbc`. + :::clojure (require '[clojure-sql.jdbc :as jdbc]) (jdbc/use-jdbc! "postgres://user:pass@localhost:5432/db") |