summaryrefslogtreecommitdiff
path: root/README.md
blob: 6e82200c402e78fc62ca0b68792b3503d5adaa9c (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# clojure-sql

A DSL for [Clojure][1] to compose queries using the constructs of
[Relational Algebra][2]. The RA constructs are then compiled into an
SQL query to be run on a DBMS. `clojure-sql` doesn't connect to a
database itself, but rather can be configured to perform an action
when a query is to be executed.

`clojure-sql` provides some utility functions beyond strict RA to
allow for data to be inserted, updated and deleted.

`clojure-sql` provides no mechanism to create database schemas.

[1]: http://clojure.org/
[2]: http://en.wikipedia.org/wiki/Relational_Algebra

## Database support

`clojure-sql` aims to have an extensible compiler. Compilation of
queries is performed by multimethods which dispatch on an arbitrary
(and otherwise unused) `db` parameter. This allows the compilation of
queries to be entirely special-cased per database. By default
`clojure-sql` will produce SQL for PostgreSQL.

## Usage

    (require '[clojure-sql.core :as s])

    (-> (s/table :users) ;; tables are automatically given internal aliases
        (s/project [:id :username]))
    ; => ["SELECT \"users1234\".\"id\", \"users1234\".\"username\" FROM \"users\" AS \"users1234\""]

    (-> (s/table :users)
        (s/project [:id :username])
        (s/rename {:id :uid})
        (s/join (-> (s/table :people)
                    (s/project {:id :pid, :fname :first})
                    (s/select '(= :first "Henry")))
                '(= :uid :pid))
        (s/project [:username]))
    ; => ["SELECT \"users1234\".\"username\" FROM \"users\" AS \"users1234\" JOIN \"people\" AS \"people1234\" ON (\"users1234\".\"id\" = \"people1234\".\"id\") WHERE (\"people1234\".\"fname\" = ?)" "Henry")]



[clojure.java.jdbc][3] support is provided, but must be included explicitly.

    (require '[clojure-sql.jdbc :as jdbc])

    (jdbc/use-jdbc! "postgres://user:pass@localhost:5432/db")
    ; => nil

    (deref (-> (s/table :users)
               (s/project [:id :username])))
    ; => [{:id 5, :username "mange"}]

Results are returned from queries in an eager vector.

[3]: https://github.com/clojure/java.jdbc

## License

Copyright © 2013 Carlo Zancanaro

Distributed under the Eclipse Public License, the same as Clojure.