ndevreeze.flexdb
alter-table
(alter-table db-handle table columns)Alter table, add column(s) table - keyword or string columns - vector of :keyword, string, vector (name, type) or map (:name, :type) Create table iff not exists. Return - void.
close-db
(close-db db-handle)Close a DB handle (SQLiteConnection or Postgres for now, could be changed) Could contain prepared statements and meta-data in the future
column-exists?
(column-exists? db-handle table column)Return true iff columns exists in table within db. table - keyword or string column - keyword or string
column-meta-data
(column-meta-data db-handle table)Return seq of column meta data, for table in db
column-name
(column-name column)Return column-name as :keyword for column column - either a string/keyword (then default datatype: string), or seq-of [:name :type] return vector with name and type. Used for internal map with new tables/columns within transaction.
column-with-datatype
(column-with-datatype db-spec record column-name)Determine SQL datatype of column within record. Return seq of columnname and SQL-datatype. datatype could be more than one :keyword
columns
(columns db-handle table)Return set of column names (as keywords) in table in database table can be a string or keyword. Look in both DB metadata (with query) as well as newly added tables/columns (in cache)
create-table
(create-table db-handle table columns)Create a table table: :keyword or string columns: vector of: :keyword, string, vector (name, type) or map (:name, :type). always include a generated ‘id’ column. Iff an id-field exists in columns, create an ‘id_’ field. return - void.
current-connection
(current-connection db-handle)Return current connection/transaction handle, or the db-spec if none exists. Use for working within transactions.
db-class-map
Mapping of java-class to conversion function wrt sql types JSR310 LocalDate and LocalDateTime are supported, no need for conversion.
db-value
(db-value val)Convert a value to a type understood by jdbc/sql. Can also be some JSR 310 types
dquoted
(dquoted par)Convert par to a string and surround it with double quotes. With respect to bracketed table and column names for reserved words. This seems to work both in SQLite and Postgres (Postgres does not like brackets)
dquoted-record
(dquoted-record record)Convert keys in record/map to string and surround with double quotes. Wrt using reserved words in the table. Maybe only do this for the list of 145 (SQlite) reserved words
exec
(exec db-handle sql)(exec db-handle sql params)Execute a DDL/DML query. Also handle optional params-sequence given.
get-first-id
(get-first-id db-handle res)Return created id of first row of result-set. This is different for SQLite and Postgres
id-spec
(id-spec handle)(id-spec handle id-field)Generate id-column spec for database type, given db handle
in-transaction
macro
(in-transaction handle & body)Run body expressions in a transaction based on handle. Works in conjunction with set-transaction and current-transaction. Returns result of last expression
in-transaction?
(in-transaction? db-handle)Return true if handle is currently within a transaction
insert
(insert db-handle table record)Insert record into table, adding table/columns if needed. Default/pessimistic version: first check if table and columns exist and create/alter table where needed; then insert record. Also call db-values on record.
insert-no-check
(insert-no-check db-handle table record)Insert record into table, no checks if table/columns exist. Return generated id.
insert-opt
(insert-opt db-handle table record)Insert record into table, adding table/columns if needed. Optimistic version: first try to insert; if fails, add table/column(s) and try again. This version might be faster, need some tests.
new-columns
(new-columns db-spec cols record)Determine new columns (including types) for table based on record. cols - the current columns of table (seq of keywords) record - the hashmap with needed columns of table (columns als keywords) return - seq of new columns
open-db-spec
(open-db-spec db-spec)Open DB connection given a spec, could be SQLite or Postgresql, maybe others
open-db-sqlite
(open-db-sqlite db-name)Open SQLite DB and return handle (SQLiteConnection for now, could be changed. Return connection both directly in map as within returned db-spec within map. db-name - path to DB, existing or new, as string
query
(query db-handle sql)(query db-handle sql params)Perform a SQL (select) query, return sequence of maps. Also handle optional params-sequence given.
set-init-function
(set-init-function db-handle f)Set a function to be executed on each new connection/transaction. Function should accept one parameter, the db handle.
set-transaction
(set-transaction db-handle t-con)Mark transaction handle given by with-db-transaction in this handle. Also exec init-function on this transaction iff it is set. Keep list of new tables/columns created in this transaction. Also call :init-function when given (eg. for percentile function). Fail if transaction already started
sql-db-type
(sql-db-type db-spec value)Determine sql type of a value For now only integer, float, string and date-time types for Postgres. Default to varchar if no match. sql-type dependent on db-spec, eg with date/time values. throws an exception if type cannot be determined. (maybe should return varchar in that case)
sql-db-types
SQL types for combinations of database and Clojure/Java values Includes defaults per datatype and also per database type.
surround-brackets
(surround-brackets par)Convert par to a string and surround it with brackets. Wrt bracketed table and column names for reserved words
table-exists?
(table-exists? db-handle table)Given db-handle and table, returns true iff table exists within DB, false otherwise. Table can be keyword or string.
table-meta-data
(table-meta-data db-handle)Return seq of table meta data, but for now without column info
unset-transaction
(unset-transaction db-handle)Unmark transaction handle given by with-db-transaction in this handle. Reset new columns/tables, should be available als meta-data after commit. Fail if no transaction started