2018-11-27 17:37:41 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* db_test.cc
|
|
|
|
* An Tao
|
|
|
|
*
|
|
|
|
* Copyright 2018, An Tao. All rights reserved.
|
|
|
|
* Use of this source code is governed by a MIT license
|
|
|
|
* that can be found in the License file.
|
|
|
|
*
|
|
|
|
* Drogon
|
2019-05-17 22:49:09 +08:00
|
|
|
*
|
2018-11-27 17:37:41 +08:00
|
|
|
* Drogon database test program
|
2019-05-17 22:49:09 +08:00
|
|
|
*
|
2018-11-27 17:37:41 +08:00
|
|
|
*/
|
2019-07-26 22:22:12 +08:00
|
|
|
#include <drogon/config.h>
|
2018-11-27 17:37:41 +08:00
|
|
|
#include <drogon/orm/DbClient.h>
|
2021-02-07 10:34:49 +08:00
|
|
|
#include <drogon/orm/DbTypes.h>
|
2019-05-17 22:49:09 +08:00
|
|
|
#include <trantor/utils/Logger.h>
|
2019-09-28 20:35:05 +08:00
|
|
|
#include <chrono>
|
2020-04-11 11:46:47 +08:00
|
|
|
#include <iostream>
|
2020-01-25 11:58:20 +08:00
|
|
|
#include <thread>
|
2019-08-16 23:41:03 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2020-04-11 11:46:47 +08:00
|
|
|
#include "mysql/Users.h"
|
|
|
|
#include "postgresql/Users.h"
|
|
|
|
#include "sqlite3/Users.h"
|
|
|
|
|
2020-01-25 11:58:20 +08:00
|
|
|
using namespace std::chrono_literals;
|
2018-11-27 17:37:41 +08:00
|
|
|
using namespace drogon::orm;
|
2019-08-17 20:05:32 +08:00
|
|
|
|
2018-11-27 17:37:41 +08:00
|
|
|
#define RESET "\033[0m"
|
|
|
|
#define RED "\033[31m" /* Red */
|
|
|
|
#define GREEN "\033[32m" /* Green */
|
2019-08-16 23:41:03 +08:00
|
|
|
|
2021-02-06 17:05:58 +08:00
|
|
|
#ifdef __cpp_impl_coroutine
|
|
|
|
constexpr int postgre_tests = 46;
|
|
|
|
constexpr int mysql_tests = 47;
|
|
|
|
constexpr int sqlite_tests = 49;
|
|
|
|
#else
|
2020-10-29 20:09:16 +08:00
|
|
|
constexpr int postgre_tests = 44;
|
2020-07-03 12:19:40 +08:00
|
|
|
constexpr int mysql_tests = 45;
|
2020-06-09 00:54:12 +08:00
|
|
|
constexpr int sqlite_tests = 47;
|
2021-02-06 17:05:58 +08:00
|
|
|
#endif
|
2020-04-11 11:46:47 +08:00
|
|
|
|
|
|
|
int test_count = 0;
|
2019-08-16 23:41:03 +08:00
|
|
|
int counter = 0;
|
2019-10-04 14:17:48 +08:00
|
|
|
int gLoops = 1;
|
2019-08-16 23:41:03 +08:00
|
|
|
std::promise<int> pro;
|
2019-08-17 12:40:39 +08:00
|
|
|
auto globalf = pro.get_future();
|
2019-08-16 23:41:03 +08:00
|
|
|
|
2019-09-28 20:35:05 +08:00
|
|
|
using namespace std::chrono_literals;
|
2020-04-11 11:46:47 +08:00
|
|
|
|
|
|
|
int get_test_count();
|
|
|
|
|
2019-08-16 23:41:03 +08:00
|
|
|
void addCount(int &count, std::promise<int> &pro)
|
|
|
|
{
|
|
|
|
++count;
|
2019-08-17 12:40:39 +08:00
|
|
|
// LOG_DEBUG << count;
|
2020-04-11 11:46:47 +08:00
|
|
|
if (count == test_count)
|
2019-08-16 23:41:03 +08:00
|
|
|
{
|
|
|
|
pro.set_value(1);
|
|
|
|
}
|
|
|
|
}
|
2018-11-27 17:37:41 +08:00
|
|
|
|
|
|
|
void testOutput(bool isGood, const std::string &testMessage)
|
|
|
|
{
|
|
|
|
if (isGood)
|
|
|
|
{
|
2019-08-17 20:05:32 +08:00
|
|
|
std::cout << GREEN << counter + 1 << ".\t" << testMessage << "\t\tOK\n";
|
2018-11-27 17:37:41 +08:00
|
|
|
std::cout << RESET;
|
2019-08-16 23:41:03 +08:00
|
|
|
addCount(counter, pro);
|
2018-11-27 17:37:41 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout << RED << testMessage << "\t\tBAD\n";
|
|
|
|
std::cout << RESET;
|
2019-07-26 22:22:12 +08:00
|
|
|
exit(1);
|
2018-11-27 17:37:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 11:46:47 +08:00
|
|
|
void doPostgreTest(const drogon::orm::DbClientPtr &clientPtr)
|
2018-11-27 17:37:41 +08:00
|
|
|
{
|
2019-05-17 22:49:09 +08:00
|
|
|
// Prepare the test environment
|
2019-05-18 11:11:45 +08:00
|
|
|
*clientPtr << "DROP TABLE IF EXISTS USERS" >> [](const Result &r) {
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(true, "postgresql - Prepare the test environment(0)");
|
2019-05-18 11:11:45 +08:00
|
|
|
} >> [](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false, "postgresql - Prepare the test environment(0)");
|
2019-05-18 11:11:45 +08:00
|
|
|
};
|
2018-11-27 17:37:41 +08:00
|
|
|
*clientPtr << "CREATE TABLE users \
|
|
|
|
(\
|
|
|
|
user_id character varying(32),\
|
|
|
|
user_name character varying(64),\
|
|
|
|
password character varying(64),\
|
|
|
|
org_name character varying(20),\
|
|
|
|
signature character varying(50),\
|
|
|
|
avatar_id character varying(32),\
|
|
|
|
id serial PRIMARY KEY,\
|
|
|
|
salt character varying(20),\
|
|
|
|
admin boolean DEFAULT false,\
|
|
|
|
CONSTRAINT user_id_org UNIQUE(user_id, org_name)\
|
|
|
|
)" >>
|
2019-05-18 11:11:45 +08:00
|
|
|
[](const Result &r) {
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(true, "postgresql - Prepare the test environment(1)");
|
2019-05-18 11:11:45 +08:00
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
2018-11-27 17:37:41 +08:00
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false, "postgresql - Prepare the test environment(1)");
|
2018-11-27 17:37:41 +08:00
|
|
|
};
|
|
|
|
/// Test1:DbClient streaming-type interface
|
|
|
|
/// 1.1 insert,non-blocking
|
2019-08-17 12:40:39 +08:00
|
|
|
*clientPtr << "insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values($1,$2,$3,$4) returning *"
|
2018-11-27 17:37:41 +08:00
|
|
|
<< "pg"
|
|
|
|
<< "postgresql"
|
|
|
|
<< "123"
|
|
|
|
<< "default" >>
|
|
|
|
[](const Result &r) {
|
2019-05-17 22:49:09 +08:00
|
|
|
// std::cout << "id=" << r[0]["id"].as<int64_t>() << std::endl;
|
2019-05-18 11:11:45 +08:00
|
|
|
testOutput(r[0]["id"].as<int64_t>() == 1,
|
2020-04-11 11:46:47 +08:00
|
|
|
"postgresql - DbClient streaming-type interface(0)");
|
2018-11-27 17:37:41 +08:00
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient streaming-type interface(0)");
|
2018-11-27 17:37:41 +08:00
|
|
|
};
|
2019-05-17 22:49:09 +08:00
|
|
|
/// 1.2 insert,blocking
|
2021-02-07 10:34:49 +08:00
|
|
|
*clientPtr
|
|
|
|
<< "insert into users (user_id,user_name,admin,password,org_name) "
|
|
|
|
"values($1,$2,$3,$4,$5) returning *"
|
|
|
|
<< "pg1"
|
|
|
|
<< "postgresql1" << drogon::orm::DefaultValue{} << "123"
|
|
|
|
<< "default" << Mode::Blocking >>
|
2018-11-27 17:37:41 +08:00
|
|
|
[](const Result &r) {
|
2019-05-17 22:49:09 +08:00
|
|
|
// std::cout << "id=" << r[0]["id"].as<int64_t>() << std::endl;
|
2019-05-18 11:11:45 +08:00
|
|
|
testOutput(r[0]["id"].as<int64_t>() == 2,
|
2020-04-11 11:46:47 +08:00
|
|
|
"postgresql - DbClient streaming-type interface(1)");
|
2018-11-27 17:37:41 +08:00
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient streaming-type interface(1)");
|
2018-11-27 17:37:41 +08:00
|
|
|
};
|
2019-05-17 22:49:09 +08:00
|
|
|
/// 1.3 query,no-blocking
|
2019-05-18 11:11:45 +08:00
|
|
|
*clientPtr << "select * from users where 1 = 1" << Mode::NonBlocking >>
|
|
|
|
[](const Result &r) {
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r.size() == 2,
|
|
|
|
"postgresql - DbClient streaming-type interface(2)");
|
2019-05-18 11:11:45 +08:00
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient streaming-type interface(2)");
|
2019-05-18 11:11:45 +08:00
|
|
|
};
|
2019-05-17 22:49:09 +08:00
|
|
|
/// 1.4 query,blocking
|
2019-05-18 11:11:45 +08:00
|
|
|
*clientPtr << "select * from users where 1 = 1" << Mode::Blocking >>
|
|
|
|
[](const Result &r) {
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r.size() == 2,
|
|
|
|
"postgresql - DbClient streaming-type interface(3)");
|
2019-08-16 23:41:03 +08:00
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient streaming-type interface(3)");
|
2019-08-16 23:41:03 +08:00
|
|
|
};
|
|
|
|
/// 1.5 query,blocking
|
|
|
|
int count = 0;
|
|
|
|
*clientPtr << "select user_name, user_id, id from users where 1 = 1"
|
|
|
|
<< Mode::Blocking >>
|
|
|
|
[&count](bool isNull,
|
|
|
|
const std::string &name,
|
2020-04-25 02:12:44 +08:00
|
|
|
std::string &&user_id,
|
2019-08-16 23:41:03 +08:00
|
|
|
int id) {
|
|
|
|
if (!isNull)
|
2019-11-21 11:27:47 +08:00
|
|
|
++count;
|
2019-08-16 23:41:03 +08:00
|
|
|
else
|
2019-05-18 11:11:45 +08:00
|
|
|
{
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(count == 2,
|
|
|
|
"postgresql - DbClient streaming-type interface(4)");
|
2019-05-18 11:11:45 +08:00
|
|
|
}
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient streaming-type interface(4)");
|
2019-05-18 11:11:45 +08:00
|
|
|
};
|
2019-08-17 12:40:39 +08:00
|
|
|
/// 1.6 query, parameter binding
|
|
|
|
*clientPtr << "select * from users where id = $1" << 1 >>
|
|
|
|
[](const Result &r) {
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r.size() == 1,
|
|
|
|
"postgresql - DbClient streaming-type interface(5)");
|
2019-08-17 12:40:39 +08:00
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient streaming-type interface(5)");
|
2019-08-17 12:40:39 +08:00
|
|
|
};
|
|
|
|
/// 1.7 query, parameter binding
|
|
|
|
*clientPtr << "select * from users where user_id = $1 and user_name = $2"
|
|
|
|
<< "pg1"
|
|
|
|
<< "postgresql1" >>
|
|
|
|
[](const Result &r) {
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r.size() == 1,
|
|
|
|
"postgresql - DbClient streaming-type interface(6)");
|
2019-08-17 12:40:39 +08:00
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient streaming-type interface(6)");
|
2019-08-17 12:40:39 +08:00
|
|
|
};
|
|
|
|
/// 1.8 delete
|
|
|
|
*clientPtr << "delete from users where user_id = $1 and user_name = $2"
|
|
|
|
<< "pg1"
|
|
|
|
<< "postgresql1" >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.affectedRows() == 1,
|
2020-04-11 11:46:47 +08:00
|
|
|
"postgresql - DbClient streaming-type interface(7)");
|
2019-08-17 12:40:39 +08:00
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient streaming-type interface(7)");
|
2019-08-17 12:40:39 +08:00
|
|
|
};
|
|
|
|
/// 1.9 update
|
|
|
|
*clientPtr << "update users set user_id = $1, user_name = $2 where user_id "
|
|
|
|
"= $3 and user_name = $4"
|
|
|
|
<< "pg1"
|
|
|
|
<< "postgresql1"
|
|
|
|
<< "pg"
|
|
|
|
<< "postgresql" >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.affectedRows() == 1,
|
2020-04-11 11:46:47 +08:00
|
|
|
"postgresql - DbClient streaming-type interface(8)");
|
2019-08-17 12:40:39 +08:00
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient streaming-type interface(8)");
|
|
|
|
};
|
|
|
|
/// 1.10 clean up
|
|
|
|
*clientPtr << "truncate table users restart identity" >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(true,
|
|
|
|
"postgresql - DbClient streaming-type interface(9)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << "error:" << e.base().what() << std::endl;
|
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient streaming-type interface(9)");
|
2019-08-17 12:40:39 +08:00
|
|
|
};
|
|
|
|
/// Test asynchronous method
|
|
|
|
/// 2.1 insert
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"insert into users \
|
|
|
|
(user_id,user_name,password,org_name) \
|
|
|
|
values($1,$2,$3,$4) returning *",
|
|
|
|
[](const Result &r) {
|
|
|
|
// std::cout << "id=" << r[0]["id"].as<int64_t>() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r[0]["id"].as<int64_t>() == 1,
|
|
|
|
"postgresql - DbClient asynchronous interface(0)");
|
2019-08-17 12:40:39 +08:00
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient asynchronous interface(0)");
|
2019-08-17 12:40:39 +08:00
|
|
|
},
|
|
|
|
"pg",
|
|
|
|
"postgresql",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
/// 2.2 insert
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"insert into users \
|
|
|
|
(user_id,user_name,password,org_name) \
|
|
|
|
values($1,$2,$3,$4)",
|
|
|
|
[](const Result &r) {
|
|
|
|
// std::cout << "id=" << r[0]["id"].as<int64_t>() << std::endl;
|
|
|
|
testOutput(r.affectedRows() == 1,
|
2020-04-11 11:46:47 +08:00
|
|
|
"postgresql - DbClient asynchronous interface(1)");
|
2019-08-17 12:40:39 +08:00
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient asynchronous interface(1)");
|
2019-08-17 12:40:39 +08:00
|
|
|
},
|
|
|
|
"pg1",
|
|
|
|
"postgresql1",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
/// 2.3 query
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"select * from users where 1 = 1",
|
|
|
|
[](const Result &r) {
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r.size() == 2,
|
|
|
|
"postgresql - DbClient asynchronous interface(2)");
|
2019-08-17 12:40:39 +08:00
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient asynchronous interface(2)");
|
2019-08-17 12:40:39 +08:00
|
|
|
});
|
|
|
|
/// 2.2 query, parameter binding
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"select * from users where id = $1",
|
|
|
|
[](const Result &r) {
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r.size() == 1,
|
|
|
|
"postgresql - DbClient asynchronous interface(3)");
|
2019-08-17 12:40:39 +08:00
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient asynchronous interface(3)");
|
2019-08-17 12:40:39 +08:00
|
|
|
},
|
|
|
|
1);
|
|
|
|
/// 2.3 query, parameter binding
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"select * from users where user_id = $1 and user_name = $2",
|
|
|
|
[](const Result &r) {
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r.size() == 1,
|
|
|
|
"postgresql - DbClient asynchronous interface(4)");
|
2019-08-17 12:40:39 +08:00
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient asynchronous interface(4)");
|
2019-08-17 12:40:39 +08:00
|
|
|
},
|
|
|
|
"pg1",
|
|
|
|
"postgresql1");
|
|
|
|
/// 2.4 delete
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"delete from users where user_id = $1 and user_name = $2",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.affectedRows() == 1,
|
2020-04-11 11:46:47 +08:00
|
|
|
"postgresql - DbClient asynchronous interface(5)");
|
2019-08-17 12:40:39 +08:00
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient asynchronous interface(5)");
|
2019-08-17 12:40:39 +08:00
|
|
|
},
|
|
|
|
"pg1",
|
|
|
|
"postgresql1");
|
|
|
|
/// 2.5 update
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"update users set user_id = $1, user_name = $2 where user_id "
|
|
|
|
"= $3 and user_name = $4",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.affectedRows() == 1,
|
2020-04-11 11:46:47 +08:00
|
|
|
"postgresql - DbClient asynchronous interface(6)");
|
2019-08-17 12:40:39 +08:00
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient asynchronous interface(6)");
|
2019-08-17 12:40:39 +08:00
|
|
|
},
|
|
|
|
"pg1",
|
|
|
|
"postgresql1",
|
|
|
|
"pg",
|
|
|
|
"postgresql");
|
2020-04-11 11:46:47 +08:00
|
|
|
/// 2.6 clean up
|
2019-08-17 12:40:39 +08:00
|
|
|
clientPtr->execSqlAsync(
|
2020-04-11 11:46:47 +08:00
|
|
|
"truncate table users restart identity",
|
2019-08-17 12:40:39 +08:00
|
|
|
[](const Result &r) {
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(true, "postgresql - DbClient asynchronous interface(7)");
|
2019-08-17 12:40:39 +08:00
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << "error:" << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - DbClient asynchronous interface(7)");
|
2019-08-17 12:40:39 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
/// Test synchronous method
|
|
|
|
/// 3.1 insert
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values($1,$2,$3,$4) returning *",
|
|
|
|
"pg",
|
|
|
|
"postgresql",
|
|
|
|
"123",
|
|
|
|
"default");
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r[0]["id"].as<int64_t>() == 1,
|
|
|
|
"postgresql - DbClient synchronous interface(0)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false, "postgresql - DbClient asynchronous interface(0)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
/// 3.2 insert
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values($1,$2,$3,$4)",
|
|
|
|
"pg1",
|
|
|
|
"postgresql1",
|
|
|
|
"123",
|
|
|
|
"default");
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"postgresql - DbClient synchronous interface(1)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false, "postgresql - DbClient asynchronous interface(1)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
/// 3.3 query
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"select * from users where user_id=$1 and user_name=$2",
|
|
|
|
"pg1",
|
|
|
|
"postgresql1");
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r.size() == 1,
|
|
|
|
"postgresql - DbClient synchronous interface(2)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false, "postgresql - DbClient asynchronous interface(2)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
/// 3.4 query for none
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"select * from users where user_id=$1 and user_name=$2",
|
|
|
|
"pg111",
|
|
|
|
"postgresql1");
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r.size() == 0,
|
|
|
|
"postgresql - DbClient synchronous interface(3)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false, "postgresql - DbClient asynchronous interface(3)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
/// 3.5 bad sql
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
2019-12-11 14:23:52 +08:00
|
|
|
"select * from users where user_id=$1 and user_name='1234'",
|
2019-08-17 12:40:39 +08:00
|
|
|
"pg111",
|
|
|
|
"postgresql1");
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r.size() == 0,
|
|
|
|
"postgresql - DbClient synchronous interface(4)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
// std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(true, "postgresql - DbClient asynchronous interface(4)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
2020-04-11 11:46:47 +08:00
|
|
|
/// 3.6 clean up
|
2019-08-17 12:40:39 +08:00
|
|
|
try
|
|
|
|
{
|
2020-04-11 11:46:47 +08:00
|
|
|
auto r =
|
|
|
|
clientPtr->execSqlSync("truncate table users restart identity");
|
|
|
|
testOutput(true, "postgresql - DbClient synchronous interface(5)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
// std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false, "postgresql - DbClient asynchronous interface(5)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
/// Test future interface
|
|
|
|
/// 4.1 insert
|
|
|
|
auto f = clientPtr->execSqlAsyncFuture(
|
|
|
|
"insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values($1,$2,$3,$4) returning *",
|
|
|
|
"pg",
|
|
|
|
"postgresql",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r[0]["id"].as<int64_t>() == 1,
|
|
|
|
"postgresql - DbClient future interface(0)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false, "postgresql - DbClient future interface(0)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
/// 4.2 insert
|
|
|
|
f = clientPtr->execSqlAsyncFuture(
|
|
|
|
"insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values($1,$2,$3,$4)",
|
|
|
|
"pg1",
|
|
|
|
"postgresql1",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"postgresql - DbClient future interface(1)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false, "postgresql - DbClient future interface(1)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
/// 4.3 query
|
|
|
|
f = clientPtr->execSqlAsyncFuture(
|
|
|
|
"select * from users where user_id=$1 and user_name=$2",
|
|
|
|
"pg1",
|
|
|
|
"postgresql1");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r.size() == 1, "postgresql - DbClient future interface(2)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false, "postgresql - DbClient future interface(2)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
/// 4.4 query for none
|
|
|
|
f = clientPtr->execSqlAsyncFuture(
|
|
|
|
"select * from users where user_id=$1 and user_name=$2",
|
|
|
|
"pg111",
|
|
|
|
"postgresql1");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r.size() == 0, "postgresql - DbClient future interface(3)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false, "postgresql - DbClient future interface(3)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
/// 4.5 bad sql
|
|
|
|
f = clientPtr->execSqlAsyncFuture(
|
|
|
|
"select * from users where user_id=$1 and user_name='12'",
|
|
|
|
"pg111",
|
|
|
|
"postgresql1");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(r.size() == 0, "postgresql - DbClient future interface(4)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
// std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(true, "postgresql - DbClient future interface(4)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
2020-04-11 11:46:47 +08:00
|
|
|
/// 4.6 clean up
|
|
|
|
f = clientPtr->execSqlAsyncFuture("truncate table users restart identity");
|
2019-08-17 12:40:39 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(true, "postgresql - DbClient future interface(5)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
// std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false, "postgresql - DbClient future interface(5)");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 5 Test Result and Row exception throwing
|
|
|
|
// 5.1 query for none and try to access
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"select * from users where user_id=$1 and user_name=$2",
|
|
|
|
"pg111",
|
|
|
|
"postgresql1");
|
|
|
|
r.at(0);
|
|
|
|
testOutput(false, "postgresql - Result throwing exceptions(0)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "postgresql - Result throwing exceptions(0)");
|
|
|
|
}
|
|
|
|
// 5.2 insert one just for setup
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values($1,$2,$3,$4) returning *",
|
|
|
|
"pg",
|
|
|
|
"postgresql",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
testOutput(r[0]["id"].as<int64_t>() == 1,
|
|
|
|
"postgresql - Row throwing exceptions(0)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "postgresql - Row throwing exceptions(0)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5.3 try to access nonexistent column by name
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync("select * from users");
|
|
|
|
auto row = r.at(0);
|
|
|
|
row["imaginary_column"];
|
|
|
|
testOutput(false, "postgresql - Row throwing exceptions(1)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "postgresql - Row throwing exceptions(1)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5.4 try to access nonexistent column by index
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync("select * from users");
|
|
|
|
auto row = r.at(0);
|
|
|
|
row.at(420);
|
|
|
|
testOutput(false, "postgresql - Row throwing exceptions(2)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "postgresql - Row throwing exceptions(2)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5.5 cleanup
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r =
|
|
|
|
clientPtr->execSqlSync("truncate table users restart identity");
|
|
|
|
testOutput(true, "postgresql - Row throwing exceptions(3)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "postgresql - Row throwing exceptions(3)");
|
2019-08-17 12:40:39 +08:00
|
|
|
}
|
2020-04-11 11:46:47 +08:00
|
|
|
|
2019-08-17 20:05:32 +08:00
|
|
|
/// Test ORM mapper
|
2020-04-11 11:46:47 +08:00
|
|
|
/// 6.1 insert, noneblocking
|
|
|
|
using namespace drogon_model::postgres;
|
2019-08-17 20:05:32 +08:00
|
|
|
drogon::orm::Mapper<Users> mapper(clientPtr);
|
|
|
|
Users user;
|
|
|
|
user.setUserId("pg");
|
|
|
|
user.setUserName("postgres");
|
|
|
|
user.setPassword("123");
|
|
|
|
user.setOrgName("default");
|
|
|
|
mapper.insert(
|
|
|
|
user,
|
|
|
|
[](Users ret) {
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(ret.getPrimaryKey() == 1,
|
|
|
|
"postgresql - ORM mapper asynchronous interface(0)");
|
2019-08-17 20:05:32 +08:00
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - ORM mapper asynchronous interface(0)");
|
2019-08-17 20:05:32 +08:00
|
|
|
});
|
2020-10-29 20:09:16 +08:00
|
|
|
|
|
|
|
/// 6.1.5 insert future
|
|
|
|
user.setUserId("pg_future");
|
|
|
|
auto fu = mapper.insertFuture(user);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto u = fu.get();
|
|
|
|
testOutput(true,
|
|
|
|
"postgresql - ORM mapper asynchronous future interface(0)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false,
|
|
|
|
"postgresql - ORM mapper asynchronous future interface(0)");
|
|
|
|
}
|
|
|
|
|
2020-04-11 11:46:47 +08:00
|
|
|
/// 6.2 insert
|
2019-08-17 20:05:32 +08:00
|
|
|
user.setUserId("pg1");
|
|
|
|
user.setUserName("postgres1");
|
|
|
|
mapper.insert(
|
|
|
|
user,
|
|
|
|
[](Users ret) {
|
2020-10-29 20:09:16 +08:00
|
|
|
testOutput(ret.getPrimaryKey() == 3,
|
2020-04-11 11:46:47 +08:00
|
|
|
"postgresql - ORM mapper asynchronous interface(1)");
|
2019-08-17 20:05:32 +08:00
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - ORM mapper asynchronous interface(1)");
|
2019-08-17 20:05:32 +08:00
|
|
|
});
|
2020-04-11 11:46:47 +08:00
|
|
|
/// 6.3 select where in
|
2019-09-30 21:34:30 +08:00
|
|
|
mapper.findBy(
|
|
|
|
Criteria(Users::Cols::_id,
|
2020-01-25 11:58:20 +08:00
|
|
|
CompareOperator::In,
|
2020-04-11 11:46:47 +08:00
|
|
|
std::vector<int32_t>{2, 200}),
|
2019-09-30 21:34:30 +08:00
|
|
|
[](std::vector<Users> users) {
|
|
|
|
testOutput(users.size() == 1,
|
2020-04-11 11:46:47 +08:00
|
|
|
"postgresql - ORM mapper asynchronous interface(2)");
|
2019-09-30 21:34:30 +08:00
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-04-11 11:46:47 +08:00
|
|
|
testOutput(false,
|
|
|
|
"postgresql - ORM mapper asynchronous interface(2)");
|
2019-09-30 21:34:30 +08:00
|
|
|
});
|
2020-06-09 00:54:12 +08:00
|
|
|
/// 6.3.5 count
|
|
|
|
mapper.count(
|
|
|
|
drogon::orm::Criteria(Users::Cols::_id, CompareOperator::EQ, 2020),
|
|
|
|
[](const size_t c) {
|
|
|
|
testOutput(c == 0,
|
|
|
|
"postgresql - ORM mapper asynchronous interface(3)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false,
|
|
|
|
"postgresql - ORM mapper asynchronous interface(3)");
|
|
|
|
});
|
2020-04-11 11:46:47 +08:00
|
|
|
/// 6.4 find by primary key. blocking
|
2019-09-30 21:34:30 +08:00
|
|
|
try
|
|
|
|
{
|
2020-04-11 11:46:47 +08:00
|
|
|
auto user = mapper.findByPrimaryKey(2);
|
2020-06-09 00:54:12 +08:00
|
|
|
testOutput(true, "postgresql - ORM mapper synchronous interface(0)");
|
2020-07-03 12:19:40 +08:00
|
|
|
Users newUser;
|
|
|
|
newUser.setId(user.getValueOfId());
|
|
|
|
newUser.setSalt("xxx");
|
|
|
|
auto c = mapper.update(newUser);
|
|
|
|
testOutput(c == 1, "postgresql - ORM mapper synchronous interface(1)");
|
2019-09-30 21:34:30 +08:00
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-06-09 00:54:12 +08:00
|
|
|
testOutput(false, "postgresql - ORM mapper synchronous interface(0)");
|
2019-09-30 21:34:30 +08:00
|
|
|
}
|
2021-02-06 17:05:58 +08:00
|
|
|
#ifdef __cpp_impl_coroutine
|
|
|
|
auto coro_test = [clientPtr]() -> drogon::Task<> {
|
|
|
|
/// 7 Test coroutines.
|
|
|
|
/// This is by no means comprehensive. But coroutine API is esentially a
|
|
|
|
/// wrapper arround callbacks. The purpose is to test the interface
|
|
|
|
/// works 7.1 Basic queries
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto result =
|
|
|
|
co_await clientPtr->execSqlCoro("select * from users;");
|
|
|
|
testOutput(result.size() != 0,
|
|
|
|
"postgresql - DbClient coroutine interface(0)");
|
|
|
|
}
|
|
|
|
catch (const Failure &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
testOutput(false, "postgresql - DbClient coroutine interface(0)");
|
|
|
|
}
|
|
|
|
/// 7.2 Parameter binding
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto result = co_await clientPtr->execSqlCoro(
|
|
|
|
"select * from users where 1=$1;", 1);
|
|
|
|
testOutput(result.size() != 0,
|
|
|
|
"postgresql - DbClient coroutine interface(1)");
|
|
|
|
}
|
|
|
|
catch (const Failure &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
testOutput(false, "postgresql - DbClient coroutine interface(1)");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
drogon::sync_wait(coro_test());
|
|
|
|
#endif
|
2019-10-04 14:17:48 +08:00
|
|
|
}
|
2020-04-11 11:46:47 +08:00
|
|
|
|
|
|
|
void doMysqlTest(const drogon::orm::DbClientPtr &clientPtr)
|
2019-10-04 14:17:48 +08:00
|
|
|
{
|
2020-04-11 11:46:47 +08:00
|
|
|
// Prepare the test environment
|
|
|
|
*clientPtr << "CREATE DATABASE IF NOT EXISTS drogonTestMysql" >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(true, "mysql - Prepare the test environment(0)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - Prepare the test environment(0)");
|
|
|
|
};
|
|
|
|
*clientPtr << "USE drogonTestMysql" >> [](const Result &r) {
|
|
|
|
testOutput(true, "mysql - Prepare the test environment(0)");
|
|
|
|
} >> [](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - Prepare the test environment(0)");
|
|
|
|
};
|
|
|
|
// mysql is case sensitive
|
|
|
|
*clientPtr << "DROP TABLE IF EXISTS users" >> [](const Result &r) {
|
|
|
|
testOutput(true, "mysql - Prepare the test environment(1)");
|
|
|
|
} >> [](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - Prepare the test environment(1)");
|
|
|
|
};
|
|
|
|
*clientPtr << "CREATE TABLE users \
|
|
|
|
(\
|
|
|
|
id int(11) auto_increment PRIMARY KEY,\
|
|
|
|
user_id varchar(32),\
|
|
|
|
user_name varchar(64),\
|
|
|
|
password varchar(64),\
|
|
|
|
org_name varchar(20),\
|
|
|
|
signature varchar(50),\
|
|
|
|
avatar_id varchar(32),\
|
|
|
|
salt character varying(20),\
|
|
|
|
admin boolean DEFAULT false,\
|
|
|
|
CONSTRAINT user_id_org UNIQUE(user_id, org_name)\
|
|
|
|
)" >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(true, "mysql - Prepare the test environment(2)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - Prepare the test environment(2)");
|
|
|
|
};
|
|
|
|
/// Test1:DbClient streaming-type interface
|
|
|
|
/// 1.1 insert,non-blocking
|
2021-02-07 10:34:49 +08:00
|
|
|
*clientPtr
|
|
|
|
<< "insert into users (user_id,user_name,password,org_name,admin) "
|
|
|
|
"values(?,?,?,?,?)"
|
|
|
|
<< "pg"
|
|
|
|
<< "postgresql"
|
|
|
|
<< "123"
|
|
|
|
<< "default" << drogon::orm::DefaultValue{} >>
|
2020-04-11 11:46:47 +08:00
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.insertId() == 1,
|
|
|
|
"mysql - DbClient streaming-type interface(0)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient streaming-type interface(0)");
|
|
|
|
};
|
|
|
|
/// 1.2 insert,blocking
|
|
|
|
*clientPtr << "insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values(?,?,?,?)"
|
|
|
|
<< "pg1"
|
|
|
|
<< "postgresql1"
|
|
|
|
<< "123"
|
|
|
|
<< "default" << Mode::Blocking >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.insertId() == 2,
|
|
|
|
"mysql - DbClient streaming-type interface(1)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient streaming-type interface(1)");
|
|
|
|
};
|
|
|
|
/// 1.3 query,no-blocking
|
|
|
|
*clientPtr << "select * from users where 1 = 1" << Mode::NonBlocking >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.size() == 2,
|
|
|
|
"mysql - DbClient streaming-type interface(2)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient streaming-type interface(2)");
|
|
|
|
};
|
|
|
|
/// 1.4 query,blocking
|
|
|
|
*clientPtr << "select * from users where 1 = 1" << Mode::Blocking >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.size() == 2,
|
|
|
|
"mysql - DbClient streaming-type interface(3)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient streaming-type interface(3)");
|
|
|
|
};
|
|
|
|
/// 1.5 query,blocking
|
|
|
|
int count = 0;
|
|
|
|
*clientPtr << "select user_name, user_id, id from users where 1 = 1"
|
|
|
|
<< Mode::Blocking >>
|
|
|
|
[&count](bool isNull,
|
|
|
|
const std::string &name,
|
2020-04-25 02:12:44 +08:00
|
|
|
std::string &&user_id,
|
2020-04-11 11:46:47 +08:00
|
|
|
int id) {
|
|
|
|
if (!isNull)
|
|
|
|
++count;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
testOutput(count == 2,
|
|
|
|
"mysql - DbClient streaming-type interface(4)");
|
|
|
|
}
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient streaming-type interface(4)");
|
|
|
|
};
|
|
|
|
/// 1.6 query, parameter binding
|
|
|
|
*clientPtr << "select * from users where id = ?" << 1 >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.size() == 1,
|
|
|
|
"mysql - DbClient streaming-type interface(5)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient streaming-type interface(5)");
|
|
|
|
};
|
|
|
|
/// 1.7 query, parameter binding
|
|
|
|
*clientPtr << "select * from users where user_id = ? and user_name = ?"
|
|
|
|
<< "pg1"
|
|
|
|
<< "postgresql1" >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.size() == 1,
|
|
|
|
"mysql - DbClient streaming-type interface(6)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient streaming-type interface(6)");
|
|
|
|
};
|
|
|
|
/// 1.8 delete
|
|
|
|
*clientPtr << "delete from users where user_id = ? and user_name = ?"
|
|
|
|
<< "pg1"
|
|
|
|
<< "postgresql1" >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"mysql - DbClient streaming-type interface(7)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient streaming-type interface(7)");
|
|
|
|
};
|
|
|
|
/// 1.9 update
|
|
|
|
*clientPtr << "update users set user_id = ?, user_name = ? where user_id "
|
|
|
|
"= ? and user_name = ?"
|
|
|
|
<< "pg1"
|
|
|
|
<< "postgresql1"
|
|
|
|
<< "pg"
|
|
|
|
<< "postgresql" >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"mysql - DbClient streaming-type interface(8)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient streaming-type interface(8)");
|
|
|
|
};
|
|
|
|
/// 1.10 truncate
|
|
|
|
*clientPtr << "truncate table users" >> [](const Result &r) {
|
|
|
|
testOutput(true, "mysql - DbClient streaming-type interface(9)");
|
|
|
|
} >> [](const DrogonDbException &e) {
|
|
|
|
std::cerr << "error:" << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient streaming-type interface(9)");
|
|
|
|
};
|
|
|
|
/// Test asynchronous method
|
|
|
|
/// 2.1 insert
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"insert into users \
|
|
|
|
(user_id,user_name,password,org_name) \
|
|
|
|
values(?,?,?,?)",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.insertId() != 0,
|
|
|
|
"mysql - DbClient asynchronous interface(0)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient asynchronous interface(0)");
|
|
|
|
},
|
|
|
|
"pg",
|
|
|
|
"postgresql",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
/// 2.2 insert
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"insert into users \
|
|
|
|
(user_id,user_name,password,org_name) \
|
|
|
|
values(?,?,?,?)",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"mysql - DbClient asynchronous interface(1)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient asynchronous interface(1)");
|
|
|
|
},
|
|
|
|
"pg1",
|
|
|
|
"postgresql1",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
/// 2.3 query
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"select * from users where 1 = 1",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.size() == 2,
|
|
|
|
"mysql - DbClient asynchronous interface(2)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient asynchronous interface(2)");
|
|
|
|
});
|
|
|
|
/// 2.2 query, parameter binding
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"select * from users where id = ?",
|
|
|
|
[](const Result &r) {
|
|
|
|
// std::cout << r.size() << "\n";
|
|
|
|
testOutput(r.size() == 1,
|
|
|
|
"mysql - DbClient asynchronous interface(3)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient asynchronous interface(3)");
|
|
|
|
},
|
|
|
|
1);
|
|
|
|
/// 2.3 query, parameter binding
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"select * from users where user_id = ? and user_name = ?",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.size() == 1,
|
|
|
|
"mysql - DbClient asynchronous interface(4)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient asynchronous interface(4)");
|
|
|
|
},
|
|
|
|
"pg1",
|
|
|
|
"postgresql1");
|
|
|
|
/// 2.4 delete
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"delete from users where user_id = ? and user_name = ?",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"mysql - DbClient asynchronous interface(5)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient asynchronous interface(5)");
|
|
|
|
},
|
|
|
|
"pg1",
|
|
|
|
"postgresql1");
|
|
|
|
/// 2.5 update
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"update users set user_id = ?, user_name = ? where user_id "
|
|
|
|
"= ? and user_name = ?",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"mysql - DbClient asynchronous interface(6)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient asynchronous interface(6)");
|
|
|
|
},
|
|
|
|
"pg1",
|
|
|
|
"postgresql1",
|
|
|
|
"pg",
|
|
|
|
"postgresql");
|
|
|
|
/// 2.6 truncate
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"truncate table users",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(true, "mysql - DbClient asynchronous interface(7)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << "error:" << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient asynchronous interface(7)");
|
|
|
|
});
|
|
|
|
|
|
|
|
/// Test synchronous method
|
|
|
|
/// 3.1 insert
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values(?,?,?,?)",
|
|
|
|
"pg",
|
|
|
|
"postgresql",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
// std::cout << r.insertId();
|
|
|
|
testOutput(r.insertId() == 1,
|
|
|
|
"mysql - DbClient synchronous interface(0)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient asynchronous interface(0)");
|
|
|
|
}
|
|
|
|
/// 3.2 insert
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values(?,?,?,?)",
|
|
|
|
"pg1",
|
|
|
|
"postgresql1",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"mysql - DbClient synchronous interface(1)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient asynchronous interface(1)");
|
|
|
|
}
|
|
|
|
/// 3.3 query
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"select * from users where user_id=? and user_name=?",
|
|
|
|
"pg1",
|
|
|
|
"postgresql1");
|
|
|
|
testOutput(r.size() == 1, "mysql - DbClient synchronous interface(2)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient asynchronous interface(2)");
|
|
|
|
}
|
|
|
|
/// 3.4 query for none
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"select * from users where user_id=? and user_name=?",
|
|
|
|
"pg111",
|
|
|
|
"postgresql1");
|
|
|
|
testOutput(r.size() == 0, "mysql - DbClient synchronous interface(3)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient asynchronous interface(3)");
|
|
|
|
}
|
|
|
|
/// 3.5 bad sql
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"select * from users where user_id=? and user_name='1234'",
|
|
|
|
"pg111",
|
|
|
|
"postgresql1");
|
|
|
|
testOutput(r.size() == 0, "mysql - DbClient synchronous interface(4)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
// std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "mysql - DbClient asynchronous interface(4)");
|
|
|
|
}
|
|
|
|
/// 3.6 truncate
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync("truncate table users");
|
|
|
|
testOutput(true, "mysql - DbClient synchronous interface(5)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
// std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "mysql - DbClient asynchronous interface(5)");
|
|
|
|
}
|
|
|
|
/// Test future interface
|
|
|
|
/// 4.1 insert
|
|
|
|
auto f = clientPtr->execSqlAsyncFuture(
|
|
|
|
"insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values(?,?,?,?) ",
|
|
|
|
"pg",
|
|
|
|
"postgresql",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
|
|
|
testOutput(r.insertId() == 1, "mysql - DbClient future interface(0)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient future interface(0)");
|
|
|
|
}
|
|
|
|
/// 4.2 insert
|
|
|
|
f = clientPtr->execSqlAsyncFuture(
|
|
|
|
"insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values(?,?,?,?)",
|
|
|
|
"pg1",
|
|
|
|
"postgresql1",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"mysql - DbClient future interface(1)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient future interface(1)");
|
|
|
|
}
|
|
|
|
/// 4.3 query
|
|
|
|
f = clientPtr->execSqlAsyncFuture(
|
|
|
|
"select * from users where user_id=? and user_name=?",
|
|
|
|
"pg1",
|
|
|
|
"postgresql1");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
|
|
|
testOutput(r.size() == 1, "mysql - DbClient future interface(2)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient future interface(2)");
|
|
|
|
}
|
|
|
|
/// 4.4 query for none
|
|
|
|
f = clientPtr->execSqlAsyncFuture(
|
|
|
|
"select * from users where user_id=? and user_name=?",
|
|
|
|
"pg111",
|
|
|
|
"postgresql1");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
|
|
|
testOutput(r.size() == 0, "mysql - DbClient future interface(3)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient future interface(3)");
|
|
|
|
}
|
|
|
|
/// 4.5 bad sql
|
|
|
|
f = clientPtr->execSqlAsyncFuture(
|
|
|
|
"select * from users where user_id=? and user_name='12'",
|
|
|
|
"pg111",
|
|
|
|
"postgresql1");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
|
|
|
testOutput(r.size() == 0, "mysql - DbClient future interface(4)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
// std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "mysql - DbClient future interface(4)");
|
|
|
|
}
|
|
|
|
/// 4.6 truncate
|
|
|
|
f = clientPtr->execSqlAsyncFuture("truncate table users");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
|
|
|
testOutput(true, "mysql - DbClient future interface(5)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
// std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - DbClient future interface(5)");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 5 Test Result and Row exception throwing
|
|
|
|
// 5.1 query for none and try to access
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"select * from users where user_id=? and user_name=?",
|
|
|
|
"pg111",
|
|
|
|
"postgresql1");
|
|
|
|
r.at(0);
|
|
|
|
testOutput(false, "mysql - Result throwing exceptions(0)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "mysql - Result throwing exceptions(0)");
|
|
|
|
}
|
|
|
|
// 5.2 insert one just for setup
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values(?,?,?,?)",
|
|
|
|
"pg",
|
|
|
|
"postgresql",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
testOutput(r.insertId() == 1, "mysql - Row throwing exceptions(0)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - Row throwing exceptions(0)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5.3 try to access nonexistent column by name
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync("select * from users");
|
|
|
|
auto row = r.at(0);
|
|
|
|
row["imaginary_column"];
|
|
|
|
testOutput(false, "mysql - Row throwing exceptions(1)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "mysql - Row throwing exceptions(1)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5.4 try to access nonexistent column by index
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync("select * from users");
|
|
|
|
auto row = r.at(0);
|
|
|
|
row.at(420);
|
|
|
|
testOutput(false, "mysql - Row throwing exceptions(2)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "mysql - Row throwing exceptions(2)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5.5 cleanup
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync("truncate table users");
|
|
|
|
testOutput(true, "mysql - Row throwing exceptions(3)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - Row throwing exceptions(3)");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test ORM mapper
|
|
|
|
/// 6.1 insert, noneblocking
|
|
|
|
using namespace drogon_model::drogonTestMysql;
|
|
|
|
drogon::orm::Mapper<Users> mapper(clientPtr);
|
|
|
|
Users user;
|
|
|
|
user.setUserId("pg");
|
|
|
|
user.setUserName("postgres");
|
|
|
|
user.setPassword("123");
|
|
|
|
user.setOrgName("default");
|
|
|
|
mapper.insert(
|
|
|
|
user,
|
|
|
|
[](Users ret) {
|
|
|
|
testOutput(ret.getPrimaryKey() == 1,
|
|
|
|
"mysql - ORM mapper asynchronous interface(0)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - ORM mapper asynchronous interface(0)");
|
|
|
|
});
|
2020-06-09 00:54:12 +08:00
|
|
|
/// 6.1.5 count
|
|
|
|
mapper.count(
|
|
|
|
drogon::orm::Criteria(Users::Cols::_id, CompareOperator::EQ, 1),
|
|
|
|
[](const size_t c) {
|
|
|
|
testOutput(c == 1, "mysql - ORM mapper asynchronous interface(1)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "mysql - ORM mapper asynchronous interface(1)");
|
|
|
|
});
|
2020-04-11 11:46:47 +08:00
|
|
|
/// 6.2 insert
|
|
|
|
user.setUserId("pg1");
|
|
|
|
user.setUserName("postgres1");
|
|
|
|
mapper.insert(
|
|
|
|
user,
|
|
|
|
[](Users ret) {
|
|
|
|
testOutput(ret.getPrimaryKey() == 2,
|
2020-06-09 00:54:12 +08:00
|
|
|
"mysql - ORM mapper asynchronous interface(2)");
|
2020-04-11 11:46:47 +08:00
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-06-09 00:54:12 +08:00
|
|
|
testOutput(false, "mysql - ORM mapper asynchronous interface(2)");
|
2020-04-11 11:46:47 +08:00
|
|
|
});
|
|
|
|
/// 6.3 select where in
|
|
|
|
mapper.findBy(
|
|
|
|
Criteria(Users::Cols::_id,
|
|
|
|
CompareOperator::In,
|
|
|
|
std::vector<int32_t>{2, 200}),
|
|
|
|
[](std::vector<Users> users) {
|
|
|
|
testOutput(users.size() == 1,
|
2020-06-09 00:54:12 +08:00
|
|
|
"mysql - ORM mapper asynchronous interface(3)");
|
2020-04-11 11:46:47 +08:00
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-06-09 00:54:12 +08:00
|
|
|
testOutput(false, "mysql - ORM mapper asynchronous interface(3)");
|
2020-04-11 11:46:47 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
/// 6.4 find by primary key. blocking
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto user = mapper.findByPrimaryKey(1);
|
2020-06-09 00:54:12 +08:00
|
|
|
testOutput(true, "mysql - ORM mapper synchronous interface(0)");
|
2020-07-03 12:19:40 +08:00
|
|
|
Users newUser;
|
|
|
|
newUser.setId(user.getValueOfId());
|
|
|
|
newUser.setSalt("xxx");
|
|
|
|
auto c = mapper.update(newUser);
|
|
|
|
testOutput(c == 1, "mysql - ORM mapper synchronous interface(1)");
|
2020-04-11 11:46:47 +08:00
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-06-09 00:54:12 +08:00
|
|
|
testOutput(false, "mysql - ORM mapper synchronous interface(0)");
|
2020-04-11 11:46:47 +08:00
|
|
|
}
|
2021-02-06 17:05:58 +08:00
|
|
|
#ifdef __cpp_impl_coroutine
|
|
|
|
auto coro_test = [clientPtr]() -> drogon::Task<> {
|
|
|
|
/// 7 Test coroutines.
|
|
|
|
/// This is by no means comprehensive. But coroutine API is esentially a
|
|
|
|
/// wrapper arround callbacks. The purpose is to test the interface
|
|
|
|
/// works 7.1 Basic queries
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto result =
|
|
|
|
co_await clientPtr->execSqlCoro("select * from users;");
|
|
|
|
testOutput(result.size() != 0,
|
|
|
|
"postgresql - DbClient coroutine interface(0)");
|
|
|
|
}
|
|
|
|
catch (const Failure &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
testOutput(false, "postgresql - DbClient coroutine interface(0)");
|
|
|
|
}
|
|
|
|
/// 7.2 Parameter binding
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto result = co_await clientPtr->execSqlCoro(
|
|
|
|
"select * from users where 1=?;", 1);
|
|
|
|
testOutput(result.size() != 0,
|
|
|
|
"postgresql - DbClient coroutine interface(1)");
|
|
|
|
}
|
|
|
|
catch (const Failure &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
testOutput(false, "postgresql - DbClient coroutine interface(1)");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
drogon::sync_wait(coro_test());
|
|
|
|
#endif
|
2020-04-11 11:46:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void doSqliteTest(const drogon::orm::DbClientPtr &clientPtr)
|
|
|
|
{
|
|
|
|
// Prepare the test environment
|
|
|
|
*clientPtr << "DROP TABLE IF EXISTS users" >> [](const Result &r) {
|
|
|
|
testOutput(true, "sqlite3 - Prepare the test environment(0)");
|
|
|
|
} >> [](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - Prepare the test environment(0)");
|
|
|
|
};
|
|
|
|
*clientPtr << "CREATE TABLE users \
|
|
|
|
(\
|
|
|
|
id INTEGER PRIMARY KEY autoincrement,\
|
|
|
|
user_id varchar(32),\
|
|
|
|
user_name varchar(64),\
|
|
|
|
password varchar(64),\
|
|
|
|
org_name varchar(20),\
|
|
|
|
signature varchar(50),\
|
|
|
|
avatar_id varchar(32),\
|
|
|
|
salt character varchar(20),\
|
|
|
|
admin boolean DEFAULT false,\
|
|
|
|
CONSTRAINT user_id_org UNIQUE(user_id, org_name)\
|
|
|
|
)" >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(true, "sqlite3 - Prepare the test environment(1)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - Prepare the test environment(1)");
|
|
|
|
};
|
|
|
|
/// Test1:DbClient streaming-type interface
|
|
|
|
/// 1.1 insert,non-blocking
|
|
|
|
*clientPtr << "insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values(?,?,?,?)"
|
|
|
|
<< "pg"
|
|
|
|
<< "postgresql"
|
|
|
|
<< "123"
|
|
|
|
<< "default" >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.insertId() == 1,
|
|
|
|
"sqlite3 - DbClient streaming-type interface(0)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient streaming-type interface(0)");
|
|
|
|
};
|
|
|
|
/// 1.2 insert,blocking
|
|
|
|
*clientPtr << "insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values(?,?,?,?)"
|
|
|
|
<< "pg1"
|
|
|
|
<< "postgresql1"
|
|
|
|
<< "123"
|
|
|
|
<< "default" << Mode::Blocking >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.insertId() == 2,
|
|
|
|
"sqlite3 - DbClient streaming-type interface(1)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient streaming-type interface(1)");
|
|
|
|
};
|
|
|
|
/// 1.3 query,no-blocking
|
|
|
|
*clientPtr << "select * from users where 1 = 1" << Mode::NonBlocking >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.size() == 2,
|
|
|
|
"sqlite3 - DbClient streaming-type interface(2)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient streaming-type interface(2)");
|
|
|
|
};
|
|
|
|
/// 1.4 query,blocking
|
|
|
|
*clientPtr << "select * from users where 1 = 1" << Mode::Blocking >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.size() == 2,
|
|
|
|
"sqlite3 - DbClient streaming-type interface(3)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient streaming-type interface(3)");
|
|
|
|
};
|
|
|
|
/// 1.5 query,blocking
|
|
|
|
int count = 0;
|
|
|
|
*clientPtr << "select user_name, user_id, id from users where 1 = 1"
|
|
|
|
<< Mode::Blocking >>
|
|
|
|
[&count](bool isNull,
|
|
|
|
const std::string &name,
|
2020-04-25 02:12:44 +08:00
|
|
|
std::string &&user_id,
|
2020-04-11 11:46:47 +08:00
|
|
|
int id) {
|
|
|
|
if (!isNull)
|
|
|
|
++count;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
testOutput(count == 2,
|
|
|
|
"sqlite3 - DbClient streaming-type interface(4)");
|
|
|
|
}
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient streaming-type interface(4)");
|
|
|
|
};
|
|
|
|
/// 1.6 query, parameter binding
|
|
|
|
*clientPtr << "select * from users where id = ?" << 1 >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.size() == 1,
|
|
|
|
"sqlite3 - DbClient streaming-type interface(5)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient streaming-type interface(5)");
|
|
|
|
};
|
|
|
|
/// 1.7 query, parameter binding
|
|
|
|
*clientPtr << "select * from users where user_id = ? and user_name = ?"
|
|
|
|
<< "pg1"
|
|
|
|
<< "postgresql1" >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.size() == 1,
|
|
|
|
"sqlite3 - DbClient streaming-type interface(6)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient streaming-type interface(6)");
|
|
|
|
};
|
|
|
|
/// 1.8 delete
|
|
|
|
*clientPtr << "delete from users where user_id = ? and user_name = ?"
|
|
|
|
<< "pg1"
|
|
|
|
<< "postgresql1" >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"sqlite3 - DbClient streaming-type interface(7)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient streaming-type interface(7)");
|
|
|
|
};
|
|
|
|
/// 1.9 update
|
|
|
|
*clientPtr << "update users set user_id = ?, user_name = ? where user_id "
|
|
|
|
"= ? and user_name = ?"
|
|
|
|
<< "pg1"
|
|
|
|
<< "postgresql1"
|
|
|
|
<< "pg"
|
|
|
|
<< "postgresql" >>
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"sqlite3 - DbClient streaming-type interface(8)");
|
|
|
|
} >>
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient streaming-type interface(8)");
|
|
|
|
};
|
|
|
|
/// 1.10 clean up
|
|
|
|
*clientPtr << "delete from users" >> [](const Result &r) {
|
|
|
|
testOutput(true, "sqlite3 - DbClient streaming-type interface(9.1)");
|
|
|
|
} >> [](const DrogonDbException &e) {
|
|
|
|
std::cerr << "error:" << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient streaming-type interface(9.1)");
|
|
|
|
};
|
|
|
|
*clientPtr << "UPDATE sqlite_sequence SET seq = 0" >> [](const Result &r) {
|
|
|
|
testOutput(true, "sqlite3 - DbClient streaming-type interface(9.2)");
|
|
|
|
} >> [](const DrogonDbException &e) {
|
|
|
|
std::cerr << "error:" << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient streaming-type interface(9.2)");
|
|
|
|
};
|
|
|
|
/// Test asynchronous method
|
|
|
|
/// 2.1 insert
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"insert into users \
|
|
|
|
(user_id,user_name,password,org_name) \
|
|
|
|
values(?,?,?,?)",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.insertId() == 1,
|
|
|
|
"sqlite3 - DbClient asynchronous interface(0)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient asynchronous interface(0)");
|
|
|
|
},
|
|
|
|
"pg",
|
|
|
|
"postgresql",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
/// 2.2 insert
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"insert into users \
|
|
|
|
(user_id,user_name,password,org_name) \
|
|
|
|
values(?,?,?,?)",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"sqlite3 - DbClient asynchronous interface(1)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient asynchronous interface(1)");
|
|
|
|
},
|
|
|
|
"pg1",
|
|
|
|
"postgresql1",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
/// 2.3 query
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"select * from users where 1 = 1",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.size() == 2,
|
|
|
|
"sqlite3 - DbClient asynchronous interface(2)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient asynchronous interface(2)");
|
|
|
|
});
|
|
|
|
/// 2.2 query, parameter binding
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"select * from users where id = ?",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.size() == 1,
|
|
|
|
"sqlite3 - DbClient asynchronous interface(3)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient asynchronous interface(3)");
|
|
|
|
},
|
|
|
|
1);
|
|
|
|
/// 2.3 query, parameter binding
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"select * from users where user_id = ? and user_name = ?",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.size() == 1,
|
|
|
|
"sqlite3 - DbClient asynchronous interface(4)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient asynchronous interface(4)");
|
|
|
|
},
|
|
|
|
"pg1",
|
|
|
|
"postgresql1");
|
|
|
|
/// 2.4 delete
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"delete from users where user_id = ? and user_name = ?",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"sqlite3 - DbClient asynchronous interface(5)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient asynchronous interface(5)");
|
|
|
|
},
|
|
|
|
"pg1",
|
|
|
|
"postgresql1");
|
|
|
|
/// 2.5 update
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"update users set user_id = ?, user_name = ? where user_id "
|
|
|
|
"= ? and user_name = ?",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"sqlite3 - DbClient asynchronous interface(6)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient asynchronous interface(6)");
|
|
|
|
},
|
|
|
|
"pg1",
|
|
|
|
"postgresql1",
|
|
|
|
"pg",
|
|
|
|
"postgresql");
|
|
|
|
/// 2.6 clean up
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"delete from users",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(true, "sqlite3 - DbClient asynchronous interface(7.1)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << "error:" << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient asynchronous interface(7.1)");
|
|
|
|
});
|
|
|
|
clientPtr->execSqlAsync(
|
|
|
|
"UPDATE sqlite_sequence SET seq = 0",
|
|
|
|
[](const Result &r) {
|
|
|
|
testOutput(true, "sqlite3 - DbClient asynchronous interface(7.2)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << "error:" << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient asynchronous interface(7.2)");
|
|
|
|
});
|
|
|
|
|
|
|
|
/// Test synchronous method
|
|
|
|
/// 3.1 insert
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values(?,?,?,?)",
|
|
|
|
"pg",
|
|
|
|
"postgresql",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
testOutput(r.insertId() == 1,
|
|
|
|
"sqlite3 - DbClient synchronous interface(0)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient asynchronous interface(0)");
|
|
|
|
}
|
|
|
|
/// 3.2 insert
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values(?,?,?,?)",
|
|
|
|
"pg1",
|
|
|
|
"postgresql1",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"sqlite3 - DbClient synchronous interface(1)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient asynchronous interface(1)");
|
|
|
|
}
|
|
|
|
/// 3.3 query
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"select * from users where user_id=? and user_name=?",
|
|
|
|
"pg1",
|
|
|
|
"postgresql1");
|
|
|
|
testOutput(r.size() == 1,
|
|
|
|
"sqlite3 - DbClient synchronous interface(2)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient asynchronous interface(2)");
|
|
|
|
}
|
|
|
|
/// 3.4 query for none
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"select * from users where user_id=? and user_name=?",
|
|
|
|
"pg111",
|
|
|
|
"postgresql1");
|
|
|
|
testOutput(r.size() == 0,
|
|
|
|
"sqlite3 - DbClient synchronous interface(3)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient asynchronous interface(3)");
|
|
|
|
}
|
|
|
|
/// 3.5 bad sql
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"select * from users where user_id=? and user_name='1234'",
|
|
|
|
"pg111",
|
|
|
|
"postgresql1");
|
|
|
|
testOutput(r.size() == 0,
|
|
|
|
"sqlite3 - DbClient synchronous interface(4)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
// std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "sqlite3 - DbClient asynchronous interface(4)");
|
|
|
|
}
|
|
|
|
/// 3.6 clean up
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync("delete from users");
|
|
|
|
testOutput(true, "sqlite3 - DbClient synchronous interface(5.1)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
// std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "sqlite3 - DbClient asynchronous interface(5.1)");
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync("UPDATE sqlite_sequence SET seq = 0");
|
|
|
|
testOutput(true, "sqlite3 - DbClient synchronous interface(5.2)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
// std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "sqlite3 - DbClient asynchronous interface(5.2)");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test future interface
|
|
|
|
/// 4.1 insert
|
|
|
|
auto f = clientPtr->execSqlAsyncFuture(
|
|
|
|
"insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values(?,?,?,?) ",
|
|
|
|
"pg",
|
|
|
|
"postgresql",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
|
|
|
testOutput(r.insertId() == 1, "sqlite3 - DbClient future interface(0)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient future interface(0)");
|
|
|
|
}
|
|
|
|
/// 4.2 insert
|
|
|
|
f = clientPtr->execSqlAsyncFuture(
|
|
|
|
"insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values(?,?,?,?)",
|
|
|
|
"pg1",
|
|
|
|
"postgresql1",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
|
|
|
testOutput(r.affectedRows() == 1,
|
|
|
|
"sqlite3 - DbClient future interface(1)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient future interface(1)");
|
|
|
|
}
|
|
|
|
/// 4.3 query
|
|
|
|
f = clientPtr->execSqlAsyncFuture(
|
|
|
|
"select * from users where user_id=? and user_name=?",
|
|
|
|
"pg1",
|
|
|
|
"postgresql1");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
|
|
|
testOutput(r.size() == 1, "sqlite3 - DbClient future interface(2)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient future interface(2)");
|
|
|
|
}
|
|
|
|
/// 4.4 query for none
|
|
|
|
f = clientPtr->execSqlAsyncFuture(
|
|
|
|
"select * from users where user_id=? and user_name=?",
|
|
|
|
"pg111",
|
|
|
|
"postgresql1");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
|
|
|
testOutput(r.size() == 0, "sqlite3 - DbClient future interface(3)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient future interface(3)");
|
|
|
|
}
|
|
|
|
/// 4.5 bad sql
|
|
|
|
f = clientPtr->execSqlAsyncFuture(
|
|
|
|
"select * from users where user_id=? and user_name='12'",
|
|
|
|
"pg111",
|
|
|
|
"postgresql1");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
|
|
|
testOutput(r.size() == 0, "sqlite3 - DbClient future interface(4)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
// std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "sqlite3 - DbClient future interface(4)");
|
|
|
|
}
|
|
|
|
/// 4.6 clean up
|
|
|
|
f = clientPtr->execSqlAsyncFuture("delete from users");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
|
|
|
testOutput(true, "sqlite3 - DbClient future interface(5.1)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
// std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient future interface(5.1)");
|
|
|
|
}
|
|
|
|
f = clientPtr->execSqlAsyncFuture("UPDATE sqlite_sequence SET seq = 0");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = f.get();
|
|
|
|
testOutput(true, "sqlite3 - DbClient future interface(5.2)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
// std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - DbClient future interface(5.2)");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 5 Test Result and Row exception throwing
|
|
|
|
// 5.1 query for none and try to access
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"select * from users where user_id=? and user_name=?",
|
|
|
|
"pg111",
|
|
|
|
"postgresql1");
|
|
|
|
r.at(0);
|
|
|
|
testOutput(false, "sqlite3 - Result throwing exceptions(0)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "sqlite3 - Result throwing exceptions(0)");
|
|
|
|
}
|
|
|
|
// 5.2 insert one just for setup
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync(
|
|
|
|
"insert into users (user_id,user_name,password,org_name) "
|
|
|
|
"values(?,?,?,?)",
|
|
|
|
"pg",
|
|
|
|
"postgresql",
|
|
|
|
"123",
|
|
|
|
"default");
|
|
|
|
testOutput(r.insertId() == 1, "sqlite3 - Row throwing exceptions(0)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - Row throwing exceptions(0)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5.3 try to access nonexistent column by name
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync("select * from users");
|
|
|
|
auto row = r.at(0);
|
|
|
|
row["imaginary_column"];
|
|
|
|
testOutput(false, "sqlite3 - Row throwing exceptions(1)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "sqlite3 - Row throwing exceptions(1)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5.4 try to access nonexistent column by index
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync("select * from users");
|
|
|
|
auto row = r.at(0);
|
|
|
|
row.at(420);
|
|
|
|
testOutput(false, "sqlite3 - Row throwing exceptions(2)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(true, "sqlite3 - Row throwing exceptions(2)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5.5 cleanup
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync("delete from users");
|
|
|
|
testOutput(true, "sqlite3 - Row throwing exceptions(3)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - Row throwing exceptions(3)");
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto r = clientPtr->execSqlSync("UPDATE sqlite_sequence SET seq = 0");
|
|
|
|
testOutput(true, "sqlite3 - Row throwing exceptions(3)");
|
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - Row throwing exceptions(3)");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test ORM mapper TODO
|
|
|
|
/// 5.1 insert, noneblocking
|
|
|
|
using namespace drogon_model::sqlite3;
|
|
|
|
drogon::orm::Mapper<Users> mapper(clientPtr);
|
|
|
|
Users user;
|
|
|
|
user.setUserId("pg");
|
|
|
|
user.setUserName("postgres");
|
|
|
|
user.setPassword("123");
|
|
|
|
user.setOrgName("default");
|
|
|
|
mapper.insert(
|
|
|
|
user,
|
|
|
|
[](Users ret) {
|
|
|
|
testOutput(ret.getPrimaryKey() == 1,
|
|
|
|
"sqlite3 - ORM mapper asynchronous interface(0)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - ORM mapper asynchronous interface(0)");
|
|
|
|
});
|
|
|
|
/// 5.2 insert
|
|
|
|
user.setUserId("pg1");
|
|
|
|
user.setUserName("postgres1");
|
|
|
|
mapper.insert(
|
|
|
|
user,
|
|
|
|
[](Users ret) {
|
|
|
|
testOutput(ret.getPrimaryKey() == 2,
|
|
|
|
"sqlite3 - ORM mapper asynchronous interface(1)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - ORM mapper asynchronous interface(1)");
|
|
|
|
});
|
|
|
|
/// 5.3 select where in
|
|
|
|
mapper.findBy(
|
|
|
|
Criteria(Users::Cols::_id,
|
|
|
|
CompareOperator::In,
|
|
|
|
std::vector<int32_t>{2, 200}),
|
|
|
|
[](std::vector<Users> users) {
|
|
|
|
testOutput(users.size() == 1,
|
|
|
|
"sqlite3 - ORM mapper asynchronous interface(2)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - ORM mapper asynchronous interface(2)");
|
|
|
|
});
|
2020-06-09 00:54:12 +08:00
|
|
|
/// 5.3.5 count
|
|
|
|
mapper.count(
|
|
|
|
drogon::orm::Criteria(Users::Cols::_id, CompareOperator::EQ, 2),
|
|
|
|
[](const size_t c) {
|
|
|
|
testOutput(c == 1,
|
|
|
|
"sqlite3 - ORM mapper asynchronous interface(3)");
|
|
|
|
},
|
|
|
|
[](const DrogonDbException &e) {
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
|
|
|
testOutput(false, "sqlite3 - ORM mapper asynchronous interface(3)");
|
|
|
|
});
|
2020-04-11 11:46:47 +08:00
|
|
|
/// 5.4 find by primary key. blocking
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto user = mapper.findByPrimaryKey(1);
|
2020-06-09 00:54:12 +08:00
|
|
|
testOutput(true, "sqlite3 - ORM mapper synchronous interface(0)");
|
2020-04-11 11:46:47 +08:00
|
|
|
}
|
|
|
|
catch (const DrogonDbException &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.base().what() << std::endl;
|
2020-06-09 00:54:12 +08:00
|
|
|
testOutput(false, "sqlite3 - ORM mapper synchronous interface(0)");
|
2020-04-11 11:46:47 +08:00
|
|
|
}
|
2021-02-06 17:05:58 +08:00
|
|
|
#ifdef __cpp_impl_coroutine
|
|
|
|
auto coro_test = [clientPtr]() -> drogon::Task<> {
|
|
|
|
/// 7 Test coroutines.
|
|
|
|
/// This is by no means comprehensive. But coroutine API is esentially a
|
|
|
|
/// wrapper arround callbacks. The purpose is to test the interface
|
|
|
|
/// works 7.1 Basic queries
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto result =
|
|
|
|
co_await clientPtr->execSqlCoro("select * from users;");
|
|
|
|
testOutput(result.size() != 0,
|
|
|
|
"postgresql - DbClient coroutine interface(0)");
|
|
|
|
}
|
|
|
|
catch (const Failure &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
testOutput(false, "postgresql - DbClient coroutine interface(0)");
|
|
|
|
}
|
|
|
|
/// 7.2 Parameter binding
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto result = co_await clientPtr->execSqlCoro(
|
|
|
|
"select * from users where 1=?;", 1);
|
|
|
|
testOutput(result.size() != 0,
|
|
|
|
"postgresql - DbClient coroutine interface(1)");
|
|
|
|
}
|
|
|
|
catch (const Failure &e)
|
|
|
|
{
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
testOutput(false, "postgresql - DbClient coroutine interface(1)");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
drogon::sync_wait(coro_test());
|
|
|
|
#endif
|
2020-04-11 11:46:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
trantor::Logger::setLogLevel(trantor::Logger::kDebug);
|
|
|
|
#if USE_POSTGRESQL
|
|
|
|
auto postgre_client = DbClient::newPgClient(
|
2020-12-13 18:12:30 +08:00
|
|
|
"host=127.0.0.1 port=5432 dbname=postgres user=postgres password=12345 "
|
2020-06-20 20:21:14 +08:00
|
|
|
"client_encoding=utf8",
|
|
|
|
1);
|
2020-04-11 11:46:47 +08:00
|
|
|
#endif
|
|
|
|
#if USE_MYSQL
|
2020-06-20 20:21:14 +08:00
|
|
|
auto mysql_client = DbClient::newMysqlClient(
|
|
|
|
"host=localhost port=3306 user=root client_encoding=utf8mb4", 1);
|
2020-04-11 11:46:47 +08:00
|
|
|
#endif
|
|
|
|
#if USE_SQLITE3
|
|
|
|
auto sqlite_client = DbClient::newSqlite3Client("filename=:memory:", 1);
|
|
|
|
#endif
|
|
|
|
LOG_DEBUG << "start!";
|
|
|
|
std::this_thread::sleep_for(1s);
|
|
|
|
if (argc == 2)
|
|
|
|
{
|
|
|
|
gLoops = atoi(argv[1]);
|
|
|
|
}
|
|
|
|
test_count = get_test_count();
|
|
|
|
for (int i = 0; i < gLoops; ++i)
|
|
|
|
{
|
|
|
|
#if USE_POSTGRESQL
|
|
|
|
doPostgreTest(postgre_client);
|
|
|
|
#endif
|
|
|
|
#if USE_MYSQL
|
|
|
|
doMysqlTest(mysql_client);
|
|
|
|
#endif
|
|
|
|
#if USE_SQLITE3
|
|
|
|
doSqliteTest(sqlite_client);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
globalf.get();
|
|
|
|
std::this_thread::sleep_for(0.008s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_test_count()
|
|
|
|
{
|
|
|
|
int test_count = 0;
|
|
|
|
#if USE_POSTGRESQL
|
|
|
|
test_count += postgre_tests * gLoops;
|
|
|
|
#endif
|
|
|
|
#if USE_MYSQL
|
|
|
|
test_count += mysql_tests * gLoops;
|
|
|
|
#endif
|
|
|
|
#if USE_SQLITE3
|
|
|
|
test_count += sqlite_tests * gLoops;
|
|
|
|
#endif
|
|
|
|
return test_count;
|
2018-12-03 18:12:19 +08:00
|
|
|
}
|