mysql++ 连接池版本_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > mysql++ 连接池版本

mysql++ 连接池版本

 2012/10/15 10:40:47  guozhiwei  程序员俱乐部  我要评论(0)
  • 摘要:目录结构.|--Makefile|--include||--mysql_conn.h|`--sns_concern.h|--lib||--Db|||--Makefile||`--mysql_conn.cpp||--Makefile||--sns_concern.cpp|`--sns_concern.o|--ma2|--ma2.cpp`--ma2.o头文件:#ifndefMYSQL_CONN_H#defineMYSQL_CONN_H#include<iostream>
  • 标签:连接 连接池 SQL MySQL 版本

目录结构

?

.

|-- Makefile

|-- include

| ? |-- mysql_conn.h

| ? `-- sns_concern.h

|-- lib

| ? |-- Db

| ? | ? |-- Makefile

| ? | ? `-- mysql_conn.cpp

| ? |-- Makefile

| ? |-- sns_concern.cpp

| ? `-- sns_concern.o

|-- ma2

|-- ma2.cpp

`-- ma2.o

?

?

头文件

#ifndef MYSQL_CONN_H
#define MYSQL_CONN_H
#include <iostream>
#include <string>
#include <mysql++.h>

using namespace std;
using namespace mysqlpp;

class mypool: public mysqlpp::ConnectionPool
{
public:
    char * _user;
    char * _pwd;
    char * _host;
    char * _db;
    mypool(char* user, char* pwd, char* host, char* db) ;
    ~mypool();
    Connection* create();
    void destroy(Connection *cp);
    unsigned int max_idle_time();
    int get_pool_size();
};


class mysql_conn
{
    public:
        mypool *myptr;
        Connection *con;
        mysql_conn(string dbid, char *charset);
        ~mysql_conn();
        StoreQueryResult mysql_query(string query_str, SQLQueryParms params=NULL);
        int mysql_execute(string query_str,SQLQueryParms params=NULL);
        int get_insert_id();
        static mysql_conn get_instance(string dbid);
        void hello();
        int commit();
        int rollback();
};

#endif

?

cpp文件

#include <iostream>
#include <map>
#include <mysql++.h>

#include "mysql_conn.h"

using namespace std;
using namespace mysqlpp;


mypool::mypool(char* user, char* pwd, char* host, char* db)
{
        _user = user;
        _pwd = pwd;
        _host = host;
        _db = db;
}

mypool::~mypool()
{
    clear();
}

Connection* mypool::create()
{
        return new Connection(_db, _host, _user, _pwd);
}

void mypool::destroy(Connection *cp)
{
        delete cp;
}

unsigned int mypool::max_idle_time()
{
        return 60;
}
   
int mypool::get_pool_size()
{
        return this->size();
}


map<string,mypool *> dbpoolmap;


mysql_conn mysql_conn::get_instance(string dbid)
{
    char *user = "poker_wrt";
    char *pwd = "poker_wrt";
    char *host = "192.168.0.246";
    char *db = "test";
    char *charset = "gbk";
    if(dbpoolmap.count(dbid) == 0)
    {
        dbpoolmap[dbid] = 0;
    }
    if (0 == dbpoolmap[dbid])
    {
        dbpoolmap[dbid] = new mypool(user, pwd, host, db);
    }
    mysql_conn instance(dbid,charset);
    return instance;
}


mysql_conn::mysql_conn(string dbid, char *charset)
{
    myptr = dbpoolmap[dbid];
    con = myptr->grab();
    string str = "set names " ;
    str = str + charset;
    this->mysql_execute(str);
    this->mysql_execute("set autocommit=0");
}

mysql_conn::~mysql_conn()
{
    cout << "in destruct func" << endl;
    myptr->release(con);
}

void mysql_conn::hello()
{
    cout << "from hello" << endl;
}

StoreQueryResult mysql_conn::mysql_query(string query_str, SQLQueryParms params)
{
    Query query = con->query(query_str);
    query.parse();
    StoreQueryResult res = query.store(params) ;
    return res;
}


int mysql_conn::mysql_execute(string query_str,SQLQueryParms params)
{
    Query query = con->query(query_str);
    query.parse();
    query.execute(params);
    return query.affected_rows();
}

int mysql_conn::get_insert_id()
{
    Query query = con->query();
    return query.insert_id();
}

int mysql_conn::commit()
{
    Transaction trans(*con);
    trans.commit(); 
    return 1;
}

int mysql_conn::rollback()
{
    Transaction trans(*con);
    trans.rollback(); 
    return 1;
}
?

?

代码在附件中

?

  • mysqlblogpool.tar.gz (325.2 KB)
  • 下载次数: 1
发表评论
用户名: 匿名