2015-08-02 18:21:24 +08:00
// mysql.cpp : <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ̨Ӧ<CCA8> ó<EFBFBD> <C3B3> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ڵ㡣
//
# include "acl_cpp/lib_acl.hpp"
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 "
" ) " ;
static bool db_create ( const char * dbaddr , const char * dbname ,
const char * dbuser , const char * dbpass )
{
acl : : db_mysql db ( dbaddr , " mysql " , dbuser , dbpass ) ;
if ( db . open ( ) = = false )
{
printf ( " open %s@mysql error, dbuser: %s, dbpass: %s \r \n " ,
dbaddr , dbuser , dbpass ) ;
return false ;
}
printf ( " mysqlclient lib's version: %ld, info: %s \r \n " ,
db . mysql_libversion ( ) , db . mysql_client_info ( ) ) ;
acl : : string sql ;
sql . format ( " use mysql " ) ;
if ( db . sql_update ( sql . c_str ( ) ) = = false )
{
printf ( " '%s' error: %s \r \n " , sql . c_str ( ) , db . get_error ( ) ) ;
return false ;
}
db . free_result ( ) ;
sql . format ( " create database %s character set utf8; \r \n " , dbname ) ;
if ( db . sql_update ( sql . c_str ( ) ) = = false )
{
printf ( " '%s' error: %s \r \n " , sql . c_str ( ) , db . get_error ( ) ) ;
return false ;
}
db . free_result ( ) ;
sql . format ( " grant CREATE,DROP,INSERT,DELETE,UPDATE,SELECT on %s.* to %s " ,
dbname , dbuser ) ;
if ( db . sql_update ( sql . c_str ( ) ) = = false )
{
printf ( " '%s' error: %s \r \n " , sql . c_str ( ) , db . get_error ( ) ) ;
return false ;
}
db . free_result ( ) ;
sql = " flush privileges " ;
if ( db . sql_update ( sql . c_str ( ) ) = = false )
{
printf ( " '%s' error: %s \r \n " , sql . c_str ( ) , db . get_error ( ) ) ;
return false ;
}
db . free_result ( ) ;
printf ( " create db(%s) ok \r \n " , dbname ) ;
return true ;
}
static bool tbl_create ( const char * dbaddr , const char * dbname ,
const char * dbuser , const char * dbpass )
{
acl : : db_mysql db ( dbaddr , dbname , dbuser , dbpass ) ;
if ( db . open ( ) = = false )
{
printf ( " open %s@mysql error, dbuser: %s, dbpass: %s \r \n " ,
dbaddr , dbuser , dbpass ) ;
return false ;
}
printf ( " mysqlclient lib's version: %ld, info: %s \r \n " ,
db . mysql_libversion ( ) , db . mysql_client_info ( ) ) ;
if ( db . tbl_exists ( " group_tbl " ) )
{
printf ( " table exist \r \n " ) ;
return ( true ) ;
}
else if ( db . sql_update ( CREATE_TBL ) = = false )
{
printf ( " sql error \r \n " ) ;
return ( false ) ;
}
else
{
printf ( " create table ok \r \n " ) ;
return ( true ) ;
}
}
// <20> <> <EFBFBD> ӱ<EFBFBD> <D3B1> <EFBFBD> <EFBFBD> <EFBFBD>
static bool tbl_insert ( acl : : db_handle & db , int n )
{
acl : : query query ;
2015-08-09 22:42:51 +08:00
query . create_sql ( " insert into group_tbl(group_name, uvip_tbl, "
" update_date) values(:group, :test, :date) " )
2015-08-02 18:21:24 +08:00
. set_format ( " group " , " group:%d " , n )
. set_parameter ( " test " , " test " )
. set_date ( " date " , time ( NULL ) , " %Y-%m-%d " ) ;
if ( db . exec_update ( query ) = = false )
return ( false ) ;
const acl : : db_rows * result = db . get_result ( ) ;
if ( result )
{
const std : : vector < acl : : db_row * > & rows = result - > get_rows ( ) ;
for ( size_t i = 0 ; i < rows . size ( ) ; i + + )
{
const acl : : db_row * row = rows [ i ] ;
for ( size_t j = 0 ; j < row - > length ( ) ; j + + )
printf ( " %s, " , ( * row ) [ j ] ) ;
printf ( " \r \n " ) ;
}
}
db . free_result ( ) ;
return ( true ) ;
}
// <20> <> ѯ<EFBFBD> <D1AF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
static int tbl_select ( acl : : db_handle & db , int n )
{
acl : : query query ;
2015-08-09 22:42:51 +08:00
query . create_sql ( " select * from group_tbl where group_name=:group "
" and uvip_tbl=:test " )
2015-08-02 18:21:24 +08:00
. set_format ( " group " , " group:%d " , n )
. set_format ( " test " , " test " ) ;
if ( db . exec_select ( query ) = = false )
{
printf ( " select sql error \r \n " ) ;
return ( - 1 ) ;
}
printf ( " \r \n --------------------------------------------------- \r \n " ) ;
// <20> г <EFBFBD> <D0B3> <EFBFBD> ѯ<EFBFBD> <D1AF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> һ
const acl : : db_rows * result = db . get_result ( ) ;
if ( result )
{
const std : : vector < acl : : db_row * > & rows = result - > get_rows ( ) ;
for ( size_t i = 0 ; i < rows . size ( ) ; i + + )
{
if ( n > 100 )
continue ;
const acl : : db_row * row = rows [ i ] ;
for ( size_t j = 0 ; j < row - > length ( ) ; j + + )
printf ( " %s, " , ( * row ) [ j ] ) ;
printf ( " \r \n " ) ;
}
}
// <20> г <EFBFBD> <D0B3> <EFBFBD> ѯ<EFBFBD> <D1AF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
for ( size_t i = 0 ; i < db . length ( ) ; i + + )
{
if ( n > 100 )
continue ;
const acl : : db_row * row = db [ i ] ;
// ȡ<> <C8A1> <EFBFBD> <EFBFBD> <EFBFBD> м<EFBFBD> ¼<EFBFBD> <C2BC> ij<EFBFBD> <C4B3> <EFBFBD> ֶε<D6B6> ֵ
const char * ptr = ( * row ) [ " group_name " ] ;
if ( ptr = = NULL )
{
printf ( " error, no group name \r \n " ) ;
continue ;
}
printf ( " group_name=%s: " , ptr ) ;
for ( size_t j = 0 ; j < row - > length ( ) ; j + + )
printf ( " %s, " , ( * row ) [ j ] ) ;
printf ( " \r \n " ) ;
}
// <20> г <EFBFBD> <D0B3> <EFBFBD> ѯ<EFBFBD> <D1AF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
const std : : vector < acl : : db_row * > * rows = db . get_rows ( ) ;
if ( rows )
{
std : : vector < acl : : db_row * > : : const_iterator cit = rows - > begin ( ) ;
for ( ; cit ! = rows - > end ( ) ; cit + + )
{
if ( n > 100 )
continue ;
const acl : : db_row * row = * cit ;
for ( size_t j = 0 ; j < row - > length ( ) ; j + + )
printf ( " %s, " , ( * row ) [ j ] ) ;
printf ( " \r \n " ) ;
}
}
int ret = ( int ) db . length ( ) ;
// <20> ͷŲ<CDB7> ѯ<EFBFBD> <D1AF> <EFBFBD> <EFBFBD>
db . free_result ( ) ;
return ( ret ) ;
}
// ɾ<> <C9BE> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
static bool tbl_delete ( acl : : db_handle & db , int n )
{
acl : : query query ;
2015-08-09 22:42:51 +08:00
query . create_sql ( " delete from group_tbl where group_name=:group " )
. set_format ( " group " , " group-%d " , n ) ;
2015-08-02 18:21:24 +08:00
if ( db . exec_update ( query ) = = false )
{
printf ( " delete sql error \r \n " ) ;
return ( false ) ;
}
for ( size_t i = 0 ; i < db . length ( ) ; i + + )
{
const acl : : db_row * row = db [ i ] ;
for ( size_t j = 0 ; j < row - > length ( ) ; j + + )
printf ( " %s, " , ( * row ) [ j ] ) ;
printf ( " \r \n " ) ;
}
// <20> ͷŲ<CDB7> ѯ<EFBFBD> <D1AF> <EFBFBD> <EFBFBD>
db . free_result ( ) ;
return ( true ) ;
}
//////////////////////////////////////////////////////////////////////////////
class db_thread : public acl : : thread
{
public :
db_thread ( acl : : db_pool & dp , int max ) : pool_ ( dp ) , max_ ( max ) { }
~ db_thread ( ) { }
protected :
void * run ( )
{
int n = 0 ;
for ( int i = 0 ; i < max_ ; i + + )
{
acl : : db_handle * db = pool_ . peek_open ( ) ;
if ( db = = NULL )
{
printf ( " peek db connection error \r \n " ) ;
break ;
}
bool ret = tbl_insert ( * db , i ) ;
if ( ret )
{
printf ( " >>insert ok: i=%d, affected: %d \r " ,
i , db - > affect_count ( ) ) ;
n + + ;
}
else
printf ( " >>insert error: i = %d \r \n " , i ) ;
pool_ . put ( db ) ;
}
printf ( " \r \n " ) ;
printf ( " >>insert total: %d \r \n " , n ) ;
n = 0 ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ѯ<EFBFBD> <D1AF> <EFBFBD> <EFBFBD>
for ( int i = 0 ; i < max_ ; i + + )
{
acl : : db_handle * db = pool_ . peek_open ( ) ;
if ( db = = NULL )
{
printf ( " peek db connection error \r \n " ) ;
break ;
}
int ret = tbl_select ( * db , i ) ;
if ( ret > = 0 )
{
n + = ret ;
printf ( " >>select ok: i=%d, ret=%d \r " , i , ret ) ;
}
else
printf ( " >>select error: i = %d \r \n " , i ) ;
pool_ . put ( db ) ;
}
printf ( " \r \n " ) ;
printf ( " >>select total: %d \r \n " , n ) ;
// <20> <> <EFBFBD> <EFBFBD> ɾ<EFBFBD> <C9BE> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
for ( int i = 0 ; i < max_ ; i + + )
{
acl : : db_handle * db = pool_ . peek_open ( ) ;
if ( db = = NULL )
{
printf ( " peek db connection error \r \n " ) ;
break ;
}
bool ret = tbl_delete ( * db , i ) ;
if ( ret )
printf ( " >>delete ok: %d, affected: %d \r " ,
i , ( int ) db - > affect_count ( ) ) ;
else
printf ( " >>delete error: i = %d \r \n " , i ) ;
pool_ . put ( db ) ;
}
printf ( " \r \n " ) ;
return NULL ;
}
private :
acl : : db_pool & pool_ ;
int max_ ;
} ;
//////////////////////////////////////////////////////////////////////////////
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 ) ;
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
else
path = libname ;
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 ) ;
acl : : string dbaddr ( " 127.0.0.1:3306 " ) ;
2015-08-19 20:10:52 +08:00
acl : : string dbname ( " acl_db " ) , dbuser ( " root " ) , dbpass ;
2015-08-02 18:21:24 +08:00
out . format ( " Enter dbaddr [default: %s]: " , dbaddr . c_str ( ) ) ;
if ( in . gets ( line ) & & ! line . empty ( ) )
dbaddr = line ;
out . format ( " Enter dbname [default: %s]: " , dbname . c_str ( ) ) ;
if ( in . gets ( line ) & & ! line . empty ( ) )
dbname = line ;
out . format ( " Enter dbuser [default: %s]: " , dbuser . c_str ( ) ) ;
if ( in . gets ( line ) & & ! line . empty ( ) )
dbuser = line ;
out . format ( " Enter dbpass [default: %s]: " , dbpass . c_str ( ) ) ;
if ( in . gets ( line ) & & ! line . empty ( ) )
dbpass = line ;
out . format ( " dbname: %s, dbuser: %s, dbpass: %s \r \n " ,
dbname . c_str ( ) , dbuser . c_str ( ) , dbpass . c_str ( ) ) ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ҫ<EFBFBD> <D2AA> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ݿ⣬<DDBF> <E2A3AC> <EFBFBD> <EFBFBD> Ҫ<EFBFBD> <D2AA> root <20> <> <EFBFBD> ݽ<EFBFBD> <DDBD> д<EFBFBD> <D0B4> <EFBFBD>
out . format ( " Do you want to create %s? yes|no: " , dbname . c_str ( ) ) ;
if ( in . gets ( line ) & & ( line = = " yes " | | line = = " y " ) )
{
if ( dbuser ! = " root " )
{
printf ( " dbuser must be root for create db \r \n " ) ;
dbuser = " root " ;
}
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ݿ<EFBFBD>
if ( db_create ( dbaddr , dbname , dbuser , dbpass ) = = false )
{
printf ( " create db failed, enter any key to exit. \r \n " ) ;
getchar ( ) ;
return 1 ;
}
printf ( " create db %s ok, enter any key to continue \r \n " ,
dbname . c_str ( ) ) ;
( void ) in . gets ( line ) ;
}
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ݱ<EFBFBD> <DDB1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
if ( tbl_create ( dbaddr , dbname , dbuser , dbpass ) = = false )
{
printf ( " create table error \r \n " ) ;
getchar ( ) ;
return 1 ;
}
//////////////////////////////////////////////////////////////////////
out . puts ( " Enter any key to continue ... " ) ;
( void ) in . gets ( line ) ;
// <20> <> <EFBFBD> ӳ<EFBFBD> <D3B3> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ƣ<EFBFBD> <C6A3> <EFBFBD> ֵӦ<D6B5> <D3A6> <EFBFBD> ̳߳<DFB3> <CCB3> е <EFBFBD> <D0B5> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ߳<EFBFBD> <DFB3> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ȣ <EFBFBD>
// <20> Ա<EFBFBD> ֤ÿ<D6A4> <C3BF> <EFBFBD> ̶߳<DFB3> <CCB6> <EFBFBD> <EFBFBD> Ի<EFBFBD> <D4BB> <EFBFBD> һ <EFBFBD> <D2BB> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
int dblimit = 10 ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ݿ<EFBFBD> <DDBF> <EFBFBD> <EFBFBD> ӳض<D3B3> <D8B6> <EFBFBD>
2015-10-25 14:15:07 +08:00
acl : : mysql_conf dbconf ( dbaddr , dbname ) ;
dbconf . set_dbuser ( dbuser ) . set_dbpass ( dbpass ) . set_dblimit ( dblimit ) ;
acl : : db_pool * dp = new acl : : mysql_pool ( dbconf ) ;
2015-08-02 18:21:24 +08:00
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ӳ<EFBFBD> <D3B3> <EFBFBD> ÿ<EFBFBD> <C3BF> <EFBFBD> <EFBFBD> <EFBFBD> ӵĿ<D3B5> <C4BF> <EFBFBD> ʱ<EFBFBD> <CAB1> (<28> <> )
dp - > set_idle ( 120 ) ;
// ÿ<> <C3BF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ߳<EFBFBD> <DFB3> ڲ<EFBFBD> <DAB2> <EFBFBD> <EFBFBD> <EFBFBD> ÿ<EFBFBD> <C3BF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ִ<EFBFBD> е Ĵ<D0B5> <C4B4> <EFBFBD>
int max = 10 ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ߳<EFBFBD>
std : : vector < db_thread * > threads ;
for ( int i = 0 ; i < dblimit ; i + + )
{
db_thread * thread = new db_thread ( * dp , max ) ;
threads . push_back ( thread ) ;
thread - > set_detachable ( false ) ;
thread - > start ( ) ;
}
// <20> ȴ<EFBFBD> <C8B4> <EFBFBD> <EFBFBD> й<EFBFBD> <D0B9> <EFBFBD> <EFBFBD> ߳<EFBFBD> <DFB3> ˳<EFBFBD>
std : : vector < db_thread * > : : iterator it ;
for ( it = threads . begin ( ) ; it ! = threads . end ( ) ; + + it )
{
( * it ) - > wait ( NULL ) ;
delete * it ;
}
//#ifndef WIN32
// mysql_server_end();
// mysql_thread_end();
//#endif
delete dp ;
printf ( " Enter any key to exit. \r \n " ) ;
getchar ( ) ;
return 0 ;
}