Fixed column types, according to http://www.sqlite.org/datatype3.html
This commit is contained in:
@@ -602,7 +602,7 @@ parse_table_info(Info) ->
|
|||||||
build_table_info([], Acc) ->
|
build_table_info([], Acc) ->
|
||||||
lists:reverse(Acc);
|
lists:reverse(Acc);
|
||||||
build_table_info([[ColName, ColType] | Tl], Acc) ->
|
build_table_info([[ColName, ColType] | Tl], Acc) ->
|
||||||
build_table_info(Tl, [{list_to_atom(ColName), sqlite3_lib:col_type(ColType)}| Acc]);
|
build_table_info(Tl, [{list_to_atom(ColName), sqlite3_lib:col_type_to_atom(ColType)}| Acc]);
|
||||||
build_table_info([[ColName, ColType, "PRIMARY", "KEY"] | Tl], Acc) ->
|
build_table_info([[ColName, ColType, "PRIMARY", "KEY"] | Tl], Acc) ->
|
||||||
build_table_info(Tl, [{list_to_atom(ColName), primary_key}| Acc]).
|
build_table_info(Tl, [{list_to_atom(ColName), primary_key}| Acc]).
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
-include("sqlite3.hrl").
|
-include("sqlite3.hrl").
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([col_type/1]).
|
-export([col_type_to_atom/1]).
|
||||||
-export([value_to_sql/1, value_to_sql_unsafe/1, escape/1]).
|
-export([value_to_sql/1, value_to_sql_unsafe/1, escape/1]).
|
||||||
-export([write_value_sql/1, write_col_sql/1]).
|
-export([write_value_sql/1, write_col_sql/1]).
|
||||||
-export([create_table_sql/2, write_sql/2, read_sql/3, delete_sql/3, drop_table/1]).
|
-export([create_table_sql/2, write_sql/2, read_sql/3, delete_sql/3, drop_table/1]).
|
||||||
@@ -21,27 +21,32 @@
|
|||||||
%% API
|
%% API
|
||||||
%%====================================================================
|
%%====================================================================
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% @spec col_type(Type :: term()) -> term()
|
%% @spec col_type_to_string(Type :: atom()) -> string()
|
||||||
%% @doc Maps sqlite3 column type.
|
%% @doc Maps sqlite3 column type.
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec(col_type/1::(atom() | string()) -> atom() | string()).
|
-spec(col_type_to_string/1::(atom()) -> string()).
|
||||||
col_type(integer) ->
|
col_type_to_string(integer) ->
|
||||||
"INTEGER";
|
"INTEGER";
|
||||||
col_type("INTEGER") ->
|
col_type_to_string(text) ->
|
||||||
integer;
|
|
||||||
col_type(text) ->
|
|
||||||
"TEXT";
|
"TEXT";
|
||||||
col_type("TEXT") ->
|
col_type_to_string(double) ->
|
||||||
|
"REAL";
|
||||||
|
col_type_to_string(real) ->
|
||||||
|
"REAL".
|
||||||
|
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% @spec col_type_to_atom(Type :: string()) -> atom()
|
||||||
|
%% @doc Maps sqlite3 column type.
|
||||||
|
%% @end
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
-spec(col_type_to_atom/1::(string()) -> atom()).
|
||||||
|
col_type_to_atom("INTEGER") ->
|
||||||
|
integer;
|
||||||
|
col_type_to_atom("TEXT") ->
|
||||||
text;
|
text;
|
||||||
col_type(double) ->
|
col_type_to_atom("REAL") ->
|
||||||
"DOUBLE";
|
double.
|
||||||
col_type("DOUBLE") ->
|
|
||||||
double;
|
|
||||||
col_type(date) ->
|
|
||||||
"DATE";
|
|
||||||
col_type("DATE") ->
|
|
||||||
date.
|
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% @spec value_to_sql_unsafe(Value :: sql_value()) -> iodata()
|
%% @spec value_to_sql_unsafe(Value :: sql_value()) -> iodata()
|
||||||
@@ -159,15 +164,16 @@ read_cols_sql (Columns) ->
|
|||||||
%% ColName = atom()
|
%% ColName = atom()
|
||||||
%% Type = string()
|
%% Type = string()
|
||||||
%% @doc Generates a table create stmt in SQL.
|
%% @doc Generates a table create stmt in SQL.
|
||||||
|
%% First column listed is considered the primary key.
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec(create_table_sql/2::(atom(), [{atom(), string()}]) -> string()).
|
-spec(create_table_sql/2::(atom(), [{atom(), string()}]) -> string()).
|
||||||
create_table_sql(Tbl, [{ColName, Type} | Tl]) ->
|
create_table_sql(Tbl, [{ColName, Type} | Tl]) ->
|
||||||
CT = ["CREATE TABLE ", atom_to_list(Tbl), " "],
|
CT = ["CREATE TABLE ", atom_to_list(Tbl), " "],
|
||||||
Start = ["(", atom_to_list(ColName), " ", sqlite3_lib:col_type(Type), " PRIMARY KEY, "],
|
Start = ["(", atom_to_list(ColName), " ", col_type_to_string(Type), " PRIMARY KEY, "],
|
||||||
End = [map_intersperse(
|
End = [map_intersperse(
|
||||||
fun({Name0, Type0}) ->
|
fun({Name0, Type0}) ->
|
||||||
[atom_to_list(Name0), " ", sqlite3_lib:col_type(Type0)]
|
[atom_to_list(Name0), " ", col_type_to_string(Type0)]
|
||||||
end, Tl, ", "), ");"],
|
end, Tl, ", "), ");"],
|
||||||
[CT, Start, End].
|
[CT, Start, End].
|
||||||
|
|
||||||
@@ -185,7 +191,7 @@ create_table_sql(Tbl, [{ColName, Type} | Tl]) ->
|
|||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec (update_sql/4::(atom (), atom (), atom (), [{atom (), sql_value ()}]) -> string ()).
|
-spec (update_sql/4::(atom (), atom (), atom (), [{atom (), sql_value ()}]) -> string ()).
|
||||||
update_sql (Tbl, Key, Value, Data) ->
|
update_sql (Tbl, Key, Value, Data) ->
|
||||||
["UPDATE ", atom_to_list(Tbl), " SET ", sqlite3_lib:update_set_sql(Data),
|
["UPDATE ", atom_to_list(Tbl), " SET ", update_set_sql(Data),
|
||||||
" WHERE ", atom_to_list(Key), " = ", value_to_sql(Value), ";"].
|
" WHERE ", atom_to_list(Key), " = ", value_to_sql(Value), ";"].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user