Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mauricioszabo/relational-clojure
Relational port for Clojure
https://github.com/mauricioszabo/relational-clojure
Last synced: about 3 hours ago
JSON representation
Relational port for Clojure
- Host: GitHub
- URL: https://github.com/mauricioszabo/relational-clojure
- Owner: mauricioszabo
- License: epl-1.0
- Created: 2016-04-18T02:36:50.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-04-23T17:36:40.000Z (over 7 years ago)
- Last Synced: 2024-11-09T05:31:48.438Z (about 2 months ago)
- Language: Clojure
- Size: 53.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Relational Clojure
An experiment to abstract SQL fragments into Clojure constructions.
## Usage
*WARNING - Not working yet!*
Most constructions are aliased in the `sql` namespace. You can run with:
```clojure
(use '[relational.sql]); SELECT "foo"."bar"
(sql/select (sql/attribute "foo" "bar")); WHERE "foo"."bar" = 'baz' OR "foo"."bar" = 'quox'
(sql/where
(sql/or
(sql/= (sql/attribute "foo" "bar") "baz")
(sql/= (sql/attribute "foo" "bar") "quox"))); NILs are supported - they are ignored
(sql/where nil) ; => ""
(sql/where
(sql/or
(sql/= (sql/attribute "foo" "bar") "baz")
nil)) ; => WHERE "foo"."bar" = 'baz'
```But it is tedious to do this all the time. So, we have a bunch of macros in `relational.core` to help:
```clojure
(use '[relational.core :refer :all]); SELECT "foo"."bar" FROM "foo" WHERE "foo"."bar" = 'baz'
(query
(select :foo.bar)
(from :foo)
(where (= :foo.bar "baz")))
```But, Relational constructions doesn't return strings. They return `IPartial`s, constructions that respond to `partial-fn`. These return a function that expect a single parameter - `db` - that are hashes used to configure a database connection.
When we run this function, it'll return a tuple (a vector with 2 positions) - a SQL string with placeholders `?`, and another vector with the parameters.
```clojure
(use '[relational.core :refer :all])(db-for mysql :adapter :mysql
:host "localhost"
:user "root")(db-for pg :adapter :postgresql
:host "localhost"
:user "postgres"); SELECT "foo"."bar" FROM "foo" WHERE "foo"."bar" = 'baz'
(def q (query
(select :foo.bar)
(from :foo)
(where (= :foo.bar "baz"))))(def p (partial-fn q))
; => ["SELECT `foo`.`bar` FROM `foo` WHERE `foo`.`bar` = ?" ["baz"]]
(p mysql); => ["SELECT \"foo\".\"bar\" FROM \"foo\" WHERE \"foo\".\"bar\" = ?" ["baz"]]
(p pg)
```## Compositions
TODO
## License
Copyright © 2016 Maurício Szabo
Distributed under the MIT License.