2004-12-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
2016-05-04 09:11:18 +02:00
|
|
|
%%% File : ejabberd_sql.erl
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2016-05-04 09:11:18 +02:00
|
|
|
%%% Purpose : Serve SQL connection
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Created : 8 Dec 2004 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
|
2007-12-24 14:57:53 +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.
|
2008-08-04 15:41:16 +02:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% 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.
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
2004-12-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2016-04-20 11:27:32 +02:00
|
|
|
-module(ejabberd_sql).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2007-12-24 14:57:53 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2017-08-05 19:58:21 +02:00
|
|
|
-behaviour(p1_fsm).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
|
|
|
%% External exports
|
2008-10-06 16:18:46 +02:00
|
|
|
-export([start/1, start_link/2,
|
2005-07-13 05:24:13 +02:00
|
|
|
sql_query/2,
|
2005-11-16 03:59:05 +01:00
|
|
|
sql_query_t/1,
|
|
|
|
sql_transaction/2,
|
2009-04-24 12:27:31 +02:00
|
|
|
sql_bloc/2,
|
2017-03-31 16:37:18 +02:00
|
|
|
abort/1,
|
|
|
|
restart/1,
|
2018-03-15 15:55:05 +01:00
|
|
|
use_new_schema/0,
|
2016-05-04 20:01:05 +02:00
|
|
|
sql_query_to_iolist/1,
|
2006-01-13 11:59:52 +01:00
|
|
|
escape/1,
|
2016-05-24 17:44:07 +02:00
|
|
|
standard_escape/1,
|
2007-08-16 18:37:00 +02:00
|
|
|
escape_like/1,
|
2016-02-25 18:31:58 +01:00
|
|
|
escape_like_arg/1,
|
2016-05-12 17:32:13 +02:00
|
|
|
escape_like_arg_circumflex/1,
|
2010-07-14 13:23:21 +02:00
|
|
|
to_bool/1,
|
2015-04-08 14:31:28 +02:00
|
|
|
sqlite_db/1,
|
|
|
|
sqlite_file/1,
|
2015-10-07 00:06:58 +02:00
|
|
|
encode_term/1,
|
|
|
|
decode_term/1,
|
2015-09-13 16:41:54 +02:00
|
|
|
odbc_config/0,
|
|
|
|
freetds_config/0,
|
|
|
|
odbcinst_config/0,
|
|
|
|
init_mssql/1,
|
2017-05-05 12:20:28 +02:00
|
|
|
keep_alive/2]).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2010-01-31 12:41:28 +01:00
|
|
|
%% gen_fsm callbacks
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_event/3, handle_sync_event/4,
|
|
|
|
handle_info/3, terminate/3, print_state/1,
|
2010-01-31 12:41:28 +01:00
|
|
|
code_change/4]).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([connecting/2, connecting/3,
|
2015-06-01 14:38:27 +02:00
|
|
|
session_established/2, session_established/3,
|
|
|
|
opt_type/1]).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-02-09 17:23:15 +01:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2005-12-22 10:40:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(state,
|
|
|
|
{db_ref = self() :: pid(),
|
2015-10-07 00:06:58 +02:00
|
|
|
db_type = odbc :: pgsql | mysql | sqlite | odbc | mssql,
|
2016-02-11 18:00:00 +01:00
|
|
|
db_version = undefined :: undefined | non_neg_integer(),
|
2015-10-07 00:06:58 +02:00
|
|
|
start_interval = 0 :: non_neg_integer(),
|
|
|
|
host = <<"">> :: binary(),
|
2017-03-10 13:12:43 +01:00
|
|
|
pending_requests :: p1_queue:queue()}).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2016-04-20 11:27:32 +02:00
|
|
|
-define(STATE_KEY, ejabberd_sql_state).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-04-20 11:27:32 +02:00
|
|
|
-define(NESTING_KEY, ejabberd_sql_nesting_level).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-07-28 15:46:28 +02:00
|
|
|
-define(TOP_LEVEL_TXN, 0).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-03-10 16:53:53 +01:00
|
|
|
-define(PGSQL_PORT, 5432).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-01-02 18:39:04 +01:00
|
|
|
-define(MYSQL_PORT, 3306).
|
2005-11-16 03:59:05 +01:00
|
|
|
|
2015-09-13 16:41:54 +02:00
|
|
|
-define(MSSQL_PORT, 1433).
|
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
-define(MAX_TRANSACTION_RESTARTS, 10).
|
|
|
|
|
2015-10-15 10:07:06 +02:00
|
|
|
-define(KEEPALIVE_QUERY, [<<"SELECT 1;">>]).
|
2007-08-16 18:37:00 +02:00
|
|
|
|
2016-04-20 11:27:32 +02:00
|
|
|
-define(PREPARE_KEY, ejabberd_sql_prepare).
|
2016-02-09 17:23:15 +01:00
|
|
|
|
2018-03-15 15:55:05 +01:00
|
|
|
-ifdef(NEW_SQL_SCHEMA).
|
|
|
|
-define(USE_NEW_SCHEMA_DEFAULT, true).
|
|
|
|
-else.
|
|
|
|
-define(USE_NEW_SCHEMA_DEFAULT, false).
|
|
|
|
-endif.
|
|
|
|
|
2010-01-31 12:41:28 +01:00
|
|
|
%%-define(DBGFSM, true).
|
|
|
|
|
|
|
|
-ifdef(DBGFSM).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2010-01-31 12:41:28 +01:00
|
|
|
-define(FSMOPTS, [{debug, [trace]}]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2010-01-31 12:41:28 +01:00
|
|
|
-else.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2010-01-31 12:41:28 +01:00
|
|
|
-define(FSMOPTS, []).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2010-01-31 12:41:28 +01:00
|
|
|
-endif.
|
|
|
|
|
2004-12-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2005-07-13 05:24:13 +02:00
|
|
|
start(Host) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:start(ejabberd_sql, [Host],
|
2013-03-14 10:33:02 +01:00
|
|
|
fsm_limit_opts() ++ (?FSMOPTS)).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2008-10-06 16:18:46 +02:00
|
|
|
start_link(Host, StartInterval) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:start_link(ejabberd_sql,
|
2013-03-14 10:33:02 +01:00
|
|
|
[Host, StartInterval],
|
|
|
|
fsm_limit_opts() ++ (?FSMOPTS)).
|
|
|
|
|
2016-02-26 13:45:28 +01:00
|
|
|
-type sql_query() :: [sql_query() | binary()] | #sql_query{} |
|
|
|
|
fun(() -> any()) | fun((atom(), _) -> any()).
|
2013-03-14 10:33:02 +01:00
|
|
|
-type sql_query_result() :: {updated, non_neg_integer()} |
|
|
|
|
{error, binary()} |
|
|
|
|
{selected, [binary()],
|
2016-02-09 17:23:15 +01:00
|
|
|
[[binary()]]} |
|
2016-02-25 18:31:58 +01:00
|
|
|
{selected, [any()]}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec sql_query(binary(), sql_query()) -> sql_query_result().
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
sql_query(Host, Query) ->
|
2016-01-13 10:59:00 +01:00
|
|
|
check_error(sql_call(Host, {sql_query, Query}), Query).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2005-12-21 17:10:56 +01:00
|
|
|
%% SQL transaction based on a list of queries
|
2007-08-16 18:37:00 +02:00
|
|
|
%% This function automatically
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec sql_transaction(binary(), [sql_query()] | fun(() -> any())) ->
|
|
|
|
{atomic, any()} |
|
|
|
|
{aborted, any()}.
|
|
|
|
|
|
|
|
sql_transaction(Host, Queries)
|
|
|
|
when is_list(Queries) ->
|
|
|
|
F = fun () ->
|
|
|
|
lists:foreach(fun (Query) -> sql_query_t(Query) end,
|
2005-12-21 17:10:56 +01:00
|
|
|
Queries)
|
|
|
|
end,
|
|
|
|
sql_transaction(Host, F);
|
|
|
|
%% SQL transaction, based on a erlang anonymous function (F = fun)
|
2009-07-28 15:46:28 +02:00
|
|
|
sql_transaction(Host, F) when is_function(F) ->
|
|
|
|
sql_call(Host, {sql_transaction, F}).
|
2005-11-16 03:59:05 +01:00
|
|
|
|
2009-04-24 12:27:31 +02:00
|
|
|
%% SQL bloc, based on a erlang anonymous function (F = fun)
|
2013-03-14 10:33:02 +01:00
|
|
|
sql_bloc(Host, F) -> sql_call(Host, {sql_bloc, F}).
|
2009-05-21 18:19:33 +02:00
|
|
|
|
|
|
|
sql_call(Host, Msg) ->
|
|
|
|
case get(?STATE_KEY) of
|
2013-03-14 10:33:02 +01:00
|
|
|
undefined ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case ejabberd_sql_sup:get_random_pid(Host) of
|
2014-04-30 17:20:58 +02:00
|
|
|
none -> {error, <<"Unknown Host">>};
|
2014-04-24 08:34:41 +02:00
|
|
|
Pid ->
|
2017-12-17 17:46:55 +01:00
|
|
|
sync_send_event(Pid,{sql_cmd, Msg,
|
|
|
|
p1_time_compat:monotonic_time(milli_seconds)},
|
|
|
|
query_timeout(Host))
|
2014-04-24 08:34:41 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
_State -> nested_op(Msg)
|
2009-05-21 18:19:33 +02:00
|
|
|
end.
|
|
|
|
|
2017-05-05 12:20:28 +02:00
|
|
|
keep_alive(Host, PID) ->
|
2017-12-17 17:46:55 +01:00
|
|
|
sync_send_event(PID,
|
|
|
|
{sql_cmd, {sql_query, ?KEEPALIVE_QUERY},
|
|
|
|
p1_time_compat:monotonic_time(milli_seconds)},
|
|
|
|
query_timeout(Host)).
|
|
|
|
|
|
|
|
sync_send_event(Pid, Msg, Timeout) ->
|
|
|
|
try p1_fsm:sync_send_event(Pid, Msg, Timeout)
|
|
|
|
catch _:{Reason, {p1_fsm, _, _}} ->
|
|
|
|
{error, Reason}
|
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec sql_query_t(sql_query()) -> sql_query_result().
|
2009-04-24 12:27:31 +02:00
|
|
|
|
2005-12-21 17:10:56 +01:00
|
|
|
%% This function is intended to be used from inside an sql_transaction:
|
2005-11-16 03:59:05 +01:00
|
|
|
sql_query_t(Query) ->
|
2009-07-28 15:46:28 +02:00
|
|
|
QRes = sql_query_internal(Query),
|
2005-11-16 03:59:05 +01:00
|
|
|
case QRes of
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, Reason} -> throw({aborted, Reason});
|
|
|
|
Rs when is_list(Rs) ->
|
|
|
|
case lists:keysearch(error, 1, Rs) of
|
|
|
|
{value, {error, Reason}} -> throw({aborted, Reason});
|
|
|
|
_ -> QRes
|
|
|
|
end;
|
|
|
|
_ -> QRes
|
2005-11-16 03:59:05 +01:00
|
|
|
end.
|
|
|
|
|
2017-03-31 16:37:18 +02:00
|
|
|
abort(Reason) ->
|
|
|
|
exit(Reason).
|
|
|
|
|
|
|
|
restart(Reason) ->
|
|
|
|
throw({aborted, Reason}).
|
|
|
|
|
2017-05-23 11:25:13 +02:00
|
|
|
-spec escape_char(char()) -> binary().
|
|
|
|
escape_char($\000) -> <<"\\0">>;
|
|
|
|
escape_char($\n) -> <<"\\n">>;
|
|
|
|
escape_char($\t) -> <<"\\t">>;
|
|
|
|
escape_char($\b) -> <<"\\b">>;
|
|
|
|
escape_char($\r) -> <<"\\r">>;
|
|
|
|
escape_char($') -> <<"''">>;
|
|
|
|
escape_char($") -> <<"\\\"">>;
|
|
|
|
escape_char($\\) -> <<"\\\\">>;
|
|
|
|
escape_char(C) -> <<C>>.
|
|
|
|
|
|
|
|
-spec escape(binary()) -> binary().
|
2013-03-14 10:33:02 +01:00
|
|
|
escape(S) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
<< <<(escape_char(Char))/binary>> || <<Char>> <= S >>.
|
2006-01-13 11:59:52 +01:00
|
|
|
|
|
|
|
%% Escape character that will confuse an SQL engine
|
|
|
|
%% Percent and underscore only need to be escaped for pattern matching like
|
|
|
|
%% statement
|
2013-03-14 10:33:02 +01:00
|
|
|
escape_like(S) when is_binary(S) ->
|
|
|
|
<< <<(escape_like(C))/binary>> || <<C>> <= S >>;
|
|
|
|
escape_like($%) -> <<"\\%">>;
|
|
|
|
escape_like($_) -> <<"\\_">>;
|
2016-05-12 17:32:13 +02:00
|
|
|
escape_like($\\) -> <<"\\\\\\\\">>;
|
2017-05-23 11:25:13 +02:00
|
|
|
escape_like(C) when is_integer(C), C >= 0, C =< 255 -> escape_char(C).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-02-25 18:31:58 +01:00
|
|
|
escape_like_arg(S) when is_binary(S) ->
|
|
|
|
<< <<(escape_like_arg(C))/binary>> || <<C>> <= S >>;
|
|
|
|
escape_like_arg($%) -> <<"\\%">>;
|
|
|
|
escape_like_arg($_) -> <<"\\_">>;
|
|
|
|
escape_like_arg($\\) -> <<"\\\\">>;
|
|
|
|
escape_like_arg(C) when is_integer(C), C >= 0, C =< 255 -> <<C>>.
|
|
|
|
|
2016-05-12 17:32:13 +02:00
|
|
|
escape_like_arg_circumflex(S) when is_binary(S) ->
|
|
|
|
<< <<(escape_like_arg_circumflex(C))/binary>> || <<C>> <= S >>;
|
|
|
|
escape_like_arg_circumflex($%) -> <<"^%">>;
|
|
|
|
escape_like_arg_circumflex($_) -> <<"^_">>;
|
|
|
|
escape_like_arg_circumflex($^) -> <<"^^">>;
|
2016-05-24 17:44:07 +02:00
|
|
|
escape_like_arg_circumflex($[) -> <<"^[">>; % For MSSQL
|
|
|
|
escape_like_arg_circumflex($]) -> <<"^]">>;
|
2016-05-12 17:32:13 +02:00
|
|
|
escape_like_arg_circumflex(C) when is_integer(C), C >= 0, C =< 255 -> <<C>>.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
to_bool(<<"t">>) -> true;
|
|
|
|
to_bool(<<"true">>) -> true;
|
|
|
|
to_bool(<<"1">>) -> true;
|
2010-07-14 13:23:21 +02:00
|
|
|
to_bool(true) -> true;
|
|
|
|
to_bool(1) -> true;
|
|
|
|
to_bool(_) -> false.
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2012-04-06 04:31:29 +02:00
|
|
|
encode_term(Term) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
escape(list_to_binary(
|
2015-05-17 19:15:52 +02:00
|
|
|
erl_prettypr:format(erl_syntax:abstract(Term),
|
|
|
|
[{paper, 65535}, {ribbon, 65535}]))).
|
2012-04-06 04:31:29 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
decode_term(Bin) ->
|
|
|
|
Str = binary_to_list(<<Bin/binary, ".">>),
|
|
|
|
{ok, Tokens, _} = erl_scan:string(Str),
|
2012-04-06 04:31:29 +02:00
|
|
|
{ok, Term} = erl_parse:parse_term(Tokens),
|
|
|
|
Term.
|
|
|
|
|
2015-04-08 14:31:28 +02:00
|
|
|
-spec sqlite_db(binary()) -> atom().
|
|
|
|
sqlite_db(Host) ->
|
|
|
|
list_to_atom("ejabberd_sqlite_" ++ binary_to_list(Host)).
|
|
|
|
|
|
|
|
-spec sqlite_file(binary()) -> string().
|
|
|
|
sqlite_file(Host) ->
|
2017-04-29 10:39:40 +02:00
|
|
|
case ejabberd_config:get_option({sql_database, Host}) of
|
2015-04-08 14:31:28 +02:00
|
|
|
undefined ->
|
|
|
|
{ok, Cwd} = file:get_cwd(),
|
|
|
|
filename:join([Cwd, "sqlite", atom_to_list(node()),
|
|
|
|
binary_to_list(Host), "ejabberd.db"]);
|
|
|
|
File ->
|
|
|
|
binary_to_list(File)
|
|
|
|
end.
|
|
|
|
|
2018-03-15 15:55:05 +01:00
|
|
|
use_new_schema() ->
|
|
|
|
ejabberd_config:get_option(new_sql_schema, ?USE_NEW_SCHEMA_DEFAULT).
|
|
|
|
|
2004-12-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
2010-01-31 12:41:28 +01:00
|
|
|
%%% Callback functions from gen_fsm
|
2004-12-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
2008-10-06 16:18:46 +02:00
|
|
|
init([Host, StartInterval]) ->
|
2017-12-17 17:46:55 +01:00
|
|
|
process_flag(trap_exit, true),
|
2017-04-29 10:39:40 +02:00
|
|
|
case ejabberd_config:get_option({sql_keepalive_interval, Host}) of
|
2013-03-14 10:33:02 +01:00
|
|
|
undefined ->
|
|
|
|
ok;
|
|
|
|
KeepaliveInterval ->
|
|
|
|
timer:apply_interval(KeepaliveInterval * 1000, ?MODULE,
|
2017-05-05 12:20:28 +02:00
|
|
|
keep_alive, [Host, self()])
|
2007-08-16 18:37:00 +02:00
|
|
|
end,
|
2010-01-31 12:41:28 +01:00
|
|
|
[DBType | _] = db_opts(Host),
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:send_event(self(), connect),
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql_sup:add_pid(Host, self()),
|
2017-04-29 10:39:40 +02:00
|
|
|
QueueType = case ejabberd_config:get_option({sql_queue_type, Host}) of
|
2017-03-10 13:12:43 +01:00
|
|
|
undefined ->
|
|
|
|
ejabberd_config:default_queue_type(Host);
|
|
|
|
Type ->
|
|
|
|
Type
|
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, connecting,
|
|
|
|
#state{db_type = DBType, host = Host,
|
2017-03-10 13:12:43 +01:00
|
|
|
pending_requests = p1_queue:new(QueueType, max_fsm_queue()),
|
2013-03-14 10:33:02 +01:00
|
|
|
start_interval = StartInterval}}.
|
2010-01-31 12:41:28 +01:00
|
|
|
|
|
|
|
connecting(connect, #state{host = Host} = State) ->
|
|
|
|
ConnectRes = case db_opts(Host) of
|
2017-05-05 15:25:10 +02:00
|
|
|
[mysql | Args] -> apply(fun mysql_connect/8, Args);
|
|
|
|
[pgsql | Args] -> apply(fun pgsql_connect/8, Args);
|
2015-03-16 19:53:19 +01:00
|
|
|
[sqlite | Args] -> apply(fun sqlite_connect/1, Args);
|
2017-05-05 15:25:10 +02:00
|
|
|
[mssql | Args] -> apply(fun odbc_connect/2, Args);
|
|
|
|
[odbc | Args] -> apply(fun odbc_connect/2, Args)
|
2010-01-31 12:41:28 +01:00
|
|
|
end,
|
|
|
|
case ConnectRes of
|
2016-02-11 18:00:00 +01:00
|
|
|
{ok, Ref} ->
|
|
|
|
erlang:monitor(process, Ref),
|
2017-04-03 15:30:02 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({{?PREPARE_KEY, _} = Key, _}) ->
|
|
|
|
erase(Key);
|
|
|
|
(_) ->
|
|
|
|
ok
|
|
|
|
end, get()),
|
2017-03-10 13:12:43 +01:00
|
|
|
PendingRequests =
|
|
|
|
p1_queue:dropwhile(
|
|
|
|
fun(Req) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:send_event(self(), Req),
|
2017-03-10 13:12:43 +01:00
|
|
|
true
|
|
|
|
end, State#state.pending_requests),
|
2016-02-11 18:00:00 +01:00
|
|
|
State1 = State#state{db_ref = Ref,
|
2017-03-10 13:12:43 +01:00
|
|
|
pending_requests = PendingRequests},
|
2016-02-11 18:00:00 +01:00
|
|
|
State2 = get_db_version(State1),
|
|
|
|
{next_state, session_established, State2};
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, Reason} ->
|
|
|
|
?INFO_MSG("~p connection failed:~n** Reason: ~p~n** "
|
|
|
|
"Retry after: ~p seconds",
|
|
|
|
[State#state.db_type, Reason,
|
|
|
|
State#state.start_interval div 1000]),
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:send_event_after(State#state.start_interval,
|
2010-01-31 12:41:28 +01:00
|
|
|
connect),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, connecting, State}
|
2010-01-31 12:41:28 +01:00
|
|
|
end;
|
|
|
|
connecting(Event, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?WARNING_MSG("unexpected event in 'connecting': ~p",
|
|
|
|
[Event]),
|
2010-01-31 12:41:28 +01:00
|
|
|
{next_state, connecting, State}.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
connecting({sql_cmd, {sql_query, ?KEEPALIVE_QUERY},
|
|
|
|
_Timestamp},
|
|
|
|
From, State) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:reply(From,
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, <<"SQL connection failed">>}),
|
2010-01-31 12:41:28 +01:00
|
|
|
{next_state, connecting, State};
|
2013-03-14 10:33:02 +01:00
|
|
|
connecting({sql_cmd, Command, Timestamp} = Req, From,
|
|
|
|
State) ->
|
|
|
|
?DEBUG("queuing pending request while connecting:~n\t~p",
|
|
|
|
[Req]),
|
2017-03-10 13:12:43 +01:00
|
|
|
PendingRequests =
|
|
|
|
try p1_queue:in({sql_cmd, Command, From, Timestamp},
|
|
|
|
State#state.pending_requests)
|
|
|
|
catch error:full ->
|
|
|
|
Q = p1_queue:dropwhile(
|
|
|
|
fun({sql_cmd, _, To, _Timestamp}) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:reply(
|
2017-03-10 13:12:43 +01:00
|
|
|
To, {error, <<"SQL connection failed">>}),
|
|
|
|
true
|
|
|
|
end, State#state.pending_requests),
|
|
|
|
p1_queue:in({sql_cmd, Command, From, Timestamp}, Q)
|
|
|
|
end,
|
2010-01-31 12:41:28 +01:00
|
|
|
{next_state, connecting,
|
2017-03-10 13:12:43 +01:00
|
|
|
State#state{pending_requests = PendingRequests}};
|
2010-01-31 12:41:28 +01:00
|
|
|
connecting(Request, {Who, _Ref}, State) ->
|
|
|
|
?WARNING_MSG("unexpected call ~p from ~p in 'connecting'",
|
|
|
|
[Request, Who]),
|
|
|
|
{reply, {error, badarg}, connecting, State}.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
session_established({sql_cmd, Command, Timestamp}, From,
|
|
|
|
State) ->
|
2010-06-11 16:35:45 +02:00
|
|
|
run_sql_cmd(Command, From, State, Timestamp);
|
2010-01-31 12:41:28 +01:00
|
|
|
session_established(Request, {Who, _Ref}, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?WARNING_MSG("unexpected call ~p from ~p in 'session_establ"
|
|
|
|
"ished'",
|
2010-01-31 12:41:28 +01:00
|
|
|
[Request, Who]),
|
|
|
|
{reply, {error, badarg}, session_established, State}.
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
session_established({sql_cmd, Command, From, Timestamp},
|
|
|
|
State) ->
|
2010-06-11 16:35:45 +02:00
|
|
|
run_sql_cmd(Command, From, State, Timestamp);
|
2010-01-31 12:41:28 +01:00
|
|
|
session_established(Event, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?WARNING_MSG("unexpected event in 'session_established': ~p",
|
|
|
|
[Event]),
|
2010-01-31 12:41:28 +01:00
|
|
|
{next_state, session_established, State}.
|
|
|
|
|
|
|
|
handle_event(_Event, StateName, State) ->
|
|
|
|
{next_state, StateName, State}.
|
|
|
|
|
|
|
|
handle_sync_event(_Event, _From, StateName, State) ->
|
|
|
|
{reply, {error, badarg}, StateName, State}.
|
|
|
|
|
|
|
|
code_change(_OldVsn, StateName, State, _Extra) ->
|
|
|
|
{ok, StateName, State}.
|
|
|
|
|
2006-01-13 19:05:10 +01:00
|
|
|
%% We receive the down signal when we loose the MySQL connection (we are
|
|
|
|
%% monitoring the connection)
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({'DOWN', _MonitorRef, process, _Pid, _Info},
|
|
|
|
_StateName, State) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:send_event(self(), connect),
|
2010-01-31 12:41:28 +01:00
|
|
|
{next_state, connecting, State};
|
|
|
|
handle_info(Info, StateName, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?WARNING_MSG("unexpected info in ~p: ~p",
|
|
|
|
[StateName, Info]),
|
2010-01-31 12:41:28 +01:00
|
|
|
{next_state, StateName, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, _StateName, State) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql_sup:remove_pid(State#state.host, self()),
|
2008-12-03 11:58:19 +01:00
|
|
|
case State#state.db_type of
|
2015-03-16 19:53:19 +01:00
|
|
|
mysql -> catch p1_mysql_conn:stop(State#state.db_ref);
|
2015-04-08 14:31:28 +02:00
|
|
|
sqlite -> catch sqlite3:close(sqlite_db(State#state.host));
|
2015-03-16 19:53:19 +01:00
|
|
|
_ -> ok
|
2008-12-03 11:58:19 +01:00
|
|
|
end,
|
2004-12-14 00:00:12 +01:00
|
|
|
ok.
|
|
|
|
|
2010-04-09 19:25:00 +02:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: print_state/1
|
|
|
|
%% Purpose: Prepare the state to be printed on error log
|
|
|
|
%% Returns: State to print
|
|
|
|
%%----------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
print_state(State) -> State.
|
|
|
|
|
2004-12-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
2009-05-21 18:19:33 +02:00
|
|
|
|
2010-06-11 16:35:45 +02:00
|
|
|
run_sql_cmd(Command, From, State, Timestamp) ->
|
2017-05-05 12:20:28 +02:00
|
|
|
QueryTimeout = query_timeout(State#state.host),
|
2015-12-04 15:08:43 +01:00
|
|
|
case p1_time_compat:monotonic_time(milli_seconds) - Timestamp of
|
2017-05-05 12:20:28 +02:00
|
|
|
Age when Age < QueryTimeout ->
|
2013-03-14 10:33:02 +01:00
|
|
|
put(?NESTING_KEY, ?TOP_LEVEL_TXN),
|
|
|
|
put(?STATE_KEY, State),
|
|
|
|
abort_on_driver_error(outer_op(Command), From);
|
|
|
|
Age ->
|
|
|
|
?ERROR_MSG("Database was not available or too slow, "
|
|
|
|
"discarding ~p milliseconds old request~n~p~n",
|
|
|
|
[Age, Command]),
|
|
|
|
{next_state, session_established, State}
|
2010-06-11 16:35:45 +02:00
|
|
|
end.
|
|
|
|
|
2009-07-28 15:46:28 +02:00
|
|
|
%% Only called by handle_call, only handles top level operations.
|
|
|
|
%% @spec outer_op(Op) -> {error, Reason} | {aborted, Reason} | {atomic, Result}
|
|
|
|
outer_op({sql_query, Query}) ->
|
|
|
|
sql_query_internal(Query);
|
|
|
|
outer_op({sql_transaction, F}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
outer_transaction(F, ?MAX_TRANSACTION_RESTARTS, <<"">>);
|
|
|
|
outer_op({sql_bloc, F}) -> execute_bloc(F).
|
2009-07-28 15:46:28 +02:00
|
|
|
|
|
|
|
%% Called via sql_query/transaction/bloc from client code when inside a
|
|
|
|
%% nested operation
|
|
|
|
nested_op({sql_query, Query}) ->
|
|
|
|
sql_query_internal(Query);
|
|
|
|
nested_op({sql_transaction, F}) ->
|
|
|
|
NestingLevel = get(?NESTING_KEY),
|
2013-03-14 10:33:02 +01:00
|
|
|
if NestingLevel =:= (?TOP_LEVEL_TXN) ->
|
|
|
|
outer_transaction(F, ?MAX_TRANSACTION_RESTARTS, <<"">>);
|
|
|
|
true -> inner_transaction(F)
|
2009-07-28 15:46:28 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
nested_op({sql_bloc, F}) -> execute_bloc(F).
|
2009-07-28 15:46:28 +02:00
|
|
|
|
|
|
|
%% Never retry nested transactions - only outer transactions
|
|
|
|
inner_transaction(F) ->
|
|
|
|
PreviousNestingLevel = get(?NESTING_KEY),
|
|
|
|
case get(?NESTING_KEY) of
|
2013-03-14 10:33:02 +01:00
|
|
|
?TOP_LEVEL_TXN ->
|
|
|
|
{backtrace, T} = process_info(self(), backtrace),
|
|
|
|
?ERROR_MSG("inner transaction called at outer txn "
|
|
|
|
"level. Trace: ~s",
|
|
|
|
[T]),
|
|
|
|
erlang:exit(implementation_faulty);
|
|
|
|
_N -> ok
|
2009-07-28 15:46:28 +02:00
|
|
|
end,
|
|
|
|
put(?NESTING_KEY, PreviousNestingLevel + 1),
|
|
|
|
Result = (catch F()),
|
|
|
|
put(?NESTING_KEY, PreviousNestingLevel),
|
|
|
|
case Result of
|
2013-03-14 10:33:02 +01:00
|
|
|
{aborted, Reason} -> {aborted, Reason};
|
|
|
|
{'EXIT', Reason} -> {'EXIT', Reason};
|
|
|
|
{atomic, Res} -> {atomic, Res};
|
|
|
|
Res -> {atomic, Res}
|
2005-11-16 03:59:05 +01:00
|
|
|
end.
|
|
|
|
|
2009-07-28 15:46:28 +02:00
|
|
|
outer_transaction(F, NRestarts, _Reason) ->
|
|
|
|
PreviousNestingLevel = get(?NESTING_KEY),
|
|
|
|
case get(?NESTING_KEY) of
|
2013-03-14 10:33:02 +01:00
|
|
|
?TOP_LEVEL_TXN -> ok;
|
|
|
|
_N ->
|
|
|
|
{backtrace, T} = process_info(self(), backtrace),
|
|
|
|
?ERROR_MSG("outer transaction called at inner txn "
|
|
|
|
"level. Trace: ~s",
|
|
|
|
[T]),
|
|
|
|
erlang:exit(implementation_faulty)
|
2009-07-28 15:46:28 +02:00
|
|
|
end,
|
2015-09-13 13:20:31 +02:00
|
|
|
sql_query_internal([<<"begin;">>]),
|
2009-07-28 15:46:28 +02:00
|
|
|
put(?NESTING_KEY, PreviousNestingLevel + 1),
|
|
|
|
Result = (catch F()),
|
|
|
|
put(?NESTING_KEY, PreviousNestingLevel),
|
|
|
|
case Result of
|
2013-03-14 10:33:02 +01:00
|
|
|
{aborted, Reason} when NRestarts > 0 ->
|
2015-09-13 13:20:31 +02:00
|
|
|
sql_query_internal([<<"rollback;">>]),
|
2013-03-14 10:33:02 +01:00
|
|
|
outer_transaction(F, NRestarts - 1, Reason);
|
|
|
|
{aborted, Reason} when NRestarts =:= 0 ->
|
|
|
|
?ERROR_MSG("SQL transaction restarts exceeded~n** "
|
|
|
|
"Restarts: ~p~n** Last abort reason: "
|
|
|
|
"~p~n** Stacktrace: ~p~n** When State "
|
|
|
|
"== ~p",
|
|
|
|
[?MAX_TRANSACTION_RESTARTS, Reason,
|
|
|
|
erlang:get_stacktrace(), get(?STATE_KEY)]),
|
2015-09-13 13:20:31 +02:00
|
|
|
sql_query_internal([<<"rollback;">>]),
|
2013-03-14 10:33:02 +01:00
|
|
|
{aborted, Reason};
|
|
|
|
{'EXIT', Reason} ->
|
2015-09-13 13:20:31 +02:00
|
|
|
sql_query_internal([<<"rollback;">>]), {aborted, Reason};
|
|
|
|
Res -> sql_query_internal([<<"commit;">>]), {atomic, Res}
|
2005-11-16 03:59:05 +01:00
|
|
|
end.
|
|
|
|
|
2009-07-28 15:46:28 +02:00
|
|
|
execute_bloc(F) ->
|
|
|
|
case catch F() of
|
2013-03-14 10:33:02 +01:00
|
|
|
{aborted, Reason} -> {aborted, Reason};
|
|
|
|
{'EXIT', Reason} -> {aborted, Reason};
|
|
|
|
Res -> {atomic, Res}
|
2009-04-24 12:27:31 +02:00
|
|
|
end.
|
|
|
|
|
2016-02-18 15:38:25 +01:00
|
|
|
execute_fun(F) when is_function(F, 0) ->
|
|
|
|
F();
|
|
|
|
execute_fun(F) when is_function(F, 2) ->
|
|
|
|
State = get(?STATE_KEY),
|
|
|
|
F(State#state.db_type, State#state.db_version).
|
|
|
|
|
2016-02-11 18:00:00 +01:00
|
|
|
sql_query_internal([{_, _} | _] = Queries) ->
|
|
|
|
State = get(?STATE_KEY),
|
|
|
|
case select_sql_query(Queries, State) of
|
|
|
|
undefined ->
|
|
|
|
{error, <<"no matching query for the current DBMS found">>};
|
|
|
|
Query ->
|
|
|
|
sql_query_internal(Query)
|
|
|
|
end;
|
2016-02-09 17:23:15 +01:00
|
|
|
sql_query_internal(#sql_query{} = Query) ->
|
|
|
|
State = get(?STATE_KEY),
|
|
|
|
Res =
|
|
|
|
try
|
|
|
|
case State#state.db_type of
|
|
|
|
odbc ->
|
|
|
|
generic_sql_query(Query);
|
2016-03-24 13:03:19 +01:00
|
|
|
mssql ->
|
2016-05-12 17:32:13 +02:00
|
|
|
mssql_sql_query(Query);
|
2016-02-09 17:23:15 +01:00
|
|
|
pgsql ->
|
|
|
|
Key = {?PREPARE_KEY, Query#sql_query.hash},
|
|
|
|
case get(Key) of
|
|
|
|
undefined ->
|
|
|
|
case pgsql_prepare(Query, State) of
|
|
|
|
{ok, _, _, _} ->
|
|
|
|
put(Key, prepared);
|
|
|
|
{error, Error} ->
|
|
|
|
?ERROR_MSG("PREPARE failed for SQL query "
|
|
|
|
"at ~p: ~p",
|
|
|
|
[Query#sql_query.loc, Error]),
|
|
|
|
put(Key, ignore)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
case get(Key) of
|
|
|
|
prepared ->
|
|
|
|
pgsql_execute_sql_query(Query, State);
|
|
|
|
_ ->
|
|
|
|
generic_sql_query(Query)
|
|
|
|
end;
|
|
|
|
mysql ->
|
|
|
|
generic_sql_query(Query);
|
|
|
|
sqlite ->
|
2016-05-12 17:32:13 +02:00
|
|
|
sqlite_sql_query(Query)
|
2016-02-09 17:23:15 +01:00
|
|
|
end
|
|
|
|
catch
|
|
|
|
Class:Reason ->
|
|
|
|
ST = erlang:get_stacktrace(),
|
|
|
|
?ERROR_MSG("Internal error while processing SQL query: ~p",
|
|
|
|
[{Class, Reason, ST}]),
|
|
|
|
{error, <<"internal error">>}
|
|
|
|
end,
|
|
|
|
case Res of
|
|
|
|
{error, <<"No SQL-driver information available.">>} ->
|
|
|
|
{updated, 0};
|
|
|
|
_Else -> Res
|
|
|
|
end;
|
2016-02-18 15:38:25 +01:00
|
|
|
sql_query_internal(F) when is_function(F) ->
|
|
|
|
case catch execute_fun(F) of
|
|
|
|
{'EXIT', Reason} -> {error, Reason};
|
|
|
|
Res -> Res
|
|
|
|
end;
|
2009-07-28 15:46:28 +02:00
|
|
|
sql_query_internal(Query) ->
|
|
|
|
State = get(?STATE_KEY),
|
2015-09-13 13:20:31 +02:00
|
|
|
?DEBUG("SQL: \"~s\"", [Query]),
|
2017-05-05 12:20:28 +02:00
|
|
|
QueryTimeout = query_timeout(State#state.host),
|
2009-07-28 15:46:28 +02:00
|
|
|
Res = case State#state.db_type of
|
2013-03-14 10:33:02 +01:00
|
|
|
odbc ->
|
2016-03-24 13:03:19 +01:00
|
|
|
to_odbc(odbc:sql_query(State#state.db_ref, [Query],
|
2017-05-05 12:20:28 +02:00
|
|
|
QueryTimeout - 1000));
|
2016-03-24 13:03:19 +01:00
|
|
|
mssql ->
|
|
|
|
to_odbc(odbc:sql_query(State#state.db_ref, [Query],
|
2017-05-05 12:20:28 +02:00
|
|
|
QueryTimeout - 1000));
|
2013-03-14 10:33:02 +01:00
|
|
|
pgsql ->
|
2017-02-28 12:19:07 +01:00
|
|
|
pgsql_to_odbc(pgsql:squery(State#state.db_ref, Query,
|
2017-05-05 12:20:28 +02:00
|
|
|
QueryTimeout - 1000));
|
2013-03-14 10:33:02 +01:00
|
|
|
mysql ->
|
2013-09-18 13:09:10 +02:00
|
|
|
R = mysql_to_odbc(p1_mysql_conn:squery(State#state.db_ref,
|
2013-03-14 10:33:02 +01:00
|
|
|
[Query], self(),
|
2017-05-05 12:20:28 +02:00
|
|
|
[{timeout, QueryTimeout - 1000},
|
2013-03-14 10:33:02 +01:00
|
|
|
{result_type, binary}])),
|
|
|
|
%% ?INFO_MSG("MySQL, Received result~n~p~n", [R]),
|
2015-04-08 14:31:28 +02:00
|
|
|
R;
|
|
|
|
sqlite ->
|
|
|
|
Host = State#state.host,
|
|
|
|
sqlite_to_odbc(Host, sqlite3:sql_exec(sqlite_db(Host), Query))
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2009-07-28 15:46:28 +02:00
|
|
|
case Res of
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, <<"No SQL-driver information available.">>} ->
|
|
|
|
{updated, 0};
|
|
|
|
_Else -> Res
|
2009-05-21 18:19:33 +02:00
|
|
|
end.
|
|
|
|
|
2016-02-11 18:00:00 +01:00
|
|
|
select_sql_query(Queries, State) ->
|
|
|
|
select_sql_query(
|
|
|
|
Queries, State#state.db_type, State#state.db_version, undefined).
|
|
|
|
|
|
|
|
select_sql_query([], _Type, _Version, undefined) ->
|
|
|
|
undefined;
|
|
|
|
select_sql_query([], _Type, _Version, Query) ->
|
|
|
|
Query;
|
2016-02-11 18:05:00 +01:00
|
|
|
select_sql_query([{any, Query} | _], _Type, _Version, _) ->
|
|
|
|
Query;
|
2016-02-11 18:00:00 +01:00
|
|
|
select_sql_query([{Type, Query} | _], Type, _Version, _) ->
|
|
|
|
Query;
|
|
|
|
select_sql_query([{{Type, _Version1}, Query1} | Rest], Type, undefined, _) ->
|
|
|
|
select_sql_query(Rest, Type, undefined, Query1);
|
|
|
|
select_sql_query([{{Type, Version1}, Query1} | Rest], Type, Version, Query) ->
|
|
|
|
if
|
|
|
|
Version >= Version1 ->
|
|
|
|
Query1;
|
|
|
|
true ->
|
|
|
|
select_sql_query(Rest, Type, Version, Query)
|
|
|
|
end;
|
|
|
|
select_sql_query([{_, _} | Rest], Type, Version, Query) ->
|
|
|
|
select_sql_query(Rest, Type, Version, Query).
|
|
|
|
|
2016-02-09 17:23:15 +01:00
|
|
|
generic_sql_query(SQLQuery) ->
|
|
|
|
sql_query_format_res(
|
|
|
|
sql_query_internal(generic_sql_query_format(SQLQuery)),
|
|
|
|
SQLQuery).
|
|
|
|
|
|
|
|
generic_sql_query_format(SQLQuery) ->
|
|
|
|
Args = (SQLQuery#sql_query.args)(generic_escape()),
|
|
|
|
(SQLQuery#sql_query.format_query)(Args).
|
|
|
|
|
|
|
|
generic_escape() ->
|
|
|
|
#sql_escape{string = fun(X) -> <<"'", (escape(X))/binary, "'">> end,
|
2017-04-11 12:13:58 +02:00
|
|
|
integer = fun(X) -> misc:i2l(X) end,
|
2016-02-09 17:23:15 +01:00
|
|
|
boolean = fun(true) -> <<"1">>;
|
|
|
|
(false) -> <<"0">>
|
|
|
|
end
|
|
|
|
}.
|
|
|
|
|
2016-05-12 17:32:13 +02:00
|
|
|
sqlite_sql_query(SQLQuery) ->
|
|
|
|
sql_query_format_res(
|
|
|
|
sql_query_internal(sqlite_sql_query_format(SQLQuery)),
|
|
|
|
SQLQuery).
|
|
|
|
|
|
|
|
sqlite_sql_query_format(SQLQuery) ->
|
|
|
|
Args = (SQLQuery#sql_query.args)(sqlite_escape()),
|
|
|
|
(SQLQuery#sql_query.format_query)(Args).
|
|
|
|
|
|
|
|
sqlite_escape() ->
|
|
|
|
#sql_escape{string = fun(X) -> <<"'", (standard_escape(X))/binary, "'">> end,
|
2017-04-11 12:13:58 +02:00
|
|
|
integer = fun(X) -> misc:i2l(X) end,
|
2016-05-12 17:32:13 +02:00
|
|
|
boolean = fun(true) -> <<"1">>;
|
|
|
|
(false) -> <<"0">>
|
|
|
|
end
|
|
|
|
}.
|
|
|
|
|
|
|
|
standard_escape(S) ->
|
|
|
|
<< <<(case Char of
|
|
|
|
$' -> << "''" >>;
|
|
|
|
_ -> << Char >>
|
|
|
|
end)/binary>> || <<Char>> <= S >>.
|
|
|
|
|
|
|
|
mssql_sql_query(SQLQuery) ->
|
|
|
|
sqlite_sql_query(SQLQuery).
|
|
|
|
|
2016-02-09 17:23:15 +01:00
|
|
|
pgsql_prepare(SQLQuery, State) ->
|
|
|
|
Escape = #sql_escape{_ = fun(X) -> X end},
|
|
|
|
N = length((SQLQuery#sql_query.args)(Escape)),
|
|
|
|
Args = [<<$$, (integer_to_binary(I))/binary>> || I <- lists:seq(1, N)],
|
|
|
|
Query = (SQLQuery#sql_query.format_query)(Args),
|
|
|
|
pgsql:prepare(State#state.db_ref, SQLQuery#sql_query.hash, Query).
|
|
|
|
|
|
|
|
pgsql_execute_escape() ->
|
|
|
|
#sql_escape{string = fun(X) -> X end,
|
2017-04-11 12:13:58 +02:00
|
|
|
integer = fun(X) -> [misc:i2l(X)] end,
|
2016-02-23 00:56:43 +01:00
|
|
|
boolean = fun(true) -> "1";
|
|
|
|
(false) -> "0"
|
2016-02-09 17:23:15 +01:00
|
|
|
end
|
|
|
|
}.
|
|
|
|
|
|
|
|
pgsql_execute_sql_query(SQLQuery, State) ->
|
|
|
|
Args = (SQLQuery#sql_query.args)(pgsql_execute_escape()),
|
|
|
|
ExecuteRes =
|
|
|
|
pgsql:execute(State#state.db_ref, SQLQuery#sql_query.hash, Args),
|
2016-02-18 15:38:25 +01:00
|
|
|
% {T, ExecuteRes} =
|
|
|
|
% timer:tc(pgsql, execute, [State#state.db_ref, SQLQuery#sql_query.hash, Args]),
|
|
|
|
% io:format("T ~s ~p~n", [SQLQuery#sql_query.hash, T]),
|
2016-02-09 17:23:15 +01:00
|
|
|
Res = pgsql_execute_to_odbc(ExecuteRes),
|
|
|
|
sql_query_format_res(Res, SQLQuery).
|
|
|
|
|
|
|
|
|
|
|
|
sql_query_format_res({selected, _, Rows}, SQLQuery) ->
|
|
|
|
Res =
|
|
|
|
lists:flatmap(
|
|
|
|
fun(Row) ->
|
|
|
|
try
|
|
|
|
[(SQLQuery#sql_query.format_res)(Row)]
|
|
|
|
catch
|
|
|
|
Class:Reason ->
|
|
|
|
ST = erlang:get_stacktrace(),
|
|
|
|
?ERROR_MSG("Error while processing "
|
|
|
|
"SQL query result: ~p~n"
|
|
|
|
"row: ~p",
|
|
|
|
[{Class, Reason, ST}, Row]),
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, Rows),
|
|
|
|
{selected, Res};
|
|
|
|
sql_query_format_res(Res, _SQLQuery) ->
|
|
|
|
Res.
|
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
sql_query_to_iolist(SQLQuery) ->
|
|
|
|
generic_sql_query_format(SQLQuery).
|
|
|
|
|
2009-07-28 15:46:28 +02:00
|
|
|
%% Generate the OTP callback return tuple depending on the driver result.
|
2013-03-14 10:33:02 +01:00
|
|
|
abort_on_driver_error({error, <<"query timed out">>} =
|
|
|
|
Reply,
|
|
|
|
From) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:reply(From, Reply),
|
2010-01-31 12:41:28 +01:00
|
|
|
{stop, timeout, get(?STATE_KEY)};
|
2013-03-14 10:33:02 +01:00
|
|
|
abort_on_driver_error({error,
|
|
|
|
<<"Failed sending data on socket", _/binary>>} =
|
|
|
|
Reply,
|
2010-01-31 12:41:28 +01:00
|
|
|
From) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:reply(From, Reply),
|
2010-01-31 12:41:28 +01:00
|
|
|
{stop, closed, get(?STATE_KEY)};
|
|
|
|
abort_on_driver_error(Reply, From) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:reply(From, Reply),
|
2010-01-31 12:41:28 +01:00
|
|
|
{next_state, session_established, get(?STATE_KEY)}.
|
2009-05-21 18:19:33 +02:00
|
|
|
|
2006-01-02 18:39:04 +01:00
|
|
|
%% == pure ODBC code
|
|
|
|
|
|
|
|
%% part of init/1
|
|
|
|
%% Open an ODBC database connection
|
2017-05-05 15:25:10 +02:00
|
|
|
odbc_connect(SQLServer, Timeout) ->
|
2013-04-08 11:12:54 +02:00
|
|
|
ejabberd:start_app(odbc),
|
2015-09-13 13:20:31 +02:00
|
|
|
odbc:connect(binary_to_list(SQLServer),
|
|
|
|
[{scrollable_cursors, off},
|
|
|
|
{tuple_row, off},
|
2017-05-05 15:25:10 +02:00
|
|
|
{timeout, Timeout},
|
2015-09-13 13:20:31 +02:00
|
|
|
{binary_strings, on}]).
|
2006-01-02 18:39:04 +01:00
|
|
|
|
2015-03-16 19:53:19 +01:00
|
|
|
%% == Native SQLite code
|
|
|
|
|
|
|
|
%% part of init/1
|
|
|
|
%% Open a database connection to SQLite
|
|
|
|
|
2015-04-08 14:31:28 +02:00
|
|
|
sqlite_connect(Host) ->
|
|
|
|
File = sqlite_file(Host),
|
|
|
|
case filelib:ensure_dir(File) of
|
|
|
|
ok ->
|
|
|
|
case sqlite3:open(sqlite_db(Host), [{file, File}]) of
|
|
|
|
{ok, Ref} ->
|
|
|
|
sqlite3:sql_exec(
|
|
|
|
sqlite_db(Host), "pragma foreign_keys = on"),
|
|
|
|
{ok, Ref};
|
|
|
|
{error, {already_started, Ref}} ->
|
|
|
|
{ok, Ref};
|
|
|
|
{error, Reason} ->
|
|
|
|
{error, Reason}
|
|
|
|
end;
|
|
|
|
Err ->
|
|
|
|
Err
|
2015-03-16 19:53:19 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% Convert SQLite query result to Erlang ODBC result formalism
|
2015-04-08 14:31:28 +02:00
|
|
|
sqlite_to_odbc(Host, ok) ->
|
|
|
|
{updated, sqlite3:changes(sqlite_db(Host))};
|
|
|
|
sqlite_to_odbc(Host, {rowid, _}) ->
|
|
|
|
{updated, sqlite3:changes(sqlite_db(Host))};
|
|
|
|
sqlite_to_odbc(_Host, [{columns, Columns}, {rows, TRows}]) ->
|
2015-04-08 13:38:04 +02:00
|
|
|
Rows = [lists:map(
|
|
|
|
fun(I) when is_integer(I) ->
|
2016-09-24 22:34:28 +02:00
|
|
|
integer_to_binary(I);
|
2015-04-08 13:38:04 +02:00
|
|
|
(B) ->
|
|
|
|
B
|
|
|
|
end, tuple_to_list(Row)) || Row <- TRows],
|
|
|
|
{selected, [list_to_binary(C) || C <- Columns], Rows};
|
2015-04-08 14:31:28 +02:00
|
|
|
sqlite_to_odbc(_Host, {error, _Code, Reason}) ->
|
2015-03-16 19:53:19 +01:00
|
|
|
{error, Reason};
|
2015-04-08 14:31:28 +02:00
|
|
|
sqlite_to_odbc(_Host, _) ->
|
2015-03-16 19:53:19 +01:00
|
|
|
{updated, undefined}.
|
|
|
|
|
2006-01-02 18:39:04 +01:00
|
|
|
%% == Native PostgreSQL code
|
|
|
|
|
|
|
|
%% part of init/1
|
|
|
|
%% Open a database connection to PostgreSQL
|
2017-05-05 15:25:10 +02:00
|
|
|
pgsql_connect(Server, Port, DB, Username, Password, ConnectTimeout,
|
|
|
|
Transport, SSLOpts) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case pgsql:connect([{host, Server},
|
|
|
|
{database, DB},
|
|
|
|
{user, Username},
|
|
|
|
{password, Password},
|
|
|
|
{port, Port},
|
2017-01-10 15:40:38 +01:00
|
|
|
{transport, Transport},
|
2017-05-05 15:25:10 +02:00
|
|
|
{connect_timeout, ConnectTimeout},
|
2017-01-10 15:40:38 +01:00
|
|
|
{as_binary, true}|SSLOpts]) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, Ref} ->
|
2016-07-22 16:33:40 +02:00
|
|
|
pgsql:squery(Ref, [<<"alter database \"">>, DB, <<"\" set ">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"standard_conforming_strings='off';">>]),
|
2015-03-09 14:37:35 +01:00
|
|
|
pgsql:squery(Ref, [<<"set standard_conforming_strings to 'off';">>]),
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, Ref};
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
2006-01-02 18:39:04 +01:00
|
|
|
|
|
|
|
%% Convert PostgreSQL query result to Erlang ODBC result formalism
|
2005-10-13 03:36:07 +02:00
|
|
|
pgsql_to_odbc({ok, PGSQLResult}) ->
|
|
|
|
case PGSQLResult of
|
2013-03-14 10:33:02 +01:00
|
|
|
[Item] -> pgsql_item_to_odbc(Item);
|
|
|
|
Items -> [pgsql_item_to_odbc(Item) || Item <- Items]
|
2005-10-13 03:36:07 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
pgsql_item_to_odbc({<<"SELECT", _/binary>>, Rows,
|
|
|
|
Recs}) ->
|
|
|
|
{selected, [element(1, Row) || Row <- Rows], Recs};
|
2013-07-21 12:24:36 +02:00
|
|
|
pgsql_item_to_odbc({<<"FETCH", _/binary>>, Rows,
|
|
|
|
Recs}) ->
|
|
|
|
{selected, [element(1, Row) || Row <- Rows], Recs};
|
2013-03-14 10:33:02 +01:00
|
|
|
pgsql_item_to_odbc(<<"INSERT ", OIDN/binary>>) ->
|
|
|
|
[_OID, N] = str:tokens(OIDN, <<" ">>),
|
2016-09-24 22:34:28 +02:00
|
|
|
{updated, binary_to_integer(N)};
|
2013-03-14 10:33:02 +01:00
|
|
|
pgsql_item_to_odbc(<<"DELETE ", N/binary>>) ->
|
2016-09-24 22:34:28 +02:00
|
|
|
{updated, binary_to_integer(N)};
|
2013-03-14 10:33:02 +01:00
|
|
|
pgsql_item_to_odbc(<<"UPDATE ", N/binary>>) ->
|
2016-09-24 22:34:28 +02:00
|
|
|
{updated, binary_to_integer(N)};
|
2013-03-14 10:33:02 +01:00
|
|
|
pgsql_item_to_odbc({error, Error}) -> {error, Error};
|
|
|
|
pgsql_item_to_odbc(_) -> {updated, undefined}.
|
2005-10-13 03:36:07 +02:00
|
|
|
|
2016-02-09 17:23:15 +01:00
|
|
|
pgsql_execute_to_odbc({ok, {<<"SELECT", _/binary>>, Rows}}) ->
|
|
|
|
{selected, [], [[Field || {_, Field} <- Row] || Row <- Rows]};
|
|
|
|
pgsql_execute_to_odbc({ok, {'INSERT', N}}) ->
|
|
|
|
{updated, N};
|
|
|
|
pgsql_execute_to_odbc({ok, {'DELETE', N}}) ->
|
|
|
|
{updated, N};
|
|
|
|
pgsql_execute_to_odbc({ok, {'UPDATE', N}}) ->
|
|
|
|
{updated, N};
|
|
|
|
pgsql_execute_to_odbc({error, Error}) -> {error, Error};
|
|
|
|
pgsql_execute_to_odbc(_) -> {updated, undefined}.
|
|
|
|
|
|
|
|
|
2006-01-02 18:39:04 +01:00
|
|
|
%% == Native MySQL code
|
|
|
|
|
|
|
|
%% part of init/1
|
|
|
|
%% Open a database connection to MySQL
|
2017-05-05 15:25:10 +02:00
|
|
|
mysql_connect(Server, Port, DB, Username, Password, ConnectTimeout, _, _) ->
|
2013-09-18 13:09:10 +02:00
|
|
|
case p1_mysql_conn:start(binary_to_list(Server), Port,
|
2015-10-07 00:06:58 +02:00
|
|
|
binary_to_list(Username),
|
|
|
|
binary_to_list(Password),
|
2017-05-05 15:25:10 +02:00
|
|
|
binary_to_list(DB),
|
|
|
|
ConnectTimeout, fun log/3)
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
2015-10-07 00:06:58 +02:00
|
|
|
{ok, Ref} ->
|
|
|
|
p1_mysql_conn:fetch(
|
2016-01-26 13:47:24 +01:00
|
|
|
Ref, [<<"set names 'utf8mb4' collate 'utf8mb4_bin';">>], self()),
|
2015-10-07 00:06:58 +02:00
|
|
|
{ok, Ref};
|
|
|
|
Err -> Err
|
2006-01-02 18:39:04 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% Convert MySQL query result to Erlang ODBC result formalism
|
|
|
|
mysql_to_odbc({updated, MySQLRes}) ->
|
2013-09-18 13:09:10 +02:00
|
|
|
{updated, p1_mysql:get_result_affected_rows(MySQLRes)};
|
2006-01-02 18:39:04 +01:00
|
|
|
mysql_to_odbc({data, MySQLRes}) ->
|
2013-09-18 13:09:10 +02:00
|
|
|
mysql_item_to_odbc(p1_mysql:get_result_field_info(MySQLRes),
|
|
|
|
p1_mysql:get_result_rows(MySQLRes));
|
2013-03-14 10:33:02 +01:00
|
|
|
mysql_to_odbc({error, MySQLRes})
|
2014-07-04 17:28:19 +02:00
|
|
|
when is_binary(MySQLRes) ->
|
2007-07-17 09:09:04 +02:00
|
|
|
{error, MySQLRes};
|
2014-07-04 17:28:19 +02:00
|
|
|
mysql_to_odbc({error, MySQLRes})
|
|
|
|
when is_list(MySQLRes) ->
|
|
|
|
{error, list_to_binary(MySQLRes)};
|
2006-01-02 18:39:04 +01:00
|
|
|
mysql_to_odbc({error, MySQLRes}) ->
|
2014-07-04 17:28:19 +02:00
|
|
|
{error, p1_mysql:get_result_reason(MySQLRes)};
|
|
|
|
mysql_to_odbc(ok) ->
|
|
|
|
ok.
|
|
|
|
|
2006-01-02 18:39:04 +01:00
|
|
|
|
|
|
|
%% When tabular data is returned, convert it to the ODBC formalism
|
|
|
|
mysql_item_to_odbc(Columns, Recs) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{selected, [element(2, Column) || Column <- Columns], Recs}.
|
|
|
|
|
|
|
|
to_odbc({selected, Columns, Recs}) ->
|
2015-09-13 13:20:31 +02:00
|
|
|
Rows = [lists:map(
|
|
|
|
fun(I) when is_integer(I) ->
|
2016-09-24 22:34:28 +02:00
|
|
|
integer_to_binary(I);
|
2015-09-13 13:20:31 +02:00
|
|
|
(B) ->
|
|
|
|
B
|
|
|
|
end, Row) || Row <- Recs],
|
|
|
|
{selected, [list_to_binary(C) || C <- Columns], Rows};
|
|
|
|
to_odbc({error, Reason}) when is_list(Reason) ->
|
|
|
|
{error, list_to_binary(Reason)};
|
2013-03-14 10:33:02 +01:00
|
|
|
to_odbc(Res) ->
|
|
|
|
Res.
|
2007-08-16 18:37:00 +02:00
|
|
|
|
2016-02-11 18:00:00 +01:00
|
|
|
get_db_version(#state{db_type = pgsql} = State) ->
|
|
|
|
case pgsql:squery(State#state.db_ref,
|
|
|
|
<<"select current_setting('server_version_num')">>) of
|
|
|
|
{ok, [{_, _, [[SVersion]]}]} ->
|
|
|
|
case catch binary_to_integer(SVersion) of
|
|
|
|
Version when is_integer(Version) ->
|
|
|
|
State#state{db_version = Version};
|
|
|
|
Error ->
|
|
|
|
?WARNING_MSG("error getting pgsql version: ~p", [Error]),
|
|
|
|
State
|
|
|
|
end;
|
|
|
|
Res ->
|
|
|
|
?WARNING_MSG("error getting pgsql version: ~p", [Res]),
|
|
|
|
State
|
|
|
|
end;
|
|
|
|
get_db_version(State) ->
|
|
|
|
State.
|
|
|
|
|
2008-10-13 17:25:30 +02:00
|
|
|
log(Level, Format, Args) ->
|
|
|
|
case Level of
|
2013-03-14 10:33:02 +01:00
|
|
|
debug -> ?DEBUG(Format, Args);
|
|
|
|
normal -> ?INFO_MSG(Format, Args);
|
|
|
|
error -> ?ERROR_MSG(Format, Args)
|
2008-10-13 17:25:30 +02:00
|
|
|
end.
|
2010-01-31 12:41:28 +01:00
|
|
|
|
|
|
|
db_opts(Host) ->
|
2017-04-29 10:39:40 +02:00
|
|
|
Type = ejabberd_config:get_option({sql_type, Host}, odbc),
|
|
|
|
Server = ejabberd_config:get_option({sql_server, Host}, <<"localhost">>),
|
2017-05-05 15:25:10 +02:00
|
|
|
Timeout = timer:seconds(
|
|
|
|
ejabberd_config:get_option({sql_connect_timeout, Host}, 5)),
|
2017-04-29 10:39:40 +02:00
|
|
|
Transport = case ejabberd_config:get_option({sql_ssl, Host}, false) of
|
2017-01-10 15:40:38 +01:00
|
|
|
false -> tcp;
|
|
|
|
true -> ssl
|
|
|
|
end,
|
|
|
|
warn_if_ssl_unsupported(Transport, Type),
|
2013-08-12 14:25:05 +02:00
|
|
|
case Type of
|
|
|
|
odbc ->
|
2017-05-05 15:25:10 +02:00
|
|
|
[odbc, Server, Timeout];
|
2015-03-16 19:53:19 +01:00
|
|
|
sqlite ->
|
2015-04-08 14:31:28 +02:00
|
|
|
[sqlite, Host];
|
2013-08-12 14:25:05 +02:00
|
|
|
_ ->
|
|
|
|
Port = ejabberd_config:get_option(
|
2016-04-20 11:27:32 +02:00
|
|
|
{sql_port, Host},
|
2013-08-12 14:25:05 +02:00
|
|
|
case Type of
|
2015-09-13 16:41:54 +02:00
|
|
|
mssql -> ?MSSQL_PORT;
|
2013-08-12 14:25:05 +02:00
|
|
|
mysql -> ?MYSQL_PORT;
|
|
|
|
pgsql -> ?PGSQL_PORT
|
|
|
|
end),
|
2016-04-20 11:27:32 +02:00
|
|
|
DB = ejabberd_config:get_option({sql_database, Host},
|
2013-08-12 14:25:05 +02:00
|
|
|
<<"ejabberd">>),
|
2016-04-20 11:27:32 +02:00
|
|
|
User = ejabberd_config:get_option({sql_username, Host},
|
2013-08-12 14:25:05 +02:00
|
|
|
<<"ejabberd">>),
|
2016-04-20 11:27:32 +02:00
|
|
|
Pass = ejabberd_config:get_option({sql_password, Host},
|
2013-08-12 14:25:05 +02:00
|
|
|
<<"">>),
|
2017-01-10 15:40:38 +01:00
|
|
|
SSLOpts = get_ssl_opts(Transport, Host),
|
2015-09-13 16:41:54 +02:00
|
|
|
case Type of
|
|
|
|
mssql ->
|
2016-03-24 13:03:19 +01:00
|
|
|
[mssql, <<"DSN=", Host/binary, ";UID=", User/binary,
|
2017-05-05 15:25:10 +02:00
|
|
|
";PWD=", Pass/binary>>, Timeout];
|
2015-09-13 16:41:54 +02:00
|
|
|
_ ->
|
2017-05-05 15:25:10 +02:00
|
|
|
[Type, Server, Port, DB, User, Pass, Timeout, Transport, SSLOpts]
|
2015-09-13 16:41:54 +02:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2017-01-10 15:40:38 +01:00
|
|
|
warn_if_ssl_unsupported(tcp, _) ->
|
|
|
|
ok;
|
|
|
|
warn_if_ssl_unsupported(ssl, pgsql) ->
|
|
|
|
ok;
|
|
|
|
warn_if_ssl_unsupported(ssl, Type) ->
|
|
|
|
?WARNING_MSG("SSL connection is not supported for ~s", [Type]).
|
|
|
|
|
|
|
|
get_ssl_opts(ssl, Host) ->
|
2017-04-29 10:39:40 +02:00
|
|
|
Opts1 = case ejabberd_config:get_option({sql_ssl_certfile, Host}) of
|
2017-01-10 15:40:38 +01:00
|
|
|
undefined -> [];
|
|
|
|
CertFile -> [{certfile, CertFile}]
|
|
|
|
end,
|
2017-04-29 10:39:40 +02:00
|
|
|
Opts2 = case ejabberd_config:get_option({sql_ssl_cafile, Host}) of
|
2017-01-10 15:40:38 +01:00
|
|
|
undefined -> Opts1;
|
|
|
|
CAFile -> [{cacertfile, CAFile}|Opts1]
|
|
|
|
end,
|
2017-04-29 10:39:40 +02:00
|
|
|
case ejabberd_config:get_option({sql_ssl_verify, Host}, false) of
|
2017-01-10 15:40:38 +01:00
|
|
|
true ->
|
|
|
|
case lists:keymember(cacertfile, 1, Opts2) of
|
|
|
|
true ->
|
|
|
|
[{verify, verify_peer}|Opts2];
|
|
|
|
false ->
|
|
|
|
?WARNING_MSG("SSL verification is enabled for "
|
|
|
|
"SQL connection, but option "
|
|
|
|
"'sql_ssl_cafile' is not set; "
|
|
|
|
"verification will be disabled", []),
|
|
|
|
Opts2
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
Opts2
|
|
|
|
end;
|
|
|
|
get_ssl_opts(tcp, _) ->
|
|
|
|
[].
|
|
|
|
|
2015-09-13 16:41:54 +02:00
|
|
|
init_mssql(Host) ->
|
2017-04-29 10:39:40 +02:00
|
|
|
Server = ejabberd_config:get_option({sql_server, Host}, <<"localhost">>),
|
|
|
|
Port = ejabberd_config:get_option({sql_port, Host}, ?MSSQL_PORT),
|
|
|
|
DB = ejabberd_config:get_option({sql_database, Host}, <<"ejabberd">>),
|
2015-09-13 16:41:54 +02:00
|
|
|
FreeTDS = io_lib:fwrite("[~s]~n"
|
|
|
|
"\thost = ~s~n"
|
|
|
|
"\tport = ~p~n"
|
|
|
|
"\ttds version = 7.1~n",
|
|
|
|
[Host, Server, Port]),
|
|
|
|
ODBCINST = io_lib:fwrite("[freetds]~n"
|
|
|
|
"Description = MSSQL connection~n"
|
|
|
|
"Driver = libtdsodbc.so~n"
|
|
|
|
"Setup = libtdsS.so~n"
|
|
|
|
"UsageCount = 1~n"
|
|
|
|
"FileUsage = 1~n", []),
|
|
|
|
ODBCINI = io_lib:fwrite("[~s]~n"
|
|
|
|
"Description = MS SQL~n"
|
|
|
|
"Driver = freetds~n"
|
|
|
|
"Servername = ~s~n"
|
|
|
|
"Database = ~s~n"
|
|
|
|
"Port = ~p~n",
|
|
|
|
[Host, Host, DB, Port]),
|
|
|
|
?DEBUG("~s:~n~s", [freetds_config(), FreeTDS]),
|
|
|
|
?DEBUG("~s:~n~s", [odbcinst_config(), ODBCINST]),
|
|
|
|
?DEBUG("~s:~n~s", [odbc_config(), ODBCINI]),
|
|
|
|
case filelib:ensure_dir(freetds_config()) of
|
|
|
|
ok ->
|
|
|
|
try
|
|
|
|
ok = file:write_file(freetds_config(), FreeTDS, [append]),
|
|
|
|
ok = file:write_file(odbcinst_config(), ODBCINST),
|
|
|
|
ok = file:write_file(odbc_config(), ODBCINI, [append]),
|
|
|
|
os:putenv("ODBCSYSINI", tmp_dir()),
|
|
|
|
os:putenv("FREETDS", freetds_config()),
|
|
|
|
os:putenv("FREETDSCONF", freetds_config()),
|
|
|
|
ok
|
|
|
|
catch error:{badmatch, {error, Reason} = Err} ->
|
|
|
|
?ERROR_MSG("failed to create temporary files in ~s: ~s",
|
|
|
|
[tmp_dir(), file:format_error(Reason)]),
|
|
|
|
Err
|
|
|
|
end;
|
|
|
|
{error, Reason} = Err ->
|
|
|
|
?ERROR_MSG("failed to create temporary directory ~s: ~s",
|
|
|
|
[tmp_dir(), file:format_error(Reason)]),
|
|
|
|
Err
|
2010-01-31 12:41:28 +01:00
|
|
|
end.
|
|
|
|
|
2015-09-13 16:41:54 +02:00
|
|
|
tmp_dir() ->
|
2017-11-30 15:32:09 +01:00
|
|
|
case os:type() of
|
|
|
|
{win32, _} -> filename:join([os:getenv("HOME"), "conf"]);
|
|
|
|
_ -> filename:join(["/tmp", "ejabberd"])
|
|
|
|
end.
|
2015-09-13 16:41:54 +02:00
|
|
|
|
|
|
|
odbc_config() ->
|
|
|
|
filename:join(tmp_dir(), "odbc.ini").
|
|
|
|
|
|
|
|
freetds_config() ->
|
|
|
|
filename:join(tmp_dir(), "freetds.conf").
|
|
|
|
|
|
|
|
odbcinst_config() ->
|
|
|
|
filename:join(tmp_dir(), "odbcinst.ini").
|
|
|
|
|
2010-01-31 12:41:28 +01:00
|
|
|
max_fsm_queue() ->
|
2017-03-10 13:12:43 +01:00
|
|
|
proplists:get_value(max_queue, fsm_limit_opts(), unlimited).
|
2010-06-11 16:35:45 +02:00
|
|
|
|
2010-01-31 12:41:28 +01:00
|
|
|
fsm_limit_opts() ->
|
2017-03-10 13:12:43 +01:00
|
|
|
ejabberd_config:fsm_limit_opts([]).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2017-05-05 12:20:28 +02:00
|
|
|
query_timeout(LServer) ->
|
|
|
|
timer:seconds(
|
|
|
|
ejabberd_config:get_option({sql_query_timeout, LServer}, 60)).
|
|
|
|
|
2017-12-17 17:46:55 +01:00
|
|
|
check_error({error, Why} = Err, _Query) when Why == killed ->
|
|
|
|
Err;
|
2016-02-09 17:23:15 +01:00
|
|
|
check_error({error, Why} = Err, #sql_query{} = Query) ->
|
|
|
|
?ERROR_MSG("SQL query '~s' at ~p failed: ~p",
|
|
|
|
[Query#sql_query.hash, Query#sql_query.loc, Why]),
|
|
|
|
Err;
|
2016-01-13 10:59:00 +01:00
|
|
|
check_error({error, Why} = Err, Query) ->
|
2016-02-11 18:00:00 +01:00
|
|
|
case catch iolist_to_binary(Query) of
|
|
|
|
SQuery when is_binary(SQuery) ->
|
|
|
|
?ERROR_MSG("SQL query '~s' failed: ~p", [SQuery, Why]);
|
|
|
|
_ ->
|
|
|
|
?ERROR_MSG("SQL query ~p failed: ~p", [Query, Why])
|
|
|
|
end,
|
2016-01-13 10:59:00 +01:00
|
|
|
Err;
|
|
|
|
check_error(Result, _Query) ->
|
|
|
|
Result.
|
|
|
|
|
2017-05-08 11:59:28 +02:00
|
|
|
-spec opt_type(sql_database) -> fun((binary()) -> binary());
|
|
|
|
(sql_keepalive_interval) -> fun((pos_integer()) -> pos_integer());
|
|
|
|
(sql_password) -> fun((binary()) -> binary());
|
|
|
|
(sql_port) -> fun((0..65535) -> 0..65535);
|
|
|
|
(sql_server) -> fun((binary()) -> binary());
|
|
|
|
(sql_username) -> fun((binary()) -> binary());
|
|
|
|
(sql_ssl) -> fun((boolean()) -> boolean());
|
|
|
|
(sql_ssl_verify) -> fun((boolean()) -> boolean());
|
|
|
|
(sql_ssl_certfile) -> fun((boolean()) -> boolean());
|
|
|
|
(sql_ssl_cafile) -> fun((boolean()) -> boolean());
|
|
|
|
(sql_query_timeout) -> fun((pos_integer()) -> pos_integer());
|
|
|
|
(sql_connect_timeout) -> fun((pos_integer()) -> pos_integer());
|
|
|
|
(sql_queue_type) -> fun((ram | file) -> ram | file);
|
|
|
|
(atom()) -> [atom()].
|
2016-04-20 11:27:32 +02:00
|
|
|
opt_type(sql_database) -> fun iolist_to_binary/1;
|
|
|
|
opt_type(sql_keepalive_interval) ->
|
2015-06-01 14:38:27 +02:00
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
2016-04-20 11:27:32 +02:00
|
|
|
opt_type(sql_password) -> fun iolist_to_binary/1;
|
|
|
|
opt_type(sql_port) ->
|
2015-06-01 14:38:27 +02:00
|
|
|
fun (P) when is_integer(P), P > 0, P < 65536 -> P end;
|
2016-04-20 11:27:32 +02:00
|
|
|
opt_type(sql_server) -> fun iolist_to_binary/1;
|
|
|
|
opt_type(sql_username) -> fun iolist_to_binary/1;
|
2017-01-10 15:40:38 +01:00
|
|
|
opt_type(sql_ssl) -> fun(B) when is_boolean(B) -> B end;
|
|
|
|
opt_type(sql_ssl_verify) -> fun(B) when is_boolean(B) -> B end;
|
2017-05-23 08:27:52 +02:00
|
|
|
opt_type(sql_ssl_certfile) -> fun ejabberd_pkix:try_certfile/1;
|
2017-05-12 08:34:57 +02:00
|
|
|
opt_type(sql_ssl_cafile) -> fun misc:try_read_file/1;
|
2017-05-05 12:20:28 +02:00
|
|
|
opt_type(sql_query_timeout) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
2017-05-05 15:25:10 +02:00
|
|
|
opt_type(sql_connect_timeout) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
2017-03-10 13:12:43 +01:00
|
|
|
opt_type(sql_queue_type) ->
|
|
|
|
fun(ram) -> ram; (file) -> file end;
|
2018-03-15 15:55:05 +01:00
|
|
|
opt_type(new_sql_schema) -> fun(B) when is_boolean(B) -> B end;
|
2015-06-01 14:38:27 +02:00
|
|
|
opt_type(_) ->
|
2017-03-10 13:12:43 +01:00
|
|
|
[sql_database, sql_keepalive_interval,
|
2017-04-28 12:23:32 +02:00
|
|
|
sql_password, sql_port, sql_server,
|
2017-05-23 08:27:52 +02:00
|
|
|
sql_username, sql_ssl, sql_ssl_verify, sql_ssl_certfile,
|
2017-05-05 15:25:10 +02:00
|
|
|
sql_ssl_cafile, sql_queue_type, sql_query_timeout,
|
2018-03-15 15:55:05 +01:00
|
|
|
sql_connect_timeout,
|
|
|
|
new_sql_schema].
|