Skip to content

Commit

Permalink
test async db query
Browse files Browse the repository at this point in the history
  • Loading branch information
hggq committed Jan 17, 2025
1 parent 3a326f3 commit e89754b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
35 changes: 29 additions & 6 deletions controller/src/techempower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ asio::awaitable<std::string> techempowerdb(std::shared_ptr<httppeer> peer)
unsigned int rd_num = rand_range(1, 10000);
myworld.where("id", rd_num);
myworld.limit(1);
co_await myworld.async_fetch_one();
std::size_t n = co_await myworld.async_fetch_one();
if (n == 0)
{
n = co_await myworld.async_fetch_one();
}

peer->output = myworld.data_tojson();
co_return "";
}
Expand All @@ -69,7 +74,11 @@ asio::awaitable<std::string> techempowerqueries(std::shared_ptr<httppeer> peer)
myworld.wheresql.clear();
unsigned int rd_num = rand_range(1, 10000);
myworld.where("id", rd_num);
co_await myworld.async_fetch_append();
std::size_t n = co_await myworld.async_fetch_append();
if (n == 0)
{
n = co_await myworld.async_fetch_append();
}
}

peer->output = myworld.to_json();
Expand All @@ -83,7 +92,12 @@ asio::awaitable<std::string> techempowerfortunes(std::shared_ptr<httppeer> peer)
peer->set_header("Date", get_gmttime());

auto myfortune = orm::Fortune();
co_await myfortune.async_fetch();
std::size_t n = co_await myfortune.async_fetch();
if (n == 0)
{
n = co_await myfortune.async_fetch();
}

myfortune.data.id = 0;
myfortune.data.message = "Additional fortune added at request time.";
myfortune.record.push_back(myfortune.data);
Expand Down Expand Up @@ -130,13 +144,22 @@ asio::awaitable<std::string> techempowerupdates(std::shared_ptr<httppeer> peer)
{
myworld.wheresql.clear();
myworld.where("id", rand_range(1, 10000));
co_await myworld.async_fetch_append();
std::size_t n = co_await myworld.async_fetch_append();
if (n == 0)
{
n = co_await myworld.async_fetch_append();
}
if (myworld.effect() > 0)
{
unsigned int j = myworld.record.size() - 1;
myworld.data.randomnumber = rand_range(1, 10000);
myworld.record[j].randomnumber = myworld.data.randomnumber;
co_await myworld.async_update("randomnumber");

n = co_await myworld.async_update("randomnumber");
if (n == 0)
{
n = co_await myworld.async_update("randomnumber");
}
}
}
peer->output = myworld.to_json();
Expand Down Expand Up @@ -247,4 +270,4 @@ asio::awaitable<std::string> techempowercached_db(std::shared_ptr<httppeer> peer
co_return "";
}

}// namespace http
}// namespace http
36 changes: 20 additions & 16 deletions vendor/httpserver/include/mysqlorm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2358,7 +2358,7 @@ class mysql_orm : public base
if (get_cacherecord(sqlhashid))
{
iscache = false;
co_return 1;
co_return 0;
}
}

Expand Down Expand Up @@ -2388,8 +2388,8 @@ class mysql_orm : public base
iserror = true;
co_return 0;
}
auto conn = co_await conn_obj->async_get_select_conn();

//auto conn = co_await conn_obj->async_get_select_conn();
auto conn = co_await conn_obj->async_add_select_connect();
std::size_t n = co_await asio::async_write(*conn->socket, asio::buffer(querysql), asio::use_awaitable);
unsigned char result_data[4096];
std::memset(result_data, 0x00, 4096);
Expand Down Expand Up @@ -2466,6 +2466,7 @@ class mysql_orm : public base
tempnum = tempnum + name_length;
}
base::record.emplace_back(std::move(data_temp));
effect_num++;
}
}
else
Expand All @@ -2479,7 +2480,7 @@ class mysql_orm : public base
}
}
}
conn_obj->back_select_conn(conn);
//conn_obj->back_select_conn(conn);

if (iscache)
{
Expand All @@ -2490,7 +2491,7 @@ class mysql_orm : public base
iscache = false;
}
}
co_return 1;
co_return effect_num;
}
catch (const std::exception &e)
{
Expand Down Expand Up @@ -2739,7 +2740,8 @@ class mysql_orm : public base
iserror = true;
co_return 0;
}
auto conn = co_await conn_obj->async_get_select_conn();
//auto conn = co_await conn_obj->async_get_select_conn();
auto conn = co_await conn_obj->async_add_select_connect();

std::size_t n = co_await asio::async_write(*conn->socket, asio::buffer(querysql), asio::use_awaitable);
unsigned char result_data[4096];
Expand Down Expand Up @@ -2831,7 +2833,7 @@ class mysql_orm : public base
}
}
}
conn_obj->back_select_conn(conn);
//conn_obj->back_select_conn(conn);

if (iscache)
{
Expand All @@ -2842,7 +2844,7 @@ class mysql_orm : public base
iscache = false;
}
}
co_return 1;
co_return effect_num;
}
catch (const std::exception &e)
{
Expand Down Expand Up @@ -3064,7 +3066,7 @@ class mysql_orm : public base
if (get_cacherecord(sqlhashid))
{
iscache = false;
co_return 1;
co_return 0;
}
}

Expand Down Expand Up @@ -3094,14 +3096,13 @@ class mysql_orm : public base
iserror = true;
co_return 0;
}
auto conn = co_await conn_obj->async_get_select_conn();
//auto conn = co_await conn_obj->async_get_select_conn();
auto conn = co_await conn_obj->async_add_select_connect();

std::size_t n = co_await asio::async_write(*conn->socket, asio::buffer(querysql), asio::use_awaitable);
unsigned char result_data[4096];
std::memset(result_data, 0x00, 4096);

asio::error_code ec;

// 就是查出原始表字段
pack_info_t temp_pack_data;
temp_pack_data.seq_id = 1;
Expand Down Expand Up @@ -3174,6 +3175,7 @@ class mysql_orm : public base
tempnum = tempnum + name_length;
}
base::record.emplace_back(std::move(data_temp));
effect_num++;
}
else
{
Expand All @@ -3200,7 +3202,8 @@ class mysql_orm : public base
}
}
}
conn_obj->back_select_conn(conn);

//conn_obj->back_select_conn(conn);

if (iscache)
{
Expand All @@ -3211,7 +3214,7 @@ class mysql_orm : public base
iscache = false;
}
}
co_return 1;
co_return effect_num;
}
catch (const std::exception &e)
{
Expand Down Expand Up @@ -3841,7 +3844,8 @@ class mysql_orm : public base
iserror = true;
co_return 0;
}
auto conn = co_await conn_obj->async_get_edit_conn();
//auto conn = co_await conn_obj->async_get_edit_conn();
auto conn = co_await conn_obj->async_add_edit_connect();

std::size_t n = co_await asio::async_write(*conn->socket, asio::buffer(querysql), asio::use_awaitable);
unsigned char result_data[4096];
Expand All @@ -3850,7 +3854,7 @@ class mysql_orm : public base
asio::error_code ec;
unsigned int offset = 0;
n = co_await conn->socket->async_read_some(asio::buffer(result_data), asio::use_awaitable);
conn_obj->back_edit_conn(conn);
//conn_obj->back_edit_conn(conn);
if (ec)
{
error_msg = "read error !";
Expand Down

0 comments on commit e89754b

Please sign in to comment.