-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove libmysqlclient unnecessary dependency; fixed MysqlServiceProxy…
… error check
- Loading branch information
1 parent
c907db1
commit 81e9d81
Showing
10 changed files
with
161 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,15 @@ using trpc::mysql::MysqlBlob; | |
|
||
DEFINE_string(client_config, "fiber_client_client_config.yaml", "trpc cpp framework client_config file"); | ||
|
||
|
||
#define MYSQL_ERROR_CHECK(status, res) if(!s.OK()) { \ | ||
TRPC_FMT_ERROR("status: {}", s.ToString()); \ | ||
return; \ | ||
} else if(!res.OK()) { \ | ||
TRPC_FMT_ERROR("MySQL error: {}", res.GetErrorMessage()); \ | ||
return; \ | ||
} \ | ||
|
||
MysqlBlob GenRandomBlob(std::size_t length) { | ||
std::string random_data; | ||
random_data.reserve(length); | ||
|
@@ -93,25 +102,17 @@ void TestQuery(std::shared_ptr<trpc::mysql::MysqlServiceProxy>& proxy) { | |
MysqlResults<int, std::string> res; | ||
trpc::Status s = proxy->Query(ctx, res, "select id, username from users where id = ? and username = ?", 3, "carol"); | ||
|
||
if(!s.OK()) { | ||
TRPC_FMT_ERROR("status: {}", s.ToString()); | ||
} else if(!res.OK()) { | ||
TRPC_FMT_ERROR("MySQL error: {}", res.GetErrorMessage()); | ||
} else { | ||
// const std::vector<std::tuple<int, std::string>>& res_set | ||
auto& res_set = res.ResultSet(); | ||
int id = std::get<0>(res_set[0]); | ||
std::string username = std::get<1>(res_set[0]); | ||
std::cout << "id: " << id << ", username: " << username << std::endl; | ||
} | ||
MYSQL_ERROR_CHECK(s, res); | ||
// const std::vector<std::tuple<int, std::string>>& res_set | ||
auto& res_set = res.ResultSet(); | ||
int id = std::get<0>(res_set[0]); | ||
std::string username = std::get<1>(res_set[0]); | ||
std::cout << "id: " << id << ", username: " << username << std::endl; | ||
|
||
MysqlResults<NativeString> res2; | ||
s = proxy->Query(ctx, res2, "select * from users"); | ||
|
||
if(!s.OK()) { | ||
TRPC_FMT_ERROR("status: {}", s.ToString()); | ||
return; | ||
} | ||
MYSQL_ERROR_CHECK(s, res2); | ||
|
||
int col_index = 0; | ||
int row_index = 0; | ||
|
@@ -139,27 +140,33 @@ void TestUpdate(std::shared_ptr<trpc::mysql::MysqlServiceProxy>& proxy) { | |
MysqlTime mtime; | ||
mtime.SetYear(2024).SetMonth(9).SetDay(10); | ||
|
||
proxy->Execute(ctx, exec_res, | ||
trpc::Status s = proxy->Execute(ctx, exec_res, | ||
"insert into users (username, email, created_at)" | ||
"values (\"jack\", \"[email protected]\", ?)", | ||
mtime); | ||
MYSQL_ERROR_CHECK(s, exec_res); | ||
|
||
if(1 == exec_res.GetAffectedRowNum()) | ||
std::cout << "Insert one\n"; | ||
|
||
ctx = trpc::MakeClientContext(proxy); | ||
proxy->Execute(ctx, query_res, "select email, created_at from users where username = ?", | ||
s = proxy->Execute(ctx, query_res, "select email, created_at from users where username = ?", | ||
"jack"); | ||
MYSQL_ERROR_CHECK(s, query_res); | ||
auto& res_vec = query_res.ResultSet(); | ||
std::cout << std::get<0>(res_vec[0]) << std::endl; | ||
|
||
ctx = trpc::MakeClientContext(proxy); | ||
proxy->Execute(ctx, exec_res, "delete from users where username = \"jack\""); | ||
s = proxy->Execute(ctx, exec_res, "delete from users where username = \"jack\""); | ||
MYSQL_ERROR_CHECK(s, exec_res); | ||
if(1 == exec_res.GetAffectedRowNum()) | ||
std::cout << "Delete one\n"; | ||
|
||
ctx = trpc::MakeClientContext(proxy); | ||
proxy->Execute(ctx, query_res, "select email, created_at from users where username = ?", | ||
s = proxy->Execute(ctx, query_res, "select email, created_at from users where username = ?", | ||
"jack"); | ||
MYSQL_ERROR_CHECK(s, query_res); | ||
|
||
if(query_res.ResultSet().empty()) | ||
std::cout << R"(No user "jack" in users)" << std::endl; | ||
} | ||
|
@@ -175,8 +182,13 @@ void TestTime(std::shared_ptr<trpc::mysql::MysqlServiceProxy>& proxy) { | |
// Use MysqlTime | ||
MysqlResults<MysqlTime> time_res; | ||
|
||
proxy->Query(ctx, str_res, "select created_at from users"); | ||
proxy->Query(ctx, time_res, "select created_at from users"); | ||
trpc::Status s; | ||
s = proxy->Query(ctx, str_res, "select created_at from users"); | ||
MYSQL_ERROR_CHECK(s, str_res); | ||
|
||
|
||
s = proxy->Query(ctx, time_res, "select created_at from users"); | ||
MYSQL_ERROR_CHECK(s, time_res); | ||
|
||
std::string_view str_time = str_res.ResultSet()[0][0]; | ||
MysqlTime my_time = std::get<0>(time_res.ResultSet()[0]); | ||
|
@@ -381,16 +393,19 @@ void TestBlob(std::shared_ptr<trpc::mysql::MysqlServiceProxy>& proxy) { | |
|
||
MysqlResults<NativeString> str_res; | ||
|
||
proxy->Query(ctx, bind_blob_res, "select meta from users where username = ?", "jack"); | ||
s = proxy->Query(ctx, bind_blob_res, "select meta from users where username = ?", "jack"); | ||
MYSQL_ERROR_CHECK(s, bind_blob_res); | ||
if(std::get<0>(bind_blob_res.ResultSet()[0]) == blob) | ||
std::cout << "same blob\n"; | ||
|
||
|
||
proxy->Query(ctx, bind_str_res, "select meta from users where username = ?", "jack"); | ||
s = proxy->Query(ctx, bind_str_res, "select meta from users where username = ?", "jack"); | ||
MYSQL_ERROR_CHECK(s, bind_str_res); | ||
if(std::get<0>(bind_str_res.ResultSet()[0]) == blob.AsStringView()) | ||
std::cout << "same blob\n"; | ||
|
||
proxy->Query(ctx, str_res, "select meta from users where username = ?", "jack"); | ||
s = proxy->Query(ctx, str_res, "select meta from users where username = ?", "jack"); | ||
MYSQL_ERROR_CHECK(s, str_res); | ||
auto str_view = str_res.ResultSet()[0][0]; | ||
if(MysqlBlob(std::string(str_view)) == blob) | ||
std::cout << "same blob\n"; | ||
|
@@ -399,12 +414,12 @@ void TestBlob(std::shared_ptr<trpc::mysql::MysqlServiceProxy>& proxy) { | |
|
||
int Run() { | ||
auto proxy = ::trpc::GetTrpcClient()->GetProxy<::trpc::mysql::MysqlServiceProxy>("mysql_server"); | ||
// TestQuery(proxy); | ||
// TestUpdate(proxy); | ||
// TestCommit(proxy); | ||
// TestRollback(proxy); | ||
// TestError(proxy); | ||
// TestBlob(proxy); | ||
TestQuery(proxy); | ||
TestUpdate(proxy); | ||
TestCommit(proxy); | ||
TestRollback(proxy); | ||
TestError(proxy); | ||
TestBlob(proxy); | ||
TestTime(proxy); | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.