Skip to content

Instantly share code, notes, and snippets.

View defndaines's full-sized avatar

Michael S. Daines defndaines

View GitHub Profile

SQLite, JDBC, Clojure, and Booleans

Most RDBMS' do not support a native Boolean type; Postgres is one of the few that actually does, allowing the programmer use a native Boolean type in both their application code and database code. Oracle, and many others, does not at all, and so most programmers resort to some sort of convention whereby the set of values of a Boolean-like column is restricted to 1 or 0, or 'T' or 'F', or 'Y', or 'N'.

SQLite is in a third category; it does support a Boolean type but the boolean keyword is actually a synonym for the tinyint type, and the values are actually managed in the database as 1 for true, and 0 for false. Moreover, when you query the database, you only get back out the tinyint representations. This complicates matters if you wish to take advantage of the boolean type in the database and in your application code without having to constantly transform values both in and out of it.

Here's an example; let's create a brand new database:

@noprompt
noprompt / permutations.clj
Created September 21, 2017 17:19
Permutations in Clojure (based on Heap's algorithm)
(defn swap
"Swap the elements at positions `i` and `j` in `v`."
{:private true}
[v i j]
(-> v
(assoc i (get v j))
(assoc j (get v i))))
;; SEE: https://en.wikipedia.org/wiki/Heap%27s_algorithm
(defn permutations [coll]