From 49dc2e9439ca42cff7476f868fa4ce7bf26a8a2a Mon Sep 17 00:00:00 2001 From: Alexey Romanov Date: Thu, 28 Apr 2011 10:21:54 +0400 Subject: [PATCH] Support single constraints without lists --- src/sqlite3_lib.erl | 6 +++--- test/sqlite3_test.erl | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/sqlite3_lib.erl b/src/sqlite3_lib.erl index 75b1698..9b704c8 100644 --- a/src/sqlite3_lib.erl +++ b/src/sqlite3_lib.erl @@ -373,8 +373,7 @@ hex_str_to_bin([X, Y | Tail], Acc) -> column_sql_for_create_table({Name, Type}) -> [atom_to_list(Name), " ", col_type_to_string(Type)]; column_sql_for_create_table({Name, Type, Constraints}) -> - [atom_to_list(Name), " ", col_type_to_string(Type), - " " | map_intersperse(fun constraint_sql/1, Constraints, " ")]. + [atom_to_list(Name), " ", col_type_to_string(Type), " ", constraint_sql(Constraints)]. -spec pk_constraint_sql(any()) -> iolist(). pk_constraint_sql(Constraint) -> @@ -392,7 +391,8 @@ constraint_sql(Constraint) -> {primary_key, C} -> ["PRIMARY KEY ", pk_constraint_sql(C)]; unique -> "UNIQUE"; not_null -> "NOT NULL"; - {default, DefaultValue} -> ["DEFAULT ", value_to_sql(DefaultValue)] + {default, DefaultValue} -> ["DEFAULT ", value_to_sql(DefaultValue)]; + List -> map_intersperse(fun constraint_sql/1, List, " ") end. -spec table_constraint_sql(any()) -> iolist(). diff --git a/test/sqlite3_test.erl b/test/sqlite3_test.erl index 29a4ab2..1bfe276 100644 --- a/test/sqlite3_test.erl +++ b/test/sqlite3_test.erl @@ -62,7 +62,11 @@ basic_functionality() -> Columns = ["id", "name", "age", "wage"], AllRows = [{1, <<"abby">>, 20, 2000}, {2, <<"marge">>, 30, 2000}], AbbyOnly = [{1, <<"abby">>, 20, 2000}], - TableInfo = [{id, integer, [{primary_key, [asc, autoincrement]}]}, {name, text, [not_null, unique]}, {age, integer}, {wage, integer}], + TableInfo = [{id, integer, [{primary_key, [asc, autoincrement]}]}, + {name, text, [not_null, unique]}, + {age, integer, not_null}, + {wage, integer}], + TableInfo1 = lists:keyreplace(age, 1, TableInfo, {age, integer, [not_null]}), drop_all_tables(ct), ?WARN_ERROR_MESSAGE, ?assertEqual( @@ -76,7 +80,7 @@ basic_functionality() -> [user, sqlite_sequence], sqlite3:list_tables(ct)), ?assertEqual( - TableInfo, + TableInfo1, sqlite3:table_info(ct, user)), ?assertEqual( {rowid, 1},