diff --git a/examples/features/mysql/client/create_table.sql b/examples/features/mysql/client/create_table.sql new file mode 100644 index 00000000..c0219291 --- /dev/null +++ b/examples/features/mysql/client/create_table.sql @@ -0,0 +1,20 @@ +CREATE DATABASE IF NOT EXISTS test; + +USE test; + +DROP TABLE IF EXISTS users; + +CREATE TABLE `users` ( + `id` int NOT NULL AUTO_INCREMENT, + `username` varchar(50) NOT NULL, + `email` varchar(100) DEFAULT NULL, + `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP, + `meta` blob, + PRIMARY KEY (`id`) +); + +INSERT INTO users (username, email, created_at, meta) VALUES + ('alice', 'alice@example.com', '2024-09-08 13:16:24', NULL), + ('bob', 'bob@abc.com', '2024-09-08 13:16:24', NULL), + ('carol', 'carol@example.com', '2024-09-08 13:16:24', NULL), + ('rose', NULL, '2024-09-08 13:16:53', NULL); \ No newline at end of file diff --git a/examples/features/mysql/client/fiber/fiber_client.cc b/examples/features/mysql/client/fiber/fiber_client.cc index 866fb856..7735c2f2 100644 --- a/examples/features/mysql/client/fiber/fiber_client.cc +++ b/examples/features/mysql/client/fiber/fiber_client.cc @@ -23,7 +23,7 @@ #include "trpc/client/service_proxy.h" #include "trpc/client/trpc_client.h" #include "trpc/common/runtime_manager.h" - +#include "trpc/util/random.h" #include "trpc/log/trpc_log.h" using trpc::mysql::OnlyExec; @@ -32,7 +32,7 @@ using trpc::mysql::NativeString; using trpc::mysql::MysqlResults; using trpc::mysql::MysqlTime; using trpc::mysql::TransactionHandle; - +using trpc::mysql::MysqlBlob; DEFINE_string(client_config, "fiber_client_client_config.yaml", "trpc cpp framework client_config file"); @@ -45,6 +45,18 @@ void printResult(const std::vector>& res_data) { } } +MysqlBlob GenRandomBlob(std::size_t length) { + std::string random_data; + random_data.reserve(length); + + for (std::size_t i = 0; i < length; ++i) { + char random_byte = static_cast(trpc::Random(0, 255)); + random_data.push_back(random_byte); + } + + return MysqlBlob(std::move(random_data)); +} + void PrintResultTable(const MysqlResults& res) { std::vector fields_name; bool flag = false; @@ -87,6 +99,7 @@ void PrintResultTable(const MysqlResults& res) { } void TestQuery(std::shared_ptr& proxy) { + std::cout << "TestQuery\n"; trpc::ClientContextPtr ctx = trpc::MakeClientContext(proxy); MysqlResults res; proxy->Query(ctx, res, "select id, username from users where id = ? and username = ?", 3, "carol"); @@ -108,7 +121,7 @@ void TestQuery(std::shared_ptr& proxy) { } void TestUpdate(std::shared_ptr& proxy) { - std::cout << "\n\n\n"; + std::cout << "\n\nTestUpdate\n"; trpc::ClientContextPtr ctx = trpc::MakeClientContext(proxy); MysqlResults exec_res; MysqlResults query_res; @@ -269,6 +282,63 @@ void TestRollback(std::shared_ptr& proxy) { } +void TestError(std::shared_ptr& proxy) { + std::cout << "\nTestError\n"; + MysqlResults res; + trpc::ClientContextPtr ctx = trpc::MakeClientContext(proxy); + + // Make context timeout + ctx->SetTimeout(0); + trpc::Status s = proxy->Query(ctx, res, "select id from users where username = ?", "alice"); + if(!s.OK()) + std::cout << s.ToString() << std::endl; + + ctx = trpc::MakeClientContext(proxy); + s = proxy->Query(ctx, res, "select id from users where usernames = ?", "alice"); + if(!s.OK()) + return; + if(!res.IsSuccess()) + std::cout << res.GetErrorMessage() << std::endl; + +} + +void TestBlob(std::shared_ptr& proxy) { + std::cout << "\nTestBlob\n"; + + MysqlResults exec_res; + MysqlResults special_res; + MysqlResults str_res; + MysqlResults itr_res; + MysqlBlob blob(GenRandomBlob(1024)); + + trpc::ClientContextPtr ctx = trpc::MakeClientContext(proxy); + // MysqlBlob + trpc::Status s = proxy->Execute(ctx, exec_res, + "insert into users (username, email, meta)" + "values (\"jack\", \"jack@abc.com\", ?)", + blob); + if(s.OK() && exec_res.IsSuccess()) + std::cout << "blob inserted.\n"; + else + return; + + proxy->Query(ctx, special_res, "select meta from users where username = ?", "jack"); + if(std::get<0>(special_res.GetResultSet()[0]) == blob) + std::cout << "same blob\n"; + + proxy->Query(ctx, str_res, "select meta from users where username = ?", "jack"); + auto str_view = str_res.GetResultSet()[0][0]; + if(MysqlBlob(std::string(str_view)) == blob) + std::cout << "same blob\n"; + + proxy->Query(ctx, itr_res, "select meta from users where username = ?", "jack"); + for (auto row : itr_res) { + MysqlBlob data(std::string(row.GetFieldData(0))); + if(data == blob) + std::cout << "same blob\n"; + } + +} int Run() { auto proxy = ::trpc::GetTrpcClient()->GetProxy<::trpc::mysql::MysqlServiceProxy>("mysql_server"); @@ -276,6 +346,8 @@ int Run() { TestUpdate(proxy); TestCommit(proxy); TestRollback(proxy); + TestError(proxy); + TestBlob(proxy); return 0; } @@ -298,7 +370,9 @@ void ParseClientConfig(int argc, char* argv[]) { int main(int argc, char* argv[]) { ParseClientConfig(argc, argv); - + std::cout << "************************************\n" + << "************fiber_client************\n" + << "************************************\n\n"; // If the business code is running in trpc pure client mode, // the business code needs to be running in the `RunInTrpcRuntime` function return ::trpc::RunInTrpcRuntime([]() { return Run(); }); diff --git a/examples/features/mysql/client/future/future_client.cc b/examples/features/mysql/client/future/future_client.cc index 07b62fa8..7aaf6652 100644 --- a/examples/features/mysql/client/future/future_client.cc +++ b/examples/features/mysql/client/future/future_client.cc @@ -116,7 +116,6 @@ void TestAsyncTx(std::shared_ptr& proxy) { trpc::ClientContextPtr ctx = trpc::MakeClientContext(proxy); proxy->Query(ctx, query_res, "select * from users"); - table_rows = query_res.GetResultSet().size(); // Do two query separately in the same one transaction and the handle will be moved to handle2 auto fu = proxy->AsyncBegin(ctx) @@ -241,6 +240,9 @@ void ParseClientConfig(int argc, char* argv[]) { int main(int argc, char* argv[]) { ParseClientConfig(argc, argv); + std::cout << "*************************************\n" + << "************future_client************\n" + << "*************************************\n\n"; // If the business code is running in trpc pure client mode, // the business code needs to be running in the `RunInTrpcRuntime` function return ::trpc::RunInTrpcRuntime([]() { return Run(); }); diff --git a/examples/features/mysql/run.sh b/examples/features/mysql/run.sh index d11a6a7e..e161eb67 100755 --- a/examples/features/mysql/run.sh +++ b/examples/features/mysql/run.sh @@ -3,6 +3,16 @@ # building. bazel build //examples/features/mysql/client/... + +MYSQL_USER="root" +MYSQL_PASSWORD="abc123" +MYSQL_HOST="127.0.0.1" +MYSQL_PORT="3306" +SQL_FILE="examples/features/mysql/client/create_table.sql" + # run mysql client. +mysql -u"$MYSQL_USER" -p"$MYSQL_PASSWORD" -h"$MYSQL_HOST" < "$SQL_FILE" ./bazel-bin/examples/features/mysql/client/fiber/fiber_client --client_config=examples/features/mysql/client/fiber/fiber_client_config.yaml + +mysql -u"$MYSQL_USER" -p"$MYSQL_PASSWORD" -h"$MYSQL_HOST" < "$SQL_FILE" ./bazel-bin/examples/features/mysql/client/future/future_client --client_config=examples/features/mysql/client/future/future_client_config.yaml \ No newline at end of file diff --git a/examples/features/mysql/run_cmake.sh b/examples/features/mysql/run_cmake.sh index 38e31dcb..5ffac52e 100755 --- a/examples/features/mysql/run_cmake.sh +++ b/examples/features/mysql/run_cmake.sh @@ -1,10 +1,19 @@ #! /bin/bash + +MYSQL_USER="root" +MYSQL_PASSWORD="abc123" +MYSQL_HOST="127.0.0.1" +MYSQL_PORT="3306" +SQL_FILE="examples/features/mysql/client/create_table.sql" + # building. mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE=Release .. && make -j8 && cd - mkdir -p examples/features/mysql/build && cd examples/features/mysql/build && cmake -DCMAKE_BUILD_TYPE=Release .. && make -j8 && cd - # run mysql client. +mysql -u"$MYSQL_USER" -p"$MYSQL_PASSWORD" -h"$MYSQL_HOST" < "$SQL_FILE" examples/features/mysql/build/fiber_client --client_config=examples/features/mysql/client/fiber/fiber_client_config.yaml +mysql -u"$MYSQL_USER" -p"$MYSQL_PASSWORD" -h"$MYSQL_HOST" < "$SQL_FILE" examples/features/mysql/build/future_client --client_config=examples/features/mysql/client/future/future_client_config.yaml \ No newline at end of file