Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make the eunit works #21

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
Binary file modified rebar
Binary file not shown.
3 changes: 3 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{erl_opts, []}.
{deps, []}.
{cover_enabled, true}.
5 changes: 1 addition & 4 deletions src/mysql.app.src
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{application, mysql,
[{description, "MySQL Library"},
{vsn, "34"},
{modules, [mysql,
mysql_auth,
mysql_conn,
mysql_recv]},
{modules, []},
{registered, []},
{applications, [kernel, stdlib]}]}.

2 changes: 2 additions & 0 deletions src/mysql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,8 @@ encode(Val, true) when is_binary(Val) ->
quote(Val);
encode(Val, true) ->
list_to_binary(encode(Val,false));
encode(Val, false) when is_boolean(Val) ->
atom_to_list(Val);
encode(Val, false) when is_atom(Val) ->
quote(atom_to_list(Val));
encode(Val, false) when is_list(Val) ->
Expand Down
34 changes: 19 additions & 15 deletions src/mysql_conn.erl
Original file line number Diff line number Diff line change
Expand Up @@ -149,38 +149,41 @@ start(Host, Port, User, Password, Database, LogFun, Encoding, PoolId) ->
init(Host, Port, User, Password, Database,
LogFun, Encoding, PoolId, ConnPid)
end),
post_start(Pid, LogFun).
post_start(Pid).

start_link(Host, Port, User, Password, Database, LogFun, Encoding, PoolId) ->
ConnPid = self(),
Pid = spawn_link(fun () ->
init(Host, Port, User, Password, Database,
LogFun, Encoding, PoolId, ConnPid)
end),
post_start(Pid, LogFun).
post_start(Pid).

%% part of start/6 or start_link/6:
post_start(Pid, LogFun) ->
post_start(Pid) ->
receive
{mysql_conn, Pid, ok} ->
{ok, Pid};
{mysql_conn, Pid, {error, Reason}} ->
{error, Reason};
{mysql_conn, OtherPid, {error, Reason}} ->
% Ignore error message from other processes. This handles the case
% when mysql is shutdown and takes more than 5 secs to close the
% listener socket.
?Log2(LogFun, debug, "Ignoring message from process ~p | Reason: ~p",
[OtherPid, Reason]),
post_start(Pid, LogFun);
Unknown ->
?Log2(LogFun, error,
"received unknown signal: ~p", [Unknown]),
post_start(Pid, LogFun)
{'EXIT', Pid, Reason} ->
{error, Reason}
after 5000 ->
%% same behavior as in proc_lib:sync_wait/2
unlink(Pid),
exit(Pid, kill),
flush(Pid),
{error, "timed out"}
end.

flush(Pid) ->
receive
{'EXIT', Pid, _} ->
true
after 0 ->
true
end.

%%--------------------------------------------------------------------
%% Function: fetch(Pid, Query, From)
%% fetch(Pid, Query, From, Timeout)
Expand Down Expand Up @@ -512,8 +515,9 @@ do_execute(State, Name, Params, ExpectedVersion) ->

prepare_and_exec(State, Name, Version, Stmt, Params) ->
NameBin = atom_to_binary(Name),
StmtEscaped = binary:replace(Stmt,<<"'">>,<<"\\'">>,[global]),
StmtBin = <<"PREPARE ", NameBin/binary, " FROM '",
Stmt/binary, "'">>,
StmtEscaped/binary, "'">>,
case do_query(State, StmtBin) of
{updated, _} ->
State1 =
Expand Down
66 changes: 0 additions & 66 deletions test/mysql_test.erl

This file was deleted.

99 changes: 99 additions & 0 deletions test/mysql_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
%% file: mysql_test.erl
%% author: Yariv Sadan ([email protected])
%% for license see COPYING
%% improved by ZhengXujin ([email protected])

%%
%% mysql script:
%% delimiter $$
%%
%% CREATE DATABASE `erlang_mysql_driver` /*!40100 DEFAULT CHARACTER SET utf8 */$$
%%
%% CREATE TABLE `developer` (
%% `name` varchar(255) NOT NULL,
%% `country` varchar(255) DEFAULT NULL,
%% PRIMARY KEY (`name`)
%% ) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
%%

-module(mysql_tests).
-include_lib("eunit/include/eunit.hrl").

-define(DB, "erlang_mysql_driver").

mysql_test() ->
%% Start the MySQL dispatcher and create the first connection
%% to the database. 'p1' is the connection pool identifier.
mysql:start_link(p1, "localhost", "root", "", ?DB),

%% Add 2 more connections to the connection pool
mysql:connect(p1, "localhost", undefined, "root", "", ?DB,
true),
mysql:connect(p1, "localhost", undefined, "root", "", ?DB,
true),

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

mysql:fetch(p1, <<"INSERT INTO developer(name, country) VALUES "
"('Claes (Klacke) Wikstrom', 'Sweden'),"
"('Ulf Wiger', 'USA')">>),

%% Execute a query (using a binary)
Result1 = mysql:fetch(p1, <<"SELECT * FROM developer">>),
io:format("Result1: ~p~n", [Result1]),

Except1 = {data,{mysql_result,[{<<"developer">>,<<"name">>,255,'VAR_STRING'},
{<<"developer">>,<<"country">>,255,'VAR_STRING'}],
[[<<"Claes (Klacke) Wikstrom">>,<<"Sweden">>],
[<<"Ulf Wiger">>,<<"USA">>]],
0,0,[],0,[]}},

?assertEqual(Except1, 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">>),

Except2 = {data,{mysql_result,[{<<"developer">>,<<"name">>,255,'VAR_STRING'},
{<<"developer">>,<<"country">>,255,
'VAR_STRING'}],
[[<<"Claes (Klacke) Wikstrom">>,<<"Sweden">>],
[<<"Ulf Wiger">>,<<"Sweden">>]],
0,0,[],0,[]}},

?assertEqual(Except2, Result2),

mysql:transaction(
p1,
fun() -> mysql:fetch(<<"INSERT INTO developer(name, country) VALUES "
"('Joe Armstrong', 'USA')">>),
mysql:fetch(<<"DELETE FROM developer WHERE name like "
"'Claes%'">>)
end),

Result3 = mysql:fetch(p1, <<"SELECT * FROM developer">>),

Except3 = {data,{mysql_result,[{<<"developer">>,<<"name">>,255,'VAR_STRING'},
{<<"developer">>,<<"country">>,255,
'VAR_STRING'}],
[[<<"Joe Armstrong">>,<<"USA">>],
[<<"Ulf Wiger">>,<<"Sweden">>]],
0,0,[],0,[]}},

?assertEqual(Except3, Result3),

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

{aborted, {{error, foo}, {rollback_result, _RollCmd}}} = mysql:transaction(
p1,
fun() -> mysql:execute(delete_all),
throw({error, foo})
end),

Result4 = mysql:fetch(p1, <<"SELECT * FROM developer">>),
?assertEqual(Except3, Result4).