Continued support for constraints

This commit is contained in:
Alexey Romanov
2010-10-12 13:48:58 +04:00
parent e8e6470172
commit 2b4afbe1b6
4 changed files with 4076 additions and 3985 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -616,7 +616,18 @@ build_constraints(["DEFAULT", DefaultValue | Tail]) -> [{default, sqlite3_lib:sq
% build_constraints(["CHECK", Check | Tail]) -> ... % build_constraints(["CHECK", Check | Tail]) -> ...
% build_constraints(["REFERENCES", Check | Tail]) -> ... % build_constraints(["REFERENCES", Check | Tail]) -> ...
conflict_clause(["ON", "CONFLICT", ResolutionString | Tail]) ->
Resolution = case ResolutionString of
"ROLLBACK" -> rollback;
"ABORT" -> abort;
"FAIL" -> fail;
"IGNORE" -> ignore;
"REPLACE" -> replace
end,
{{on_conflict, Resolution}, Tail};
conflict_clause(NoOnConflictClause) ->
{no_on_conflict, NoOnConflictClause}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @type sql_value() = number() | 'null' | iodata(). %% @type sql_value() = number() | 'null' | iodata().
%% %%

View File

@@ -170,21 +170,24 @@ read_cols_sql(Columns) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec create_table_sql(Tbl :: atom(), [{Column, Type}]) -> iolist() %% @spec create_table_sql(Tbl :: atom(), [{Column, Type}]) -> iolist()
%% Tbl = atom() %% Tbl = atom()
%% ColumnData = {Column, Type} | {Column, Type, Constraints}
%% Column = atom() %% Column = atom()
%% Type = atom() %% Type = atom()
%% Constraints = [any()]
%% @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(atom(), [{atom(), atom()}]) -> iolist(). -spec create_table_sql(atom(), [{atom(), atom()} | {atom(), atom(), [any()]}]) -> iolist().
create_table_sql(Tbl, [{ColName, Type} | Tl]) -> create_table_sql(Tbl, Columns) ->
CT = ["CREATE TABLE ", atom_to_list(Tbl), " "], ColumnNameTypeFun =
Start = ["(", atom_to_list(ColName), " ", col_type_to_string(Type), " PRIMARY KEY, "], fun({Name0, Type0}) ->
End = [map_intersperse( [atom_to_list(Name0), " ", col_type_to_string(Type0)];
fun({Name0, Type0}) -> ({Name0, Type0, Constraints}) ->
[atom_to_list(Name0), " ", col_type_to_string(Type0)] [atom_to_list(Name0), " ", col_type_to_string(Type0),
end, Tl, ", "), ");"], " " | map_intersperse(fun constraint_sql/1, Constraints, " ")]
[CT, Start, End]. end,
["CREATE TABLE ", atom_to_list(Tbl), " (",
map_intersperse(ColumnNameTypeFun, Columns, ", "), ");"].
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec update_sql(Tbl, Key, Value, Data) -> iolist() %% @spec update_sql(Tbl, Key, Value, Data) -> iolist()
@@ -304,6 +307,16 @@ sql_string(StringWithEscapedQuotes) ->
-spec sql_blob(string()) -> string(). -spec sql_blob(string()) -> string().
sql_blob(Blob) -> Blob. sql_blob(Blob) -> Blob.
-spec constraint_sql([any()]) -> iolist().
constraint_sql(Constraint) ->
case Constraint of
primary_key -> "PRIMARY KEY";
{primary_key, desc} -> "PRIMARY KEY DESC";
unique -> "UNIQUE";
not_null -> "NOT NULL";
{default, DefaultValue} -> ["DEFAULT ", value_to_sql(DefaultValue)]
end.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @type sql_value() = number() | 'null' | iodata(). %% @type sql_value() = number() | 'null' | iodata().
%% %%

View File

@@ -6,7 +6,7 @@
test() -> test() ->
file:delete("ct.db"), file:delete("ct.db"),
sqlite3:open(ct), sqlite3:open(ct),
sqlite3:create_table(ct, user, [{id, integer}, {name, text}, {age, integer}, {wage, integer}]), sqlite3:create_table(ct, user, [{id, integer, [primary_key]}, {name, text}, {age, integer}, {wage, integer}]),
[user] = sqlite3:list_tables(ct), [user] = sqlite3:list_tables(ct),
[{id, integer, [primary_key]}, {name, text}, {age, integer}, {wage, integer}] = sqlite3:table_info(ct, user), [{id, integer, [primary_key]}, {name, text}, {age, integer}, {wage, integer}] = sqlite3:table_info(ct, user),
{id, Id1} = sqlite3:write(ct, user, [{name, "abby"}, {age, 20}, {wage, 2000}]), {id, Id1} = sqlite3:write(ct, user, [{name, "abby"}, {age, 20}, {wage, 2000}]),