Chinaunix首页 | 论坛 | 博客
  • 博客访问: 87291
  • 博文数量: 19
  • 博客积分: 487
  • 博客等级: 下士
  • 技术积分: 205
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-18 15:57
文章分类

全部博文(19)

文章存档

2011年(19)

我的朋友

分类: Mysql/postgreSQL

2011-03-24 11:10:46

SQLite.hpp
  1. #ifndef _SQLITE_H
  2. #define _SQLITE_H

  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <sqlite3.h>
  6. #include <strings.h>
  7. #include <string.h>
  8. #include <iostream>
  9. #include <sstream>
  10. #include "daemon.hpp"
  11. #include "Base64.hpp"

  12. #include <map>
  13. #include <set>
  14. #include <vector>


  15. using namespace std;
  16. using namespace PWRD;
  17. using namespace PWRD::Marshal;

  18. namespace PWRD{
  19.     namespace SQL{
  20. #define CLIENT_PORT 11180
  21.         typedef struct sqlite3 * Connection;    
  22.         typedef struct sqlite3_stmt * Statement;
  23.         typedef vector<string> Vector;
  24.         typedef map<int, Vector> AddrMap;
  25.         typedef set<Vector> ResultSet;

  26.         class SQLite{
  27.             public:
  28.                 static SQLite* get_instance(const char *);
  29.                 static AddrMap & lookup();
  30.                 static ResultSet & qurey(string ip, int port);
  31.                 static int update(string ip, int port, string file, long offset = 0);
  32.                 static int record(string ip, int port, string file, string chars);
  33.                 static int config(string ip, int port, string file, string conf);
  34.                 static bool status();
  35.             private:
  36.                 SQLite();
  37.                 int connect(const char *);    
  38.                 int disconnect();
  39.                 static SQLite *instance_;
  40.                 static AddrMap addr_map_;
  41.                 static Connection conn_;
  42.                 static Statement stmt_;
  43.                 static ResultSet result_set_;
  44.                 static bool status_;
  45.                 static string dbs_;
  46.         };


  47.     };
  48. };
  49. #endif

