Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/mysql.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@
mysql_recv]},
{registered, []},
{applications, [kernel, stdlib]}]}.

34 changes: 17 additions & 17 deletions src/mysql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
%%% provide a simpler, Mnesia-style transaction interface. Also,
%%% moved much of the prepared statement handling code to mysql_conn.erl
%%% and added versioning to prepared statements.
%%%
%%%
%%%
%%% Usage:
%%%
Expand Down Expand Up @@ -65,7 +65,7 @@
%%% ErrSqlState = mysql:get_result_err_sql_state(MysqlRes)
%%% with Reason and ErrSqlState = string()
%%% and ErrCode = integer()
%%%
%%%
%%% If you just want a single MySQL connection, or want to manage your
%%% connections yourself, you can use the mysql_conn module as a
%%% stand-alone single MySQL connection. See the comment at the top of
Expand Down Expand Up @@ -98,7 +98,7 @@
fetch/1,
fetch/2,
fetch/3,

prepare/2,
execute/1,
execute/2,
Expand Down Expand Up @@ -142,7 +142,7 @@

-record(conn, {
pool_id, %% atom(), the pool's id
pid, %% pid(), mysql_conn process
pid, %% pid(), mysql_conn process
reconnect, %% true | false, should mysql_dispatcher try
%% to reconnect if this connection dies?
host, %% string()
Expand All @@ -156,15 +156,15 @@
-record(state, {
%% gb_tree mapping connection
%% pool id to a connection pool tuple
conn_pools = gb_trees:empty(),
conn_pools = gb_trees:empty(),


%% gb_tree mapping connection Pid
%% to pool id
pids_pools = gb_trees:empty(),
pids_pools = gb_trees:empty(),

%% function for logging,
log_fun,
log_fun,


%% maps names to {Statement::binary(), Version::integer()} values
Expand All @@ -187,7 +187,7 @@
LogFun(?MODULE,?LINE,Level,fun()-> {Msg,[]} end)).
-define(Log2(LogFun,Level,Msg,Params),
LogFun(?MODULE,?LINE,Level,fun()-> {Msg,Params} end)).


log(Module, Line, _Level, FormatFun) ->
{Format, Arguments} = FormatFun(),
Expand Down Expand Up @@ -307,7 +307,7 @@ new_conn(PoolId, ConnPid, Reconnect, Host, Port, User, Password, Database,
database = Database,
encoding = Encoding
};
false ->
false ->
#conn{pool_id = PoolId,
pid = ConnPid,
reconnect = false}
Expand All @@ -333,7 +333,7 @@ fetch(Query) ->
fetch(PoolId, Query) ->
fetch(PoolId, Query, undefined).

fetch(PoolId, Query, Timeout) ->
fetch(PoolId, Query, Timeout) ->
case get(?STATE_VAR) of
undefined ->
call_server({fetch, PoolId, Query}, Timeout);
Expand Down Expand Up @@ -471,7 +471,7 @@ get_result_field_info(#mysql_result{fieldinfo = FieldInfo}) ->
FieldInfo.

%% @doc Extract the Rows from MySQL Result on data received
%%
%%
%% @spec get_result_rows(MySQLRes::mysql_result()) -> [Row::list()]
get_result_rows(#mysql_result{rows=AllRows}) ->
AllRows.
Expand Down Expand Up @@ -645,7 +645,7 @@ handle_info({'DOWN', _MonitorRef, process, Pid, Info}, State) ->
"received 'DOWN' signal from pid ~p not in my list", [Pid]),
{noreply, State}
end.

terminate(Reason, State) ->
LogFun = State#state.log_fun,
LogLevel = case Reason of
Expand All @@ -672,7 +672,7 @@ fetch_queries(PoolId, From, State, QueryList) ->

with_next_conn(PoolId, State, Fun) ->
case get_next_conn(PoolId, State) of
{ok, Conn, NewState} ->
{ok, Conn, NewState} ->
Fun(Conn, NewState);
error ->
%% we have no active connection matching PoolId
Expand All @@ -691,7 +691,7 @@ add_conn(Conn, State) ->
erlang:monitor(process, Conn#conn.pid),
PoolId = Conn#conn.pool_id,
ConnPools = State#state.conn_pools,
NewPool =
NewPool =
case gb_trees:lookup(PoolId, ConnPools) of
none ->
{[Conn],[]};
Expand Down Expand Up @@ -724,7 +724,7 @@ remove_pid_from_lists(Pid, Conns1, Conns2) ->
{NewConns1, Conn} ->
{Conn, {NewConns1, Conns2}}
end.

remove_conn(Pid, State) ->
PidsPools = State#state.pids_pools,
case gb_trees:lookup(Pid, PidsPools) of
Expand Down
32 changes: 16 additions & 16 deletions src/mysql_conn.erl
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ loop(State) ->
%% GenSrvFrom is either a gen_server:call/3 From term(),
%% or a pid if no gen_server was used to make the query
send_reply(GenSrvFrom, Res) when is_pid(GenSrvFrom) ->
%% The query was not sent using gen_server mechanisms
%% The query was not sent using gen_server mechanisms
GenSrvFrom ! {fetch_result, self(), Res};
send_reply(GenSrvFrom, Res) ->
gen_server:reply(GenSrvFrom, Res).
Expand Down Expand Up @@ -482,7 +482,7 @@ do_queries(State, Queries) ->

%% Execute a list of queries, returning the response for the last query.
%% If a query returns an error before the last query is executed, the
%% loop is aborted and the error is returned.
%% loop is aborted and the error is returned.
do_queries(Sock, RecvPid, LogFun, Queries, Version) ->
catch
lists:foldl(
Expand All @@ -495,7 +495,7 @@ do_queries(Sock, RecvPid, LogFun, Queries, Version) ->

do_transaction(State, Fun) ->
case do_query(State, <<"BEGIN">>) of
{error, _} = Err ->
{error, _} = Err ->
{aborted, Err};
_ ->
case catch Fun() of
Expand Down Expand Up @@ -566,7 +566,7 @@ make_statements_for_execute(Name, Params) ->
ParamNums = lists:seq(1, NumParams),

NameBin = atom_to_binary(Name),

ParamNames =
lists:foldl(
fun(Num, Acc) ->
Expand Down Expand Up @@ -699,12 +699,12 @@ get_query_response(LogFun, RecvPid, Version) ->
{updated, #mysql_result{affectedrows=AffectedRows, insertid=InsertId}};
255 ->
case get_error_data(Rest, Version) of
{Code, {SqlState, Message}} ->
{Code, {SqlState, Message}} ->
% MYSQL_4_1 error data
{error, #mysql_result{error=Message,
{error, #mysql_result{error=Message,
errcode=Code,
errsqlstate=SqlState}};
{Code, Message} ->
{Code, Message} ->
% MYSQL_4_0 error data
{error, #mysql_result{error=Message,
errcode=Code}}
Expand All @@ -717,12 +717,12 @@ get_query_response(LogFun, RecvPid, Version) ->
{ok, Rows} ->
{data, #mysql_result{fieldinfo=Fields,
rows=Rows}};
{error, {Code, {SqlState, Message}}} ->
{error, {Code, {SqlState, Message}}} ->
% MYSQL_4_1 error data
{error, #mysql_result{error=Message,
{error, #mysql_result{error=Message,
errcode=Code,
errsqlstate=SqlState}};
{error, {Code, Message}} ->
{error, {Code, Message}} ->
% MYSQL_4_0 error data
{error, #mysql_result{error=Message,
errcode=Code}}
Expand Down Expand Up @@ -767,7 +767,7 @@ get_fields(LogFun, RecvPid, Res, ?MYSQL_4_0) ->
Field,
Length,
%% TODO: Check on MySQL 4.0 if types are specified
%% using the same 4.1 formalism and could
%% using the same 4.1 formalism and could
%% be expanded to atoms:
Type},
get_fields(LogFun, RecvPid, [This | Res], ?MYSQL_4_0)
Expand Down Expand Up @@ -798,7 +798,7 @@ get_fields(LogFun, RecvPid, Res, ?MYSQL_4_1) ->
Length:32/little, Type:8/little,
_Flags:16/little, _Decimals:8/little,
_Rest7/binary>> = Rest6,

This = {Table,
Field,
Length,
Expand Down Expand Up @@ -827,7 +827,7 @@ get_rows(Fields, LogFun, RecvPid, Res, Version) ->
<<254:8, Rest/binary>> when size(Rest) < 8 ->
{ok, lists:reverse(Res)};
<<255:8, Rest/binary>> ->
{Code, ErrData} = get_error_data(Rest, Version),
{Code, ErrData} = get_error_data(Rest, Version),
{error, {Code, ErrData}};
_ ->
{ok, This} = get_row(Fields, Packet, []),
Expand All @@ -852,7 +852,7 @@ get_row([Field | OtherFields], Data, Res) ->

get_with_length(Bin) when is_binary(Bin) ->
{Length, Rest} = get_lcb(Bin),
case get_lcb(Bin) of
case get_lcb(Bin) of
{null, Rest} -> {null, Rest};
_ -> split_binary(Rest, Length)
end.
Expand Down Expand Up @@ -900,7 +900,7 @@ normalize_version([$4,$.,$1|_T], _LogFun) ->
?MYSQL_4_1;
normalize_version([$5|_T], _LogFun) ->
%% MySQL version 5.x protocol is compliant with MySQL 4.1.x:
?MYSQL_4_1;
?MYSQL_4_1;
normalize_version(_Other, LogFun) ->
?Log(LogFun, error, "MySQL version not supported: MySQL Erlang module "
"might not work correctly."),
Expand Down Expand Up @@ -982,7 +982,7 @@ convert_type(Val, ColType) ->
_Other ->
Val
end.

get_error_data(ErrPacket, ?MYSQL_4_0) ->
<<Code:16/little, Message/binary>> = ErrPacket,
{Code, binary_to_list(Message)};
Expand Down
18 changes: 8 additions & 10 deletions test/mysql_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
test() ->
compile:file("/usr/local/lib/erlang/lib/mysql/mysql.erl"),
compile:file("/usr/local/lib/erlang/lib/mysql/mysql_conn.erl"),

%% Start the MySQL dispatcher and create the first connection
%% to the database. 'p1' is the connection pool identifier.
mysql:start_link(p1, "localhost", "root", "password", "test"),
Expand All @@ -18,7 +18,7 @@ test() ->
true),
mysql:connect(p1, "localhost", undefined, "root", "password", "test",
true),

mysql:fetch(p1, <<"DELETE FROM developer">>),

mysql:fetch(p1, <<"INSERT INTO developer(name, country) VALUES "
Expand All @@ -28,17 +28,17 @@ test() ->
%% Execute a query (using a binary)
Result1 = mysql:fetch(p1, <<"SELECT * FROM developer">>),
io:format("Result1: ~p~n", [Result1]),

%% Register a prepared statement
mysql:prepare(update_developer_country,
<<"UPDATE developer SET country=? where name like ?">>),

%% Execute the prepared statement
mysql:execute(p1, update_developer_country, [<<"Sweden">>, <<"%Wiger">>]),

Result2 = mysql:fetch(p1, <<"SELECT * FROM developer">>),
io:format("Result2: ~p~n", [Result2]),

mysql:transaction(
p1,
fun() -> mysql:fetch(<<"INSERT INTO developer(name, country) VALUES "
Expand All @@ -49,7 +49,7 @@ test() ->

Result3 = mysql:fetch(p1, <<"SELECT * FROM developer">>),
io:format("Result3: ~p~n", [Result3]),

mysql:prepare(delete_all, <<"DELETE FROM developer">>),

{aborted, {{error, foo}, _}} =
Expand All @@ -70,7 +70,5 @@ test() ->
Result5 = mysql:fetch(p1, <<"SELECT * FROM numbers WHERE name='t1'">>),
{data, {mysql_result, _, [[<<"t1">>, D]], _, _, _, _, _}} = Result5,
mysql:fetch(p1, <<"DELETE FROM numbers">>),

ok.