Skip to content
Simple tool for forming SQL queries
Clojure
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
src/finder
test/finder/test
.gitignore
.travis.yml
README.md
project.clj

README.md

Build Status

Finder

Finder is a simple tool for creating query data for clojure.java.jdbc. There is Marginalia documentation available.

Usage

To use Finder just import the library and use it inside your query.

(ns my.namespace
  (require [clojure.java.jdbc :as sql]
           [finder.core :as f]))

(sql/with-connection cnn
  (sql/with-query-results res
    (f/by-id :users 1)))

Installation

You can install Finder with Leiningen and Clojars.

[sql-finder "x.x.x"]

See Clojars for latest version number.

And/Or

By default clauses are and'd together...

(f/where :users {:name "foo"
                 :email "blah"})

But you can specify or'd groups just by using a vector..

(f/where :users [{:last_name "bar"}
                 {:first_name "foo"}])

Multiple Values

You can also do SQL 'in' clauses using a set.

(f/where :users {:id #{1 2 3}}

Which will create id in (1 2 3) (the in operator isn't actually supported by JDBC prepared statements, so its appoximated by Finder just using a series of comparisons).

Finder tries to do what you probably mean when it comes to empty sets. So doing {:id #{}} will translate to (1 = 0) (ie. not matching anything, rather than it being ommitted and matching everything instead).

Order/Limit/Offset

The third argument to where allows you to specify extra options like ordering results, limiting, and offsetting.

(f/where :users {} {:order :name})
(f/where :users {} {:order {:name asc :email :desc}
                    :limit 10})
(f/where :emails {:user_id 1}
                 {:limit 20
                  :offset 10})

Custom Comparators

By default field values are matched with =, but you can specify another operator if you like using a vector.

(f/where :users {:age ['< 50]})

Function API

(f/where TABLE PARAM_MAP OPTS)
(f/by TABLE FIELD ID OPTS)
(f/by-id TABLE ID)
(f/all TABLE OPTS)

Unit Tests

Unit tests can be run with Midje (lein-midje plugin required)

lein midje
Something went wrong with that request. Please try again.