SQLite.cpp

 

  1. #include "SQLite.hpp"
  2. namespace PWRD{
  3.     namespace SQL{
  4.         //----------------------------------------------------------------------------

  5.         //Private

  6.         //----------------------------------------------------------------------------

  7.         AddrMap SQLite::addr_map_;
  8.         ResultSet SQLite::result_set_;
  9.         Connection SQLite::conn_;
  10.         Statement SQLite::stmt_;
  11.         string SQLite::dbs_;
  12.         SQLite * SQLite::instance_ = NULL;
  13.         bool SQLite::status_ = false;

  14.         SQLite * SQLite::get_instance(const char *db){

  15.             if(NULL == instance_){
  16.                 instance_ = new SQLite();    
  17.             }        

  18.             instance_->connect(db);

  19.             return instance_;
  20.         }

  21.         SQLite::SQLite(){
  22.         
  23.         }
  24.         
  25.         int SQLite::connect(const char * db){
  26.             dbs_ = "configure";
  27.             if(NULL == db){
  28.                 db = dbs_.c_str();            
  29.             }    

  30.             if(SQLITE_OK != sqlite3_open(db, &conn_)){
  31.                 logs.write_log(DEBUG, "Connect to sqlite3 failed");    
  32.                 status_ = false;
  33.                 return -1;
  34.             }

  35.             status_ = true;
  36.             return 1;
  37.         }
  38.         int SQLite::disconnect(){
  39.             sqlite3_close(conn_);
  40.             status_ = false;
  41.             return 1;

  42.         }

  43.         //----------------------------------------------------------------------------

  44.         //Public

  45.         //----------------------------------------------------------------------------

  46.         AddrMap & SQLite::lookup(){

  47.             //clear the map

  48.             addr_map_.erase(addr_map_.begin(), addr_map_.end());
  49.             char * sql = "select * from configs";        
  50.             char *pzTail;

  51.             if (SQLITE_OK != sqlite3_prepare(conn_, sql, strlen(sql), &stmt_, (const char **)&pzTail)){
  52.                 logs.write_log(NORMAL, "SQLite sql: %s", sql);    
  53.                 return addr_map_;
  54.             }

  55.             int row = 0, step = 0, column_size = 0;
  56.             while(1){
  57.                 switch (sqlite3_step(stmt_))
  58.                 {
  59.                     case SQLITE_ROW:
  60.                         logs.write_log(DEBUG, "SQLITE_ROW ");
  61.                         break;
  62.                     case SQLITE_BUSY:
  63.                         logs.write_log(DEBUG, "SQLITE_BUSY ");
  64.                         goto ERR;
  65.                     case SQLITE_MISUSE:
  66.                         logs.write_log(DEBUG, "SQLITE_MISUSE ");
  67.                         goto ERR;
  68.                     case SQLITE_ERROR:
  69.                         logs.write_log(DEBUG, "SQLITE_ERROR ");
  70.                         goto ERR;
  71.                     case SQLITE_DONE:
  72.                         logs.write_log(DEBUG, "SQLITE_DONE ");
  73.                         goto END;
  74.                     default:
  75.                         goto ERR;
  76.                 }


  77.                 if(0 == row){
  78.                     column_size = sqlite3_column_count(stmt_);
  79.                     logs.write_log(NORMAL, "Lookup %d columns", column_size);
  80.                 }


  81.                 const char *result;
  82.                 Vector vect;
  83.                 step = 0;
  84.                 while(step < column_size){
  85.                     result = (const char*)sqlite3_column_text(stmt_, step);
  86.                     if(NULL == result){
  87.                         vect.push_back("");        
  88.                     }
  89.                     string value(result);
  90.                     cout << result << " " ;
  91.                     vect.push_back(value);
  92.                     step++;
  93.                 }
  94.                 addr_map_[row] = vect;    
  95.                 cout << endl;
  96.                 row++;
  97.             }//while 1

  98.             if (SQLITE_OK != sqlite3_finalize(stmt_))
  99.                 goto ERR;
  100. END:
  101.             /* 释放stmt_ 必须要释放否者内存泄漏 */
  102.             if (SQLITE_OK != sqlite3_finalize(stmt_))
  103.                 goto ERR;

  104.             //disconnect();

  105.             return addr_map_;

  106. ERR:
  107.             printf("%s\n", sqlite3_errmsg(conn_));
  108.             return addr_map_;


  109.         }

  110.         ResultSet &SQLite::qurey(string ip, int port){
  111.             //clear

  112.             result_set_.erase(result_set_.begin(), result_set_.end());
  113.             logs.write_log(DEBUG, "qurey addr:%s port:%d", ip.c_str(), port);
  114.             AddrMap::iterator iter = addr_map_.begin();
  115.             for( ; iter != addr_map_.end(); iter++){
  116.                 Vector vect = iter->second;    
  117.                 Vector result;
  118.                 if( !ip.compare(vect[1]) && port == std::atoi((vect[2]).c_str())){
  119.                     result.push_back(vect[2]);    
  120.                     result.push_back(vect[3]);    
  121.                     result.push_back(vect[4]);    
  122.                     result.push_back(vect[5]);    
  123.                     result.push_back(vect[6]);    

  124.                     //insert to result_set

  125.                     result_set_.insert(result);
  126.                 }

  127.             }
  128.             return result_set_;
  129.         }

  130.         int SQLite::update(string ip, int port, string file, long offset){
  131.             string sql = "update configs set offset = ";    
  132.             stringstream ss;
  133.             string offset_str = "";
  134.             ss << offset;
  135.             ss >> offset_str;
  136.             sql += offset_str;
  137.             sql += " where ip = \"";
  138.              sql += ip;
  139.              sql += "\" and port = ";
  140.             ss.clear();
  141.             string port_str = "";
  142.             ss << port;
  143.             ss >> port_str;
  144.          sql += port_str;
  145.          sql += " and file = \"";
  146.             sql += file;
  147.             sql += "\";";

  148.             logs.write_log(DEBUG, "update str=%s|port=%d|file=%s|offset=%d|info=begin", sql.c_str(), port, file.c_str(), offset);

  149.             char *pzTail = 0;

  150.             //update

  151.             if(sqlite3_exec(conn_, sql.c_str(), 0, 0, &pzTail)){
  152.                 goto ERR;    
  153.             }

  154.             logs.write_log(DEBUG, "update str=%s|port=%d|file=%s|offset=%d|info=end", sql.c_str(), port, file.c_str(), offset);
  155.             return 1;

  156. ERR:
  157.             logs.write_log(DEBUG, "update str=%s|port=%d|file=%s|offset=%d|info=update failed:%s", sql.c_str(), port, file.c_str(), offset, sqlite3_errmsg(conn_));
  158.             return -1;

  159.         }

  160.         int SQLite::record(string ip, int port, string file, string chars){
  161.             string sql = "update configs set chars = \"";    
  162.             sql += chars;
  163.             sql += "\" where ip = \"";
  164.             sql += ip;
  165.             sql += "\" and port = ";
  166.             stringstream ss;
  167.             string port_str = "";
  168.             ss << port;
  169.             ss >> port_str;
  170.             sql += port_str;
  171.              sql += " and file = \"";
  172.             sql += file;
  173.             sql += "\";";

  174.             logs.write_log(DEBUG, "update sql=%s|port=%d|file=%s|chars=%s|info=begin", sql.c_str(), port, file.c_str(), chars.c_str());

  175.             char *pzTail = 0;//sql.c_str();


  176.             if(sqlite3_exec(conn_, sql.c_str(), 0, 0, &pzTail)){
  177.                 goto ERR;    
  178.             }

  179.             logs.write_log(DEBUG, "update sql=%s|port=%d|file=%s|chars=%s|info=end", sql.c_str(), port, file.c_str(), chars.c_str());
  180.             return 1;

  181. ERR:
  182.             logs.write_log(DEBUG, "update sql=%s|port=%d|file=%s|chars=%s|info=update failed:%s", sql.c_str(), port, file.c_str(), chars.c_str(), sqlite3_errmsg(conn_));

  183.             return -1;

  184.         }

  185.         int SQLite::config(string ip, int port, string file, string conf){
  186.             string base64_conf = Base64::encode(reinterpret_cast<const unsigned char*>(conf.c_str()), conf.length());
  187.             string sql = "update configs set conf = \"";    
  188.             sql += base64_conf;
  189.             sql += "\" where ip = \"";
  190.             sql += ip;
  191.             sql += "\" and port = ";
  192.             stringstream ss;
  193.             string port_str = "";
  194.             ss << port;
  195.             ss >> port_str;
  196.             sql += port_str;
  197.              sql += " and file = \"";
  198.             sql += file;
  199.             sql += "\";";

  200.             logs.write_log(DEBUG, "update sql=%s|port=%d|file=%s|base64_conf=%s|info=begin", sql.c_str(), port, file.c_str(), base64_conf.c_str());

  201.             char *pzTail = 0;//sql.c_str();


  202.             if(sqlite3_exec(conn_, sql.c_str(), 0, 0, &pzTail)){
  203.                 goto ERR;    
  204.             }

  205.             logs.write_log(DEBUG, "update sql=%s|port=%d|file=%s|base64_conf=%s|info=end", sql.c_str(), port, file.c_str(), base64_conf.c_str());
  206.             return 1;

  207. ERR:
  208.             logs.write_log(DEBUG, "update sql=%s|port=%d|file=%s|base64_conf=%s|info=update failed:%s", sql.c_str(), port, file.c_str(), base64_conf.c_str(), sqlite3_errmsg(conn_));

  209.             return -1;

  210.         }


  211.         bool SQLite::status(){
  212.             return status_;
  213.         }

  214.     };

  215. };
阅读(983) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~