Fixed tests, added support for non-atom column ids

This commit is contained in:
Alexey Romanov
2012-02-24 17:13:02 +04:00
parent 600656b2f5
commit 0611b80884
4 changed files with 168 additions and 151 deletions

View File

@@ -20,7 +20,9 @@
-endif. -endif.
-define(NULL_ATOM, null). -define(NULL_ATOM, null).
-type table() :: atom() | binary() | string(). -type sql_id() :: atom() | binary() | string().
-type table_id() :: sql_id().
-type column_id() :: sql_id().
-type sql_value() :: number() | ?NULL_ATOM | iodata() | {blob, binary()}. -type sql_value() :: number() | ?NULL_ATOM | iodata() | {blob, binary()}.
-type sql_type() :: integer | text | double | real | blob | string(). -type sql_type() :: integer | text | double | real | blob | string().
@@ -31,9 +33,9 @@
-type column_constraints() :: column_constraint() | [column_constraint()]. -type column_constraints() :: column_constraint() | [column_constraint()].
-type table_constraint() :: {primary_key, [atom()]} | {unique, [atom()]}. -type table_constraint() :: {primary_key, [atom()]} | {unique, [atom()]}.
-type table_constraints() :: table_constraint() | [table_constraint()]. -type table_constraints() :: table_constraint() | [table_constraint()].
-type table_info() :: [{atom(), sql_type()} | {atom(), sql_type(), column_constraints()}]. -type table_info() :: [{column_id(), sql_type()} | {column_id(), sql_type(), column_constraints()}].
-type sqlite_error() :: {error, integer(), string()} | {error, term()}. -type sqlite_error() :: {error, integer(), string()} | {error, term()}.
-type sql_params() :: [sql_value() | {atom() | string() | integer(), sql_value()}]. -type sql_params() :: [sql_value() | {atom() | string() | integer(), sql_value()}].
-type sql_non_query_result() :: ok | sqlite_error() | {rowid, integer()}. -type sql_non_query_result() :: ok | sqlite_error() | {rowid, integer()}.
-type sql_result() :: sql_non_query_result() | [{columns, [string()]} | {rows, [tuple()]}]. -type sql_result() :: sql_non_query_result() | [{columns, [column_id()]} | {rows, [tuple()]}].

View File

