Skip to content

Commit

Permalink
update example mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
KosmosFult committed Oct 12, 2024
1 parent f6256d7 commit a6c6f86
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 5 deletions.
20 changes: 20 additions & 0 deletions examples/features/mysql/client/create_table.sql
Original file line number Diff line number Diff line change
@@ -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);
82 changes: 78 additions & 4 deletions examples/features/mysql/client/fiber/fiber_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -45,6 +45,18 @@ void printResult(const std::vector<std::tuple<int, std::string>>& 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<char>(trpc::Random<uint8_t>(0, 255));
random_data.push_back(random_byte);
}

return MysqlBlob(std::move(random_data));
}

void PrintResultTable(const MysqlResults<IterMode>& res) {
std::vector<std::string> fields_name;
bool flag = false;
Expand Down Expand Up @@ -87,6 +99,7 @@ void PrintResultTable(const MysqlResults<IterMode>& res) {
}

void TestQuery(std::shared_ptr<trpc::mysql::MysqlServiceProxy>& proxy) {
std::cout << "TestQuery\n";
trpc::ClientContextPtr ctx = trpc::MakeClientContext(proxy);
MysqlResults<int, std::string> res;
proxy->Query(ctx, res, "select id, username from users where id = ? and username = ?", 3, "carol");
Expand All @@ -108,7 +121,7 @@ void TestQuery(std::shared_ptr<trpc::mysql::MysqlServiceProxy>& proxy) {
}

void TestUpdate(std::shared_ptr<trpc::mysql::MysqlServiceProxy>& proxy) {
std::cout << "\n\n\n";
std::cout << "\n\nTestUpdate\n";
trpc::ClientContextPtr ctx = trpc::MakeClientContext(proxy);
MysqlResults<OnlyExec> exec_res;
MysqlResults<std::string, MysqlTime> query_res;
Expand Down Expand Up @@ -269,13 +282,72 @@ void TestRollback(std::shared_ptr<trpc::mysql::MysqlServiceProxy>& proxy) {
}


void TestError(std::shared_ptr<trpc::mysql::MysqlServiceProxy>& proxy) {
std::cout << "\nTestError\n";
MysqlResults<int> 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<trpc::mysql::MysqlServiceProxy>& proxy) {
std::cout << "\nTestBlob\n";

MysqlResults<OnlyExec> exec_res;
MysqlResults<MysqlBlob> special_res;
MysqlResults<NativeString> str_res;
MysqlResults<IterMode> 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");
TestQuery(proxy);
TestUpdate(proxy);
TestCommit(proxy);
TestRollback(proxy);
TestError(proxy);
TestBlob(proxy);
return 0;
}

Expand All @@ -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(); });
Expand Down
4 changes: 3 additions & 1 deletion examples/features/mysql/client/future/future_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ void TestAsyncTx(std::shared_ptr<trpc::mysql::MysqlServiceProxy>& 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)
Expand Down Expand Up @@ -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(); });
Expand Down
10 changes: 10 additions & 0 deletions examples/features/mysql/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions examples/features/mysql/run_cmake.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a6c6f86

Please sign in to comment.