summaryrefslogtreecommitdiff
path: root/src/clojure_sql/core.clj
blob: 67b2c4d2fb80860b97b2d2a0e967c1404d371356 (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
65
66
67
(ns clojure-sql.core
  (:refer-clojure :exclude [sort take drop])
  (:require [clojure.set :as set]
            [clojure-sql.compiler :as c]
            [clojure-sql.dsl :as d]
            [clojure-sql.query :as q]
            [clojure-sql.util :as u]
            [clojure.walk]))

(defmacro ^:private pull [ns & vlist]
  `(do ~@(for [i vlist
               :let [sym (symbol (name ns) (name i))]] 
           `(def ~(with-meta i
                    (u/map-vals (fn [x] `(quote ~x)) (meta (resolve sym))))
              ~sym))
       nil))

(pull clojure-sql.dsl
      table join
      project rename distinct
      prefix-names-matching prefix-names as-subobject
      select
      group
      sort take drop
      union intersection
      set-default-query-executor!)

(defmethod print-method clojure_sql.query.Query [query writer]
  (binding [*out* writer]
    (pr (c/compile-select nil query))))


(defn run-query
  "Run a select query. Return value is determined by query executor."
  [query]
  (assert (:executor query) "Cannot execute a query without a query executor")
  (q/query (:executor query) query))

(defn insert!
  "Insert a number of records into a table, setting each column to the
  corresponding value from the record. Return value is determined by
  query executor."
  [query & records]
  (assert (:executor query) "Cannot execute a query without a query executor")
  (q/insert! (:executor query) query records))

(defn update!
  "Update everything which would have been selected by the query,
  setting each field in the query to the corresponding column in the
  table. Return value is determined by query executor.

  NOTE: if the map does not have a given key then this will result in
  a NULL being included in the query. To avoid this restrict the query
  with `project` before calling `update!`."

  [query partial-record]
  (assert (:executor query) "Cannot execute a query without a query executor")
  (q/update! (:executor query) query partial-record))

(defn delete!
  "Delete everything which would have been selected by the
  query. Return value is determined by query executor."
  [query]
  (assert (:executor query) "Cannot execute a query without a query executor")
  (q/delete! (:executor query) query))