<commit_before>-module(sql_bridge_mysql_otp).
-behaviour(sql_bridge_adapter).
-include("compat.hrl").

-export([start/0,
		 connect/5,
		 query/4,
		 schema_db_column/0,
		 encode/1,
		 start_transaction/1,
		 commit_transaction/1,
		 rollback_transaction/1,
		 with_transaction/2
		]).

%% 
-export([maybe_replace_tokens/2]).


start() ->
	application:start(poolboy),
	ok.

connect(DB, User, Pass, Host, Port) when is_atom(DB) ->
	WorkerArgs = [
		{database, atom_to_list(DB)},
		{host, Host},
		{user, User},
		{password, <PASSWORD>},
		{port, Port}
	],
	sql_bridge_utils:start_poolboy_pool(DB, WorkerArgs, mysql),
	ok.

start_transaction(DB) ->
    case sql_bridge_utils:trans_depth(DB) of
        0 ->
            query(none, DB, "BEGIN", []);
        _ ->
            ok
    end,
    sql_bridge_utils:trans_deeper(DB),
    sql_bridge_utils:checkout_pool(DB),
    ok.

rollback_transaction(DB) ->
    sql_bridge_utils:trans_shallower(DB),
	query(none, DB, "ROLLBACK", []),
	sql_bridge_utils:checkin_pool(DB),
	ok.

commit_transaction(DB) ->
    case sql_bridge_utils:trans_depth(DB) of
        1 ->
            Res = query(none, DB, "COMMIT", []),
            sql_bridge_utils:clear_trans_depth(DB),
            Res;
        X when X > 1 ->
            sql_bridge_utils:trans_shallower(DB),
            ok %% mysql does not supported nested transactions, so we'll only commit on the last depth
    end,
	sql_bridge_utils:checkin_pool(DB),
	ok.

with_transaction(DB, Fun) ->
	start_transaction(DB),
	try Fun() of
		Res ->
			commit_transaction(DB),
			Res
	catch
		Error:Class ->
            ErrMsg = [{Error, Class}, erlang:get_stacktrace()],
            wf:warning("Errored Query: ~p",[ErrMsg]),
			rollback_transaction(DB),
			{error, ErrMsg}
	end.

query(Type, DB, Q, ParamList) ->
	try query_catched(Type, DB, Q, ParamList)
	catch<commit_msg>Should not be using wf:warning. Use error logger<commit_after>-module(sql_bridge_mysql_otp).
-behaviour(sql_bridge_adapter).
-include("compat.hrl").

-export([start/0,
		 connect/5,
		 query/4,
		 schema_db_column/0,
		 encode/1,
		 start_transaction/1,
		 commit_transaction/1,
		 rollback_transaction/1,
		 with_transaction/2
		]).

%% 
-export([maybe_replace_tokens/2]).


start() ->
	application:start(poolboy),
	ok.

connect(DB, User, Pass, Host, Port) when is_atom(DB) ->
	WorkerArgs = [
		{database, atom_to_list(DB)},
		{host, Host},
		{user, User},
		{password, <PASSWORD>},
		{port, Port}
	],
	sql_bridge_utils:start_poolboy_pool(DB, WorkerArgs, mysql),
	ok.

start_transaction(DB) ->
    case sql_bridge_utils:trans_depth(DB) of
        0 ->
            query(none, DB, "BEGIN", []);
        _ ->
            ok
    end,
    sql_bridge_utils:trans_deeper(DB),
    sql_bridge_utils:checkout_pool(DB),
    ok.

rollback_transaction(DB) ->
    sql_bridge_utils:trans_shallower(DB),
	query(none, DB, "ROLLBACK", []),
	sql_bridge_utils:checkin_pool(DB),
	ok.

commit_transaction(DB) ->
    case sql_bridge_utils:trans_depth(DB) of
        1 ->
            Res = query(none, DB, "COMMIT", []),
            sql_bridge_utils:clear_trans_depth(DB),
            Res;
        X when X > 1 ->
            sql_bridge_utils:trans_shallower(DB),
            ok %% mysql does not supported nested transactions, so we'll only commit on the last depth
    end,
	sql_bridge_utils:checkin_pool(DB),
	ok.

with_transaction(DB, Fun) ->
	start_transaction(DB),
	try Fun() of
		Res ->
			commit_transaction(DB),
			Res
	catch
		Error:Class ->
            ErrMsg = [{Error, Class}, erlang:get_stacktrace()],
            error_logger:info_msg("Errored Query: ~p",[ErrMsg]),
			rollback_transaction(DB),
			{error, ErrMsg}
	end.

query(Type, DB, Q, ParamList) ->
	try query_catched(Type, DB, Q, ParamList)
	catch
{'fraction_non_alphanumeric': 0.1382808328884143, 'fraction_numerical': 0.0069407367859049655, 'mean_word_length': 3.2053872053872055, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '2858337', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1331, 'n_words': 254}