(refer-clojure :exclude '[distinct group-by update]) (require '[clojure.string :as s] '[sqlingvo.core :as c :refer [db sql select from where copy as group-by having insert intersect join refresh-materialized-view update returning truncate union values drop-table except ast delete explain drop-materialized-view]] '[sqlingvo.util :as util]) (def db (db :mysql))

Click here to make these examples interactive with ClojureScript.

sqlingvo.core

as

(as expr alias & [columns])

Parse expr and return an expr with and AS clause using alias.

asc

(asc expr)

Parse expr and return an ORDER BY expr using ascending order.

ast

(ast stmt)

Returns the abstract syntax tree of stmt.

cascade

(cascade condition)

Add a CASCADE clause to an SQL statement.

chain-state

(chain-state body)

check

(check expr)

Add a CHECK clause to an SQL statement.

column

(column name type & {:as options})

Add a column to stmt.

compose

(compose stmt & body)

Compose multiple SQL statements.

concurrently

(concurrently condition)

Add a CONCURRENTLY clause to a SQL statement.

continue-identity

(continue-identity condition)

Add a CONTINUE IDENTITY clause to an SQL statement.

copy

(copy db table columns & body)

Build a COPY statement.

Examples:

(copy db :country []
  (from :stdin))

Another example:

(copy db :country []
  (from "/usr1/proj/bray/sql/country_data"))

create-table

(create-table db table & body)

Build a CREATE TABLE statement.

db

(db spec & [opts])

Return a new database for spec.

db?

(db? x)

Return true if x is a database, otherwise false.

delete

(delete db table & body)

Build a DELETE statement.

Examples:

(delete db :continents)

Another example:

