Skip to content

Commit

Permalink
SQL Select functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
egobrain committed Dec 5, 2012
1 parent 5fc62ba commit f1a5b9f
Show file tree
Hide file tree
Showing 9 changed files with 477 additions and 27 deletions.
4 changes: 2 additions & 2 deletions include/types.hrl
Expand Up @@ -43,13 +43,13 @@

-record(many_to_one,{
local_id :: field_name(),
model :: model_name(),
remote_model :: model_name(),
remote_id :: field_name()
}).

-record(one_to_many,{
local_id :: field_name(),
model :: model_name(),
remote_model :: model_name(),
remote_id :: field_name()
}).

Expand Down
20 changes: 10 additions & 10 deletions priv/templates/crud.dtl
Expand Up @@ -22,14 +22,14 @@ get(
%\ {% endfor %}
) ->
dip_orm(
select({{model_name}}),
select({{model}}),
where(
%| {% for index_field in index_fields %}
%| {% if not forloop.first %}
%\
,
andalso
%| {% endif %}
{{index_field.name}} = {{index_field.name|capfirst}}
{{index_field.name}} == {{index_field.name|capfirst}}
%\ {% endfor %}
)).

Expand All @@ -46,20 +46,20 @@ save({{model_name|capfirst}}) ->
]).
save({{model_name|capfirst}},ChangedFields,true) ->
dip_orm(
insert({{model_name}}),
insert({{model}}),
values(ChangedFields)
);
save({{model_name|capfirst}},ChangedFields,field) ->
dip_orm(
update({{model_name}}),
update({{model}}),
values(ChangedFields),
where(
%| {% for index_field in index_fields %}
%| {% if not forloop.first %}
%\
,
andalso
%| {% endif %}
{{index_field.name}} = {{model_name|capfirst}}#{{model_name}}.{{index_field.name}}
{{index_field.name}} == {{model_name|capfirst}}#{{model_name}}.{{index_field.name}}
%\ {% endfor %}
)).

Expand All @@ -69,14 +69,14 @@ save({{model_name|capfirst}},ChangedFields,field) ->
Reason :: db_error.
delete({{model_name|capfirst}}) ->
dip_orm(
delete({{model_name}}),
delete({{model}}),
where(
%| {% for index_field in index_fields %}
%| {% if not forloop.first %}
%\
,
andalso
%| {% endif %}
{{index_field.name}} = {{model_name|capfirst}}#{{model_name}}.{{index_field.name}}
{{index_field.name}} == {{model_name|capfirst}}#{{model_name}}.{{index_field.name}}
%\ {% endfor %}
)).

Expand Down
2 changes: 1 addition & 1 deletion src/dip_orm_compiler.erl
Expand Up @@ -43,7 +43,7 @@ models(RebarConfig,_AppFile) ->
RawDBModelConfigs),
DBModelConfigs2 <- dip_orm_configs:fill_links(DBModelConfigs),
write_db_models(DBModelConfigs2,GlobalConfig),
dip_orm_config_file:write(dip_orm_config,DBModelConfigs2,GlobalConfig)
dip_orm_config_file:write(dip_orm,DBModelConfigs2,GlobalConfig)
]).

%% ===================================================================
Expand Down
79 changes: 74 additions & 5 deletions src/dip_orm_configs.erl
Expand Up @@ -17,6 +17,16 @@
get_db_model_config/1,
fill_links/1
]).

-export([
find_model/2,
get_model_field/2,

field/2,
model/2,

find_link/2
]).
-include("log.hrl").

%% ===================================================================
Expand All @@ -39,6 +49,65 @@
%%% API
%% ===================================================================

find_model(ModelName,Models) ->
do([error_m ||
ModelName2 <- not_null_binary(ModelName),
find_model_(ModelName2,Models)
]).

