Current section
Files
Jump to
Current section
Files
lib/sqlitex.ex
defmodule Sqlitex do
def close(db) do
:esqlite3.close(db)
end
def open(path) when is_binary(path), do: open(String.to_char_list(path))
def open(path) do
:esqlite3.open(path)
end
def with_db(path, fun) do
{:ok, db} = open(path)
res = fun.(db)
close(db)
res
end
def exec(db, sql) do
:esqlite3.exec(sql, db)
end
def query(db, sql, opts \\ []), do: Sqlitex.Query.query(db, sql, opts)
@doc """
Create a new table `name` where `table_opts` are a list of table constraints
and `cols` are a keyword list of columns. The following table constraints are
supported: `:temp` and `:primary_key`. Example:
**[:temp, {:primary_key, [:id]}]**
Columns can be passed as:
* name: :type
* name: {:type, constraints}
where constraints is a list of column constraints. The following column constraints
are supported: `:primary_key`, `:not_null` and `:autoincrement`. Example:
**id: :integer, name: {:text, [:not_null]}**
"""
def create_table(db, name, table_opts \\ [], cols) do
stmt = Sqlitex.SqlBuilder.create_table(name, table_opts, cols)
exec(db, stmt)
end
end