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,6 +616,17 @@ build_constraints(["DEFAULT", DefaultValue | Tail]) -> [{default, sqlite3_lib:sq
% build_constraints(["CHECK", 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().

View File

@@ -170,21 +170,24 @@ read_cols_sql(Columns) ->
%%--------------------------------------------------------------------
%% @spec create_table_sql(Tbl :: atom(), [{Column, Type}]) -> iolist()
%% Tbl = atom()
%% ColumnData = {Column, Type} | {Column, Type, Constraints}
%% Column = atom()
%% Type = atom()
%% Constraints = [any()]
%% @doc Generates a table create stmt in SQL.
%% First column listed is considered the primary key.
%% @end
%%--------------------------------------------------------------------
-spec create_table_sql(atom(), [{atom(), atom()}]) -> iolist().
create_table_sql(Tbl, [{ColName, Type} | Tl]) ->
CT = ["CREATE TABLE ", atom_to_list(Tbl), " "],
Start = ["(", atom_to_list(ColName), " ", col_type_to_string(Type), " PRIMARY KEY, "],
End = [map_intersperse(
fun({Name0, Type0}) ->
[atom_to_list(Name0), " ", col_type_to_string(Type0)]
end, Tl, ", "), ");"],
[CT, Start, End].
-spec create_table_sql(atom(), [{atom(), atom()} | {atom(), atom(), [any()]}]) -> iolist().
create_table_sql(Tbl, Columns) ->
ColumnNameTypeFun =
fun({Name0, Type0}) ->
[atom_to_list(Name0), " ", col_type_to_string(Type0)];
({Name0, Type0, Constraints}) ->
[atom_to_list(Name0), " ", col_type_to_string(Type0),
" " | map_intersperse(fun constraint_sql/1, Constraints, " ")]
end,
["CREATE TABLE ", atom_to_list(Tbl), " (",
map_intersperse(ColumnNameTypeFun, Columns, ", "), ");"].
%%--------------------------------------------------------------------
%% @spec update_sql(Tbl, Key, Value, Data) -> iolist()
@@ -304,6 +307,16 @@ sql_string(StringWithEscapedQuotes) ->
-spec sql_blob(string()) -> string().
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().
%%

View File

@@ -6,7 +6,7 @@
test() ->
file:delete("ct.db"),
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),
[{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}]),