2016-02-09 17:23:15 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_sql_pt.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Description : Parse transform for SQL queries
|
|
|
|
%%% Created : 20 Jan 2016 by Alexey Shchepin <alexey@process-one.net>
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2016-02-09 17:23:15 +01:00
|
|
|
-module(ejabberd_sql_pt).
|
|
|
|
|
|
|
|
%% API
|
2017-11-02 15:03:30 +01:00
|
|
|
-export([parse_transform/2, format_error/1]).
|
2016-02-09 17:23:15 +01:00
|
|
|
|
2018-03-15 15:55:05 +01:00
|
|
|
%-export([parse/2]).
|
2016-02-09 17:23:15 +01:00
|
|
|
|
|
|
|
-include("ejabberd_sql_pt.hrl").
|
|
|
|
|
|
|
|
-record(state, {loc,
|
|
|
|
'query' = [],
|
|
|
|
params = [],
|
|
|
|
param_pos = 0,
|
|
|
|
args = [],
|
|
|
|
res = [],
|
|
|
|
res_vars = [],
|
2017-11-02 15:03:30 +01:00
|
|
|
res_pos = 0,
|
2017-11-03 15:42:53 +01:00
|
|
|
server_host_used = false,
|
2018-03-15 15:55:05 +01:00
|
|
|
used_vars = [],
|
|
|
|
use_new_schema}).
|
2016-02-09 17:23:15 +01:00
|
|
|
|
|
|
|
-define(QUERY_RECORD, "sql_query").
|
|
|
|
|
|
|
|
-define(ESCAPE_RECORD, "sql_escape").
|
|
|
|
-define(ESCAPE_VAR, "__SQLEscape").
|
|
|
|
|
|
|
|
-define(MOD, sql__module_).
|
|
|
|
|
2017-11-02 15:03:30 +01:00
|
|
|
-ifdef(NEW_SQL_SCHEMA).
|
|
|
|
-define(USE_NEW_SCHEMA, true).
|
|
|
|
-else.
|
|
|
|
-define(USE_NEW_SCHEMA, false).
|
|
|
|
-endif.
|
|
|
|
|
2016-02-09 17:23:15 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function:
|
|
|
|
%% Description:
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
parse_transform(AST, _Options) ->
|
|
|
|
%io:format("PT: ~p~nOpts: ~p~n", [AST, Options]),
|
2017-11-02 15:03:30 +01:00
|
|
|
put(warnings, []),
|
2016-02-09 17:23:15 +01:00
|
|
|
NewAST = top_transform(AST),
|
|
|
|
%io:format("NewPT: ~p~n", [NewAST]),
|
2017-11-02 15:03:30 +01:00
|
|
|
NewAST ++ get(warnings).
|
2016-02-09 17:23:15 +01:00
|
|
|
|
|
|
|
|
2017-11-02 15:03:30 +01:00
|
|
|
format_error(no_server_host) ->
|
|
|
|
"server_host field is not used".
|
2016-02-09 17:23:15 +01:00
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
|
|
|
|
transform(Form) ->
|
|
|
|
case erl_syntax:type(Form) of
|
|
|
|
application ->
|
|
|
|
case erl_syntax_lib:analyze_application(Form) of
|
|
|
|
{?SQL_MARK, 1} ->
|
|
|
|
case erl_syntax:application_arguments(Form) of
|
|
|
|
[Arg] ->
|
|
|
|
case erl_syntax:type(Arg) of
|
|
|
|
string ->
|
2018-03-15 15:55:05 +01:00
|
|
|
transform_sql(Arg);
|
2016-02-09 17:23:15 +01:00
|
|
|
_ ->
|
|
|
|
throw({error, erl_syntax:get_pos(Form),
|
|
|
|
"?SQL argument must be "
|
|
|
|
"a constant string"})
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
throw({error, erl_syntax:get_pos(Form),
|
|
|
|
"wrong number of ?SQL args"})
|
|
|
|
end;
|
2016-02-18 15:38:25 +01:00
|
|
|
{?SQL_UPSERT_MARK, 2} ->
|
|
|
|
case erl_syntax:application_arguments(Form) of
|
|
|
|
[TableArg, FieldsArg] ->
|
|
|
|
case {erl_syntax:type(TableArg),
|
|
|
|
erl_syntax:is_proper_list(FieldsArg)}of
|
|
|
|
{string, true} ->
|
2018-03-15 15:55:05 +01:00
|
|
|
transform_upsert(Form, TableArg, FieldsArg);
|
2016-02-18 15:38:25 +01:00
|
|
|
_ ->
|
|
|
|
throw({error, erl_syntax:get_pos(Form),
|
|
|
|
"?SQL_UPSERT arguments must be "
|
|
|
|
"a constant string and a list"})
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
throw({error, erl_syntax:get_pos(Form),
|
|
|
|
"wrong number of ?SQL_UPSERT args"})
|
|
|
|
end;
|
2017-11-02 15:03:30 +01:00
|
|
|
{?SQL_INSERT_MARK, 2} ->
|
|
|
|
case erl_syntax:application_arguments(Form) of
|
|
|
|
[TableArg, FieldsArg] ->
|
|
|
|
case {erl_syntax:type(TableArg),
|
|
|
|
erl_syntax:is_proper_list(FieldsArg)}of
|
|
|
|
{string, true} ->
|
2018-03-15 15:55:05 +01:00
|
|
|
transform_insert(Form, TableArg, FieldsArg);
|
2017-11-02 15:03:30 +01:00
|
|
|
_ ->
|
|
|
|
throw({error, erl_syntax:get_pos(Form),
|
|
|
|
"?SQL_INSERT arguments must be "
|
|
|
|
"a constant string and a list"})
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
throw({error, erl_syntax:get_pos(Form),
|
|
|
|
"wrong number of ?SQL_INSERT args"})
|
|
|
|
end;
|
2016-02-09 17:23:15 +01:00
|
|
|
_ ->
|
|
|
|
Form
|
|
|
|
end;
|
|
|
|
attribute ->
|
|
|
|
case erl_syntax:atom_value(erl_syntax:attribute_name(Form)) of
|
|
|
|
module ->
|
|
|
|
case erl_syntax:attribute_arguments(Form) of
|
|
|
|
[M | _] ->
|
|
|
|
Module = erl_syntax:atom_value(M),
|
|
|
|
%io:format("module ~p~n", [Module]),
|
|
|
|
put(?MOD, Module),
|
|
|
|
Form;
|
|
|
|
_ ->
|
|
|
|
Form
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Form
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Form
|
|
|
|
end.
|
|
|
|
|
|
|
|
top_transform(Forms) when is_list(Forms) ->
|
|
|
|
lists:map(
|
|
|
|
fun(Form) ->
|
|
|
|
try
|
|
|
|
Form2 = erl_syntax_lib:map(
|
|
|
|
fun(Node) ->
|
|
|
|
%io:format("asd ~p~n", [Node]),
|
|
|
|
transform(Node)
|
|
|
|
end, Form),
|
|
|
|
Form3 = erl_syntax:revert(Form2),
|
|
|
|
Form3
|
|
|
|
catch
|
|
|
|
throw:{error, Line, Error} ->
|
|
|
|
{error, {Line, erl_parse, Error}}
|
|
|
|
end
|
|
|
|
end, Forms).
|
|
|
|
|
2018-03-15 15:55:05 +01:00
|
|
|
transform_sql(Arg) ->
|
|
|
|
S = erl_syntax:string_value(Arg),
|
|
|
|
Pos = erl_syntax:get_pos(Arg),
|
|
|
|
ParseRes = parse(S, Pos, true),
|
|
|
|
ParseResOld = parse(S, Pos, false),
|
|
|
|
case ParseRes#state.server_host_used of
|
|
|
|
{true, _SHVar} ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
add_warning(
|
|
|
|
Pos, no_server_host),
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
set_pos(
|
|
|
|
make_schema_check(
|
|
|
|
make_sql_query(ParseRes),
|
|
|
|
make_sql_query(ParseResOld)
|
|
|
|
),
|
|
|
|
Pos).
|
|
|
|
|
|
|
|
transform_upsert(Form, TableArg, FieldsArg) ->
|
|
|
|
Table = erl_syntax:string_value(TableArg),
|
|
|
|
ParseRes =
|
|
|
|
parse_upsert(
|
|
|
|
erl_syntax:list_elements(FieldsArg)),
|
|
|
|
Pos = erl_syntax:get_pos(Form),
|
|
|
|
case lists:keymember(
|
|
|
|
"server_host", 1, ParseRes) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
add_warning(Pos, no_server_host)
|
|
|
|
end,
|
|
|
|
ParseResOld =
|
|
|
|
filter_upsert_sh(Table, ParseRes),
|
|
|
|
set_pos(
|
|
|
|
make_schema_check(
|
|
|
|
make_sql_upsert(Table, ParseRes, Pos),
|
|
|
|
make_sql_upsert(Table, ParseResOld, Pos)
|
|
|
|
),
|
|
|
|
Pos).
|
|
|
|
|
|
|
|
transform_insert(Form, TableArg, FieldsArg) ->
|
|
|
|
Table = erl_syntax:string_value(TableArg),
|
|
|
|
ParseRes =
|
|
|
|
parse_insert(
|
|
|
|
erl_syntax:list_elements(FieldsArg)),
|
|
|
|
Pos = erl_syntax:get_pos(Form),
|
|
|
|
case lists:keymember(
|
|
|
|
"server_host", 1, ParseRes) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
add_warning(Pos, no_server_host)
|
|
|
|
end,
|
|
|
|
ParseResOld =
|
|
|
|
filter_upsert_sh(Table, ParseRes),
|
|
|
|
set_pos(
|
|
|
|
make_schema_check(
|
|
|
|
make_sql_insert(Table, ParseRes),
|
|
|
|
make_sql_insert(Table, ParseResOld)
|
|
|
|
),
|
|
|
|
Pos).
|
|
|
|
|
|
|
|
|
|
|
|
parse(S, Loc, UseNewSchema) ->
|
|
|
|
parse1(S, [],
|
|
|
|
#state{loc = Loc,
|
|
|
|
use_new_schema = UseNewSchema}).
|
|
|
|
|
|
|
|
parse(S, ParamPos, Loc, UseNewSchema) ->
|
|
|
|
parse1(S, [],
|
|
|
|
#state{loc = Loc,
|
|
|
|
param_pos = ParamPos,
|
|
|
|
use_new_schema = UseNewSchema}).
|
2016-02-18 15:38:25 +01:00
|
|
|
|
2016-02-09 17:23:15 +01:00
|
|
|
parse1([], Acc, State) ->
|
|
|
|
State1 = append_string(lists:reverse(Acc), State),
|
|
|
|
State1#state{'query' = lists:reverse(State1#state.'query'),
|
|
|
|
params = lists:reverse(State1#state.params),
|
|
|
|
args = lists:reverse(State1#state.args),
|
|
|
|
res = lists:reverse(State1#state.res),
|
|
|
|
res_vars = lists:reverse(State1#state.res_vars)
|
|
|
|
};
|
|
|
|
parse1([$@, $( | S], Acc, State) ->
|
|
|
|
State1 = append_string(lists:reverse(Acc), State),
|
2017-11-02 15:03:30 +01:00
|
|
|
{Name, Type, S1, State2} = parse_name(S, false, State1),
|
2016-02-09 17:23:15 +01:00
|
|
|
Var = "__V" ++ integer_to_list(State2#state.res_pos),
|
|
|
|
EVar = erl_syntax:variable(Var),
|
|
|
|
Convert =
|
|
|
|
case Type of
|
|
|
|
integer ->
|
|
|
|
erl_syntax:application(
|
|
|
|
erl_syntax:atom(binary_to_integer),
|
|
|
|
[EVar]);
|
|
|
|
string ->
|
|
|
|
EVar;
|
|
|
|
boolean ->
|
|
|
|
erl_syntax:application(
|
2016-04-20 11:27:32 +02:00
|
|
|
erl_syntax:atom(ejabberd_sql),
|
2016-02-09 17:23:15 +01:00
|
|
|
erl_syntax:atom(to_bool),
|
|
|
|
[EVar])
|
|
|
|
end,
|
|
|
|
State3 = append_string(Name, State2),
|
|
|
|
State4 = State3#state{res_pos = State3#state.res_pos + 1,
|
|
|
|
res = [Convert | State3#state.res],
|
|
|
|
res_vars = [EVar | State3#state.res_vars]},
|
|
|
|
parse1(S1, [], State4);
|
|
|
|
parse1([$%, $( | S], Acc, State) ->
|
|
|
|
State1 = append_string(lists:reverse(Acc), State),
|
2017-11-02 15:03:30 +01:00
|
|
|
{Name, Type, S1, State2} = parse_name(S, true, State1),
|
2016-02-18 15:38:25 +01:00
|
|
|
Var = State2#state.param_pos,
|
2016-02-09 17:23:15 +01:00
|
|
|
State4 =
|
2017-11-02 15:03:30 +01:00
|
|
|
case Type of
|
|
|
|
host ->
|
2017-11-03 15:42:53 +01:00
|
|
|
State3 =
|
|
|
|
State2#state{server_host_used = {true, Name},
|
|
|
|
used_vars = [Name | State2#state.used_vars]},
|
2018-03-15 15:55:05 +01:00
|
|
|
case State#state.use_new_schema of
|
2017-11-02 15:03:30 +01:00
|
|
|
true ->
|
|
|
|
Convert =
|
|
|
|
erl_syntax:application(
|
|
|
|
erl_syntax:record_access(
|
|
|
|
erl_syntax:variable(?ESCAPE_VAR),
|
|
|
|
erl_syntax:atom(?ESCAPE_RECORD),
|
|
|
|
erl_syntax:atom(string)),
|
|
|
|
[erl_syntax:variable(Name)]),
|
|
|
|
State3#state{'query' = [{var, Var},
|
|
|
|
{str, "server_host="} |
|
|
|
|
State3#state.'query'],
|
|
|
|
args = [Convert | State3#state.args],
|
|
|
|
params = [Var | State3#state.params],
|
|
|
|
param_pos = State3#state.param_pos + 1};
|
|
|
|
false ->
|
|
|
|
append_string("0=0", State3)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Convert =
|
|
|
|
erl_syntax:application(
|
|
|
|
erl_syntax:record_access(
|
|
|
|
erl_syntax:variable(?ESCAPE_VAR),
|
|
|
|
erl_syntax:atom(?ESCAPE_RECORD),
|
|
|
|
erl_syntax:atom(Type)),
|
|
|
|
[erl_syntax:variable(Name)]),
|
|
|
|
State2#state{'query' = [{var, Var} | State2#state.'query'],
|
|
|
|
args = [Convert | State2#state.args],
|
|
|
|
params = [Var | State2#state.params],
|
2017-11-03 15:42:53 +01:00
|
|
|
param_pos = State2#state.param_pos + 1,
|
|
|
|
used_vars = [Name | State2#state.used_vars]}
|
2017-11-02 15:03:30 +01:00
|
|
|
end,
|
2016-02-09 17:23:15 +01:00
|
|
|
parse1(S1, [], State4);
|
|
|
|
parse1([C | S], Acc, State) ->
|
|
|
|
parse1(S, [C | Acc], State).
|
|
|
|
|
|
|
|
append_string([], State) ->
|
|
|
|
State;
|
|
|
|
append_string(S, State) ->
|
|
|
|
State#state{query = [{str, S} | State#state.query]}.
|
|
|
|
|
2017-11-02 15:03:30 +01:00
|
|
|
parse_name(S, IsArg, State) ->
|
|
|
|
parse_name(S, [], 0, IsArg, State).
|
2016-02-09 17:23:15 +01:00
|
|
|
|
2017-11-02 15:03:30 +01:00
|
|
|
parse_name([], _Acc, _Depth, _IsArg, State) ->
|
2016-02-11 18:01:32 +01:00
|
|
|
throw({error, State#state.loc,
|
|
|
|
"expected ')', found end of string"});
|
2017-11-02 15:03:30 +01:00
|
|
|
parse_name([$), T | S], Acc, 0, IsArg, State) ->
|
2016-02-09 17:23:15 +01:00
|
|
|
Type =
|
|
|
|
case T of
|
|
|
|
$d -> integer;
|
|
|
|
$s -> string;
|
|
|
|
$b -> boolean;
|
2017-11-02 15:03:30 +01:00
|
|
|
$H when IsArg -> host;
|
2016-02-09 17:23:15 +01:00
|
|
|
_ ->
|
2016-02-11 18:01:32 +01:00
|
|
|
throw({error, State#state.loc,
|
|
|
|
["unknown type specifier '", T, "'"]})
|
2016-02-09 17:23:15 +01:00
|
|
|
end,
|
|
|
|
{lists:reverse(Acc), Type, S, State};
|
2017-11-02 15:03:30 +01:00
|
|
|
parse_name([$)], _Acc, 0, _IsArg, State) ->
|
2016-02-11 18:01:32 +01:00
|
|
|
throw({error, State#state.loc,
|
|
|
|
"expected type specifier, found end of string"});
|
2017-11-02 15:03:30 +01:00
|
|
|
parse_name([$( = C | S], Acc, Depth, IsArg, State) ->
|
|
|
|
parse_name(S, [C | Acc], Depth + 1, IsArg, State);
|
|
|
|
parse_name([$) = C | S], Acc, Depth, IsArg, State) ->
|
|
|
|
parse_name(S, [C | Acc], Depth - 1, IsArg, State);
|
|
|
|
parse_name([C | S], Acc, Depth, IsArg, State) ->
|
|
|
|
parse_name(S, [C | Acc], Depth, IsArg, State).
|
2016-02-09 17:23:15 +01:00
|
|
|
|
|
|
|
|
2016-02-18 15:38:25 +01:00
|
|
|
make_var(V) ->
|
|
|
|
Var = "__V" ++ integer_to_list(V),
|
|
|
|
erl_syntax:variable(Var).
|
|
|
|
|
|
|
|
|
2016-02-09 17:23:15 +01:00
|
|
|
make_sql_query(State) ->
|
2018-03-15 15:55:05 +01:00
|
|
|
Hash = erlang:phash2(State#state{loc = undefined, use_new_schema = true}),
|
2016-02-09 17:23:15 +01:00
|
|
|
SHash = <<"Q", (integer_to_binary(Hash))/binary>>,
|
|
|
|
Query = pack_query(State#state.'query'),
|
|
|
|
EQuery =
|
|
|
|
lists:map(
|
|
|
|
fun({str, S}) ->
|
|
|
|
erl_syntax:binary(
|
|
|
|
[erl_syntax:binary_field(
|
|
|
|
erl_syntax:string(S))]);
|
2016-02-18 15:38:25 +01:00
|
|
|
({var, V}) -> make_var(V)
|
2016-02-09 17:23:15 +01:00
|
|
|
end, Query),
|
|
|
|
erl_syntax:record_expr(
|
|
|
|
erl_syntax:atom(?QUERY_RECORD),
|
|
|
|
[erl_syntax:record_field(
|
|
|
|
erl_syntax:atom(hash),
|
|
|
|
%erl_syntax:abstract(SHash)
|
|
|
|
erl_syntax:binary(
|
|
|
|
[erl_syntax:binary_field(
|
|
|
|
erl_syntax:string(binary_to_list(SHash)))])),
|
|
|
|
erl_syntax:record_field(
|
|
|
|
erl_syntax:atom(args),
|
|
|
|
erl_syntax:fun_expr(
|
|
|
|
[erl_syntax:clause(
|
|
|
|
[erl_syntax:variable(?ESCAPE_VAR)],
|
|
|
|
none,
|
|
|
|
[erl_syntax:list(State#state.args)]
|
|
|
|
)])),
|
|
|
|
erl_syntax:record_field(
|
|
|
|
erl_syntax:atom(format_query),
|
|
|
|
erl_syntax:fun_expr(
|
|
|
|
[erl_syntax:clause(
|
2016-02-18 15:38:25 +01:00
|
|
|
[erl_syntax:list(lists:map(fun make_var/1, State#state.params))],
|
2016-02-09 17:23:15 +01:00
|
|
|
none,
|
|
|
|
[erl_syntax:list(EQuery)]
|
|
|
|
)])),
|
|
|
|
erl_syntax:record_field(
|
|
|
|
erl_syntax:atom(format_res),
|
|
|
|
erl_syntax:fun_expr(
|
|
|
|
[erl_syntax:clause(
|
|
|
|
[erl_syntax:list(State#state.res_vars)],
|
|
|
|
none,
|
|
|
|
[erl_syntax:tuple(State#state.res)]
|
|
|
|
)])),
|
|
|
|
erl_syntax:record_field(
|
|
|
|
erl_syntax:atom(loc),
|
|
|
|
erl_syntax:abstract({get(?MOD), State#state.loc}))
|
|
|
|
]).
|
|
|
|
|
|
|
|
pack_query([]) ->
|
|
|
|
[];
|
|
|
|
pack_query([{str, S1}, {str, S2} | Rest]) ->
|
|
|
|
pack_query([{str, S1 ++ S2} | Rest]);
|
|
|
|
pack_query([X | Rest]) ->
|
|
|
|
[X | pack_query(Rest)].
|
|
|
|
|
2016-02-18 15:38:25 +01:00
|
|
|
|
|
|
|
parse_upsert(Fields) ->
|
|
|
|
{Fs, _} =
|
|
|
|
lists:foldr(
|
|
|
|
fun(F, {Acc, Param}) ->
|
|
|
|
case erl_syntax:type(F) of
|
|
|
|
string ->
|
|
|
|
V = erl_syntax:string_value(F),
|
|
|
|
{_, _, State} = Res =
|
|
|
|
parse_upsert_field(
|
|
|
|
V, Param, erl_syntax:get_pos(F)),
|
|
|
|
{[Res | Acc], State#state.param_pos};
|
|
|
|
_ ->
|
|
|
|
throw({error, erl_syntax:get_pos(F),
|
|
|
|
"?SQL_UPSERT field must be "
|
|
|
|
"a constant string"})
|
|
|
|
end
|
|
|
|
end, {[], 0}, Fields),
|
2016-05-04 20:01:05 +02:00
|
|
|
%io:format("upsert ~p~n", [{Fields, Fs}]),
|
2016-02-18 15:38:25 +01:00
|
|
|
Fs.
|
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
%% key | {Update}
|
2016-02-18 15:38:25 +01:00
|
|
|
parse_upsert_field([$! | S], ParamPos, Loc) ->
|
|
|
|
{Name, ParseState} = parse_upsert_field1(S, [], ParamPos, Loc),
|
2016-05-04 20:01:05 +02:00
|
|
|
{Name, key, ParseState};
|
|
|
|
parse_upsert_field([$- | S], ParamPos, Loc) ->
|
|
|
|
{Name, ParseState} = parse_upsert_field1(S, [], ParamPos, Loc),
|
|
|
|
{Name, {false}, ParseState};
|
2016-02-18 15:38:25 +01:00
|
|
|
parse_upsert_field(S, ParamPos, Loc) ->
|
|
|
|
{Name, ParseState} = parse_upsert_field1(S, [], ParamPos, Loc),
|
2016-05-04 20:01:05 +02:00
|
|
|
{Name, {true}, ParseState}.
|
2016-02-18 15:38:25 +01:00
|
|
|
|
|
|
|
parse_upsert_field1([], _Acc, _ParamPos, Loc) ->
|
|
|
|
throw({error, Loc,
|
|
|
|
"?SQL_UPSERT fields must have the "
|
2016-05-04 20:01:05 +02:00
|
|
|
"following form: \"[!-]name=value\""});
|
2016-02-18 15:38:25 +01:00
|
|
|
parse_upsert_field1([$= | S], Acc, ParamPos, Loc) ->
|
2018-03-15 15:55:05 +01:00
|
|
|
{lists:reverse(Acc), parse(S, ParamPos, Loc, true)};
|
2016-02-18 15:38:25 +01:00
|
|
|
parse_upsert_field1([C | S], Acc, ParamPos, Loc) ->
|
|
|
|
parse_upsert_field1(S, [C | Acc], ParamPos, Loc).
|
|
|
|
|
|
|
|
|
2016-02-29 14:56:25 +01:00
|
|
|
make_sql_upsert(Table, ParseRes, Pos) ->
|
|
|
|
check_upsert(ParseRes, Pos),
|
2016-02-18 15:38:25 +01:00
|
|
|
erl_syntax:fun_expr(
|
|
|
|
[erl_syntax:clause(
|
|
|
|
[erl_syntax:atom(pgsql), erl_syntax:variable("__Version")],
|
|
|
|
[erl_syntax:infix_expr(
|
|
|
|
erl_syntax:variable("__Version"),
|
|
|
|
erl_syntax:operator('>='),
|
|
|
|
erl_syntax:integer(90100))],
|
|
|
|
[make_sql_upsert_pgsql901(Table, ParseRes),
|
|
|
|
erl_syntax:atom(ok)]),
|
|
|
|
erl_syntax:clause(
|
|
|
|
[erl_syntax:underscore(), erl_syntax:underscore()],
|
|
|
|
none,
|
|
|
|
[make_sql_upsert_generic(Table, ParseRes)])
|
|
|
|
]).
|
|
|
|
|
|
|
|
make_sql_upsert_generic(Table, ParseRes) ->
|
|
|
|
Update = make_sql_query(make_sql_upsert_update(Table, ParseRes)),
|
|
|
|
Insert = make_sql_query(make_sql_upsert_insert(Table, ParseRes)),
|
|
|
|
InsertBranch =
|
|
|
|
erl_syntax:case_expr(
|
|
|
|
erl_syntax:application(
|
2016-04-20 11:27:32 +02:00
|
|
|
erl_syntax:atom(ejabberd_sql),
|
2016-02-18 15:38:25 +01:00
|
|
|
erl_syntax:atom(sql_query_t),
|
|
|
|
[Insert]),
|
|
|
|
[erl_syntax:clause(
|
|
|
|
[erl_syntax:abstract({updated, 1})],
|
|
|
|
none,
|
|
|
|
[erl_syntax:atom(ok)]),
|
|
|
|
erl_syntax:clause(
|
|
|
|
[erl_syntax:variable("__UpdateRes")],
|
|
|
|
none,
|
|
|
|
[erl_syntax:variable("__UpdateRes")])]),
|
|
|
|
erl_syntax:case_expr(
|
|
|
|
erl_syntax:application(
|
2016-04-20 11:27:32 +02:00
|
|
|
erl_syntax:atom(ejabberd_sql),
|
2016-02-18 15:38:25 +01:00
|
|
|
erl_syntax:atom(sql_query_t),
|
|
|
|
[Update]),
|
|
|
|
[erl_syntax:clause(
|
|
|
|
[erl_syntax:abstract({updated, 1})],
|
|
|
|
none,
|
|
|
|
[erl_syntax:atom(ok)]),
|
|
|
|
erl_syntax:clause(
|
|
|
|
[erl_syntax:underscore()],
|
|
|
|
none,
|
|
|
|
[InsertBranch])]).
|
|
|
|
|
|
|
|
make_sql_upsert_update(Table, ParseRes) ->
|
|
|
|
WPairs =
|
|
|
|
lists:flatmap(
|
2016-05-04 20:01:05 +02:00
|
|
|
fun({_Field, {_}, _ST}) ->
|
2016-02-18 15:38:25 +01:00
|
|
|
[];
|
2016-05-04 20:01:05 +02:00
|
|
|
({Field, key, ST}) ->
|
2016-02-18 15:38:25 +01:00
|
|
|
[ST#state{
|
|
|
|
'query' = [{str, Field}, {str, "="}] ++ ST#state.'query'
|
|
|
|
}]
|
|
|
|
end, ParseRes),
|
|
|
|
Where = join_states(WPairs, " AND "),
|
|
|
|
SPairs =
|
|
|
|
lists:flatmap(
|
2016-05-04 20:01:05 +02:00
|
|
|
fun({_Field, key, _ST}) ->
|
|
|
|
[];
|
|
|
|
({_Field, {false}, _ST}) ->
|
2016-02-18 15:38:25 +01:00
|
|
|
[];
|
2016-05-04 20:01:05 +02:00
|
|
|
({Field, {true}, ST}) ->
|
2016-02-18 15:38:25 +01:00
|
|
|
[ST#state{
|
|
|
|
'query' = [{str, Field}, {str, "="}] ++ ST#state.'query'
|
|
|
|
}]
|
|
|
|
end, ParseRes),
|
|
|
|
Set = join_states(SPairs, ", "),
|
|
|
|
State =
|
|
|
|
concat_states(
|
|
|
|
[#state{'query' = [{str, "UPDATE "}, {str, Table}, {str, " SET "}]},
|
|
|
|
Set,
|
|
|
|
#state{'query' = [{str, " WHERE "}]},
|
|
|
|
Where
|
|
|
|
]),
|
|
|
|
State.
|
|
|
|
|
|
|
|
make_sql_upsert_insert(Table, ParseRes) ->
|
|
|
|
Vals =
|
|
|
|
lists:map(
|
|
|
|
fun({_Field, _, ST}) ->
|
|
|
|
ST
|
|
|
|
end, ParseRes),
|
|
|
|
Fields =
|
|
|
|
lists:map(
|
|
|
|
fun({Field, _, _ST}) ->
|
|
|
|
#state{'query' = [{str, Field}]}
|
|
|
|
end, ParseRes),
|
|
|
|
State =
|
|
|
|
concat_states(
|
|
|
|
[#state{'query' = [{str, "INSERT INTO "}, {str, Table}, {str, "("}]},
|
|
|
|
join_states(Fields, ", "),
|
|
|
|
#state{'query' = [{str, ") VALUES ("}]},
|
|
|
|
join_states(Vals, ", "),
|
2017-11-02 15:03:30 +01:00
|
|
|
#state{'query' = [{str, ");"}]}
|
2016-02-18 15:38:25 +01:00
|
|
|
]),
|
|
|
|
State.
|
|
|
|
|
|
|
|
make_sql_upsert_pgsql901(Table, ParseRes) ->
|
|
|
|
Update = make_sql_upsert_update(Table, ParseRes),
|
|
|
|
Vals =
|
|
|
|
lists:map(
|
|
|
|
fun({_Field, _, ST}) ->
|
|
|
|
ST
|
|
|
|
end, ParseRes),
|
|
|
|
Fields =
|
|
|
|
lists:map(
|
|
|
|
fun({Field, _, _ST}) ->
|
|
|
|
#state{'query' = [{str, Field}]}
|
|
|
|
end, ParseRes),
|
|
|
|
Insert =
|
|
|
|
concat_states(
|
|
|
|
[#state{'query' = [{str, "INSERT INTO "}, {str, Table}, {str, "("}]},
|
|
|
|
join_states(Fields, ", "),
|
|
|
|
#state{'query' = [{str, ") SELECT "}]},
|
|
|
|
join_states(Vals, ", "),
|
|
|
|
#state{'query' = [{str, " WHERE NOT EXISTS (SELECT * FROM upsert)"}]}
|
|
|
|
]),
|
|
|
|
State =
|
|
|
|
concat_states(
|
|
|
|
[#state{'query' = [{str, "WITH upsert AS ("}]},
|
|
|
|
Update,
|
|
|
|
#state{'query' = [{str, " RETURNING *) "}]},
|
|
|
|
Insert
|
|
|
|
]),
|
|
|
|
Upsert = make_sql_query(State),
|
|
|
|
erl_syntax:application(
|
2016-04-20 11:27:32 +02:00
|
|
|
erl_syntax:atom(ejabberd_sql),
|
2016-02-18 15:38:25 +01:00
|
|
|
erl_syntax:atom(sql_query_t),
|
|
|
|
[Upsert]).
|
|
|
|
|
|
|
|
|
2016-02-29 14:56:25 +01:00
|
|
|
check_upsert(ParseRes, Pos) ->
|
|
|
|
Set =
|
|
|
|
lists:filter(
|
|
|
|
fun({_Field, Match, _ST}) ->
|
2016-05-04 20:01:05 +02:00
|
|
|
Match /= key
|
2016-02-29 14:56:25 +01:00
|
|
|
end, ParseRes),
|
|
|
|
case Set of
|
|
|
|
[] ->
|
|
|
|
throw({error, Pos,
|
|
|
|
"No ?SQL_UPSERT fields to set, use INSERT instead"});
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
ok.
|
|
|
|
|
|
|
|
|
2017-11-02 15:03:30 +01:00
|
|
|
parse_insert(Fields) ->
|
|
|
|
{Fs, _} =
|
|
|
|
lists:foldr(
|
|
|
|
fun(F, {Acc, Param}) ->
|
|
|
|
case erl_syntax:type(F) of
|
|
|
|
string ->
|
|
|
|
V = erl_syntax:string_value(F),
|
|
|
|
{_, _, State} = Res =
|
|
|
|
parse_insert_field(
|
|
|
|
V, Param, erl_syntax:get_pos(F)),
|
|
|
|
{[Res | Acc], State#state.param_pos};
|
|
|
|
_ ->
|
|
|
|
throw({error, erl_syntax:get_pos(F),
|
|
|
|
"?SQL_INSERT field must be "
|
|
|
|
"a constant string"})
|
|
|
|
end
|
|
|
|
end, {[], 0}, Fields),
|
|
|
|
Fs.
|
|
|
|
|
|
|
|
parse_insert_field([$! | _S], _ParamPos, Loc) ->
|
|
|
|
throw({error, Loc,
|
|
|
|
"?SQL_INSERT fields must not start with \"!\""});
|
|
|
|
parse_insert_field([$- | _S], _ParamPos, Loc) ->
|
|
|
|
throw({error, Loc,
|
|
|
|
"?SQL_INSERT fields must not start with \"-\""});
|
|
|
|
parse_insert_field(S, ParamPos, Loc) ->
|
|
|
|
{Name, ParseState} = parse_insert_field1(S, [], ParamPos, Loc),
|
|
|
|
{Name, {true}, ParseState}.
|
|
|
|
|
|
|
|
parse_insert_field1([], _Acc, _ParamPos, Loc) ->
|
|
|
|
throw({error, Loc,
|
|
|
|
"?SQL_INSERT fields must have the "
|
|
|
|
"following form: \"name=value\""});
|
|
|
|
parse_insert_field1([$= | S], Acc, ParamPos, Loc) ->
|
2018-03-15 15:55:05 +01:00
|
|
|
{lists:reverse(Acc), parse(S, ParamPos, Loc, true)};
|
2017-11-02 15:03:30 +01:00
|
|
|
parse_insert_field1([C | S], Acc, ParamPos, Loc) ->
|
|
|
|
parse_insert_field1(S, [C | Acc], ParamPos, Loc).
|
|
|
|
|
|
|
|
|
|
|
|
make_sql_insert(Table, ParseRes) ->
|
|
|
|
make_sql_query(make_sql_upsert_insert(Table, ParseRes)).
|
|
|
|
|
2018-03-15 15:55:05 +01:00
|
|
|
make_schema_check(Tree, Tree) ->
|
|
|
|
Tree;
|
|
|
|
make_schema_check(New, Old) ->
|
|
|
|
erl_syntax:case_expr(
|
|
|
|
erl_syntax:application(
|
|
|
|
erl_syntax:atom(ejabberd_sql),
|
|
|
|
erl_syntax:atom(use_new_schema),
|
|
|
|
[]),
|
|
|
|
[erl_syntax:clause(
|
|
|
|
[erl_syntax:abstract(true)],
|
|
|
|
none,
|
|
|
|
[New]),
|
|
|
|
erl_syntax:clause(
|
|
|
|
[erl_syntax:abstract(false)],
|
|
|
|
none,
|
|
|
|
[Old])]).
|
|
|
|
|
2017-11-02 15:03:30 +01:00
|
|
|
|
2016-02-18 15:38:25 +01:00
|
|
|
concat_states(States) ->
|
|
|
|
lists:foldr(
|
|
|
|
fun(ST11, ST2) ->
|
|
|
|
ST1 = resolve_vars(ST11, ST2),
|
|
|
|
ST1#state{
|
|
|
|
'query' = ST1#state.'query' ++ ST2#state.'query',
|
|
|
|
params = ST1#state.params ++ ST2#state.params,
|
|
|
|
args = ST1#state.args ++ ST2#state.args,
|
|
|
|
res = ST1#state.res ++ ST2#state.res,
|
|
|
|
res_vars = ST1#state.res_vars ++ ST2#state.res_vars,
|
|
|
|
loc = case ST1#state.loc of
|
|
|
|
undefined -> ST2#state.loc;
|
|
|
|
_ -> ST1#state.loc
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end, #state{}, States).
|
|
|
|
|
|
|
|
resolve_vars(ST1, ST2) ->
|
|
|
|
Max = lists:max([0 | ST1#state.params ++ ST2#state.params]),
|
|
|
|
{Map, _} =
|
|
|
|
lists:foldl(
|
|
|
|
fun(Var, {Acc, New}) ->
|
|
|
|
case lists:member(Var, ST2#state.params) of
|
|
|
|
true ->
|
|
|
|
{dict:store(Var, New, Acc), New + 1};
|
|
|
|
false ->
|
|
|
|
{Acc, New}
|
|
|
|
end
|
|
|
|
end, {dict:new(), Max + 1}, ST1#state.params),
|
|
|
|
NewParams =
|
|
|
|
lists:map(
|
|
|
|
fun(Var) ->
|
|
|
|
case dict:find(Var, Map) of
|
|
|
|
{ok, New} ->
|
|
|
|
New;
|
|
|
|
error ->
|
|
|
|
Var
|
|
|
|
end
|
|
|
|
end, ST1#state.params),
|
|
|
|
NewQuery =
|
|
|
|
lists:map(
|
|
|
|
fun({var, Var}) ->
|
|
|
|
case dict:find(Var, Map) of
|
|
|
|
{ok, New} ->
|
|
|
|
{var, New};
|
|
|
|
error ->
|
|
|
|
{var, Var}
|
|
|
|
end;
|
|
|
|
(S) -> S
|
|
|
|
end, ST1#state.'query'),
|
|
|
|
ST1#state{params = NewParams, 'query' = NewQuery}.
|
|
|
|
|
|
|
|
|
|
|
|
join_states([], _Sep) ->
|
|
|
|
#state{};
|
|
|
|
join_states([H | T], Sep) ->
|
|
|
|
J = [[H] | [[#state{'query' = [{str, Sep}]}, X] || X <- T]],
|
|
|
|
concat_states(lists:append(J)).
|
2016-02-29 14:56:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
set_pos(Tree, Pos) ->
|
|
|
|
erl_syntax_lib:map(
|
|
|
|
fun(Node) ->
|
|
|
|
case erl_syntax:get_pos(Node) of
|
|
|
|
0 -> erl_syntax:set_pos(Node, Pos);
|
|
|
|
_ -> Node
|
|
|
|
end
|
|
|
|
end, Tree).
|
2017-11-02 15:03:30 +01:00
|
|
|
|
|
|
|
filter_upsert_sh(Table, ParseRes) ->
|
2018-03-15 15:55:05 +01:00
|
|
|
lists:filter(
|
|
|
|
fun({Field, _Match, _ST}) ->
|
|
|
|
Field /= "server_host" orelse Table == "route"
|
|
|
|
end, ParseRes).
|
2017-11-03 15:42:53 +01:00
|
|
|
|
2017-11-02 17:48:35 +01:00
|
|
|
-ifdef(ENABLE_PT_WARNINGS).
|
|
|
|
|
2017-11-02 15:03:30 +01:00
|
|
|
add_warning(Pos, Warning) ->
|
|
|
|
Marker = erl_syntax:revert(
|
|
|
|
erl_syntax:warning_marker({Pos, ?MODULE, Warning})),
|
|
|
|
put(warnings, [Marker | get(warnings)]),
|
|
|
|
ok.
|
2017-11-02 17:48:35 +01:00
|
|
|
|
|
|
|
-else.
|
|
|
|
|
|
|
|
add_warning(_Pos, _Warning) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
-endif.
|