(delete db :continents
  (where '(= :id 1)))

delimiter

(delimiter delimiter)

Add a DELIMITER clause to an SQL statement.

desc

(desc expr)

Parse expr and return an ORDER BY expr using descending order.

distinct

(distinct exprs & {:keys [on]})

Parse exprs and return a DISTINCT clause.

do-constraint

(do-constraint constraint)

Add a DO CONSTRAINT clause to a SQL statement.

do-nothing

(do-nothing)

Add a DO NOTHING clause to a SQL statement.

do-update

(do-update expr)

Add a DO UPDATE clause to a SQL statement.

drop-materialized-view

(drop-materialized-view db view & body)

Build a DROP MATERIALIZED VIEW statement.

Examples:

(drop-materialized-view db :order-summary)

drop-table

(drop-table db tables & body)

Build a DROP TABLE statement.

Examples:

(drop-table db [:continents])

Another example:

(drop-table db [:continents :countries])

encoding

(encoding encoding)

Add a ENCODING clause to an SQL statement.

except

(except & args)

Build an EXCEPT statement.

Examples:

(except
 (select db [1])
 (select db [2]))

Another example:

(except
 {:all true}
 (select db [1])
 (select db [2]))

explain

(explain db stmt & [opts])

Return an EXPLAIN statement for stmt. opts can be a map with the following key/value pairs:

  • :analyze boolean
  • :buffers boolean
  • :costs boolean
  • :format :json, :text, :yaml, :xml
  • :timing boolean
  • :verbose boolean

Examples:

(explain db
  (select db [:*]
    (from :foo)))

With analyze:

(explain db
  (select db [:*]
    (from :foo))
  {:analyze true})

from

(from & from)

Add a FROM clause to an SQL statement. The from forms can be one or more tables, :stdin, a filename or an other sub query.

Examples:

Simple select:

  (select db [:*]
    (from :continents))

Using where:

  (select db [:*]
    (from :continents :countries)
    (where '(= :continents.id :continent-id)))

Using as:

  (select db [:*]
    (from (as (select [1 2 3]) :x)))

Using copy:

  (copy db :country []
    (from :stdin))

Using copy:

  (copy db :country []
    (from "/usr1/proj/bray/sql/country_data"))

group-by

(group-by & exprs)

Add a GROUP BY clause to an SQL statement.

having

(having condition & [combine])

Add a HAVING clause to an SQL statement.

Examples:

(select db [:city ‘(max :temp-lo)] (from :weather) (group-by :city) (having ’(< (max :temp-lo) 40)))

if-exists

(if-exists condition)

Add a IF EXISTS clause to an SQL statement.

if-not-exists

(if-not-exists condition)

Add a IF EXISTS clause to an SQL statement.

inherits

(inherits & tables)

Add an INHERITS clause to an SQL statement.

insert

(insert db table columns & body)

Build a INSERT statement.

intersect

(intersect & args)

Build an INTERSECT statement.

Examples:

 (intersect
  (select db [1])
  (select db [2]))

Another example:

 (intersect
  {:all true}
  (select db [1])
  (select db [2]))

join

(join from condition & {:keys [type outer pk]})

Add a JOIN clause to a statement.

Examples:

First:

(select db [:*]
  (from :countries)
  (join :continents '(using :id)))

Second:

(select db [:*]
  (from :continents)
  (join :countries.continent-id :continents.id))

Third:

(select db [:*]
  (from :countries)
  (join :continents '(on (= :continents.id :countries.continent-id))))

like

(like table & {:as opts})

Add a LIKE clause to an SQL statement.

limit

(limit expr)

Add a LIMIT clause to an SQL statement.

nulls

(nulls expr where)

Parse expr and return an NULLS FIRST/LAST expr.

offset

(offset expr)

Add a OFFSET clause to an SQL statement.

on-conflict

(on-conflict target & body)

Add a ON CONFLICT clause to a SQL statement.

on-conflict-on-constraint

(on-conflict-on-constraint target & body)

Add a ON CONFLICT ON CONSTRAINT clause to a SQL statement.

order-by

(order-by & exprs)

Add a ORDER BY clause to an SQL statement.

primary-key

(primary-key & keys)

Add a PRIMARY KEY clause to a table.

refresh-materialized-view

(refresh-materialized-view db view & body)

Build a REFRESH MATERIALIZED VIEW statement.

Examples:

(refresh-materialized-view db :order-summary)

restart-identity

(restart-identity condition)

Add a RESTART IDENTITY clause to an SQL statement.

restrict

(restrict condition)

Add a RESTRICT clause to an SQL statement.

returning

(returning & exprs)

Add a RETURNING clause to an SQL statement.

Examples:

(insert db :distributors []
  (values [{:did 106 :dname "XYZ Widgets"}])
  (returning :*))

Another example:

(update db :films
  {:kind "Dramatic"}
  (where '(= :kind "Drama"))
  (returning :*))

select

(select db exprs & body)

Build a SELECT statement.

Examples:

(select db [1])

All the columns:

(select db [:*]
  (from :continents))

Only id and name:

(select db [:id :name]
  (from :continents))

sql

(sql stmt)

Compile stmt into a clojure.java.jdbc compatible vector.

temporary

(temporary condition)

Add a TEMPORARY clause to an SQL statement.

truncate

(truncate db tables & body)

Build a TRUNCATE statement.

Examples:

(truncate db [:continents])

Another example:

(truncate db [:continents :countries])

union

(union & args)

Build a UNION statement.

Examples:

 (union
  (select db [1])
  (select db [2]))

Another example:

 (union
  {:all true}
  (select db [1])
  (select db [2]))

update

(update db table row & body)

Build a UPDATE statement.

Examples:

(update db :films {:kind “Dramatic”} (where ’(= :kind “Drama”)))

values

(values vals)(values db vals)

Return a VALUES statement or clause.

Examples:

(values db [[1 "one"] [2 "two"] [3 "three"]])

With insert:

(insert db :distributors []
  (values [{:did 106 :dname "XYZ Widgets"}]))

where

(where condition & [combine])

Add a WHERE clause to an SQL statement.

Examples:

(select db [1]
  (where '(in 1 (1 2 3))))

Another example:

(select db [*]
  (from :continents)
  (where '(= :name "Europe")))

Another example:

(delete db :continents
  (where '(= :id 1)))

window

(window & exprs)

Add a WINDOW clause to an SQL statement.

with

(with db bindings query)

Build a WITH (common table expressions) query.

with-data

(with-data data?)

Add a WITH [NO] DATA clause to a SQL statement.