<commit_before>-module(dderl_session).

-behavior(gen_server).

-export([start/0
        , process_request/3
        , get_state/1
        , sql_to_json/1
        ]).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3, format_status/2]).

-define(SESSION_IDLE_TIMEOUT, 3600000). % 1 hour

start() ->
    {ok, Pid} = gen_server:start_link(?MODULE, [], []),
    Key = erlang:phash2({dderl_session, Pid}),
    {Key, {dderl_session, Pid}}.

get_state({?MODULE, Pid}) ->
    gen_server:call(Pid, get_state, infinity).

process_request(SessKey, WReq, {?MODULE, Pid}) ->
    Type = wrq:disp_path(WReq),
    gen_server:call(Pid, {SessKey, Type, WReq}, infinity).

-record(state, {
        key
        , session
        , statements = []
        , tref
    }).

init(_Args) ->
    io:format(user, "dderl_session ~p started...~n", [self()]),
    {ok, TRef} = timer:send_after(?SESSION_IDLE_TIMEOUT, die),
    {ok, #state{key=erlang:phash2({dderl_session, self()}),tref=TRef}}.

handle_call(get_state, _From, State) ->
    {reply, State, State};
handle_call({SessKey, Typ, WReq}, From, #state{tref=TRef, key=Key} = State) ->
    timer:cancel(TRef),
    NewKey = if SessKey =/= Key -> SessKey; true -> Key end,
    io:format(user, "[~p] process_request ~p~n", [NewKey, Typ]),
    {Rep, Resp, NewState} = process_call({Typ, WReq}, From, State#state{key=NewKey}),
    {ok, NewTRef} = timer:send_after(?SESSION_IDLE_TIMEOUT, die),
    {Rep, Resp, NewState#state{tref=NewTRef,key=NewKey}}.

process_call({"login", ReqData}, _From, #state{key=Key} = State) ->
    {struct, [{<<"login">>, {struct, BodyJson}}]} = mochijson2:decode(wrq:req_body(ReqData)),
    IpAddr   = binary_to_list(proplists:get_value(<<"ip">>, BodyJson, <<>>)),
    Port     = list_to_integer(binary_to_list(proplists:get_value(<<"port">>, BodyJson, <<>>))),
    Service  = binary_to_list(proplists:get_value(<<"service">>, BodyJson, <<>>)),
    Type      = binary_to_list(proplists:get_value(<<"type">>, BodyJson, <<>>)),
    User     = binary_to_list(proplists:get_value(<<"user">>, BodyJson, <<>>)),
    Password = binary_to_list(proplists:get_value(<<"password">>, BodyJson, <<>>)),
    io:format(user, "[~p] Params ~p~n", [Key, {IpAddr, Port, Service, Type, User, Password}]),
    {ok, Pool} =
        if Service =/= "MOCK" ->
            oci_session_pool:start_link(IpAddr, Port, {list_to_atom(Type), Service}, User, Password, []);
        true ->
            oci_session_pool:start_link(IpAddr, Port,  {list_to_atom(Type), "db.local"},  User, Password, [{port_options, [{mock_port, oci_port_mock}]}])
    end,
    %%oci_session_pool:enable_log(Pool),
    Session = oci_session_pool:get_session(Pool),
    io:format(user, "[~p] Session ~p~n", [Key, {Session,Pool}]),
    Resp = "{\"session\":" ++ integer_to_list(State#state.key) ++ "}",
    {reply, Resp, State#state{session={Session,Pool}}};
process_call({"users", _ReqData}, _From, #state{session={Session, _Pool}, key=Key} = State) ->
    Query = "select distinct owner from all_tables",
    io:format(user, "[~p] Users for ~p~n", [Key, {Session, Query}]),
    {statement, Statement} = Session:execute_sql(Query, [], 10001),
    io:format(user, "[~p] Rows ~p~n", [Key, Statement:next_rows()]),
    Resp = prepare_json_rows(Statement, Key),
    Statement:close(),
    {reply, Resp, State};
process_call({"tables", ReqData}, _From, #state{session={Session, _Pool}, key=Key} = State) ->
    {struct, [{<<"tables">>, {struct, BodyJson}}]} = mochijson2:decode(wrq:req_body(ReqData)),
    Owner = binary_to_list(proplists:get_value(<<"owner">>, BodyJson, <<>>)),
    Query = "select table_name from all_tables where owner='" ++ Owner ++ "' order by table_name desc",
    io:format(user, "[~p] Tables for ~p~n", [Key, {Session, Query}]),<commit_msg>Fixed empty DB user issue<commit_after>-module(dderl_session).

-behavior(gen_server).

-export([start/0
        , process_request/3
        , get_state/1
        , sql_to_json/1
        ]).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3, format_status/2]).

-define(SESSION_IDLE_TIMEOUT, 3600000). % 1 hour

start() ->
    {ok, Pid} = gen_server:start_link(?MODULE, [], []),
    Key = erlang:phash2({dderl_session, Pid}),
    {Key, {dderl_session, Pid}}.

get_state({?MODULE, Pid}) ->
    gen_server:call(Pid, get_state, infinity).

process_request(SessKey, WReq, {?MODULE, Pid}) ->
    Type = wrq:disp_path(WReq),
    gen_server:call(Pid, {SessKey, Type, WReq}, infinity).

-record(state, {
        key
        , session
        , statements = []
        , tref
    }).

init(_Args) ->
    io:format(user, "dderl_session ~p started...~n", [self()]),
    {ok, TRef} = timer:send_after(?SESSION_IDLE_TIMEOUT, die),
    {ok, #state{key=erlang:phash2({dderl_session, self()}),tref=TRef}}.

handle_call(get_state, _From, State) ->
    {reply, State, State};
handle_call({SessKey, Typ, WReq}, From, #state{tref=TRef, key=Key} = State) ->
    timer:cancel(TRef),
    NewKey = if SessKey =/= Key -> SessKey; true -> Key end,
    io:format(user, "[~p] process_request ~p~n", [NewKey, Typ]),
    {Rep, Resp, NewState} = process_call({Typ, WReq}, From, State#state{key=NewKey}),
    {ok, NewTRef} = timer:send_after(?SESSION_IDLE_TIMEOUT, die),
    {Rep, Resp, NewState#state{tref=NewTRef,key=NewKey}}.

process_call({"login", ReqData}, _From, #state{key=Key} = State) ->
    {struct, [{<<"login">>, {struct, BodyJson}}]} = mochijson2:decode(wrq:req_body(ReqData)),
    IpAddr   = binary_to_list(proplists:get_value(<<"ip">>, BodyJson, <<>>)),
    Port     = list_to_integer(binary_to_list(proplists:get_value(<<"port">>, BodyJson, <<>>))),
    Service  = binary_to_list(proplists:get_value(<<"service">>, BodyJson, <<>>)),
    Type      = binary_to_list(proplists:get_value(<<"type">>, BodyJson, <<>>)),
    User     = binary_to_list(proplists:get_value(<<"user">>, BodyJson, <<>>)),
    Password = binary_to_list(proplists:get_value(<<"password">>, BodyJson, <<>>)),
    io:format(user, "[~p] Params ~p~n", [Key, {IpAddr, Port, Service, Type, User, Password}]),
    {ok, Pool} =
        if Service =/= "MOCK" ->
            oci_session_pool:start_link(IpAddr, Port, {list_to_atom(Type), Service}, User, Password, []);
        true ->
            oci_session_pool:start_link(IpAddr, Port,  {list_to_atom(Type), "db.local"},  User, Password, [{port_options, [{mock_port, oci_port_mock}]}])
    end,
    %%oci_session_pool:enable_log(Pool),
    Session = oci_session_pool:get_session(Pool),
    io:format(user, "[~p] Session ~p~n", [Key, {Session,Pool}]),
    Resp = "{\"session\":" ++ integer_to_list(State#state.key) ++ "}",
    {reply, Resp, State#state{session={Session,Pool}}};
process_call({"users", _ReqData}, _From, #state{session={Session, _Pool}, key=Key} = State) ->
    Query = "select distinct owner from all_tables",
    io:format(user, "[~p] Users for ~p~n", [Key, {Session, Query}]),
    {statement, Statement} = Session:execute_sql(Query, [], 10001),
    Resp = prepare_json_rows(Statement, Key),
    io:format(user, "[~p] Users Resp ~p~n", [Key, Resp]),
    Statement:close(),
    {reply, Resp, State};
process_call({"tables", ReqData}, _From, #state{session={Session, _Pool}, key=Key} = State) ->
    {struct, [{<<"tables">>, {struct, BodyJson}}]} = mochijson2:decode(wrq:req_body(ReqData)),
    Owner = binary_to_list(proplists:get_value(<<"owner">>, BodyJson, <<>>)),
    Query = "select table_name from all_tables where owner='" ++ Owner ++ "' order by table_name desc",
    io:format(user, "[~p] Tables for ~p~n", [Key, {Session, Query}]),
{'fraction_non_alphanumeric': 0.20121228093292923, 'fraction_numerical': 0.007379101330873633, 'mean_word_length': 4.580882352941177, 'pattern_counts': {'":': 2, '<': 67, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '197786', 'n_tokens_mistral': 2886, 'n_tokens_neox': 2809, 'n_words': 508}