@@ -5,7 +5,7 @@
%%% @version 1.0.0 %%% @version 1.0.0
%%% @doc Library module for sqlite3 %%% @doc Library module for sqlite3
%%% %%%
%%% @type table() = atom() | binary() | string() %%% @type table_id() = atom() | binary() | string()
%%% @end %%% @end
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(sqlite3). -module(sqlite3).
@@ -303,7 +303,7 @@ columns_timeout(Db, Ref, Timeout) ->
gen_server:call(Db, {columns, Ref}, Timeout). gen_server:call(Db, {columns, Ref}, Timeout).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec create_table(Tbl :: table(), TblInfo :: table_info()) -> sql_non_query_result() %% @spec create_table(Tbl :: table_id(), TblInfo :: table_info()) -> sql_non_query_result()
%% @doc %% @doc
%% Creates the Tbl table using TblInfo as the table structure. The %% Creates the Tbl table using TblInfo as the table structure. The
%% table structure is a list of {column name, column type} pairs. %% table structure is a list of {column name, column type} pairs.
@@ -312,12 +312,12 @@ columns_timeout(Db, Ref, Timeout) ->
%% Returns the result of the create table call. %% Returns the result of the create table call.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec create_table(table(), table_info()) -> sql_non_query_result(). -spec create_table(table_id(), table_info()) -> sql_non_query_result().
create_table(Tbl, Columns) -> create_table(Tbl, Columns) ->
create_table(?MODULE, Tbl, Columns). create_table(?MODULE, Tbl, Columns).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec create_table(Db :: atom(), Tbl :: table(), Columns) -> sql_non_query_result() %% @spec create_table(Db :: atom(), Tbl :: table_id(), Columns) -> sql_non_query_result()
%% Columns = table_info() %% Columns = table_info()
%% @doc %% @doc
%% Creates the Tbl table in Db using Columns as the table structure. %% Creates the Tbl table in Db using Columns as the table structure.
@@ -327,12 +327,12 @@ create_table(Tbl, Columns) ->
%% Returns the result of the create table call. %% Returns the result of the create table call.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec create_table(atom(), table(), table_info()) -> sql_non_query_result(). -spec create_table(atom(), table_id(), table_info()) -> sql_non_query_result().
create_table(Db, Tbl, Columns) -> create_table(Db, Tbl, Columns) ->
gen_server:call(Db, {create_table, Tbl, Columns}). gen_server:call(Db, {create_table, Tbl, Columns}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec create_table_timeout(Db :: atom(), Tbl :: table(), Columns, Timeout :: timeout()) -> sql_non_query_result() %% @spec create_table_timeout(Db :: atom(), Tbl :: table_id(), Columns, Timeout :: timeout()) -> sql_non_query_result()
%% Columns = table_info() %% Columns = table_info()
%% @doc %% @doc
%% Creates the Tbl table in Db using Columns as the table structure. %% Creates the Tbl table in Db using Columns as the table structure.
@@ -342,12 +342,12 @@ create_table(Db, Tbl, Columns) ->
%% Returns the result of the create table call. %% Returns the result of the create table call.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec create_table_timeout(atom(), table(), table_info(), timeout()) -> sql_non_query_result(). -spec create_table_timeout(atom(), table_id(), table_info(), timeout()) -> sql_non_query_result().
create_table_timeout(Db, Tbl, Columns, Timeout) -> create_table_timeout(Db, Tbl, Columns, Timeout) ->
gen_server:call(Db, {create_table, Tbl, Columns}, Timeout). gen_server:call(Db, {create_table, Tbl, Columns}, Timeout).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec create_table(Db :: atom(), Tbl :: table(), TblInfo, Constraints) -> sql_non_query_result() %% @spec create_table(Db :: atom(), Tbl :: table_id(), TblInfo, Constraints) -> sql_non_query_result()
%% Columns = table_info() %% Columns = table_info()
%% Constraints = [term()] %% Constraints = [term()]
%% @doc %% @doc
@@ -359,13 +359,13 @@ create_table_timeout(Db, Tbl, Columns, Timeout) ->
%% Returns the result of the create table call. %% Returns the result of the create table call.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec create_table(atom(), table(), table_info(), table_constraints()) -> -spec create_table(atom(), table_id(), table_info(), table_constraints()) ->
sql_non_query_result(). sql_non_query_result().
create_table(Db, Tbl, Columns, Constraints) -> create_table(Db, Tbl, Columns, Constraints) ->
gen_server:call(Db, {create_table, Tbl, Columns, Constraints}). gen_server:call(Db, {create_table, Tbl, Columns, Constraints}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec create_table_timeout(Db :: atom(), Tbl :: table(), TblInfo, Constraints, Timeout) -> sql_non_query_result() %% @spec create_table_timeout(Db :: atom(), Tbl :: table_id(), TblInfo, Constraints, Timeout) -> sql_non_query_result()
%% Columns = table_info() %% Columns = table_info()
%% Constraints = [term()] %% Constraints = [term()]
%% @doc %% @doc
@@ -377,172 +377,177 @@ create_table(Db, Tbl, Columns, Constraints) ->
%% Returns the result of the create table call. %% Returns the result of the create table call.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec create_table_timeout(atom(), table(), table_info(), table_constraints(), timeout()) -> -spec create_table_timeout(atom(), table_id(), table_info(), table_constraints(), timeout()) ->
sql_non_query_result(). sql_non_query_result().
create_table_timeout(Db, Tbl, Columns, Constraints, Timeout) -> create_table_timeout(Db, Tbl, Columns, Constraints, Timeout) ->
gen_server:call(Db, {create_table, Tbl, Columns, Constraints}, Timeout). gen_server:call(Db, {create_table, Tbl, Columns, Constraints}, Timeout).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec list_tables() -> [table()] %% @spec list_tables() -> [table_id()]
%% @doc %% @doc
%% Returns a list of tables. %% Returns a list of tables.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec list_tables() -> [table()]. -spec list_tables() -> [table_id()].
list_tables() -> list_tables() ->
list_tables(?MODULE). list_tables(?MODULE).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec list_tables(Db :: atom()) -> [table()] %% @spec list_tables(Db :: atom()) -> [table_id()]
%% @doc %% @doc
%% Returns a list of tables for Db. %% Returns a list of tables for Db.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec list_tables(atom()) -> [table()]. -spec list_tables(atom()) -> [table_id()].
list_tables(Db) -> list_tables(Db) ->
gen_server:call(Db, list_tables). gen_server:call(Db, list_tables).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec list_tables_timeout(Db :: atom(), Timeout :: timeout()) -> [table()] %% @spec list_tables_timeout(Db :: atom(), Timeout :: timeout()) -> [table_id()]
%% @doc %% @doc
%% Returns a list of tables for Db. %% Returns a list of tables for Db.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec list_tables_timeout(atom(), timeout()) -> [table()]. -spec list_tables_timeout(atom(), timeout()) -> [table_id()].
list_tables_timeout(Db, Timeout) -> list_tables_timeout(Db, Timeout) ->
gen_server:call(Db, list_tables, Timeout). gen_server:call(Db, list_tables, Timeout).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec table_info(Tbl :: table()) -> table_info() %% @spec table_info(Tbl :: table_id()) -> table_info()
%% @doc %% @doc
%% Returns table schema for Tbl. %% Returns table schema for Tbl.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec table_info(table()) -> table_info(). -spec table_info(table_id()) -> table_info().
table_info(Tbl) -> table_info(Tbl) ->
table_info(?MODULE, Tbl). table_info(?MODULE, Tbl).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec table_info(Db :: atom(), Tbl :: table()) -> table_info() %% @spec table_info(Db :: atom(), Tbl :: table_id()) -> table_info()
%% @doc %% @doc
%% Returns table schema for Tbl in Db. %% Returns table schema for Tbl in Db.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec table_info(atom(), table()) -> table_info(). -spec table_info(atom(), table_id()) -> table_info().
table_info(Db, Tbl) -> table_info(Db, Tbl) ->
gen_server:call(Db, {table_info, Tbl}). gen_server:call(Db, {table_info, Tbl}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec table_info_timeout(Db :: atom(), Tbl :: table(), Timeout :: timeout()) -> table_info() %% @spec table_info_timeout(Db :: atom(), Tbl :: table_id(), Timeout :: timeout()) -> table_info()
%% @doc %% @doc
%% Returns table schema for Tbl in Db. %% Returns table schema for Tbl in Db.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec table_info_timeout(atom(), table(), timeout()) -> table_info(). -spec table_info_timeout(atom(), table_id(), timeout()) -> table_info().
table_info_timeout(Db, Tbl, Timeout) -> table_info_timeout(Db, Tbl, Timeout) ->
gen_server:call(Db, {table_info, Tbl}, Timeout). gen_server:call(Db, {table_info, Tbl}, Timeout).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec write(Tbl :: table(), Data) -> sql_non_query_result() %% @spec write(Tbl :: table_id(), Data) -> sql_non_query_result()
%% Data = [{Column :: atom(), Value :: sql_value()}] %% Data = [{Column :: column_id(), Value :: sql_value()}]
%% @doc %% @doc
%% Write Data into Tbl table. Value must be of the same type as %% Write Data into Tbl table. Value must be of the same type as
%% determined from table_info/2. %% determined from table_info/2.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec write(table(), [{atom(), sql_value()}]) -> sql_non_query_result(). -spec write(table_id(), [{column_id(), sql_value()}]) -> sql_non_query_result().
write(Tbl, Data) -> write(Tbl, Data) ->
write(?MODULE, Tbl, Data). write(?MODULE, Tbl, Data).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec write(Db :: atom(), Tbl :: table(), Data) -> sql_non_query_result() %% @spec write(Db :: atom(), Tbl :: table_id(), Data) -> sql_non_query_result()
%% Data = [{Column :: atom(), Value :: sql_value()}] %% Data = [{Column :: column_id(), Value :: sql_value()}]
%% @doc %% @doc
%% Write Data into Tbl table in Db database. Value must be of the %% Write Data into Tbl table in Db database. Value must be of the
%% same type as determined from table_info/3. %% same type as determined from table_info/3.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec write(atom(), table(), [{atom(), sql_value()}]) -> sql_non_query_result(). -spec write(atom(), table_id(), [{column_id(), sql_value()}]) -> sql_non_query_result().
write(Db, Tbl, Data) -> write(Db, Tbl, Data) ->
gen_server:call(Db, {write, Tbl, Data}). gen_server:call(Db, {write, Tbl, Data}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec write_timeout(Db :: atom(), Tbl :: table(), Data, Timeout :: timeout()) -> sql_non_query_result() %% @spec write_timeout(Db :: atom(), Tbl :: table_id(), Data, Timeout :: timeout()) ->
%% Data = [{Column :: atom(), Value :: sql_value()}] %% sql_non_query_result()
%% Data = [{Column :: column_id(), Value :: sql_value()}]
%% @doc %% @doc
%% Write Data into Tbl table in Db database. Value must be of the %% Write Data into Tbl table in Db database. Value must be of the
%% same type as determined from table_info/3. %% same type as determined from table_info/3.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec write_timeout(atom(), table(), [{atom(), sql_value()}], timeout()) -> sql_non_query_result(). -spec write_timeout(atom(), table_id(), [{column_id(), sql_value()}], timeout()) ->
sql_non_query_result().
write_timeout(Db, Tbl, Data, Timeout) -> write_timeout(Db, Tbl, Data, Timeout) ->
gen_server:call(Db, {write, Tbl, Data}, Timeout). gen_server:call(Db, {write, Tbl, Data}, Timeout).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec write_many(Tbl :: table(), Data) -> [sql_result()] %% @spec write_many(Tbl :: table_id(), Data) -> [sql_result()]
%% Data = [[{Column :: atom(), Value :: sql_value()}]] %% Data = [[{Column :: column_id(), Value :: sql_value()}]]
%% @doc %% @doc
%% Write all records in Data into table Tbl. Value must be of the %% Write all records in Data into table Tbl. Value must be of the
%% same type as determined from table_info/2. %% same type as determined from table_info/2.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec write_many(table(), [[{atom(), sql_value()}]]) -> [sql_result()]. -spec write_many(table_id(), [[{column_id(), sql_value()}]]) -> [sql_result()].
write_many(Tbl, Data) -> write_many(Tbl, Data) ->
write_many(?MODULE, Tbl, Data). write_many(?MODULE, Tbl, Data).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec write_many(Db :: atom(), Tbl :: table(), Data) -> [sql_result()] %% @spec write_many(Db :: atom(), Tbl :: table_id(), Data) -> [sql_result()]
%% Data = [[{Column :: atom(), Value :: sql_value()}]] %% Data = [[{Column :: column_id(), Value :: sql_value()}]]
%% @doc %% @doc
%% Write all records in Data into table Tbl in database Db. Value %% Write all records in Data into table Tbl in database Db. Value
%% must be of the same type as determined from table_info/3. %% must be of the same type as determined from table_info/3.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec write_many(atom(), table(), [[{atom(), sql_value()}]]) -> [sql_result()]. -spec write_many(atom(), table_id(), [[{column_id(), sql_value()}]]) -> [sql_result()].
write_many(Db, Tbl, Data) -> write_many(Db, Tbl, Data) ->
gen_server:call(Db, {write_many, Tbl, Data}). gen_server:call(Db, {write_many, Tbl, Data}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec write_many_timeout(Db :: atom(), Tbl :: table(), Data, Timeout :: timeout()) -> [sql_result()] %% @spec write_many_timeout(Db :: atom(), Tbl :: table_id(), Data, Timeout :: timeout()) ->
%% Data = [[{Column :: atom(), Value :: sql_value()}]] %% [sql_result()]
%% Data = [[{Column :: column_id(), Value :: sql_value()}]]
%% @doc %% @doc
%% Write all records in Data into table Tbl in database Db. Value %% Write all records in Data into table Tbl in database Db. Value
%% must be of the same type as determined from table_info/3. %% must be of the same type as determined from table_info/3.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec write_many_timeout(atom(), table(), [[{atom(), sql_value()}]], timeout()) -> [sql_result()]. -spec write_many_timeout(atom(), table_id(), [[{column_id(), sql_value()}]], timeout()) ->
[sql_result()].
write_many_timeout(Db, Tbl, Data, Timeout) -> write_many_timeout(Db, Tbl, Data, Timeout) ->
gen_server:call(Db, {write_many, Tbl, Data}, Timeout). gen_server:call(Db, {write_many, Tbl, Data}, Timeout).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec update(Tbl :: table(), {Key :: atom(), Value}, Data) -> sql_non_query_result() %% @spec update(Tbl :: table_id(), {Key :: atom(), Value}, Data) -> sql_non_query_result()
%% Value = any() %% Value = any()
%% Data = [{Column :: atom(), Value :: sql_value()}] %% Data = [{Column :: column_id(), Value :: sql_value()}]
%% @doc %% @doc
%% Updates rows into Tbl table such that the Value matches the %% Updates rows into Tbl table such that the Value matches the
%% value in Key with Data. %% value in Key with Data.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec update(table(), {atom(), sql_value()}, [{atom(), sql_value()}]) -> sql_non_query_result(). -spec update(table_id(), {column_id(), sql_value()}, [{column_id(), sql_value()}]) ->
sql_non_query_result().
update(Tbl, {Key, Value}, Data) -> update(Tbl, {Key, Value}, Data) ->
update(?MODULE, Tbl, {Key, Value}, Data). update(?MODULE, Tbl, {Key, Value}, Data).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec update(Db :: atom(), Tbl :: table(), {Key :: atom(), Value}, Data) -> sql_non_query_result() %% @spec update(Db :: atom(), Tbl :: table_id(), {Key :: column_id(), Value}, Data) -> sql_non_query_result()
%% Value = sql_value() %% Value = sql_value()
%% Data = [{Column :: atom(), Value :: sql_value()}] %% Data = [{Column :: column_id(), Value :: sql_value()}]
%% @doc %% @doc
%% Updates rows into Tbl table in Db database such that the Value %% Updates rows into Tbl table in Db database such that the Value
%% matches the value in Key with Data. %% matches the value in Key with Data.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec update(atom(), table(), {atom(), sql_value()}, [{atom(), sql_value()}]) -> -spec update(atom(), table_id(), {column_id(), sql_value()}, [{column_id(), sql_value()}]) ->
sql_non_query_result(). sql_non_query_result().
update(Db, Tbl, {Key, Value}, Data) -> update(Db, Tbl, {Key, Value}, Data) ->
gen_server:call(Db, {update, Tbl, Key, Value, Data}). gen_server:call(Db, {update, Tbl, Key, Value, Data}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec update_timeout(Db :: atom(), Tbl :: table(), {Key :: atom(), Value}, Data, Timeout :: timeout()) -> sql_non_query_result() %% @spec update_timeout(Db :: atom(), Tbl :: table_id(), {Key :: atom(), Value}, Data, Timeout :: timeout()) -> sql_non_query_result()
%% Value = sql_value() %% Value = sql_value()
%% Data = [{Column :: atom(), Value :: sql_value()}] %% Data = [{Column :: atom(), Value :: sql_value()}]
%% @doc %% @doc
@@ -550,137 +555,137 @@ update(Db, Tbl, {Key, Value}, Data) ->
%% matches the value in Key with Data. %% matches the value in Key with Data.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec update_timeout(atom(), table(), {atom(), sql_value()}, [{atom(), sql_value()}], timeout()) -> -spec update_timeout(atom(), table_id(), {column_id(), sql_value()}, [{column_id(), sql_value()}], timeout()) ->
sql_non_query_result(). sql_non_query_result().
update_timeout(Db, Tbl, {Key, Value}, Data, Timeout) -> update_timeout(Db, Tbl, {Key, Value}, Data, Timeout) ->
gen_server:call(Db, {update, Tbl, Key, Value, Data}, Timeout). gen_server:call(Db, {update, Tbl, Key, Value, Data}, Timeout).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read_all(Db :: atom(), Table :: table()) -> sql_result() %% @spec read_all(Db :: atom(), Table :: table_id()) -> sql_result()
%% @doc %% @doc
%% Reads all rows from Table in Db. %% Reads all rows from Table in Db.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec read_all(atom(), table()) -> sql_result(). -spec read_all(atom(), table_id()) -> sql_result().
read_all(Db, Tbl) -> read_all(Db, Tbl) ->
gen_server:call(Db, {read, Tbl}). gen_server:call(Db, {read, Tbl}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read_all_timeout(Db :: atom(), Table :: table(), Timeout :: timeout()) -> sql_result() %% @spec read_all_timeout(Db :: atom(), Table :: table_id(), Timeout :: timeout()) -> sql_result()
%% @doc %% @doc
%% Reads all rows from Table in Db. %% Reads all rows from Table in Db.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec read_all_timeout(atom(), table(), timeout()) -> sql_result(). -spec read_all_timeout(atom(), table_id(), timeout()) -> sql_result().
read_all_timeout(Db, Tbl, Timeout) -> read_all_timeout(Db, Tbl, Timeout) ->
gen_server:call(Db, {read, Tbl}, Timeout). gen_server:call(Db, {read, Tbl}, Timeout).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read_all(Db :: atom(), Table :: table(), Columns :: [atom()]) -> sql_result() %% @spec read_all(Db :: atom(), Table :: table_id(), Columns :: [column_id()]) -> sql_result()
%% @doc %% @doc
%% Reads Columns in all rows from Table in Db. %% Reads Columns in all rows from Table in Db.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec read_all(atom(), table(), [atom()]) -> sql_result(). -spec read_all(atom(), table_id(), [column_id()]) -> sql_result().
read_all(Db, Tbl, Columns) -> read_all(Db, Tbl, Columns) ->
gen_server:call(Db, {read, Tbl, Columns}). gen_server:call(Db, {read, Tbl, Columns}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read_all_timeout(Db :: atom(), Table :: table(), Columns :: [atom()], Timeout :: timeout()) -> sql_result() %% @spec read_all_timeout(Db :: atom(), Table :: table_id(), Columns :: [column_id()], Timeout :: timeout()) -> sql_result()
%% @doc %% @doc
%% Reads Columns in all rows from Table in Db. %% Reads Columns in all rows from Table in Db.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec read_all_timeout(atom(), table(), [atom()], timeout()) -> sql_result(). -spec read_all_timeout(atom(), table_id(), [column_id()], timeout()) -> sql_result().
read_all_timeout(Db, Tbl, Columns, Timeout) -> read_all_timeout(Db, Tbl, Columns, Timeout) ->
gen_server:call(Db, {read, Tbl, Columns}, Timeout). gen_server:call(Db, {read, Tbl, Columns}, Timeout).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read(Tbl :: table(), Key) -> sql_result() %% @spec read(Tbl :: table_id(), Key) -> sql_result()
%% Key = {Column :: atom(), Value :: sql_value()} %% Key = {Column :: column_id(), Value :: sql_value()}
%% @doc %% @doc
%% Reads a row from Tbl table such that the Value matches the %% Reads a row from Tbl table such that the Value matches the
%% value in Column. Value must have the same type as determined %% value in Column. Value must have the same type as determined
%% from table_info/2. %% from table_info/2.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec read(table(), {atom(), sql_value()}) -> sql_result(). -spec read(table_id(), {column_id(), sql_value()}) -> sql_result().
read(Tbl, Key) -> read(Tbl, Key) ->
read(?MODULE, Tbl, Key). read(?MODULE, Tbl, Key).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read(Db :: atom(), Tbl :: table(), Key) -> sql_result() %% @spec read(Db :: atom(), Tbl :: table_id(), Key) -> sql_result()
%% Key = {Column :: atom(), Value :: sql_value()} %% Key = {Column :: column_id(), Value :: sql_value()}
%% @doc %% @doc
%% Reads a row from Tbl table in Db database such that the Value %% Reads a row from Tbl table in Db database such that the Value
%% matches the value in Column. ColValue must have the same type %% matches the value in Column. ColValue must have the same type
%% as determined from table_info/3. %% as determined from table_info/3.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec read(atom(), table(), {atom(), sql_value()}) -> sql_result(). -spec read(atom(), table_id(), {column_id(), sql_value()}) -> sql_result().
read(Db, Tbl, {Column, Value}) -> read(Db, Tbl, {Column, Value}) ->
gen_server:call(Db, {read, Tbl, Column, Value}). gen_server:call(Db, {read, Tbl, Column, Value}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read(Db, Tbl, Key, Columns) -> [any()] %% @spec read(Db, Tbl, Key, Columns) -> [any()]
%% Db = atom() %% Db = atom()
%% Tbl = table() %% Tbl = table_id()
%% Key = {Column :: atom(), Value :: sql_value()} %% Key = {Column :: column_id(), Value :: sql_value()}
%% Columns = [atom()] %% Columns = [column_id()]
%% @doc %% @doc
%% Reads a row from Tbl table in Db database such that the Value %% Reads a row from Tbl table in Db database such that the Value
%% matches the value in Column. Value must have the same type as %% matches the value in Column. Value must have the same type as
%% determined from table_info/3. %% determined from table_info/3.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec read(atom(), table(), {atom(), sql_value()}, [atom()]) -> sql_result(). -spec read(atom(), table_id(), {column_id(), sql_value()}, [column_id()]) -> sql_result().
read(Db, Tbl, {Key, Value}, Columns) -> read(Db, Tbl, {Key, Value}, Columns) ->
gen_server:call(Db, {read, Tbl, Key, Value, Columns}). gen_server:call(Db, {read, Tbl, Key, Value, Columns}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read_timeout(Db :: atom(), Tbl :: table(), Key, Timeout :: timeout()) -> sql_result() %% @spec read_timeout(Db :: atom(), Tbl :: table_id(), Key, Timeout :: timeout()) -> sql_result()
%% Key = {Column :: atom(), Value :: sql_value()} %% Key = {Column :: column_id(), Value :: sql_value()}
%% @doc %% @doc
%% Reads a row from Tbl table in Db database such that the Value %% Reads a row from Tbl table in Db database such that the Value
%% matches the value in Column. ColValue must have the same type %% matches the value in Column. ColValue must have the same type
%% as determined from table_info/3. %% as determined from table_info/3.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec read_timeout(atom(), table(), {atom(), sql_value()}, timeout()) -> sql_result(). -spec read_timeout(atom(), table_id(), {column_id(), sql_value()}, timeout()) -> sql_result().
read_timeout(Db, Tbl, {Column, Value}, Timeout) -> read_timeout(Db, Tbl, {Column, Value}, Timeout) ->
gen_server:call(Db, {read, Tbl, Column, Value}, Timeout). gen_server:call(Db, {read, Tbl, Column, Value}, Timeout).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read_timeout(Db, Tbl, Key, Columns, Timeout :: timeout()) -> [any()] %% @spec read_timeout(Db, Tbl, Key, Columns, Timeout :: timeout()) -> [any()]
%% Db = atom() %% Db = atom()
%% Tbl = table() %% Tbl = table_id()
%% Key = {Column :: atom(), Value :: sql_value()} %% Key = {Column :: column_id(), Value :: sql_value()}
%% Columns = [atom()] %% Columns = [column_id()]
%% @doc %% @doc
%% Reads a row from Tbl table in Db database such that the Value %% Reads a row from Tbl table in Db database such that the Value
%% matches the value in Column. Value must have the same type as %% matches the value in Column. Value must have the same type as
%% determined from table_info/3. %% determined from table_info/3.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec read_timeout(atom(), table(), {atom(), sql_value()}, [atom()], timeout()) -> sql_result(). -spec read_timeout(atom(), table_id(), {column_id(), sql_value()}, [column_id()], timeout()) -> sql_result().
read_timeout(Db, Tbl, {Key, Value}, Columns, Timeout) -> read_timeout(Db, Tbl, {Key, Value}, Columns, Timeout) ->
gen_server:call(Db, {read, Tbl, Key, Value, Columns}, Timeout). gen_server:call(Db, {read, Tbl, Key, Value, Columns}, Timeout).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec delete(Tbl :: table(), Key) -> any() %% @spec delete(Tbl :: table_id(), Key) -> any()
%% Key = {Column :: atom(), Value :: sql_value()} %% Key = {Column :: column_id(), Value :: sql_value()}
%% @doc %% @doc
%% Delete a row from Tbl table in Db database such that the Value %% Delete a row from Tbl table in Db database such that the Value
%% matches the value in Column. %% matches the value in Column.
%% Value must have the same type as determined from table_info/3. %% Value must have the same type as determined from table_info/3.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec delete(table(), {atom(), sql_value()}) -> sql_non_query_result(). -spec delete(table_id(), {column_id(), sql_value()}) -> sql_non_query_result().
delete(Tbl, Key) -> delete(Tbl, Key) ->
delete(?MODULE, Tbl, Key). delete(?MODULE, Tbl, Key).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec delete_timeout(Db :: atom(), Tbl :: table(), Key, Timeout :: timeout()) -> sql_non_query_result() %% @spec delete_timeout(Db :: atom(), Tbl :: table_id(), Key, Timeout :: timeout()) -> sql_non_query_result()
%% Key = {Column :: atom(), Value :: sql_value()} %% Key = {Column :: atom(), Value :: sql_value()}
%% @doc %% @doc
%% Delete a row from Tbl table in Db database such that the Value %% Delete a row from Tbl table in Db database such that the Value
@@ -688,50 +693,50 @@ delete(Tbl, Key) ->
%% Value must have the same type as determined from table_info/3. %% Value must have the same type as determined from table_info/3.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec delete_timeout(atom(), table(), {atom(), any()}, timeout()) -> sql_non_query_result(). -spec delete_timeout(atom(), table_id(), {column_id(), sql_value()}, timeout()) -> sql_non_query_result().
delete_timeout(Db, Tbl, Key, Timeout) -> delete_timeout(Db, Tbl, Key, Timeout) ->
gen_server:call(Db, {delete, Tbl, Key}, Timeout). gen_server:call(Db, {delete, Tbl, Key}, Timeout).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec delete(Db :: atom(), Tbl :: table(), Key) -> sql_non_query_result() %% @spec delete(Db :: atom(), Tbl :: table_id(), Key) -> sql_non_query_result()
%% Key = {Column :: atom(), Value :: sql_value()} %% Key = {Column :: column_id(), Value :: sql_value()}
%% @doc %% @doc
%% Delete a row from Tbl table in Db database such that the Value %% Delete a row from Tbl table in Db database such that the Value
%% matches the value in Column. %% matches the value in Column.
%% Value must have the same type as determined from table_info/3. %% Value must have the same type as determined from table_info/3.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec delete(atom(), table(), {atom(), any()}) -> sql_non_query_result(). -spec delete(atom(), table_id(), {column_id(), sql_value()}) -> sql_non_query_result().
delete(Db, Tbl, Key) -> delete(Db, Tbl, Key) ->
gen_server:call(Db, {delete, Tbl, Key}). gen_server:call(Db, {delete, Tbl, Key}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec drop_table(Tbl :: table()) -> sql_non_query_result() %% @spec drop_table(Tbl :: table_id()) -> sql_non_query_result()
%% @doc %% @doc
%% Drop the table Tbl. %% Drop the table Tbl.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec drop_table(table()) -> sql_non_query_result(). -spec drop_table(table_id()) -> sql_non_query_result().
drop_table(Tbl) -> drop_table(Tbl) ->
drop_table(?MODULE, Tbl). drop_table(?MODULE, Tbl).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec drop_table(Db :: atom(), Tbl :: table()) -> sql_non_query_result() %% @spec drop_table(Db :: atom(), Tbl :: table_id()) -> sql_non_query_result()
%% @doc %% @doc
%% Drop the table Tbl from Db database. %% Drop the table Tbl from Db database.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec drop_table(atom(), table()) -> sql_non_query_result(). -spec drop_table(atom(), table_id()) -> sql_non_query_result().
drop_table(Db, Tbl) -> drop_table(Db, Tbl) ->
gen_server:call(Db, {drop_table, Tbl}). gen_server:call(Db, {drop_table, Tbl}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec drop_table_timeout(Db :: atom(), Tbl :: table(), Timeout :: timeout()) -> sql_non_query_result() %% @spec drop_table_timeout(Db :: atom(), Tbl :: table_id(), Timeout :: timeout()) -> sql_non_query_result()
%% @doc %% @doc
%% Drop the table Tbl from Db database. %% Drop the table Tbl from Db database.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec drop_table_timeout(atom(), table(), timeout()) -> sql_non_query_result(). -spec drop_table_timeout(atom(), table_id(), timeout()) -> sql_non_query_result().
drop_table_timeout(Db, Tbl, Timeout) -> drop_table_timeout(Db, Tbl, Timeout) ->
gen_server:call(Db, {drop_table, Tbl}, Timeout). gen_server:call(Db, {drop_table, Tbl}, Timeout).
@@ -1205,8 +1210,11 @@ wait_result(Port) ->
end. end.
parse_table_info(Info) -> parse_table_info(Info) ->
[_, Tail] = string:tokens(Info, "()"), Info1 = re:replace(Info, <<"CHECK \\('(bin|lst|am)'='(bin|lst|am)'\\)\\)">>, "", [{return, list}]),
Cols = string:tokens(Tail, ","), {_, [$(|Rest]} = lists:splitwith(fun(C) -> C =/= $( end, Info1),
%% remove ) at the end
Rest1 = list_init(Rest),
Cols = string:tokens(Rest1, ","),
build_table_info(lists:map(fun(X) -> build_table_info(lists:map(fun(X) ->
string:tokens(X, " ") string:tokens(X, " ")
end, Cols), []). end, Cols), []).
@@ -1223,10 +1231,15 @@ build_constraints([]) -> [];
build_constraints(["PRIMARY", "KEY" | Tail]) -> build_constraints(["PRIMARY", "KEY" | Tail]) ->
{Constraint, Rest} = build_primary_key_constraint(Tail), {Constraint, Rest} = build_primary_key_constraint(Tail),
[Constraint | build_constraints(Rest)]; [Constraint | build_constraints(Rest)];
build_constraints(["UNIQUE" | Tail]) -> [unique | build_constraints(Tail)]; build_constraints(["UNIQUE" | Tail]) ->
build_constraints(["NOT", "NULL" | Tail]) -> [not_null | build_constraints(Tail)]; [unique | build_constraints(Tail)];
build_constraints(["DEFAULT", DefaultValue | Tail]) -> [{default, sqlite3_lib:sql_to_value(DefaultValue)} | build_constraints(Tail)]. build_constraints(["NOT", "NULL" | Tail]) ->
% build_constraints(["CHECK", Check | Tail]) -> ... [not_null | build_constraints(Tail)];
build_constraints(["DEFAULT", DefaultValue | Tail]) ->
[{default, sqlite3_lib:sql_to_value(DefaultValue)} | build_constraints(Tail)];
build_constraints(["CHECK", _ | Tail]) ->
%% currently ignored
build_constraints(Tail).
% build_constraints(["REFERENCES", Check | Tail]) -> ... % build_constraints(["REFERENCES", Check | Tail]) -> ...
build_primary_key_constraint(Tokens) -> build_primary_key_constraint(Tokens, []). build_primary_key_constraint(Tokens) -> build_primary_key_constraint(Tokens, []).
@@ -1243,18 +1256,20 @@ build_primary_key_constraint(Tail, Acc) ->
{{primary_key, lists:reverse(Acc)}, Tail}. {{primary_key, lists:reverse(Acc)}, Tail}.
cast_table_name(Bin, SQL) -> cast_table_name(Bin, SQL) ->
case re:run(SQL,<<"CHECK\\((.*)=(.*)\\)\\)">>,[{capture,all_but_first,binary}]) of case re:run(SQL,<<"CHECK \\('(bin|lst|am)'='(bin|lst|am)'\\)\\)">>,[{capture,all_but_first,binary}]) of
{match, [<<"'bin'">>, <<"'bin'">>]} -> {match, [<<"bin">>, <<"bin">>]} ->
Bin; Bin;
{match, [<<"'lst'">>, <<"'lst'">>]} -> {match, [<<"lst">>, <<"lst">>]} ->
binary_to_atom(Bin, latin1); unicode:characters_to_list(Bin, latin1);
{match, [<<"'am'">>, <<"'am'">>]} -> {match, [<<"am">>, <<"am">>]} ->
binary_to_atom(Bin, latin1); binary_to_atom(Bin, latin1);
_ -> _ ->
%% backwards compatible %% backwards compatible
binary_to_atom(Bin, latin1) binary_to_atom(Bin, latin1)
end. end.
list_init([_]) -> [];
list_init([H|T]) -> [H|list_init(T)].
%% conflict_clause(["ON", "CONFLICT", ResolutionString | Tail]) -> %% conflict_clause(["ON", "CONFLICT", ResolutionString | Tail]) ->
%% Resolution = case ResolutionString of %% Resolution = case ResolutionString of

View File

@@ -5,7 +5,7 @@
%%% @version 1.0.0 %%% @version 1.0.0
%%% @doc Library module for sqlite3 %%% @doc Library module for sqlite3
%%% %%%
%%% @type table() = atom() | binary() | string(). %%% @type table_id() = atom() | binary() | string().
%%% @end %%% @end
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(sqlite3_lib). -module(sqlite3_lib).
@@ -145,9 +145,9 @@ write_value_sql(Values) ->
%% @doc Creates the column/data stmt for SQL. %% @doc Creates the column/data stmt for SQL.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec write_col_sql([atom()]) -> iolist(). -spec write_col_sql([column_id()]) -> iolist().
write_col_sql(Cols) -> write_col_sql(Cols) ->
map_intersperse(fun atom_to_list/1, Cols, ", "). map_intersperse(fun to_iolist/1, Cols, ", ").
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec escape(IoData :: iodata()) -> iodata() %% @spec escape(IoData :: iodata()) -> iodata()
@@ -184,54 +184,54 @@ update_set_sql(Data) ->
map_intersperse(ColValueToSqlFun, Data, ", "). map_intersperse(ColValueToSqlFun, Data, ", ").
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read_cols_sql(Columns::[atom()]) -> iolist() %% @spec read_cols_sql(Columns::[column_id()]) -> iolist()
%% @doc %% @doc
%% Creates list of columns for select stmt. %% Creates list of columns for select stmt.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec read_cols_sql([atom()]) -> iolist(). -spec read_cols_sql([column_id()]) -> iolist().
read_cols_sql(Columns) -> read_cols_sql(Columns) ->
map_intersperse(fun atom_to_list/1, Columns, ", "). map_intersperse(fun to_iolist/1, Columns, ", ").
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec create_table_sql(Tbl :: table(), ColumnData) -> iolist() %% @spec create_table_sql(Tbl :: table_id(), ColumnData) -> iolist()
%% Tbl = table() %% Tbl = table_id()
%% ColumnData = {Column, Type} | {Column, Type, Constraints} %% ColumnData = {Column, Type} | {Column, Type, Constraints}
%% Column = atom() %% Column = column_id()
%% Type = atom() %% Type = atom()
%% Constraints = [any()] %% Constraints = [any()]
%% @doc Generates a table create stmt in SQL. %% @doc Generates a table create stmt in SQL.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec create_table_sql(table(), table_info()) -> iolist(). -spec create_table_sql(table_id(), table_info()) -> iolist().
create_table_sql(Tbl, Columns) -> create_table_sql(Tbl, Columns) ->
{Type, TName} = encode_tbl(Tbl), {Type, TName} = encode_tbl(Tbl),
["CREATE TABLE ", TName, " (", ["CREATE TABLE ", TName, " (",
map_intersperse(fun column_sql_for_create_table/1, Columns, ", "), map_intersperse(fun column_sql_for_create_table/1, Columns, ", "),
" CHECK('", Type, "'='", Type, "'));"]. ", CHECK ('", Type, "'='", Type, "'));"].
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec create_table_sql(Tbl :: table(), ColumnData, TableConstraints) -> iolist() %% @spec create_table_sql(Tbl :: table_id(), ColumnData, TableConstraints) -> iolist()
%% Tbl = table() %% Tbl = table_id()
%% ColumnData = {Column, Type} | {Column, Type, Constraints} %% ColumnData = {Column, Type} | {Column, Type, Constraints}
%% Column = atom() %% Column = column_id()
%% Type = atom() %% Type = atom()
%% Constraints = [any()] %% Constraints = [any()]
%% TableConstraints = [any()] %% TableConstraints = [any()]
%% @doc Generates a table create stmt in SQL. %% @doc Generates a table create stmt in SQL.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec create_table_sql(table(), table_info(), table_constraints()) -> iolist(). -spec create_table_sql(table_id(), table_info(), table_constraints()) -> iolist().
create_table_sql(Tbl, Columns, TblConstraints) -> create_table_sql(Tbl, Columns, TblConstraints) ->
{Type, TName} = encode_tbl(Tbl), {Type, TName} = encode_tbl(Tbl),
["CREATE TABLE ", TName, " (", ["CREATE TABLE ", TName, " (",
map_intersperse(fun column_sql_for_create_table/1, Columns, ", "), ", ", map_intersperse(fun column_sql_for_create_table/1, Columns, ", "), ", ",
table_constraint_sql(TblConstraints), table_constraint_sql(TblConstraints),
" CHECK('", Type, "'='", Type, "'));"]. ", CHECK ('", Type, "'='", Type, "'));"].
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec update_sql(Tbl, Key, Value, Data) -> iolist() %% @spec update_sql(Tbl, Key, Value, Data) -> iolist()
%% Tbl = table() %% Tbl = table_id()
%% Key = atom() %% Key = atom()
%% Value = sql_value() %% Value = sql_value()
%% Data = [{Column :: atom(), Value :: sql_value()}] %% Data = [{Column :: atom(), Value :: sql_value()}]
@@ -241,21 +241,21 @@ create_table_sql(Tbl, Columns, TblConstraints) ->
%% record with matching Value. %% record with matching Value.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec update_sql(table(), atom(), sql_value(), [{atom(), sql_value()}]) -> iolist(). -spec update_sql(table_id(), column_id(), sql_value(), [{column_id(), sql_value()}]) -> iolist().
update_sql(Tbl, Key, Value, Data) -> update_sql(Tbl, Key, Value, Data) ->
{_, TName} = encode_tbl(Tbl), {_, TName} = encode_tbl(Tbl),
["UPDATE ", TName, " SET ", update_set_sql(Data), ["UPDATE ", TName, " SET ", update_set_sql(Data),
" WHERE ", atom_to_list(Key), " = ", value_to_sql(Value), ";"]. " WHERE ", to_iolist(Key), " = ", value_to_sql(Value), ";"].
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec write_sql(Tbl, Data) -> iolist() %% @spec write_sql(Tbl, Data) -> iolist()
%% Tbl = table() %% Tbl = table_id()
%% Data = [{ColName :: atom(), Value :: sql_value()}] %% Data = [{ColName :: column_id(), Value :: sql_value()}]
%% @doc Taking Data as list of column names and values pairs it creates the %% @doc Taking Data as list of column names and values pairs it creates the
%% proper insertion SQL stmt. %% proper insertion SQL stmt.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec write_sql(table(), [{atom(), sql_value()}]) -> iolist(). -spec write_sql(table_id(), [{column_id(), sql_value()}]) -> iolist().
write_sql(Tbl, Data) -> write_sql(Tbl, Data) ->
{Cols, Values} = lists:unzip(Data), {Cols, Values} = lists:unzip(Data),
["INSERT INTO ", to_iolist(Tbl), " (", write_col_sql(Cols), ["INSERT INTO ", to_iolist(Tbl), " (", write_col_sql(Cols),
@@ -263,44 +263,44 @@ write_sql(Tbl, Data) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read_sql(Tbl) -> iolist() %% @spec read_sql(Tbl) -> iolist()
%% Tbl = table() %% Tbl = table_id()
%% @doc Returns all records from table Tbl. %% @doc Returns all records from table Tbl.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec read_sql(table()) -> iolist(). -spec read_sql(table_id()) -> iolist().
read_sql(Tbl) -> read_sql(Tbl) ->
["SELECT * FROM ", to_iolist(Tbl), ";"]. ["SELECT * FROM ", to_iolist(Tbl), ";"].
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read_sql(Tbl, Columns) -> iolist() %% @spec read_sql(Tbl, Columns) -> iolist()
%% Tbl = table() %% Tbl = table_id()
%% Columns = [atom()] %% Columns = [atom()]
%% @doc %% @doc
%% Returns only specified Columns of all records from table Tbl. %% Returns only specified Columns of all records from table Tbl.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec read_sql(table(), [atom()]) -> iolist(). -spec read_sql(table_id(), [column_id()]) -> iolist().
read_sql(Tbl, Columns) -> read_sql(Tbl, Columns) ->
["SELECT ", read_cols_sql(Columns), " FROM ", ["SELECT ", read_cols_sql(Columns), " FROM ",
to_iolist(Tbl), ";"]. to_iolist(Tbl), ";"].
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read_sql(Tbl, Key, Value) -> iolist() %% @spec read_sql(Tbl, Key, Value) -> iolist()
%% Tbl = table() %% Tbl = table_id()
%% Key = atom() %% Key = column_id()
%% Value = sql_value() %% Value = sql_value()
%% @doc Using Key as the column name searches for the record with %% @doc Using Key as the column name searches for the record with
%% matching Value. %% matching Value.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec read_sql(table(), atom(), sql_value()) -> iolist(). -spec read_sql(table_id(), column_id(), sql_value()) -> iolist().
read_sql(Tbl, Key, Value) -> read_sql(Tbl, Key, Value) ->
["SELECT * FROM ", to_iolist(Tbl), " WHERE ", atom_to_list(Key), ["SELECT * FROM ", to_iolist(Tbl), " WHERE ", to_iolist(Key),
" = ", value_to_sql(Value), ";"]. " = ", value_to_sql(Value), ";"].
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read_sql(Tbl, Key, Value, Columns) -> iolist() %% @spec read_sql(Tbl, Key, Value, Columns) -> iolist()
%% Tbl = table() %% Tbl = table_id()
%% Key = atom() %% Key = atom()
%% Value = sql_value() %% Value = sql_value()
%% Columns = [atom()] %% Columns = [atom()]
@@ -309,33 +309,33 @@ read_sql(Tbl, Key, Value) ->
%% matching Value and returns only specified Columns. %% matching Value and returns only specified Columns.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec read_sql(table(), atom(), sql_value(), [atom()]) -> iolist(). -spec read_sql(table_id(), column_id(), sql_value(), [column_id()]) -> iolist().
read_sql(Tbl, Key, Value, Columns) -> read_sql(Tbl, Key, Value, Columns) ->
["SELECT ", read_cols_sql(Columns), " FROM ", ["SELECT ", read_cols_sql(Columns), " FROM ",
to_iolist(Tbl), " WHERE ", atom_to_list(Key), " = ", to_iolist(Tbl), " WHERE ", to_iolist(Key), " = ",
value_to_sql(Value), ";"]. value_to_sql(Value), ";"].
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec delete_sql(Tbl, Key, Value) -> iolist() %% @spec delete_sql(Tbl, Key, Value) -> iolist()
%% Tbl = table() %% Tbl = table_id()
%% Key = atom() %% Key = atom()
%% Value = sql_value() %% Value = sql_value()
%% @doc Using Key as the column name searches for the record with %% @doc Using Key as the column name searches for the record with
%% matching Value then deletes that record. %% matching Value then deletes that record.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec delete_sql(table(), atom(), sql_value()) -> iolist(). -spec delete_sql(table_id(), column_id(), sql_value()) -> iolist().
delete_sql(Tbl, Key, Value) -> delete_sql(Tbl, Key, Value) ->
["DELETE FROM ", to_iolist(Tbl), " WHERE ", atom_to_list(Key), ["DELETE FROM ", to_iolist(Tbl), " WHERE ", to_iolist(Key),
" = ", value_to_sql(Value), ";"]. " = ", value_to_sql(Value), ";"].
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec drop_table_sql(Tbl) -> iolist() %% @spec drop_table_sql(Tbl) -> iolist()
%% Tbl = table() %% Tbl = table_id()
%% @doc Drop the table Tbl from the database %% @doc Drop the table Tbl from the database
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec drop_table_sql(table()) -> iolist(). -spec drop_table_sql(table_id()) -> iolist().
drop_table_sql(Tbl) -> drop_table_sql(Tbl) ->
["DROP TABLE ", to_iolist(Tbl), ";"]. ["DROP TABLE ", to_iolist(Tbl), ";"].
@@ -430,12 +430,12 @@ encode_tbl(A) when is_atom(A) ->
encode_tbl(B) when is_binary(B) -> encode_tbl(B) when is_binary(B) ->
{"bin", B}; {"bin", B};
encode_tbl(L) when is_list(L) -> encode_tbl(L) when is_list(L) ->
{"lst", list_to_binary(L)}. {"lst", L}.
to_iolist(A) when is_atom(A) -> to_iolist(A) when is_atom(A) ->
atom_to_list(A); atom_to_list(A);
to_iolist(L) when is_list(L) -> to_iolist(L) when is_list(L) ->
iolist_to_binary(L); L;
to_iolist(B) when is_binary(B) -> to_iolist(B) when is_binary(B) ->
B. B.
@@ -464,16 +464,16 @@ quote_test() ->
create_table_sql_test() -> create_table_sql_test() ->
?assertFlat( ?assertFlat(
"CREATE TABLE user (id INTEGER PRIMARY KEY, name TEXT);", "CREATE TABLE user (id INTEGER PRIMARY KEY, name TEXT, CHECK ('am'='am'));",
create_table_sql(user, [{id, integer, [primary_key]}, {name, text}])), create_table_sql(user, [{id, integer, [primary_key]}, {name, text}])),
?assertFlat( ?assertFlat(
"CREATE TABLE user (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);", "CREATE TABLE user (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, CHECK ('bin'='bin'));",
create_table_sql(user, [{id, integer, [{primary_key, autoincrement}]}, {name, text}])), create_table_sql(<<"user">>, [{id, integer, [{primary_key, autoincrement}]}, {name, text}])),
?assertFlat( ?assertFlat(
"CREATE TABLE user (id INTEGER PRIMARY KEY DESC, name TEXT);", "CREATE TABLE user (id INTEGER PRIMARY KEY DESC, name TEXT, CHECK ('lst'='lst'));",
create_table_sql(user, [{id, integer, [{primary_key, desc}]}, {name, text}])), create_table_sql("user", [{id, integer, [{primary_key, desc}]}, {name, text}])),
?assertFlat( ?assertFlat(
"CREATE TABLE user (id INTEGER, name TEXT, PRIMARY KEY(id));", "CREATE TABLE user (id INTEGER, name TEXT, PRIMARY KEY(id), CHECK ('am'='am'));",
create_table_sql(user, create_table_sql(user,
[{id, integer}, {name, text}], [{id, integer}, {name, text}],
[{primary_key, [id]}])). [{primary_key, [id]}])).
@@ -500,12 +500,12 @@ read_sql_test() ->
read_sql(user, id, 1)), read_sql(user, id, 1)),
?assertFlat( ?assertFlat(
"SELECT id, name FROM user WHERE id = 1;", "SELECT id, name FROM user WHERE id = 1;",
read_sql(user, id, 1, [id, name])). read_sql(user, <<"id">>, 1, [id, "name"])).
delete_sql_test() -> delete_sql_test() ->
?assertFlat( ?assertFlat(
"DELETE FROM user WHERE id = 1;", "DELETE FROM user WHERE id = 1;",
delete_sql(user, id, 1)). delete_sql(user, "id", 1)).
drop_table_sql_test() -> drop_table_sql_test() ->
?assertFlat( ?assertFlat(

View File

@@ -85,7 +85,7 @@ basic_functionality() ->
sqlite3:table_info(ct, user)), sqlite3:table_info(ct, user)),
?assertEqual( ?assertEqual(
{rowid, 1}, {rowid, 1},
sqlite3:write(ct, user, [{name, "abby"}, {age, 20}, {wage, 2000}])), sqlite3:write(ct, user, [{name, "abby"}, {age, 20}, {<<"wage">>, 2000}])),
?assertEqual( ?assertEqual(
{rowid, 2}, {rowid, 2},
sqlite3:write(ct, user, [{name, "marge"}, {age, 30}, {wage, 2000}])), sqlite3:write(ct, user, [{name, "marge"}, {age, 30}, {wage, 2000}])),