fix: td format_sql

This commit is contained in:
dawnwinterLiu 2023-04-15 01:46:09 +08:00
parent 430fcf95d6
commit e50aa041a1
4 changed files with 36 additions and 19 deletions

View File

@ -63,7 +63,7 @@ get_echart(ProductId, Results, Names, Interval) ->
end, [], Results),
%% io:format("~s ~p Rows = ~p.~n", [?FILE, ?LINE, Rows]),
ChildRows = lists:foldl(fun(X, Acc1) ->
Date = maps:get(<<"日期"/utf8>>, X),
Date = maps:get(<<"日期"/utf8>>, X, dgiot_datetime:format(dgiot_datetime:to_localtime(dgiot_datetime:now_secs()), <<"YY-MM-DD HH:NN:SS">>)),
maps:fold(fun(K1, V1, Acc) ->
case maps:find(K1, Acc) of
error ->

View File

@ -123,11 +123,16 @@ get_keys(ProductId, Function, <<"*">>) ->
lists:foldl(fun(X, {Names, Acc}) ->
case X of
#{<<"identifier">> := Identifier, <<"name">> := Name, <<"isstorage">> := true, <<"isshow">> := true} ->
{Names ++ [Name], <<Acc/binary, ", ", Function/binary, "(", Identifier/binary, ") ", Identifier/binary>>};
case Acc of
<<>> ->
{Names ++ [Name], <<Acc/binary, Function/binary, "(", Identifier/binary, ") ", Identifier/binary>>};
_ ->
{Names ++ [Name], <<Acc/binary, ", ", Function/binary, "(", Identifier/binary, ") ", Identifier/binary>>}
end;
_ ->
{Names, Acc}
end
end, {[], <<Function/binary, "(createdat) createdat">>}, Props);
end, {[], get_defult(Function)}, Props);
_Other ->
?LOG(debug, "~p _Other ~p", [ProductId, _Other]),
{[], <<"*">>}
@ -148,9 +153,14 @@ get_keys(ProductId, Function, Keys) ->
error ->
{Names, Acc};
Name ->
{Names ++ [Name], <<Acc/binary, ", ", Function/binary, "(", X/binary, ") ", X/binary>>}
case Acc of
<<>> ->
{Names ++ [Name], <<Acc/binary, Function/binary, "(", X/binary, ") ", X/binary>>};
_ ->
{Names ++ [Name], <<Acc/binary, ", ", Function/binary, "(", X/binary, ") ", X/binary>>}
end
end
end, {[], <<Function/binary, "(createdat) createdat">>}, List).
end, {[], get_defult(Function)}, List).
check_field(Typea, V, #{<<"specs">> := Specs}) when Typea == <<"enum">>; Typea == <<"bool">> ->
maps:get(dgiot_utils:to_binary(V), Specs, V);
@ -260,3 +270,10 @@ get_product_data(Channel, ProductId, DeviceId, Args) ->
{400, Reason}
end
end.
get_defult(<<"first">>) ->
<<"first(createdat) createdat">>;
get_defult(<<"last">>) ->
<<"last(createdat) createdat">>;
get_defult(_) ->
<<>>.

View File

@ -211,39 +211,39 @@ get_fields(Table) ->
format_sql(ProductId, DevAddr, Data) ->
case dgiot_bridge:get_product_info(ProductId) of
{ok, #{<<"thing">> := Properties}} ->
Fields =
{TagFields, ValueFields} =
case dgiot_data:get({ProductId, ?TABLEDESCRIBE}) of
Results when length(Results) > 0 ->
get_sqls(Data, ProductId, Properties, Results);
_ ->
""
{" ", " "}
end,
DeviceId = dgiot_parse_id:get_deviceid(ProductId, DevAddr),
TdChannelId = dgiot_parse_id:get_channelid(dgiot_utils:to_binary(?BRIDGE_CHL), <<"TD">>, <<"TD资源通道"/utf8>>),
DB = dgiot_tdengine:get_database(TdChannelId, ProductId),
TableName = ?Table(DeviceId),
Using1 = <<" using ", DB/binary, "_", ProductId/binary>>,
<<"INSERT INTO ", DB/binary, TableName/binary, Using1/binary, Fields/binary, ";">>;
<<"INSERT INTO ", DB/binary, TableName/binary, Using1/binary, " TAGS", TagFields/binary, " VALUES", ValueFields/binary, ";">>;
_ ->
<<"show database;">>
end.
get_sqls(Data, ProductId, Properties, Results) ->
get_sqls(Data, ProductId, Properties, Results, <<"">>).
get_sqls(Data, ProductId, Properties, Results, {<<"">>, <<"">>}).
get_sqls([], _ProductId, _Properties, _Results, Acc) ->
Acc;
get_sqls([Data | Rest], ProductId, Properties, Results, Acc) ->
get_sqls([Data | Rest], ProductId, Properties, Results, {_, Acc}) ->
Now = maps:get(<<"createdat">>, Data, now),
Sql = get_sql(Results, ProductId, Data, Now),
get_sqls(Rest, ProductId, Properties, Results, <<Acc/binary, Sql/binary>>).
{TagSql, Sql} = get_sql(Results, ProductId, Data, Now),
get_sqls(Rest, ProductId, Properties, Results, {TagSql, <<Acc/binary, Sql/binary>>}).
get_sql(Results, ProductId, Values, Now) ->
get_sql(Results, ProductId, Values, Now, {"", ""}).
get_sql(Results, ProductId, Values, Now, {"(", "("}).
get_sql([], _ProductId, _Values, _Now, {TagAcc, Acc}) ->
list_to_binary(" TAGS(" ++ TagAcc ++ ") VALUES(" ++ Acc ++ ")");
{list_to_binary(TagAcc ++ ")"), list_to_binary(Acc ++ ")")};
get_sql([Column | Results], ProductId, Values, Now, {TagAcc, Acc}) ->
NewAcc =
@ -271,15 +271,15 @@ get_value(Field, Values, ProductId, Acc) ->
case Value of
{NewValue, text} ->
case Acc of
"" ->
",\'" ++ dgiot_utils:to_list(NewValue) ++ "\'";
"(" ->
Acc ++ "\'" ++ dgiot_utils:to_list(NewValue) ++ "\'";
_ ->
Acc ++ ",\'" ++ dgiot_utils:to_list(NewValue) ++ "\'"
end;
_ ->
case Acc of
"" ->
dgiot_utils:to_list(Value);
"(" ->
Acc ++ dgiot_utils:to_list(Value);
_ ->
Acc ++ "," ++ dgiot_utils:to_list(Value)
end

@ -1 +1 @@
Subproject commit 24aa55b6b3141206a193c464e2bbefeec4b007f7
Subproject commit 9303052c9aee6e4f9cc38249a1e800e686c4c775