find_model_(ModelName,Models) ->
case lists:keyfind(ModelName,#config.name,Models) of
#config{} = Model ->
{ok,Model};
false ->
Reason = dip_utils:template("Unknown model: ~s",[ModelName]),
{error,Reason}
end.

find_link(#config{name=LocalModelName,links=Links},
#config{name=RemoteModelName}) ->
FoldFun = fun(#one_to_many{remote_model=R}=Link,_Acc) when R =:= RemoteModelName->
{ok,Link};
(#many_to_one{remote_model=R}=Link,_Acc) when R =:= RemoteModelName ->
{ok,Link};
(#one_to_many{remote_model=R}=Link,_Acc) when R =:= RemoteModelName ->
{ok,Link};
(_,Acc) ->
Acc
end,
Reason = dip_utils:template("No model '~s' is linked to '~s'",[RemoteModelName,LocalModelName]),
lists:foldl(FoldFun,{error,Reason},Links).


get_model_field(FieldName,ModelConfig) ->
do([error_m ||
FieldName2 <- not_null_binary(FieldName),
get_model_field_(FieldName2,ModelConfig)
]).
get_model_field_(FieldName,#config{fields=Fields}) ->
case lists:keyfind(FieldName,#field.name,Fields) of
#field{is_in_database=true} = Field ->
{ok,Field};
#field{is_in_database=false} ->
Reason = dip_utils:template("Field '~s' does not stores in database",[FieldName]),
{error,Reason};
false ->
Reason = dip_utils:template("Unknown field: ~s",[FieldName]),
{error,Reason}
end.

field(name,#field{name=Name}) -> Name;
field(db_type,#field{db_options=#db_options{type=Db_type}}) -> Db_type.

model(name,#config{name=Name}) -> Name;
model(db_fields,#config{fields=Fields}) ->
[F || F <- Fields,F#field.is_in_database,((F#field.record_options)#record_options.mode)#access_mode.sr].


%% ===================================================================
%%% API
%% ===================================================================

get_global_config(Config) ->
do([error_m ||
Opts <- default(option,dip_orm_options,Config,[]),
Expand Down Expand Up @@ -110,7 +179,7 @@ fill_links(ModelConfigs) ->
fill_one_to_many_links(ModelConfigs,Dict) ->
FoldFun = fun(#config{name=LocalModelName,fields=LocalFields,links=LocalLinks},Acc) ->
SubFoldFun = fun(#many_to_one{local_id=LocalID,
model=RemoteModelName,
remote_model=RemoteModelName,
remote_id=RemoteID},
Acc2) ->
do([error_m ||
Expand All @@ -120,7 +189,7 @@ fill_one_to_many_links(ModelConfigs,Dict) ->
check_fields(LocalField,RemoteField),
Link <- return(#one_to_many{
local_id=RemoteID,
model=LocalModelName,
remote_model=LocalModelName,
remote_id=LocalID
}),
return(dict:store(RemoteModelName,{RemoteFields,[Link|RemoteLinks]},Acc2))
Expand Down Expand Up @@ -162,10 +231,10 @@ get_many_to_many_links(#config{name=ModelName,links=Links}) ->
get_many_to_many_links_(_LocalModelName,[],Acc) ->
Acc;
get_many_to_many_links_(LinkModelName,[#many_to_one{local_id=LinkID_1,
model=RemoteModelName_1,
remote_model=RemoteModelName_1,
remote_id=RemoteID_1}|RestLinks],Acc) ->
FoldFun = fun(#many_to_one{local_id=LinkID_2,
model=RemoteModelName_2,
remote_model=RemoteModelName_2,
remote_id=RemoteID_2},FoldAcc) ->
[{RemoteModelName_1,#many_to_many{local_id = RemoteID_1,
link_model = LinkModelName,
Expand Down Expand Up @@ -414,7 +483,7 @@ find_links(Fields) ->
}}, Acc) ->
Link = #many_to_one{
local_id=Name,
model=Model,
remote_model=Model,
remote_id=Field
},
[Link|Acc];
Expand Down
4 changes: 3 additions & 1 deletion src/dip_orm_file.erl
Expand Up @@ -8,6 +8,8 @@
-module(dip_orm_file).
-compile({parse_transform,do}).

-include("log.hrl").

-export([extract_config/1,
get_module_name/1,
place_generated_block/2,
Expand All @@ -30,7 +32,7 @@ read_config(Filename) ->
case file:consult(Filename) of
{ok,Config} -> {ok,Config};
{error,Reason} ->
{error,{wrong_syntax,Reason}}
{error,{Filename,Reason}}
end.


Expand Down

0 comments on commit f1a5b9f

Please sign in to comment.