Permalink
Browse files

Merge pull request #48 from jgordor/master

Making date() a valid boss model datatype
  • Loading branch information...
2 parents 58410c4 + bb51bec commit 461fb74b047b3e925ada475c9e1b5a2d4553fdcc @evanmiller evanmiller committed Dec 10, 2012
Showing with 13 additions and 5 deletions.
  1. +2 −1 README.md
  2. +2 −0 src/boss_db.erl
  3. +4 −0 src/boss_record_lib.erl
  4. +5 −4 src/db_adapters/boss_db_adapter_mysql.erl
View
@@ -149,6 +149,7 @@ Accepted types are:
* string()
* binary()
* datetime()
+* date()
* timestamp() [e.g. returned by erlang:now()]
* integer()
* float()
@@ -239,4 +240,4 @@ provided by boss_db and not by the application nor by the DB). UUIDs
are useful PKs when data are being aggregated from multiple sources.
The default Id type ::serial() may be explicitly supplied. Note that
-all Id types, valid or otherwise, pass type validation.
+all Id types, valid or otherwise, pass type validation.
View
@@ -280,6 +280,8 @@ validate_record_types(Record) ->
{{{D1, D2, D3}, {T1, T2, T3}}, datetime} when is_integer(D1), is_integer(D2), is_integer(D3),
is_integer(T1), is_integer(T2), is_integer(T3) ->
true;
+ {{D1, D2, D3}, date} when is_integer(D1), is_integer(D2), is_integer(D3) ->
+ true;
{Data, integer} when is_integer(Data) ->
true;
{Data, float} when is_float(Data) ->
@@ -102,6 +102,10 @@ convert_value_to_type({{D1, D2, D3}, {T1, T2, T3}} = Val, timestamp) when is_int
convert_value_to_type({{D1, D2, D3}, {T1, T2, T3}} = Val, datetime) when is_integer(D1), is_integer(D2), is_integer(D3),
is_integer(T1), is_integer(T2), is_integer(T3) ->
Val;
+convert_value_to_type({D1, D2, D3} = Val, date) when is_integer(D1), is_integer(D2), is_integer(D3) ->
+ Val;
+convert_value_to_type({date, {D1, D2, D3} = Val}, date) when is_integer(D1), is_integer(D2), is_integer(D3) ->
+ Val;
convert_value_to_type(<<"1">>, boolean) -> true;
convert_value_to_type(<<"0">>, boolean) -> false;
convert_value_to_type(<<"true">>, boolean) -> true;
@@ -417,6 +417,9 @@ escape_sql1([C|Rest], Acc) ->
pack_datetime(DateTime) ->
"'" ++ erlydtl_filters:date(DateTime, "Y-m-d H:i:s") ++ "'".
+pack_date(Date) ->
+ "'" ++ erlydtl_filters:date(Date, "Y-m-d") ++ "'".
+
pack_now(Now) -> pack_datetime(calendar:now_to_datetime(Now)).
pack_value(null) ->
@@ -427,10 +430,8 @@ pack_value(V) when is_binary(V) ->
pack_value(binary_to_list(V));
pack_value(V) when is_list(V) ->
"'" ++ escape_sql(V) ++ "'";
-pack_value({MegaSec, Sec, MicroSec}) when is_integer(MegaSec) andalso is_integer(Sec) andalso is_integer(MicroSec) ->
- pack_now({MegaSec, Sec, MicroSec});
-pack_value({date, Date = {_,_,_}}) ->
- pack_datetime({Date,{0,0,0}});
+pack_value({_, _, _} = Val) ->
+ pack_date(Val);
pack_value({{_, _, _}, {_, _, _}} = Val) ->
pack_datetime(Val);
pack_value(Val) when is_integer(Val) ->

0 comments on commit 461fb74

Please sign in to comment.