acl/lib_acl_cpp/samples/mysql/mysql.cpp

298 lines
6.9 KiB
C++
Raw Normal View History

// mysql.cpp : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨Ӧ<CCA8>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㡣
2014-11-19 00:25:21 +08:00
//
#include "stdafx.h"
#include "lib_acl.h"
#include "acl_cpp/lib_acl.hpp"
2014-11-19 00:25:21 +08:00
const char* CREATE_TBL =
"create table group_tbl\r\n"
"(\r\n"
"group_name varchar(128) not null,\r\n"
"uvip_tbl varchar(32) not null default 'uvip_tbl',\r\n"
"access_tbl varchar(32) not null default 'access_tbl',\r\n"
"access_week_tbl varchar(32) not null default 'access_week_tbl',\r\n"
"access_month_tbl varchar(32) not null default 'access_month_tbl',\r\n"
"update_date date not null default '1970-1-1',\r\n"
"disable integer not null default 0,\r\n"
"add_by_hand integer not null default 0,\r\n"
"class_level integer not null default 0,\r\n"
"primary key(group_name, class_level)\r\n"
")";
2022-09-27 18:02:45 +08:00
static const char* __pre = "<EFBFBD>й<EFBFBD><EFBFBD><EFBFBD>";
2014-11-19 00:25:21 +08:00
static bool tbl_create(acl::db_handle& db)
{
2022-09-27 18:02:45 +08:00
if (db.tbl_exists("group_tbl")) {
2014-11-19 00:25:21 +08:00
printf("table exist\r\n");
2022-09-27 18:02:45 +08:00
return true;
} else if (db.sql_update(CREATE_TBL) == false) {
2014-11-19 00:25:21 +08:00
printf("sql error\r\n");
2022-09-27 18:02:45 +08:00
return false;
} else {
2014-11-19 00:25:21 +08:00
printf("create table ok\r\n");
2022-09-27 18:02:45 +08:00
return true;
2014-11-19 00:25:21 +08:00
}
}
// <20><><EFBFBD>ӱ<EFBFBD><D3B1><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
static bool tbl_insert(acl::db_handle& db, int n)
{
const char* sql_fmt = "insert into group_tbl(group_name, uvip_tbl)"
2022-09-27 18:02:45 +08:00
" values('%s-%d', 'test')";
2014-11-19 00:25:21 +08:00
acl::string sql;
2022-09-27 18:02:45 +08:00
sql.format(sql_fmt,
acl::strconv(__pre, strlen(__pre), "gbk", "utf8").c_str(), n);
if (db.sql_update(sql.c_str()) == false) {
return false;
}
2014-11-19 00:25:21 +08:00
const acl::db_rows* result = db.get_result();
2022-09-27 18:02:45 +08:00
if (result) {
2014-11-19 00:25:21 +08:00
const std::vector<acl::db_row*>& rows = result->get_rows();
2022-09-27 18:02:45 +08:00
for (size_t i = 0; i < rows.size(); i++) {
2014-11-19 00:25:21 +08:00
const acl::db_row* row = rows[i];
2022-09-27 18:02:45 +08:00
for (size_t j = 0; j < row->length(); j++) {
2014-11-19 00:25:21 +08:00
printf("%s, ", (*row)[j]);
2022-09-27 18:02:45 +08:00
}
2014-11-19 00:25:21 +08:00
printf("\r\n");
}
}
db.free_result();
2022-09-27 18:02:45 +08:00
return true;
2014-11-19 00:25:21 +08:00
}
// <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
static int tbl_select(acl::db_handle& db, int n)
{
const char* sql_fmt = "select * from group_tbl where"
2022-09-27 18:02:45 +08:00
" group_name='%s-%d' and uvip_tbl='test'";
2014-11-19 00:25:21 +08:00
acl::string sql;
2022-09-27 18:02:45 +08:00
sql.format(sql_fmt,
acl::strconv(__pre, strlen(__pre), "gbk", "utf8").c_str(), n);
2014-11-19 00:25:21 +08:00
2022-09-27 18:02:45 +08:00
if (db.sql_select(sql.c_str()) == false) {
2014-11-19 00:25:21 +08:00
printf("select sql error\r\n");
2022-09-27 18:02:45 +08:00
return -1;
2014-11-19 00:25:21 +08:00
}
printf("\r\n---------------------------------------------------\r\n");
// <20>г<EFBFBD><D0B3><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ
2014-11-19 00:25:21 +08:00
const acl::db_rows* result = db.get_result();
2022-09-27 18:02:45 +08:00
if (result) {
2014-11-19 00:25:21 +08:00
const std::vector<acl::db_row*>& rows = result->get_rows();
2022-09-27 18:02:45 +08:00
for (size_t i = 0; i < rows.size(); i++) {
if (n > 100) {
2014-11-19 00:25:21 +08:00
continue;
2022-09-27 18:02:45 +08:00
}
2014-11-19 00:25:21 +08:00
const acl::db_row* row = rows[i];
2022-09-27 18:02:45 +08:00
for (size_t j = 0; j < row->length(); j++) {
2014-11-19 00:25:21 +08:00
printf("%s, ", (*row)[j]);
2022-09-27 18:02:45 +08:00
}
2014-11-19 00:25:21 +08:00
printf("\r\n");
}
}
// <20>г<EFBFBD><D0B3><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2022-09-27 18:02:45 +08:00
for (size_t i = 0; i < db.length(); i++) {
if (n > 100) {
2014-11-19 00:25:21 +08:00
continue;
2022-09-27 18:02:45 +08:00
}
2014-11-19 00:25:21 +08:00
const acl::db_row* row = db[i];
// ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD>¼<EFBFBD><C2BC>ij<EFBFBD><C4B3><EFBFBD>ֶε<D6B6>ֵ
2014-11-19 00:25:21 +08:00
const char* ptr = (*row)["group_name"];
2022-09-27 18:02:45 +08:00
if (ptr == NULL) {
2014-11-19 00:25:21 +08:00
printf("error, no group name\r\n");
continue;
}
printf("group_name=%s: ", ptr);
2022-09-27 18:02:45 +08:00
for (size_t j = 0; j < row->length(); j++) {
2014-11-19 00:25:21 +08:00
printf("%s, ", (*row)[j]);
2022-09-27 18:02:45 +08:00
}
2014-11-19 00:25:21 +08:00
printf("\r\n");
}
// <20>г<EFBFBD><D0B3><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
const std::vector<acl::db_row*>* rows = db.get_rows();
2022-09-27 18:02:45 +08:00
if (rows) {
2014-11-19 00:25:21 +08:00
std::vector<acl::db_row*>::const_iterator cit = rows->begin();
2022-09-27 18:02:45 +08:00
for (; cit != rows->end(); cit++) {
if (n > 100) {
2014-11-19 00:25:21 +08:00
continue;
2022-09-27 18:02:45 +08:00
}
2014-11-19 00:25:21 +08:00
const acl::db_row* row = *cit;
2022-09-27 18:02:45 +08:00
for (size_t j = 0; j < row->length(); j++) {
2014-11-19 00:25:21 +08:00
printf("%s, ", (*row)[j]);
2022-09-27 18:02:45 +08:00
}
2014-11-19 00:25:21 +08:00
printf("\r\n");
}
}
int ret = (int) db.length();
// <20>ͷŲ<CDB7>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
db.free_result();
2022-09-27 18:02:45 +08:00
return ret;
2014-11-19 00:25:21 +08:00
}
// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
static bool tbl_delete(acl::db_handle& db, int n)
{
2022-09-27 18:02:45 +08:00
const char* sql_fmt = "delete from group_tbl where group_name='%s-%d'";
2014-11-19 00:25:21 +08:00
acl::string sql;
2022-09-27 18:02:45 +08:00
sql.format(sql_fmt,
acl::strconv(__pre, strlen(__pre), "gbk", "utf8" ).c_str(), n);
2014-11-19 00:25:21 +08:00
2022-09-27 18:02:45 +08:00
if (db.sql_update(sql.c_str()) == false) {
2014-11-19 00:25:21 +08:00
printf("delete sql error\r\n");
2022-09-27 18:02:45 +08:00
return false;
2014-11-19 00:25:21 +08:00
}
2022-09-27 18:02:45 +08:00
for (size_t i = 0; i < db.length(); i++) {
2014-11-19 00:25:21 +08:00
const acl::db_row* row = db[i];
2022-09-27 18:02:45 +08:00
for (size_t j = 0; j < row->length(); j++) {
2014-11-19 00:25:21 +08:00
printf("%s, ", (*row)[j]);
2022-09-27 18:02:45 +08:00
}
2014-11-19 00:25:21 +08:00
printf("\r\n");
}
// <20>ͷŲ<CDB7>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
db.free_result();
2022-09-27 18:02:45 +08:00
return true;
2014-11-19 00:25:21 +08:00
}
int main(void)
{
// WIN32 <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ô˺<C3B4><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD> SOCKET <20>ij<EFBFBD>ʼ<EFBFBD><CABC>
acl::acl_cpp_init();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ
acl::log::stdout_open(true);
acl::string line;
acl::stdin_stream in;
acl::stdout_stream out;
#if defined(_WIN32) || defined(_WIN64)
const char* libname = "libmysql.dll";
#else
const char* libname = "libmysqlclient_r.so";
#endif
acl::string path;
// <20><>Ϊ<EFBFBD><CEAA><EFBFBD>ö<EFBFBD>̬<EFBFBD><CCAC><EFBFBD>صķ<D8B5>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҪӦ<D2AA>ø<EFBFBD><C3B8><EFBFBD> mysql <20>ͻ<EFBFBD><CDBB>˿<EFBFBD><CBBF><EFBFBD><EFBFBD>ڵ<EFBFBD>·<EFBFBD><C2B7>
out.format("Enter %s load path: ", libname);
2022-09-27 18:02:45 +08:00
if (in.gets(line) && !line.empty()) {
#if defined(_WIN32) || defined(_WIN64)
path.format("%s\\%s", line.c_str(), libname);
#else
path.format("%s/%s", line.c_str(), libname);
#endif
2022-09-27 18:02:45 +08:00
} else {
path = libname;
2022-09-27 18:02:45 +08:00
}
out.format("%s path: %s\r\n", libname, path.c_str());
// <20><><EFBFBD>ö<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ȫ·<C8AB><C2B7>
acl::db_handle::set_loadpath(path);
2014-11-19 00:25:21 +08:00
#ifdef WIN32
const char* dbaddr = "192.168.1.251:3306";
#else
const char* dbaddr = "127.0.0.1:3306";
#endif
const char* dbname = "acl_test_db";
const char* dbuser = "acl_user", *dbpass = "111111";
acl::db_mysql db(dbaddr, dbname, dbuser, dbpass);
int max = 100;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ
2014-11-19 00:25:21 +08:00
acl_msg_stdout_enable(1);
2022-09-27 18:02:45 +08:00
if (db.open() == false) {
2014-11-19 00:25:21 +08:00
printf("open db(%s) error\r\n", dbname);
getchar();
return 1;
}
printf("open db %s ok\r\n", dbname);
2022-09-27 18:02:45 +08:00
if (tbl_create(db) == false) {
2014-11-19 00:25:21 +08:00
printf("create table error\r\n");
getchar();
return 1;
}
ACL_METER_TIME("---begin insert---");
2022-09-27 18:02:45 +08:00
for (int i = 0; i < max; i++) {
2014-11-19 00:25:21 +08:00
bool ret = tbl_insert(db, i);
2022-09-27 18:02:45 +08:00
if (ret) {
2014-11-19 00:25:21 +08:00
printf(">>insert ok: i=%d, affected: %d\r",
i, db.affect_count());
2022-09-27 18:02:45 +08:00
} else {
2014-11-19 00:25:21 +08:00
printf(">>insert error: i = %d\r\n", i);
2022-09-27 18:02:45 +08:00
}
2014-11-19 00:25:21 +08:00
}
printf("\r\n");
ACL_METER_TIME("---end insert---");
2022-09-27 18:02:45 +08:00
printf("Enter any key to select ...");
fflush(stdout);
getchar();
2014-11-19 00:25:21 +08:00
ACL_METER_TIME("---begin select---");
int n = 0;
2022-09-27 18:02:45 +08:00
for (int i = 0; i < max; i++) {
2014-11-19 00:25:21 +08:00
int ret = tbl_select(db, i);
2022-09-27 18:02:45 +08:00
if (ret >= 0) {
2014-11-19 00:25:21 +08:00
n += ret;
printf(">>select ok: i=%d, ret=%d\r", i, ret);
2022-09-27 18:02:45 +08:00
} else {
2014-11-19 00:25:21 +08:00
printf(">>select error: i = %d\r\n", i);
2022-09-27 18:02:45 +08:00
}
2014-11-19 00:25:21 +08:00
}
printf("\r\n");
printf(">>select total: %d\r\n", n);
ACL_METER_TIME("---end select---");
2022-09-27 18:02:45 +08:00
printf("Enter any key to delete ...");
fflush(stdout);
getchar();
2014-11-19 00:25:21 +08:00
ACL_METER_TIME("---begin delete---");
2022-09-27 18:02:45 +08:00
for (int i = 0; i < max; i++) {
2014-11-19 00:25:21 +08:00
bool ret = tbl_delete(db, i);
2022-09-27 18:02:45 +08:00
if (ret) {
2014-11-19 00:25:21 +08:00
printf(">>delete ok: %d, affected: %d\r",
i, (int) db.affect_count());
2022-09-27 18:02:45 +08:00
} else {
2014-11-19 00:25:21 +08:00
printf(">>delete error: i = %d\r\n", i);
2022-09-27 18:02:45 +08:00
}
2014-11-19 00:25:21 +08:00
}
printf("\r\n");
#ifndef WIN32
// mysql_server_end();
// mysql_thread_end();
#endif
printf("mysqlclient lib's version: %ld, info: %s\r\n",
db.mysql_libversion(), db.mysql_client_info());
ACL_METER_TIME("---end delete---");
printf("Enter any key to exit.\r\n");
getchar();
return 0;
}