Browse Source

update db_api

lainian 6 years ago
parent
commit
1865ff3d5f
17 changed files with 2066 additions and 5083 deletions
  1. 0 1537
      Makefile
  2. 0 36
      Makefile.am
  3. 0 1537
      Makefile.in
  4. 7 7
      autom4te.cache/requests
  5. 103 56
      card.cpp
  6. 15 15
      card.h
  7. 108 108
      db_api/CDBCommon.h
  8. 16 2
      db_api/CDBConnPool.cpp
  9. 8 1
      db_api/CDBConnPool.h
  10. 290 290
      db_api/CDBConnect.cpp
  11. 332 332
      db_api/CDBConnect.h
  12. 45 45
      db_api/CDBHelper.cpp
  13. 86 86
      db_api/CDBHelper.h
  14. 451 451
      db_api/CDBResultSet.cpp
  15. 573 573
      db_api/CDBResultSet.h
  16. 28 6
      main.cpp
  17. 4 1
      message.cpp

File diff suppressed because it is too large
+ 0 - 1537
Makefile


File diff suppressed because it is too large
+ 0 - 36
Makefile.am


File diff suppressed because it is too large
+ 0 - 1537
Makefile.in


+ 7 - 7
autom4te.cache/requests

@@ -62,8 +62,8 @@
                         'AM_CONDITIONAL' => 1,
                         'AM_SET_DEPDIR' => 1,
                         '_AM_DEPENDENCIES' => 1,
-                        'm4_include' => 1,
                         'AM_PROG_INSTALL_SH' => 1,
+                        'm4_include' => 1,
                         '_AC_AM_CONFIG_HEADER_HOOK' => 1,
                         'AU_DEFUN' => 1,
                         'AM_MAKE_INCLUDE' => 1
@@ -81,8 +81,8 @@
                         'configure.ac'
                       ],
                       {
-                        '_LT_AC_TAGCONFIG' => 1,
                         'AM_PROG_F77_C_O' => 1,
+                        '_LT_AC_TAGCONFIG' => 1,
                         'm4_pattern_forbid' => 1,
                         'AC_CANONICAL_TARGET' => 1,
                         'AC_CONFIG_LIBOBJ_DIR' => 1,
@@ -146,13 +146,13 @@
                         'AM_GNU_GETTEXT' => 1,
                         'AC_FUNC_MALLOC' => 1,
                         'AC_FC_FREEFORM' => 1,
-                        'AH_OUTPUT' => 1,
                         'AC_FC_PP_SRCEXT' => 1,
+                        'AH_OUTPUT' => 1,
                         'AM_PROG_CC_C_O' => 1,
-                        'AM_XGETTEXT_OPTION' => 1,
-                        'AC_CANONICAL_SYSTEM' => 1,
-                        'AM_CONDITIONAL' => 1,
                         'AC_FUNC_MKTIME' => 1,
+                        'AM_CONDITIONAL' => 1,
+                        'AC_CANONICAL_SYSTEM' => 1,
+                        'AM_XGETTEXT_OPTION' => 1,
                         'AC_CONFIG_HEADERS' => 1,
                         'AC_TYPE_UINT16_T' => 1,
                         'AM_POT_TOOLS' => 1,
@@ -226,8 +226,8 @@
                         'AC_CONFIG_MACRO_DIR_TRACE' => 1,
                         'AM_SET_DEPDIR' => 1,
                         '_AM_DEPENDENCIES' => 1,
-                        'm4_include' => 1,
                         'AM_PROG_INSTALL_SH' => 1,
+                        'm4_include' => 1,
                         '_AC_AM_CONFIG_HEADER_HOOK' => 1,
                         'AU_DEFUN' => 1,
                         'AM_MAKE_INCLUDE' => 1

+ 103 - 56
card.cpp

@@ -22,7 +22,7 @@
 #include "module_service/module_mgr.h"
 #include "visit.h"
 #include "his_location.h"
-
+#include "three_rates.h"
 extern config_file config;
 //一张卡一个ct的所有不同天线的信息
 struct one_ct_message_handle
@@ -122,6 +122,7 @@ struct one_ct_message_handle
 
     void calc_location()
 	{
+		log_info("calc_location:%d begin",m_card->m_id);
         auto v = m_msg_list;
 
 		std::vector<point> rc=std::move(m_loc_tool.calc_location(v));
@@ -129,6 +130,7 @@ struct one_ct_message_handle
 		if(!rc.empty()) m_card->on_location(std::move(rc),v);
 
 		reset();
+		log_info("calc_location:%d end",m_card->m_id);
 	}
 };
 struct card_message_handle
@@ -202,8 +204,10 @@ struct card_area
 struct person:card_location_base, card_area
 {
 	std::weak_ptr<monkey_person> m_monkeyPerson;
-	person(std::string type,uint32_t cardid,uint16_t needdisplay,int16_t t,int32_t deptid,int32_t level_id)
-        :card_location_base(type,cardid,needdisplay,t,deptid,level_id)
+	int m_workLine=0;
+	person(std::string type,uint32_t cardid,uint16_t needdisplay,int16_t t,int32_t deptid,int32_t level_id,uint32_t cid,int wl)
+        :card_location_base(type,cardid,needdisplay,t,deptid,level_id,cid)
+		 ,m_workLine(wl)
 	{
         m_message_handle.reset(new card_message_handle(this));
 		m_his_location_card.reset(new location_staff(m_id,m_type));
@@ -216,7 +220,7 @@ struct person:card_location_base, card_area
 
     void clear()
     {
-        m_site_area.reset(new site_area_hover);
+        //m_site_area.reset(new site_area_hover);
         m_area_tool.reset(new area_tool);
         m_mine_tool.reset(new mine_tool);
 
@@ -247,10 +251,11 @@ struct person:card_location_base, card_area
         return m_site_area;
     }
 
-	virtual void do_business(const point &pt)
+	virtual void do_business(const point &pt,double acc)
 	{
 		m_area_tool->on_point(m_id,pt,m_speed,m_type);
         m_site_area->on_point(m_id,0,this, m_type);
+		handle_three_rates(pt);
 	}
 	void reset(std::shared_ptr<monkey_person> mp)
 	{
@@ -258,6 +263,17 @@ struct person:card_location_base, card_area
 	}
 	~person(){}
 private:
+	void handle_three_rates(const point & pt)
+	{
+		card_pos cp;
+		cp.work_line=m_workLine;
+		cp.biz_stat = get_stat();
+		cp.x=pt.x;cp.y=pt.y;cp.z=pt.z;	
+		const auto lm = m_area_tool->getLandmark();
+		cp.enter_time = std::get<0>(lm)*1000;
+		cp.area_id = std::get<3>(lm);
+		put_three_rates(cp);
+	}
 	void on_timer()
 	{    
 		if(!m_mine_tool->is_attendance())
@@ -311,9 +327,10 @@ struct car:card_location_base,card_area
 {
 	int m_vehicle_category_id=0;
     int m_vehicle_type_id=0;
+	double m_acc =0;
     car(std::string type,uint32_t cardid,uint16_t needdisplay,int16_t t,int32_t deptid,
-        int32_t categoryid, int type_id,int32_t level_id)
-        :card_location_base(type,cardid,needdisplay,t,deptid,level_id)
+        int32_t categoryid, int type_id,int32_t level_id,uint32_t cid)
+        :card_location_base(type,cardid,needdisplay,t,deptid,level_id,cid)
 		,m_vehicle_category_id(categoryid)
         ,m_vehicle_type_id(type_id)
     {
@@ -345,12 +362,13 @@ struct car:card_location_base,card_area
         return m_site_area;
     }
 
-	virtual void do_business(const point &pt)
+	virtual void do_business(const point &pt,double acc)
 	{
+		m_acc=acc;
 		m_area_tool->on_point(m_id,pt,m_speed,m_type);
         m_site_area->on_point(m_id,0,this, m_type);
-
         m_mine_tool->on_point(m_id, m_type, m_vehicle_category_id);
+	handle_three_rates(pt);
 	}
 
     int get_vehicle_type_id()
@@ -360,6 +378,18 @@ struct car:card_location_base,card_area
 
 	~car(){}
 private:
+	void handle_three_rates(const point &pt)
+	{
+		card_pos cp;
+		cp.biz_stat = get_stat();
+		cp.x=pt.x;cp.y=pt.y;cp.z=pt.z;	
+		const auto lm = m_area_tool->getLandmark();
+		cp.enter_time = std::get<0>(lm)*1000;
+		cp.area_id = std::get<3>(lm);
+		cp.map_id = std::get<2>(lm);
+		cp.vibration=m_acc;
+		put_three_rates(cp);
+	}
 	void on_timer()
 	{
 		make_package();
@@ -427,20 +457,19 @@ uint64_t card_list::getId(uint32_t cardid,uint64_t type)
 	return type<<32|cardid;
 }
 
-void card_list::init_staffer(int32_t id)
+void card_list::init_staffer(int64_t id64)
 {
     std::string strategy = config.get("person.strategy","person1");
     std::string sql = "SELECT staff_id, s.card_id, c.card_type_id, s.dept_id, s.group_id, s.occupation_id, \
-            ol.occupation_level_id,s.worktype_id,s.need_display \
+            ol.occupation_level_id,s.worktype_id,s.need_display,s.work_line\
             FROM dat_staff_extend s \
             LEFT JOIN dat_card c ON s.card_id = c.card_id \
             LEFT JOIN dat_occupation o ON s.occupation_id = o.occupation_id \
             LEFT JOIN dat_occupation_level ol ON ol.occupation_level_id = o.occupation_level_id \
-            WHERE c.card_type_id = 1 AND s.duty_id = 0 AND c.state_id = 0";
+            WHERE s.duty_id = 0 AND c.state_id = 0";
 
-    int type = CT_PERSON;
-    std::string card_id_str = card_list::to_id64_str(type, static_cast<uint32_t>(id));
-    if(-1 == id)
+    std::string card_id_str = tool_other::to13str(id64);
+    if(-1 == id64)
     {
         sql.append(";");
     }
@@ -492,22 +521,24 @@ void card_list::init_staffer(int32_t id)
 
         int need_display = 0;
         DBRes.GetField( "need_display",need_display, Error );
-
+		
+        int work_line = 0;
+        DBRes.GetField( "work_line",work_line, Error );
         //for now;
-        staff_id = vsid;
+        //staff_id = vsid;
 
-        if(-1 == id)
+        if(-1 == id64)
         {
             std::shared_ptr<card_location_base> clb =
-                    std::make_shared<person>(strategy,staff_id,need_display,card_type_id,dept_id,occupation_level_id);
-            uint64_t cardid = getId(staff_id,type);
-            log_info("cardId:%llu,staff_id:%d dept_id:%d,need_display:%d--c-ard:%s",
-                     cardid,staff_id,dept_id,need_display,card_id.c_str());
+                    std::make_shared<person>(strategy,vsid,need_display,card_type_id,dept_id,occupation_level_id,staff_id,work_line);
+            uint64_t cardid = getId(vsid,card_type_id);
+            log_info("cardId:%llu,id:%d dept_id:%d,need_display:%d,card:%s:work_line:%d,staff_id:%d,type:%d",
+                     cardid,vsid,dept_id,need_display,card_id.c_str(),work_line,staff_id,card_type_id);
             map.insert({cardid,clb});
         }
         else
         {
-            auto card_id64 = card_list::to_id64(type, static_cast<uint32_t>(id));
+            auto card_id64 = card_list::to_id64(card_type_id, tool_other::id64_to_id(card_id));
             auto card_ptr = card_list::instance()->get(card_id64);
             if(card_ptr)
             {
@@ -521,25 +552,25 @@ void card_list::init_staffer(int32_t id)
             }
             else
             {
-                auto clb = std::make_shared<person>(strategy,staff_id,need_display,card_type_id,dept_id,occupation_level_id);
+                auto clb = std::make_shared<person>(strategy,vsid,need_display,card_type_id,dept_id,occupation_level_id,staff_id,work_line);
                 card_list::instance()->add(card_id64, clb);
             }
 
-            log_info("基础数据 增加或修改人卡成功:卡id:%d,卡type:%d dept_id:%d,need_display:%d,occupation_level_id:%d",
-                     id,type,dept_id,need_display,occupation_level_id);
-            std_debug("基础数据 增加或修改人卡成功:卡id:%d,卡type:%d dept_id:%d,need_display:%d,occupation_level_id:%d",
-                     id,type,dept_id,need_display,occupation_level_id);
+            log_info("基础数据 增加或修改人卡成功:卡id:%ld, dept_id:%d,need_display:%d,occupation_level_id:%d",
+                     id64,dept_id,need_display,occupation_level_id);
+            std_debug("基础数据 增加或修改人卡成功:卡id:%ld, dept_id:%d,need_display:%d,occupation_level_id:%d",
+                     id64,dept_id,need_display,occupation_level_id);
         }
     }
 
-    if(-1 == id)
+    if(-1 == id64)
     {
         log_info( "init_staffer. The record count=%d\n", nCount );
         card_list::instance()->add(map);
     }
 }
 
-void card_list::init_vehicle(int32_t id)
+void card_list::init_vehicle(int64_t id64)
 {
     std::string strategy = config.get("car.strategy","car1");
     std::string sql = "SELECT ve.vehicle_id, ve.card_id, c.card_type_id, \
@@ -553,11 +584,10 @@ void card_list::init_vehicle(int32_t id)
                         LEFT JOIN dat_group g ON ve.group_id = g.group_id \
                         LEFT JOIN dat_vehicle_type vt ON v.vehicle_type_id = vt.vehicle_type_id \
                         LEFT JOIN dat_vehicle_category vc ON vc.vehicle_category_id = vt.vehicle_category_id \
-                        WHERE c.card_type_id = 2 AND c.state_id = 0";
+                        WHERE c.state_id = 0";
 
-    int type = CT_VEHICLE;
-    std::string card_id_str = card_list::to_id64_str(type, static_cast<uint32_t>(id));
-    if(-1 == id)
+    std::string card_id_str = tool_other::to13str(id64);
+    if(-1 == id64)
     {
         sql.append(";");
     }
@@ -623,20 +653,20 @@ void card_list::init_vehicle(int32_t id)
         DBRes.GetField( "over_speed",over_speed, Error );
 
         //for now
-        vehicle_id = vsid;
+        //vehicle_id = vsid;
 
-        if(-1 == id)
+        if(-1 == id64)
         {
-            auto clb = std::make_shared<car>(strategy,vehicle_id,need_display,card_type_id,
-                                             dept_id,vehicle_category_id, vehicle_type_id,vehicle_level_id);
-            uint64_t cardid = getId(vehicle_id,type);
-            log_info("cardId:%llu,vehicle_id:%d dept_id:%d,need_display:%d---cardid:%s,categoryid:%d",
-                     cardid,vehicle_id,dept_id,need_display,card_id.c_str(),vehicle_category_id);
+            auto clb = std::make_shared<car>(strategy,vsid,need_display,card_type_id,
+                                             dept_id,vehicle_category_id, vehicle_type_id,vehicle_level_id,vehicle_id);
+            uint64_t cardid = getId(vsid,card_type_id);
+            log_info("cardId:%llu,id:%d dept_id:%d,need_display:%d-cardid:%s,categoryid:%d,vchile_id:%d,type:%d",
+                     cardid,vsid,dept_id,need_display,card_id.c_str(),vehicle_category_id,vehicle_id,card_type_id);
             map.insert({cardid,clb});
         }
         else
         {
-            auto card_id64 = card_list::to_id64(type, static_cast<uint32_t>(id));
+            auto card_id64 = card_list::to_id64(card_type_id, tool_other::id64_to_id(card_id));
             auto card_ptr = card_list::instance()->get(card_id64);
             if(card_ptr)
             {
@@ -649,19 +679,19 @@ void card_list::init_vehicle(int32_t id)
             }
             else
             {
-                auto clb = std::make_shared<car>(strategy,id,need_display,card_type_id,dept_id,
-                                                 vehicle_category_id, vehicle_type_id,vehicle_level_id);
+                auto clb = std::make_shared<car>(strategy,vsid,need_display,card_type_id,dept_id,
+                                                 vehicle_category_id, vehicle_type_id,vehicle_level_id,vehicle_id);
                 card_list::instance()->add(card_id64, clb);
             }
 
-            log_info("基础数据 增加或修改车卡成功:卡id:%d,卡type:%d dept_id:%d,need_display:%d,categoryid:%d",
-                     id,type,dept_id,need_display,vehicle_category_id);
-            std_debug("基础数据 增加或修改车卡成功:卡id:%d,卡type:%d dept_id:%d,need_display:%d,categoryid:%d",
-                     id,type,dept_id,need_display,vehicle_category_id);
+            log_info("基础数据 增加或修改车卡成功:卡id:%ld, dept_id:%d,need_display:%d,categoryid:%d",
+                     id64,dept_id,need_display,vehicle_category_id);
+            std_debug("基础数据 增加或修改车卡成功:卡id:%ld, dept_id:%d,need_display:%d,categoryid:%d",
+                     id64,dept_id,need_display,vehicle_category_id);
         }
     }
 
-    if(-1 == id)
+    if(-1 == id64)
     {
         log_info( "init_vehicle. The record count=%d\n", nCount );
         card_list::instance()->add(map);
@@ -1116,8 +1146,8 @@ void card_list::onTimer()
 	}
 }
 //-----------------card_location_base..
-card_location_base::card_location_base(std::string type,uint32_t id,uint16_t dis,int16_t t,int32_t deptid,int32_t level_id)
-	:card(id,dis,t,deptid,level_id)
+card_location_base::card_location_base(std::string type,uint32_t id,uint16_t dis,int16_t t,int32_t deptid,int32_t level_id,uint32_t cid)
+	:card(id,dis,t,deptid,level_id,cid)
 {
     select_tool_manage::instance()->create_tool(type,m_sel_tool,m_smo_tool);
 }
@@ -1141,12 +1171,11 @@ void card_location_base::on_location(const std::vector<point>&vp,const std::vect
 		y = pt.y;
 
 		Msg m;
-
 		m.type=m_type;m.x=(int)x;m.y=(int)y;m.cmd=CMD_HANDLE;m.cardid=m_type<<32|m_id;
-		cardMgr::instance()->tryPut(m);
-
-		log_info("useful:card_id:%d,ct:%d,timestamp:%llu, loc_point,x:%.2f,y:%.2f ",m_id,m_ct,m_time,pt.x,pt.y);
-		do_business(pt);
+		//cardMgr::instance()->tryPut(m);
+		double acc = lm[0].m_acc;
+		log_info("useful:card_id:%d,ct:%d,timestamp:%llu, loc_point,x:%.2f,y:%.2f acc:%.2f",m_id,m_ct,m_time,pt.x,pt.y,acc);
+		do_business(pt,acc);
 	}
 }
 
@@ -1219,6 +1248,24 @@ void  card_location_base::clear()
     //m_ct;				//ct
     m_time=0;			//时间戳
 }
+void card_location_base::put_three_rates(card_pos & cp)
+{
+	cp.rec_time=m_time;cp.type=m_type;cp.id=m_id;
+	cp.identifier_id=m_cid;cp.running_stat=m_stat;cp.final_v=m_speed;
+	log_info("three_rates:type:%d,id:%d,cid:%d",cp.type,cp.id,cp.identifier_id);
+	three_rates::get_instance()->put(cp);
+}
+
+bool card_location_base::is_person() const
+{
+    return tool_other::is_person(m_type);
+}
+
+bool card_location_base::is_vehicle() const
+{
+    return tool_other::is_vehicle(m_type);
+}
+
 card_location_base::~card_location_base()
 {
 }

+ 15 - 15
card.h

@@ -21,10 +21,12 @@ struct monkey_person;
 struct card_message_handle;
 struct mine_tool;
 struct location_card;
+struct card_pos;
 struct card:point
 {
-	card(uint32_t id,uint16_t dis,uint64_t type,int32_t deptid,int32_t level_id)
+	card(uint32_t id,uint16_t dis,uint64_t type,int32_t deptid,int32_t level_id,uint32_t cid)
 		:m_id(id)
+		,m_cid(cid)
 		,m_type(type)
 		,m_display(dis)
 		,m_speed(0)
@@ -38,6 +40,7 @@ struct card:point
 	uint64_t type_(){return m_type;}
 	uint64_t time_(){return m_time;}
 	uint32_t m_id;				//卡号
+	uint32_t m_cid;				//标识id 人staff_id 车 vehicle_id
 	uint64_t  m_type;			//类型
 	uint16_t m_display;			//1显示0不显示,往前端推送
 	double   m_speed;			//速度
@@ -58,9 +61,9 @@ struct card_location_base:card
  //   ev::timer m_timer;
 
     card_location_base()=default;
-    card_location_base(std::string type,uint32_t id,uint16_t dis,int16_t t,int32_t,int32_t );
+    card_location_base(std::string type,uint32_t id,uint16_t dis,int16_t t,int32_t,int32_t,uint32_t );
 
-	virtual void do_business(const point &pt)=0;
+	virtual void do_business(const point &pt,double acc)=0;
 //	virtual void set(ev::dynamic_loop * loop)=0;
 	virtual void on_timer()=0;
 	virtual void site_hover(int sid)=0;
@@ -79,24 +82,20 @@ struct card_location_base:card
 	void upt_card_pos(YA::_CARD_POS_ &,const point &pt);
 	void del_card_pos();
 	int get_stat();
-    bool is_person() const
-    {
-        return CT_PERSON == m_type;
-    }
+	void put_three_rates(card_pos &);
+
+    bool is_person() const;
+    bool is_vehicle() const;
 
-    bool is_vehicle() const
-    {
-        return CT_VEHICLE == m_type;
-    }
 	virtual ~card_location_base();
 };
 
 struct card_list:single_base<card_list,uint64_t,std::shared_ptr<card_location_base>>
 {
-    ///id=-1为初始化所有卡
-    void init_staffer(int32_t id);
-    ///id=-1为初始化所有卡
-    void init_vehicle(int32_t id);
+    ///id64=-1为初始化所有卡, id格式为:10000001016
+    void init_staffer(int64_t id64);
+    ///id64=-1为初始化所有卡, id格式为:10000001016
+    void init_vehicle(int64_t id64);
     //void init_staffer();
     //void init_vehicle();
     uint64_t getId(uint32_t cardid,uint64_t);
@@ -128,6 +127,7 @@ struct card_list:single_base<card_list,uint64_t,std::shared_ptr<card_location_ba
 
         return std::string(sql);
     }
+
     ~card_list(){m_bflag=true;}
 };
 

+ 108 - 108
db_api/CDBCommon.h

@@ -1,108 +1,108 @@
-/**
-* @brief
-数据库公共头文件
-
-* @version
-V 1.0.0
-
-* @author
-王益俊
-
-* @date
-创建时间:  2018-04-19\n
-
-* @note
-2018-04-19  初次创建。\n
-
-* @warning
-
-* @bug
-
-*/
-
-#ifndef DB_COMMON_H
-#define DB_COMMON_H
-
-#include <mysql.h>
-
-namespace YADB
-{
-    //----------------------------------------------------------------------------
-	//                                 数据库错误定义
-	//----------------------------------------------------------------------------
-	const int DB_ERR_NO_ERROR               = 0;//无错误
-	const int DB_ERR_NOT_CONNECT_DB         = -1;//未连接数据库
-	const int DB_ERR_EXCUTE_QUERY           = -1;//执行查询失败
-	const int DB_ERR_QUERY_RES_NOT_INITED   = -1;//查询结果集还未初始化
-
-	//----------------------------------------------------------------------------
-	//                                 其它定义
-	//----------------------------------------------------------------------------
-	const int MAX_ASYNC_EXEC_FAILED_COUNT   = 3;//最大异步执行失败次数
-	const int MAX_ASYNC_QUEQUE_CAPACITY     = 32 * 1024;//异步执行队列最大容量
-
-	/**
-	* @brief
-	数据库连接数枚举
-	*/
-	enum _DB_CONNECT_COUNT_
-	{
-		DCC_MIN_COUNT = 1,//最小连接数
-		DCC_MAX_COUNT = 60,//最大连接数
-	};
-
-	/**
-	* @brief
-	数据库连接设置结构体
-	*/
-    struct _DB_CONN_SETTING_
-	{
-		std::string Host;//数据库主机地址
-		std::string User;//用户名
-		std::string PWD;//密码
-		std::string DBName;//数据库名
-		std::string CharSet;//字符集
-		std::string stmtSQL;//预处理SQL
-		int TimeOut;//连接数据库超时(单位:秒)
-		_DB_CONN_SETTING_()
-		{
-			TimeOut = 0;
-		}
-	};
-
-	/**
-	* @brief
-	数据库连接池设置结构体
-	*/
-	struct _DB_POOL_SETTING_ : public _DB_CONN_SETTING_
-	{
-		int PoolSize;//连接池大小
-		_DB_POOL_SETTING_()
-		{
-			PoolSize = 0;
-		}
-	};
-
-    /**
-    * @brief
-    异步执行结构体
-    */
-	struct _ASYNC_SQL_
-	{
-		int FailedCount;//执行失败次数
-		std::string SQL; //SQ语句
-		_ASYNC_SQL_()
-		{
-			FailedCount = 0;
-		}
-		~_ASYNC_SQL_()
-		{
-
-		}
-	};
-}
-
-#endif
-
-
-
+/**
+* @brief
+数据库公共头文件
+
+* @version
+V 1.0.0
+
+* @author
+王益俊
+
+* @date
+创建时间:  2018-04-19\n
+
+* @note
+2018-04-19  初次创建。\n
+
+* @warning
+
+* @bug
+
+*/
+
+#ifndef DB_COMMON_H
+#define DB_COMMON_H
+
+#include <mysql.h>
+
+namespace YADB
+{
+    //----------------------------------------------------------------------------
+	//                                 数据库错误定义
+	//----------------------------------------------------------------------------
+	const int DB_ERR_NO_ERROR               = 0;//无错误
+	const int DB_ERR_NOT_CONNECT_DB         = -1;//未连接数据库
+	const int DB_ERR_EXCUTE_QUERY           = -1;//执行查询失败
+	const int DB_ERR_QUERY_RES_NOT_INITED   = -1;//查询结果集还未初始化
+
+	//----------------------------------------------------------------------------
+	//                                 其它定义
+	//----------------------------------------------------------------------------
+	const int MAX_ASYNC_EXEC_FAILED_COUNT   = 3;//最大异步执行失败次数
+	const int MAX_ASYNC_QUEQUE_CAPACITY     = 32 * 1024;//异步执行队列最大容量
+
+	/**
+	* @brief
+	数据库连接数枚举
+	*/
+	enum _DB_CONNECT_COUNT_
+	{
+		DCC_MIN_COUNT = 1,//最小连接数
+		DCC_MAX_COUNT = 60,//最大连接数
+	};
+
+	/**
+	* @brief
+	数据库连接设置结构体
+	*/
+    struct _DB_CONN_SETTING_
+	{
+		std::string Host;//数据库主机地址
+		std::string User;//用户名
+		std::string PWD;//密码
+		std::string DBName;//数据库名
+		std::string CharSet;//字符集
+		std::string stmtSQL;//预处理SQL
+		int TimeOut;//连接数据库超时(单位:秒)
+		_DB_CONN_SETTING_()
+		{
+			TimeOut = 0;
+		}
+	};
+
+	/**
+	* @brief
+	数据库连接池设置结构体
+	*/
+	struct _DB_POOL_SETTING_ : public _DB_CONN_SETTING_
+	{
+		int PoolSize;//连接池大小
+		_DB_POOL_SETTING_()
+		{
+			PoolSize = 0;
+		}
+	};
+
+    /**
+    * @brief
+    异步执行结构体
+    */
+	struct _ASYNC_SQL_
+	{
+		int FailedCount;//执行失败次数
+		std::string SQL; //SQ语句
+		_ASYNC_SQL_()
+		{
+			FailedCount = 0;
+		}
+		~_ASYNC_SQL_()
+		{
+
+		}
+	};
+}
+
+#endif
+
+
+

+ 16 - 2
db_api/CDBConnPool.cpp

@@ -327,9 +327,21 @@ namespace YADB
 		}
 
 		MYSQL_RES* pRes = pConn->Query( szSql, Error );
+	GiveBack( pConn );
         return DBRes.Bind( pRes, Error );
     }
+    
+    MYSQL_RES* CDBConnPool::Query( const char *szSql, std::string& Error)
+    {
+        CDBConnect *pConn = GetDBConnect(Error);
+        if( 0 == pConn){
+            return nullptr;
+        }
 
+        MYSQL_RES* pRes = pConn->Query(szSql,Error);
+        GiveBack(pConn);
+        return pRes;
+    }
     my_ulonglong CDBConnPool::ExecuteSql( const char *szSql, std::string& Error )
     {
     	CDBConnect *pConn = GetDBConnect( Error );
@@ -337,7 +349,9 @@ namespace YADB
 		{
 			return -1;
 		}
-
-		return pConn->ExecuteSql( szSql, Error );
+	my_ulonglong nRet = pConn->ExecuteSql( szSql, Error );
+		GiveBack( pConn );
+		return nRet;
+		//return pConn->ExecuteSql( szSql, Error );
     }
 }

+ 8 - 1
db_api/CDBConnPool.h

@@ -20,7 +20,9 @@ V 1.0.0
 
 */
 
-#pragma once
+//#pragma once
+#ifndef YADB_CONNECT_POOL_H
+#define YADB_CONNECT_POOL_H
 
 #include <boost/atomic.hpp>
 #include <boost/thread/thread.hpp>
@@ -257,6 +259,9 @@ namespace YADB
 
 		*/
 		bool Query( const char *szSql, CDBResultSet& DBRes,std::string& Error );
+        MYSQL_RES* Query( const char *szSql, std::string& Error);      
+        
+        
 		/**
 		* @brief
 		执行SQL语句函数。
@@ -283,3 +288,5 @@ namespace YADB
 typedef boost::serialization::singleton<YADB::CDBConnPool> singleton_CDBConnPool;
 #define sDBConnPool singleton_CDBConnPool::get_mutable_instance()
 #define sDBConnPool_const singleton_CDBConnPool::get_const_instance()
+
+#endif

+ 290 - 290
db_api/CDBConnect.cpp

@@ -1,290 +1,290 @@
-#include "CDBConnect.h"
-#include <string.h>
-
-namespace YADB
-{
-	CDBConnect::CDBConnect( bool IsTemp )
-	{
-		__pConn  = 0;
-		__IsTemp = IsTemp;
-		__pstmt  = 0;
-	}
-
-
-	CDBConnect::~CDBConnect()
-	{
-		Close();
-	}
-
-	bool CDBConnect::Connect( const _DB_CONN_SETTING_ & DBSetting, std::string & Error )
-	{
-		//创建
-		__pConn = mysql_init( NULL );
-
-		//超时设置
-		if ( mysql_options( __pConn, MYSQL_OPT_CONNECT_TIMEOUT, &(DBSetting.TimeOut) ) != 0 )
-		{
-			Error = "Failed to call mysql_options,";
-			Error += " LastError=";
-			Error += GetLastError();
-			return false;
-		}
-
-		//设置字符集
-		if ( mysql_set_character_set( __pConn, DBSetting.CharSet.c_str() ) != 0 )
-		{
-			Error = "Failed to call mysql_set_character_set,";
-			Error += " LastError=";
-			Error += GetLastError();
-			return false;
-		}
-
-		//连接数据库
-		if ( !mysql_real_connect( __pConn, DBSetting.Host.c_str(), DBSetting.User.c_str(), DBSetting.PWD.c_str(), DBSetting.DBName.c_str(), 0, NULL, 0 ) )
-		{
-			Error = "Failed to connect database,";
-			Error += " LastError=";
-			Error += GetLastError();
-			return false;
-		}
-
-		return true;
-	}
-
-	void CDBConnect::Close()
-	{
-		stmtClose();
-
-		if ( __pConn )
-		{
-			mysql_close( __pConn );
-			__pConn = 0;
-		}
-	}
-
-	int CDBConnect::ConnctionTest( std::string& Error )
-	{
-		int nRet = -1;
-
-		if ( 0 == __pConn )
-		{
-			Error = "Error,not connected to database!";
-			nRet = DB_ERR_NOT_CONNECT_DB;
-		}
-		else
-		{
-			nRet = mysql_ping( __pConn );
-			if ( nRet != 0 )
-			{
-				Error = "Failed to mysql_ping,";
-				Error += " LastError=";
-				Error += GetLastError();
-			}			
-		}
-
-		return nRet;
-	}
-
-	int CDBConnect::AutoCommit( bool Mode, std::string& Error )
-	{
-		if ( 0 == __pConn )
-		{
-			Error = "Error,not connected to database!";
-			return DB_ERR_NOT_CONNECT_DB;
-		}
-
-		return mysql_autocommit( __pConn, Mode );
-	}
-
-	int CDBConnect::Commit( std::string& Error )
-	{
-		if ( 0 == __pConn )
-		{
-			Error = "Error,not connected to database!";
-			return DB_ERR_NOT_CONNECT_DB;
-		}
-
-		return mysql_commit( __pConn );
-	}
-
-	int CDBConnect::RollBack( std::string& Error )
-	{
-		if ( 0 == __pConn )
-		{
-			Error = "Error,not connected to database!";
-			return DB_ERR_NOT_CONNECT_DB;
-		}
-
-		return mysql_rollback( __pConn );
-	}
-
-	CDBResultSet * CDBConnect::ExecuteQuery( const char * szSql, std::string & Error )
-	{
-		if ( 0 == __pConn )
-		{
-			Error = "Error,not connected to database!";
-			return 0;
-		}
-
-		if ( mysql_query( __pConn, szSql ) )
-		{
-			Error = "Failed to execute SQL!";
-			Error += " LastError=";
-			Error += GetLastError();
-			return 0;
-		}
-
-		if ( !__RessultSet.Bind( mysql_store_result( __pConn ), Error ) )
-		{
-			return 0;
-		}
-
-		return &__RessultSet;
-	}
-
-	MYSQL_RES* CDBConnect::Query( const char *szSql, std::string& Error )
-	{
-		if ( 0 == __pConn )
-		{
-			Error = "Error,not connected to database!";
-			return 0;
-		}
-
-		if ( mysql_query( __pConn, szSql ) )
-		{
-			Error = "Failed to execute SQL!";
-			Error += " LastError=";
-			Error += GetLastError();
-			return 0;
-		}
-
-		return mysql_store_result( __pConn );
-	}
-
-	my_ulonglong CDBConnect::ExecuteSql( const char * szSql, std::string& Error )
-	{
-		if ( 0 == __pConn )
-		{
-			Error = "Error,not connected to database!";
-			return DB_ERR_NOT_CONNECT_DB;
-		}
-
-		if ( mysql_query( __pConn, szSql ) )
-		{
-			Error = "Failed to execute SQL!";
-			Error += " LastError=";
-			Error += GetLastError();
-			return DB_ERR_EXCUTE_QUERY;
-		}
-
-		return mysql_affected_rows( __pConn );
-	}
-
-	my_ulonglong CDBConnect::ExecuteRealSql( const char * szSql, std::string & Error )
-	{
-		if ( 0 == __pConn )
-		{
-			Error = "Error,not connected to database!";
-			return DB_ERR_NOT_CONNECT_DB;
-		}
-
-		unsigned long uSqlLen = strlen( szSql );
-		if ( mysql_real_query( __pConn, szSql, uSqlLen ) )
-		{
-			Error = "Failed to execute SQL,";
-			Error += " LastError=";
-			Error += GetLastError();
-			return DB_ERR_EXCUTE_QUERY;
-		}
-
-		return mysql_affected_rows( __pConn );
-	}
-
-	const char * CDBConnect::GetLastError()
-	{
-		if ( 0 == __pConn )
-		{
-			return "";
-		}
-
-		return mysql_error( __pConn );
-	}
-
-	my_ulonglong CDBConnect::GetLastInsertID( std::string& Error )
-	{
-		if ( 0 == __pConn )
-		{
-			Error = "Error,not connected to database!";
-			return DB_ERR_NOT_CONNECT_DB;
-		}
-
-		return mysql_insert_id( __pConn  );
-	}
-
-	bool CDBConnect::IsTemp()
-	{
-		return __IsTemp;
-	}
-
-	bool CDBConnect::Preparestmt( const char *szSql, std::string & Error )
-	{
-		if ( 0 == __pConn )
-		{
-			Error = "Error,not connected to database!";
-			return DB_ERR_NOT_CONNECT_DB;
-		}
-
-		stmtClose();
-
-		__pstmt = mysql_stmt_init( __pConn );
-
-		if ( mysql_stmt_prepare( __pstmt, szSql, strlen( szSql ) ) != 0 )
-		{
-			Error = "Error, failed to mysql_stmt_prepare!";
-			Error += " LastError=";
-			Error += GetLastError();
-			return false;
-		}
-
-		return true;
-	}
-
-	bool CDBConnect::stmtExcute( MYSQL_BIND *stBinds, uint64_t * piId, std::string & Error )
-	{
-		if ( __pstmt == NULL )
-		{
-			Error = "Bind error, not called Preparestmt function!";
-			return false;
-		}
-
-		if ( mysql_stmt_bind_param( __pstmt, stBinds ) != 0 )
-		{
-			Error = "mysql_stmt_bind_param error,";
-			Error += mysql_stmt_error( __pstmt );
-			return false;
-		}
-
-		if ( mysql_stmt_execute( __pstmt ) != 0 )
-		{
-			Error = "mysql_stmt_execute error,";
-			Error += mysql_stmt_error( __pstmt );
-			return false;
-		}
-
-		if ( piId )
-		{
-			*piId = mysql_stmt_insert_id( __pstmt );
-		}
-
-		return true;
-	}
-
-	void CDBConnect::stmtClose()
-	{
-		if ( __pstmt )
-		{
-			mysql_stmt_close( __pstmt );
-			__pstmt = 0;
-		}
-	}
-}
+#include "CDBConnect.h"
+#include <string.h>
+
+namespace YADB
+{
+	CDBConnect::CDBConnect( bool IsTemp )
+	{
+		__pConn  = 0;
+		__IsTemp = IsTemp;
+		__pstmt  = 0;
+	}
+
+
+	CDBConnect::~CDBConnect()
+	{
+		Close();
+	}
+
+	bool CDBConnect::Connect( const _DB_CONN_SETTING_ & DBSetting, std::string & Error )
+	{
+		//创建
+		__pConn = mysql_init( NULL );
+
+		//超时设置
+		if ( mysql_options( __pConn, MYSQL_OPT_CONNECT_TIMEOUT, &(DBSetting.TimeOut) ) != 0 )
+		{
+			Error = "Failed to call mysql_options,";
+			Error += " LastError=";
+			Error += GetLastError();
+			return false;
+		}
+
+		//设置字符集
+		if ( mysql_set_character_set( __pConn, DBSetting.CharSet.c_str() ) != 0 )
+		{
+			Error = "Failed to call mysql_set_character_set,";
+			Error += " LastError=";
+			Error += GetLastError();
+			return false;
+		}
+
+		//连接数据库
+		if ( !mysql_real_connect( __pConn, DBSetting.Host.c_str(), DBSetting.User.c_str(), DBSetting.PWD.c_str(), DBSetting.DBName.c_str(), 0, NULL, 0 ) )
+		{
+			Error = "Failed to connect database,";
+			Error += " LastError=";
+			Error += GetLastError();
+			return false;
+		}
+
+		return true;
+	}
+
+	void CDBConnect::Close()
+	{
+		stmtClose();
+
+		if ( __pConn )
+		{
+			mysql_close( __pConn );
+			__pConn = 0;
+		}
+	}
+
+	int CDBConnect::ConnctionTest( std::string& Error )
+	{
+		int nRet = -1;
+
+		if ( 0 == __pConn )
+		{
+			Error = "Error,not connected to database!";
+			nRet = DB_ERR_NOT_CONNECT_DB;
+		}
+		else
+		{
+			nRet = mysql_ping( __pConn );
+			if ( nRet != 0 )
+			{
+				Error = "Failed to mysql_ping,";
+				Error += " LastError=";
+				Error += GetLastError();
+			}			
+		}
+
+		return nRet;
+	}
+
+	int CDBConnect::AutoCommit( bool Mode, std::string& Error )
+	{
+		if ( 0 == __pConn )
+		{
+			Error = "Error,not connected to database!";
+			return DB_ERR_NOT_CONNECT_DB;
+		}
+
+		return mysql_autocommit( __pConn, Mode );
+	}
+
+	int CDBConnect::Commit( std::string& Error )
+	{
+		if ( 0 == __pConn )
+		{
+			Error = "Error,not connected to database!";
+			return DB_ERR_NOT_CONNECT_DB;
+		}
+
+		return mysql_commit( __pConn );
+	}
+
+	int CDBConnect::RollBack( std::string& Error )
+	{
+		if ( 0 == __pConn )
+		{
+			Error = "Error,not connected to database!";
+			return DB_ERR_NOT_CONNECT_DB;
+		}
+
+		return mysql_rollback( __pConn );
+	}
+
+	CDBResultSet * CDBConnect::ExecuteQuery( const char * szSql, std::string & Error )
+	{
+		if ( 0 == __pConn )
+		{
+			Error = "Error,not connected to database!";
+			return 0;
+		}
+
+		if ( mysql_query( __pConn, szSql ) )
+		{
+			Error = "Failed to execute SQL!";
+			Error += " LastError=";
+			Error += GetLastError();
+			return 0;
+		}
+
+		if ( !__RessultSet.Bind( mysql_store_result( __pConn ), Error ) )
+		{
+			return 0;
+		}
+
+		return &__RessultSet;
+	}
+
+	MYSQL_RES* CDBConnect::Query( const char *szSql, std::string& Error )
+	{
+		if ( 0 == __pConn )
+		{
+			Error = "Error,not connected to database!";
+			return 0;
+		}
+
+		if ( mysql_query( __pConn, szSql ) )
+		{
+			Error = "Failed to execute SQL!";
+			Error += " LastError=";
+			Error += GetLastError();
+			return 0;
+		}
+
+		return mysql_store_result( __pConn );
+	}
+
+	my_ulonglong CDBConnect::ExecuteSql( const char * szSql, std::string& Error )
+	{
+		if ( 0 == __pConn )
+		{
+			Error = "Error,not connected to database!";
+			return DB_ERR_NOT_CONNECT_DB;
+		}
+
+		if ( mysql_query( __pConn, szSql ) )
+		{
+			Error = "Failed to execute SQL!";
+			Error += " LastError=";
+			Error += GetLastError();
+			return DB_ERR_EXCUTE_QUERY;
+		}
+
+		return mysql_affected_rows( __pConn );
+	}
+
+	my_ulonglong CDBConnect::ExecuteRealSql( const char * szSql, std::string & Error )
+	{
+		if ( 0 == __pConn )
+		{
+			Error = "Error,not connected to database!";
+			return DB_ERR_NOT_CONNECT_DB;
+		}
+
+		unsigned long uSqlLen = strlen( szSql );
+		if ( mysql_real_query( __pConn, szSql, uSqlLen ) )
+		{
+			Error = "Failed to execute SQL,";
+			Error += " LastError=";
+			Error += GetLastError();
+			return DB_ERR_EXCUTE_QUERY;
+		}
+
+		return mysql_affected_rows( __pConn );
+	}
+
+	const char * CDBConnect::GetLastError()
+	{
+		if ( 0 == __pConn )
+		{
+			return "";
+		}
+
+		return mysql_error( __pConn );
+	}
+
+	my_ulonglong CDBConnect::GetLastInsertID( std::string& Error )
+	{
+		if ( 0 == __pConn )
+		{
+			Error = "Error,not connected to database!";
+			return DB_ERR_NOT_CONNECT_DB;
+		}
+
+		return mysql_insert_id( __pConn  );
+	}
+
+	bool CDBConnect::IsTemp()
+	{
+		return __IsTemp;
+	}
+
+	bool CDBConnect::Preparestmt( const char *szSql, std::string & Error )
+	{
+		if ( 0 == __pConn )
+		{
+			Error = "Error,not connected to database!";
+			return DB_ERR_NOT_CONNECT_DB;
+		}
+
+		stmtClose();
+
+		__pstmt = mysql_stmt_init( __pConn );
+
+		if ( mysql_stmt_prepare( __pstmt, szSql, strlen( szSql ) ) != 0 )
+		{
+			Error = "Error, failed to mysql_stmt_prepare!";
+			Error += " LastError=";
+			Error += GetLastError();
+			return false;
+		}
+
+		return true;
+	}
+
+	bool CDBConnect::stmtExcute( MYSQL_BIND *stBinds, uint64_t * piId, std::string & Error )
+	{
+		if ( __pstmt == NULL )
+		{
+			Error = "Bind error, not called Preparestmt function!";
+			return false;
+		}
+
+		if ( mysql_stmt_bind_param( __pstmt, stBinds ) != 0 )
+		{
+			Error = "mysql_stmt_bind_param error,";
+			Error += mysql_stmt_error( __pstmt );
+			return false;
+		}
+
+		if ( mysql_stmt_execute( __pstmt ) != 0 )
+		{
+			Error = "mysql_stmt_execute error,";
+			Error += mysql_stmt_error( __pstmt );
+			return false;
+		}
+
+		if ( piId )
+		{
+			*piId = mysql_stmt_insert_id( __pstmt );
+		}
+
+		return true;
+	}
+
+	void CDBConnect::stmtClose()
+	{
+		if ( __pstmt )
+		{
+			mysql_stmt_close( __pstmt );
+			__pstmt = 0;
+		}
+	}
+}

+ 332 - 332
db_api/CDBConnect.h

@@ -1,332 +1,332 @@
-/**
-* @brief
-数据库连接类
-
-* @version
-V 1.0.0
-
-* @author
-王益俊
-
-* @date
-创建时间:  2018-04-17\n
-
-* @note
-2018-04-17  创建类。\n
-
-* @warning
-
-* @bug
-
-*/
-
-#pragma once
-
-#include <string>
-#include <mysql.h>
-#include "CDBResultSet.h"
-#include "CDBCommon.h"
-
-namespace YADB
-{
-	class CDBConnect
-	{
-	private:
-    	MYSQL * __pConn;//数据库连接
-    	CDBResultSet __RessultSet;//结果集
-    	bool __IsTemp;//是否是临时的(如果是临时使用的,归还到连接池中时会自动删除,以保持总体连接数不变)
-    	MYSQL_STMT *__pstmt;//预处理
-	public:
-		CDBConnect( bool IsTemp = false );
-		~CDBConnect();
-		/**
-		* @brief
-		连接数据库函数。
-
-		* @param  [in] const _DB_POOL_SETTING_& Setting  数据库连接设置\n
-		* @param  [out] std::string& Error  错误信息\n
-
-		* @return 返回连接数据库是否成功
-		* @return  true   成功\n
-		* @return  false  失败\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
-		*/
-		bool Connect( const _DB_CONN_SETTING_& DBSetting, std::string& Error );
-		/**
-	    * @brief
-	    关闭连接函数。
-
-	    * @param  无\n
-
-	    * @return  无\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		void Close();
-		/**
-	    * @brief
-	    测试连接函数。
-
-	    * @param [out] std::string& Error 错误信息\n
-
-	    * @return 返回测试连接是否成功
-	    * @return  0  连接测试成功\n
-	    * @return  !=0  连接测试失败\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		int ConnctionTest( std::string& Error );
-		/**
-	    * @brief
-	    设置自动提交的函数。
-
-	    * @param [in] bool Mode 是否是否自动提交\n
-	    * @param [out] std::string& Error 错误信息\n
-
-	    * @return 返回设置自动提交是否成功
-	    * @return  0  成功\n
-	    * @return  !=0  失败\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		int AutoCommit( bool Mode, std::string& Error );
-		/**
-	    * @brief
-	    提交函数。
-
-	    * @param [out] std::string& Error 错误信息\n
-
-	    * @return 返回提交是否成功
-	    * @return  0  成功\n
-	    * @return  !=0  失败\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		int Commit( std::string& Error  );
-		/**
-	    * @brief
-	    回滚函数。
-
-	    * @param [out] std::string& Error  错误信息\n
-
-	    * @return 返回回滚是否成功
-	    * @return  0  成功\n
-	    * @return  !=0  失败\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		int RollBack( std::string& Error );
-		/**
-	    * @brief
-	    执行SQL语句返回结果集函数。
-
-	    * @param [in] const char *szSql  SQL语句\n
-	    * @param [out] std::string& Error  错误信息\n
-
-	    * @return 返回结果集
-	    * @return  0  失败\n
-	    * @return  !=0  成功\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		CDBResultSet* ExecuteQuery( const char *szSql, std::string& Error );
-		/**
-	    * @brief
-	    执行SQL语句返回mysql原始结果集函数。
-
-	    * @param [in] const char *szSql  SQL语句\n
-	    * @param [out] std::string& Error  错误信息\n
-
-	    * @return 返回的mysql原始结果集
-	    * @return  ==0  失败\n
-	    * @return  !=0  成功\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		MYSQL_RES* Query( const char *szSql, std::string& Error );
-		/**
-		* @brief
-		执行SQL语句函数。
-
-		* @param [in] const char *szSql  SQL语句\n
-		* @param [out] std::string& Error  错误信息\n
-
-		* @return 返回影响到的记录数量
-		* @return  -1  失败\n
-		* @return  >=0  成功\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
-		*/
-		my_ulonglong ExecuteSql( const char *szSql, std::string& Error );
-		/**
-	    * @brief
-	    执行SQL语句函数。
-
-	    * @param [in] const char *szSql  SQL语句\n
-	    * @param [out] std::string& Error  错误信息\n
-
-	    * @return 返回影响到的记录数量
-	    * @return  -1  失败\n
-	    * @return  >=0  成功\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		my_ulonglong ExecuteRealSql( const char *szSql, std::string& Error );
-		/**
-	    * @brief
-	    获得最后一次错误信息函数。
-
-	    * @param 无\n
-
-	    * @return 返回最后一次错误
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		const char* GetLastError();
-		/**
-	    * @brief
-	    获得最后一次插入的ID函数。
-
-	    * @param [out] std::string& Error  错误信息\n
-
-	    * @return 返回最后一次插入的ID
-	    * @return  -1  失败\n
-	    * @return  >=0  成功\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		my_ulonglong GetLastInsertID( std::string& Error );
-		/**
-		* @brief
-		获得是否是临时的连接函数。
-
-		* @param 无\n
-
-		* @return 返回是否是临时的
-
-		* @note
-
-		* @warning
-
-		* @bug
-
-		*/
-		bool IsTemp();
-		/**
-	    * @brief
-	    准备stmt函数。
-
-	    * @param const char *szSql 预处理SQL语句\n
-	    * @param [out] std::string& Error  错误信息\n
-
-	    * @return 返回准备stmt是否成功
-	    * @return  false  失败\n
-	    * @return  true  成功\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		bool Preparestmt( const char *szSql, std::string& Error );
-		/**
-	    * @brief
-	    执行stmt函数。
-
-	    * @param [in] MYSQL_BIND *stBinds  要执行的BIND\n
-	    * @param [in] uint64_t *piId  执行后获得的ID\n
-	    * @param [out] std::string& Error  错误信息\n
-
-	    * @return 返回执行stmt成功还是失败
-	    * @return  false  失败\n
-	    * @return  true  成功\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		bool stmtExcute( MYSQL_BIND *stBinds, uint64_t *piId, std::string& Error );
-		/**
-	    * @brief
-	    关闭stmt函数。
-
-	    * @param 无\n
-
-	    * @return 无\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		void stmtClose();
-	};
-}
+/**
+* @brief
+数据库连接类
+
+* @version
+V 1.0.0
+
+* @author
+王益俊
+
+* @date
+创建时间:  2018-04-17\n
+
+* @note
+2018-04-17  创建类。\n
+
+* @warning
+
+* @bug
+
+*/
+
+#pragma once
+
+#include <string>
+#include <mysql.h>
+#include "CDBResultSet.h"
+#include "CDBCommon.h"
+
+namespace YADB
+{
+	class CDBConnect
+	{
+	private:
+    	MYSQL * __pConn;//数据库连接
+    	CDBResultSet __RessultSet;//结果集
+    	bool __IsTemp;//是否是临时的(如果是临时使用的,归还到连接池中时会自动删除,以保持总体连接数不变)
+    	MYSQL_STMT *__pstmt;//预处理
+	public:
+		CDBConnect( bool IsTemp = false );
+		~CDBConnect();
+		/**
+		* @brief
+		连接数据库函数。
+
+		* @param  [in] const _DB_POOL_SETTING_& Setting  数据库连接设置\n
+		* @param  [out] std::string& Error  错误信息\n
+
+		* @return 返回连接数据库是否成功
+		* @return  true   成功\n
+		* @return  false  失败\n
+
+		* @note
+
+		* @warning
+
+		* @bug
+
+		*/
+		bool Connect( const _DB_CONN_SETTING_& DBSetting, std::string& Error );
+		/**
+	    * @brief
+	    关闭连接函数。
+
+	    * @param  无\n
+
+	    * @return  无\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		void Close();
+		/**
+	    * @brief
+	    测试连接函数。
+
+	    * @param [out] std::string& Error 错误信息\n
+
+	    * @return 返回测试连接是否成功
+	    * @return  0  连接测试成功\n
+	    * @return  !=0  连接测试失败\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		int ConnctionTest( std::string& Error );
+		/**
+	    * @brief
+	    设置自动提交的函数。
+
+	    * @param [in] bool Mode 是否是否自动提交\n
+	    * @param [out] std::string& Error 错误信息\n
+
+	    * @return 返回设置自动提交是否成功
+	    * @return  0  成功\n
+	    * @return  !=0  失败\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		int AutoCommit( bool Mode, std::string& Error );
+		/**
+	    * @brief
+	    提交函数。
+
+	    * @param [out] std::string& Error 错误信息\n
+
+	    * @return 返回提交是否成功
+	    * @return  0  成功\n
+	    * @return  !=0  失败\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		int Commit( std::string& Error  );
+		/**
+	    * @brief
+	    回滚函数。
+
+	    * @param [out] std::string& Error  错误信息\n
+
+	    * @return 返回回滚是否成功
+	    * @return  0  成功\n
+	    * @return  !=0  失败\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		int RollBack( std::string& Error );
+		/**
+	    * @brief
+	    执行SQL语句返回结果集函数。
+
+	    * @param [in] const char *szSql  SQL语句\n
+	    * @param [out] std::string& Error  错误信息\n
+
+	    * @return 返回结果集
+	    * @return  0  失败\n
+	    * @return  !=0  成功\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		CDBResultSet* ExecuteQuery( const char *szSql, std::string& Error );
+		/**
+	    * @brief
+	    执行SQL语句返回mysql原始结果集函数。
+
+	    * @param [in] const char *szSql  SQL语句\n
+	    * @param [out] std::string& Error  错误信息\n
+
+	    * @return 返回的mysql原始结果集
+	    * @return  ==0  失败\n
+	    * @return  !=0  成功\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		MYSQL_RES* Query( const char *szSql, std::string& Error );
+		/**
+		* @brief
+		执行SQL语句函数。
+
+		* @param [in] const char *szSql  SQL语句\n
+		* @param [out] std::string& Error  错误信息\n
+
+		* @return 返回影响到的记录数量
+		* @return  -1  失败\n
+		* @return  >=0  成功\n
+
+		* @note
+
+		* @warning
+
+		* @bug
+
+		*/
+		my_ulonglong ExecuteSql( const char *szSql, std::string& Error );
+		/**
+	    * @brief
+	    执行SQL语句函数。
+
+	    * @param [in] const char *szSql  SQL语句\n
+	    * @param [out] std::string& Error  错误信息\n
+
+	    * @return 返回影响到的记录数量
+	    * @return  -1  失败\n
+	    * @return  >=0  成功\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		my_ulonglong ExecuteRealSql( const char *szSql, std::string& Error );
+		/**
+	    * @brief
+	    获得最后一次错误信息函数。
+
+	    * @param 无\n
+
+	    * @return 返回最后一次错误
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		const char* GetLastError();
+		/**
+	    * @brief
+	    获得最后一次插入的ID函数。
+
+	    * @param [out] std::string& Error  错误信息\n
+
+	    * @return 返回最后一次插入的ID
+	    * @return  -1  失败\n
+	    * @return  >=0  成功\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		my_ulonglong GetLastInsertID( std::string& Error );
+		/**
+		* @brief
+		获得是否是临时的连接函数。
+
+		* @param 无\n
+
+		* @return 返回是否是临时的
+
+		* @note
+
+		* @warning
+
+		* @bug
+
+		*/
+		bool IsTemp();
+		/**
+	    * @brief
+	    准备stmt函数。
+
+	    * @param const char *szSql 预处理SQL语句\n
+	    * @param [out] std::string& Error  错误信息\n
+
+	    * @return 返回准备stmt是否成功
+	    * @return  false  失败\n
+	    * @return  true  成功\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		bool Preparestmt( const char *szSql, std::string& Error );
+		/**
+	    * @brief
+	    执行stmt函数。
+
+	    * @param [in] MYSQL_BIND *stBinds  要执行的BIND\n
+	    * @param [in] uint64_t *piId  执行后获得的ID\n
+	    * @param [out] std::string& Error  错误信息\n
+
+	    * @return 返回执行stmt成功还是失败
+	    * @return  false  失败\n
+	    * @return  true  成功\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		bool stmtExcute( MYSQL_BIND *stBinds, uint64_t *piId, std::string& Error );
+		/**
+	    * @brief
+	    关闭stmt函数。
+
+	    * @param 无\n
+
+	    * @return 无\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		void stmtClose();
+	};
+}

+ 45 - 45
db_api/CDBHelper.cpp

@@ -1,45 +1,45 @@
-#include "CDBHelper.h"
-#include <stdio.h>
-#include <string.h>
-
-namespace YADB
-{
-	CDBHelper::CDBHelper()
-	{
-
-	}
-
-	CDBHelper::~CDBHelper()
-	{
-
-	}
-
-	void CDBHelper::String2DateTime( const char* szString, MYSQL_TIME & value )
-	{
-		sscanf( szString, "%d-%d-%d %d:%d:%d", &value.year, &value.month, &value.day, &value.hour, &value.minute, &value.second );
-		value.second_part = 0;//microseconds
-		value.neg = false;
-		value.time_type = MYSQL_TIMESTAMP_DATE;
-	}
-
-	void CDBHelper::DateTime2String( const MYSQL_TIME & value, char * szString, int Length )
-	{
-		char szTemp[20] = { 0 };
-		sprintf( szTemp, "%d-%d-%d %02d:%02d:%02d", \
-			value.year, value.month, value.day, value.hour, value.minute, value.second );
-		strncpy( szString, szTemp, Length );
-	}
-
-	void CDBHelper::InitMySQLTime( MYSQL_TIME & value )
-	{
-		value.year         = 0;
-		value.month        = 0;
-		value.day          = 0;
-		value.hour         = 0;
-		value.minute       = 0;
-		value.second       = 0;
-		value.neg          = false;
-		value.second_part  = 0;
-		value.time_type    = MYSQL_TIMESTAMP_DATETIME;
-	}
-}
+#include "CDBHelper.h"
+#include <stdio.h>
+#include <string.h>
+
+namespace YADB
+{
+	CDBHelper::CDBHelper()
+	{
+
+	}
+
+	CDBHelper::~CDBHelper()
+	{
+
+	}
+
+	void CDBHelper::String2DateTime( const char* szString, MYSQL_TIME & value )
+	{
+		sscanf( szString, "%d-%d-%d %d:%d:%d", &value.year, &value.month, &value.day, &value.hour, &value.minute, &value.second );
+		value.second_part = 0;//microseconds
+		value.neg = false;
+		value.time_type = MYSQL_TIMESTAMP_DATE;
+	}
+
+	void CDBHelper::DateTime2String( const MYSQL_TIME & value, char * szString, int Length )
+	{
+		char szTemp[20] = { 0 };
+		sprintf( szTemp, "%d-%d-%d %02d:%02d:%02d", \
+			value.year, value.month, value.day, value.hour, value.minute, value.second );
+		strncpy( szString, szTemp, Length );
+	}
+
+	void CDBHelper::InitMySQLTime( MYSQL_TIME & value )
+	{
+		value.year         = 0;
+		value.month        = 0;
+		value.day          = 0;
+		value.hour         = 0;
+		value.minute       = 0;
+		value.second       = 0;
+		value.neg          = false;
+		value.second_part  = 0;
+		value.time_type    = MYSQL_TIMESTAMP_DATETIME;
+	}
+}

+ 86 - 86
db_api/CDBHelper.h

@@ -1,86 +1,86 @@
-/**
-* @brief
-数据库助手类
-
-* @version
-V 1.0.0
-
-* @author
-王益俊
-
-* @date
-创建时间:  2018-04-19\n
-
-* @note
-2018-04-19  创建类。\n
-
-* @warning
-
-* @bug
-
-*/
-
-#pragma once
-
-#include <mysql.h>
-
-namespace YADB
-{
-	class CDBHelper
-	{
-	public:
-		CDBHelper();
-		~CDBHelper();
-		/**
-	    * @brief
-	    把时间字符串转成MYSQL_TIME的函数。
-
-	    * @param  [in] char* szdtString  时间字符串\n
-	    * @param  [out] MYSQL_TIME & value  转换好的MYSQL_TIME类型的值\n
-
-	    * @return 无\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		void String2DateTime( const char* szString, MYSQL_TIME & value );
-		/**
-	    * @brief
-	    把MYSQL_TIME转成时间字符串转成函数。
-
-	    * @param  [in] MYSQL_TIME & value  要转换的MYSQL_TIME类型的值\n
-	    * @param  [out] char* szdtString  时间字符串\n
-
-	    * @return 无\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		void DateTime2String( const MYSQL_TIME & value, char* szString, int Length );
-		/**
-		* @brief
-		把MYSQL_TIME初始化函数。
-
-		* @param  [out] MYSQL_TIME & value  要初始化的MYSQL_TIME值\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
-		*/
-		void InitMySQLTime( MYSQL_TIME & value );
-	};
-
-}
+/**
+* @brief
+数据库助手类
+
+* @version
+V 1.0.0
+
+* @author
+王益俊
+
+* @date
+创建时间:  2018-04-19\n
+
+* @note
+2018-04-19  创建类。\n
+
+* @warning
+
+* @bug
+
+*/
+
+#pragma once
+
+#include <mysql.h>
+
+namespace YADB
+{
+	class CDBHelper
+	{
+	public:
+		CDBHelper();
+		~CDBHelper();
+		/**
+	    * @brief
+	    把时间字符串转成MYSQL_TIME的函数。
+
+	    * @param  [in] char* szdtString  时间字符串\n
+	    * @param  [out] MYSQL_TIME & value  转换好的MYSQL_TIME类型的值\n
+
+	    * @return 无\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		void String2DateTime( const char* szString, MYSQL_TIME & value );
+		/**
+	    * @brief
+	    把MYSQL_TIME转成时间字符串转成函数。
+
+	    * @param  [in] MYSQL_TIME & value  要转换的MYSQL_TIME类型的值\n
+	    * @param  [out] char* szdtString  时间字符串\n
+
+	    * @return 无\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		void DateTime2String( const MYSQL_TIME & value, char* szString, int Length );
+		/**
+		* @brief
+		把MYSQL_TIME初始化函数。
+
+		* @param  [out] MYSQL_TIME & value  要初始化的MYSQL_TIME值\n
+
+		* @return 无\n
+
+		* @note
+
+		* @warning
+
+		* @bug
+
+		*/
+		void InitMySQLTime( MYSQL_TIME & value );
+	};
+
+}

+ 451 - 451
db_api/CDBResultSet.cpp

@@ -1,451 +1,451 @@
-#include "CDBResultSet.h"
-#include <string.h>
-
-namespace YADB
-{
-	CDBResultSet::CDBResultSet()
-	{
-		__pQueryRes  = 0;
-		__CurRow     = 0;
-		__FieldCount = 0;
-	}
-
-	CDBResultSet::~CDBResultSet()
-	{
-		Close();
-	}
-
-	void CDBResultSet::__BuildFNameIdxLst()
-	{
-		if (0 == __pQueryRes)
-		{
-			return;
-		}
-
-		for ( int i = 0; i < __FieldCount; i++ )
-		{
-			MYSQL_FIELD *pField = mysql_fetch_field_direct( __pQueryRes, i );
-			if ( pField == NULL )
-			{
-				continue;
-			}
-
-			__FNameIndexList.insert( std::make_pair( pField->name, i ) );
-		}
-	}
-
-	bool CDBResultSet::Bind( MYSQL_RES *pQueryRes, std::string& Error )
-	{
-		Close();
-
-		__pQueryRes = pQueryRes;
-		if ( __pQueryRes != 0 )
-		{
-			__FieldCount = mysql_num_fields( pQueryRes );
-		}
-		else
-		{
-			Error = "Error,pQueryRes is null!";
-			__FieldCount = 0;
-			return false;
-		}
-
-		__BuildFNameIdxLst();
-
-		return true;
-	}
-
-	void CDBResultSet::Close()
-	{
-		if ( 0 == __pQueryRes )
-		{
-			return;
-		}
-
-		mysql_free_result( __pQueryRes );
-		__pQueryRes  = 0;
-		__CurRow     = 0;
-		__FieldCount = 0;
-		__FNameIndexList.clear();
-	}
-
-	bool CDBResultSet::SeekData( my_ulonglong Offset, std::string& Error )
-	{
-		if ( 0 == __pQueryRes )
-		{
-			Error = "Error,__pQueryRes is not initialized!";
-			return false;
-		}
-
-		mysql_data_seek( __pQueryRes, Offset );
-
-		return true;
-	}
-
-	bool CDBResultSet::GetNextRecod( std::string & Error )
-	{
-		if ( 0 == __pQueryRes )
-		{
-			Error = "Error,__pQueryRes is not initialized!";
-			return false;
-		}
-
-		if ( ( __CurRow = mysql_fetch_row( __pQueryRes ) ) != NULL )
-		{
-			return true;
-		}
-		else
-		{
-			Error = "Error, failed to mysql_fetch_row!";
-			return false;
-		}
-
-		return true;
-	}
-
-	my_ulonglong CDBResultSet::GetRecordCount( std::string & Error )
-	{
-		if ( 0 == __pQueryRes )
-		{
-			Error = "Error,__pQueryRes is not initialized!";
-			return DB_ERR_QUERY_RES_NOT_INITED;
-		}
-
-		return mysql_num_rows( __pQueryRes );
-	}
-
-	bool CDBResultSet::__GetField( int fdIndex, char*& szValue, bool& IsNull, std::string& Error )
-	{
-		IsNull = false;
-
-		if ( 0 == __CurRow )
-		{
-			Error = "Error,__CurRow is not initialized!";
-			return false;
-		}
-
-		if ( fdIndex < 0 || fdIndex >= __FieldCount )
-		{
-			Error = "fdIndex is invalid!";
-			return false;
-		}
-
-		szValue = __CurRow[fdIndex];
-		if ( szValue == NULL || szValue[0] == 0)
-		{
-			IsNull = true;
-		}
-
-		return true;
-	}
-
-	bool CDBResultSet::GetField( int fdIndex, int & value, std::string & Error )
-	{
-		value = 0;
-		char* pField = 0;
-		bool IsNull = false;
-        if ( !__GetField( fdIndex, pField, IsNull, Error ) )
-        {
-            return false;
-        }
-
-        if ( !IsNull )
-        {
-        	value = atoi( pField );
-        }
-
-		return true;
-	}
-
-	bool CDBResultSet::GetField( int fdIndex, unsigned int & value, std::string & Error )
-	{
-		value = 0;
-		char* pField = 0;
-		bool IsNull = false;
-		if ( !__GetField( fdIndex, pField, IsNull, Error ) )
-		{
-			return false;
-		}
-
-		if ( !IsNull )
-		{
-			value = atoi( pField );
-		}
-
-		return true;
-	}
-
-	bool CDBResultSet::GetField( int fdIndex, long long & value, std::string & Error )
-	{
-		value = 0;
-		char* pField = 0;
-		bool IsNull = false;
-		if ( !__GetField( fdIndex, pField, IsNull, Error ) )
-		{
-			return false;
-		}
-
-		if ( !IsNull )
-		{
-			value = atoll(pField);
-		}
-
-		return true;
-	}
-
-	bool CDBResultSet::GetField( int fdIndex, std::string & value, std::string & Error )
-	{
-		value = "";
-
-		char* pField = 0;
-		bool IsNull = false;
-		if ( !__GetField( fdIndex, pField, IsNull, Error ) )
-		{
-			return false;
-		}
-
-		if ( !IsNull )
-		{
-			value = pField;
-		}
-
-		return true;
-	}
-
-	bool CDBResultSet::GetField( int fdIndex, char * value, int Length, std::string & Error )
-	{
-		memset( value, 0, Length );
-
-		char* pField = 0;
-		bool IsNull = false;
-		if ( !__GetField( fdIndex, pField, IsNull, Error ) )
-		{
-			return false;
-		}
-
-		if ( !IsNull )
-		{
-			strncpy( value, pField, Length );
-		}
-
-		return true;
-	}
-
-	bool CDBResultSet::GetField( int fdIndex, bool & value, std::string & Error )
-	{
-		value = false;
-		char* pField = 0;
-		bool IsNull = false;
-		if ( !__GetField( fdIndex, pField, IsNull, Error ) )
-		{
-			return false;
-		}
-
-		if ( !IsNull )
-		{
-			value = atoi( pField );
-		}
-
-		return true;
-	}
-
-	bool CDBResultSet::GetField( int fdIndex, float & value, std::string & Error )
-	{
-		value = 0.0;
-		char* pField = 0;
-		bool IsNull = false;
-		if ( !__GetField( fdIndex, pField, IsNull, Error ) )
-		{
-			return false;
-		}
-
-		if ( !IsNull )
-		{
-			value = atof( pField );
-		}
-
-		return true;
-    }
-
-	bool CDBResultSet::GetField( int fdIndex, double & value, std::string & Error )
-	{
-		value = 0.0;
-		char* pField = 0;
-		bool IsNull = false;
-		if (!__GetField( fdIndex, pField, IsNull, Error ) )
-		{
-			return false;
-		}
-
-		if ( !IsNull )
-		{
-			value = std::stod( pField );
-		}
-
-		return true;
-	}
-
-	bool CDBResultSet::GetField( int fdIndex, MYSQL_TIME & value, std::string & Error )
-	{
-		__DBHelper.InitMySQLTime( value );
-		char* pField = 0;
-		bool IsNull = false;
-		if ( !__GetField( fdIndex, pField, IsNull, Error ) )
-		{
-			return false;
-		}
-
-		if ( !IsNull )
-		{
-			__DBHelper.String2DateTime( pField, value );
-		}
-
-		return true;
-	}
-
-	int CDBResultSet::GetFdIndexByName( const char * fdName, std::string & Error )
-	{
-		std::map<std::string,int>::iterator mit_fi;
-		mit_fi = __FNameIndexList.find( fdName );
-		if ( mit_fi != __FNameIndexList.end() )
-		{
-			return mit_fi->second;
-		}
-
-		Error = "Failed to find field: ";
-		Error += fdName;
-
-		return -1;
-	}
-
-	bool CDBResultSet::GetField( const char * fdName, int & value, std::string & Error )
-	{
-		int fdIndex = GetFdIndexByName( fdName, Error );
-
-		if ( fdIndex >= 0 )
-		{
-			return GetField( fdIndex, value, Error );
-		}
-
-		return false;
-	}
-
-	bool CDBResultSet::GetField( const char * fdName, long long & value, std::string & Error )
-	{
-		int fdIndex = GetFdIndexByName( fdName, Error );
-
-		if ( fdIndex >= 0 )
-		{
-			return GetField( fdIndex, value, Error );
-		}
-
-		return false;
-	}
-
-	bool CDBResultSet::GetField( const char * fdName, unsigned int & value, std::string & Error )
-	{
-		int fdIndex = GetFdIndexByName( fdName, Error );
-
-		if ( fdIndex >= 0 )
-		{
-			return GetField( fdIndex, value, Error );
-		}
-
-		return false;
-	}
-
-	bool CDBResultSet::GetField( const char * fdName, std::string & value, std::string & Error )
-	{
-		int fdIndex = GetFdIndexByName( fdName, Error );
-
-		if ( fdIndex >= 0 )
-		{
-			return GetField( fdIndex, value, Error );
-		}
-
-		return false;
-	}
-
-	bool CDBResultSet::GetField( const char * fdName, char * value, int Length, std::string & Error )
-	{
-		int fdIndex = GetFdIndexByName( fdName, Error );
-
-		if ( fdIndex >= 0 )
-		{
-			return GetField( fdIndex, value, Length, Error );
-		}
-
-		return false;
-	}
-
-	bool CDBResultSet::GetField( const char * fdName, bool & value, std::string & Error )
-	{
-		int fdIndex = GetFdIndexByName( fdName, Error );
-
-		if ( fdIndex >= 0 )
-		{
-			return GetField( fdIndex, value, Error );
-		}
-
-		return false;
-	}
-
-	bool CDBResultSet::GetField( const char * fdName, float & value, std::string & Error )
-	{
-		int fdIndex = GetFdIndexByName( fdName, Error );
-
-		if ( fdIndex >= 0 )
-		{
-			return GetField( fdIndex, value, Error );
-		}
-
-		return false;
-	}
-
-	bool CDBResultSet::GetField( const char * fdName, double & value, std::string & Error )
-	{
-		int fdIndex = GetFdIndexByName( fdName, Error );
-
-		if ( fdIndex >= 0 )
-		{
-			return GetField( fdIndex, value, Error );
-		}
-
-		return false;
-	}
-
-	bool CDBResultSet::GetField( const char * fdName, MYSQL_TIME & value, std::string & Error )
-	{
-		int fdIndex = GetFdIndexByName( fdName, Error );
-
-		if ( fdIndex >= 0 )
-		{
-			return GetField( fdIndex, value, Error );
-		}
-
-		return false;
-	}
-
-	int CDBResultSet::IsNull( int fdIndex, std::string& Error )
-	{
-		if (0 == __CurRow)
-		{
-			Error = "Error,__CurRow is not initialized!";
-			return -1;
-		}
-
-		if ( fdIndex < 0 || fdIndex >= __FieldCount )
-		{
-			Error = "fdIndex is invalid!";
-			return -1;
-		}
-
-		char* pField = __CurRow[fdIndex];
-		if ( pField == NULL || pField[0] == 0 )
-		{
-			return 1;
-		}
-
-		return 0;
-	}
-}
+#include "CDBResultSet.h"
+#include <string.h>
+
+namespace YADB
+{
+	CDBResultSet::CDBResultSet()
+	{
+		__pQueryRes  = 0;
+		__CurRow     = 0;
+		__FieldCount = 0;
+	}
+
+	CDBResultSet::~CDBResultSet()
+	{
+		Close();
+	}
+
+	void CDBResultSet::__BuildFNameIdxLst()
+	{
+		if (0 == __pQueryRes)
+		{
+			return;
+		}
+
+		for ( int i = 0; i < __FieldCount; i++ )
+		{
+			MYSQL_FIELD *pField = mysql_fetch_field_direct( __pQueryRes, i );
+			if ( pField == NULL )
+			{
+				continue;
+			}
+
+			__FNameIndexList.insert( std::make_pair( pField->name, i ) );
+		}
+	}
+
+	bool CDBResultSet::Bind( MYSQL_RES *pQueryRes, std::string& Error )
+	{
+		Close();
+
+		__pQueryRes = pQueryRes;
+		if ( __pQueryRes != 0 )
+		{
+			__FieldCount = mysql_num_fields( pQueryRes );
+		}
+		else
+		{
+			Error = "Error,pQueryRes is null!";
+			__FieldCount = 0;
+			return false;
+		}
+
+		__BuildFNameIdxLst();
+
+		return true;
+	}
+
+	void CDBResultSet::Close()
+	{
+		if ( 0 == __pQueryRes )
+		{
+			return;
+		}
+
+		mysql_free_result( __pQueryRes );
+		__pQueryRes  = 0;
+		__CurRow     = 0;
+		__FieldCount = 0;
+		__FNameIndexList.clear();
+	}
+
+	bool CDBResultSet::SeekData( my_ulonglong Offset, std::string& Error )
+	{
+		if ( 0 == __pQueryRes )
+		{
+			Error = "Error,__pQueryRes is not initialized!";
+			return false;
+		}
+
+		mysql_data_seek( __pQueryRes, Offset );
+
+		return true;
+	}
+
+	bool CDBResultSet::GetNextRecod( std::string & Error )
+	{
+		if ( 0 == __pQueryRes )
+		{
+			Error = "Error,__pQueryRes is not initialized!";
+			return false;
+		}
+
+		if ( ( __CurRow = mysql_fetch_row( __pQueryRes ) ) != NULL )
+		{
+			return true;
+		}
+		else
+		{
+			Error = "Error, failed to mysql_fetch_row!";
+			return false;
+		}
+
+		return true;
+	}
+
+	my_ulonglong CDBResultSet::GetRecordCount( std::string & Error )
+	{
+		if ( 0 == __pQueryRes )
+		{
+			Error = "Error,__pQueryRes is not initialized!";
+			return DB_ERR_QUERY_RES_NOT_INITED;
+		}
+
+		return mysql_num_rows( __pQueryRes );
+	}
+
+	bool CDBResultSet::__GetField( int fdIndex, char*& szValue, bool& IsNull, std::string& Error )
+	{
+		IsNull = false;
+
+		if ( 0 == __CurRow )
+		{
+			Error = "Error,__CurRow is not initialized!";
+			return false;
+		}
+
+		if ( fdIndex < 0 || fdIndex >= __FieldCount )
+		{
+			Error = "fdIndex is invalid!";
+			return false;
+		}
+
+		szValue = __CurRow[fdIndex];
+		if ( szValue == NULL || szValue[0] == 0)
+		{
+			IsNull = true;
+		}
+
+		return true;
+	}
+
+	bool CDBResultSet::GetField( int fdIndex, int & value, std::string & Error )
+	{
+		value = 0;
+		char* pField = 0;
+		bool IsNull = false;
+        if ( !__GetField( fdIndex, pField, IsNull, Error ) )
+        {
+            return false;
+        }
+
+        if ( !IsNull )
+        {
+        	value = atoi( pField );
+        }
+
+		return true;
+	}
+
+	bool CDBResultSet::GetField( int fdIndex, unsigned int & value, std::string & Error )
+	{
+		value = 0;
+		char* pField = 0;
+		bool IsNull = false;
+		if ( !__GetField( fdIndex, pField, IsNull, Error ) )
+		{
+			return false;
+		}
+
+		if ( !IsNull )
+		{
+			value = atoi( pField );
+		}
+
+		return true;
+	}
+
+	bool CDBResultSet::GetField( int fdIndex, long long & value, std::string & Error )
+	{
+		value = 0;
+		char* pField = 0;
+		bool IsNull = false;
+		if ( !__GetField( fdIndex, pField, IsNull, Error ) )
+		{
+			return false;
+		}
+
+		if ( !IsNull )
+		{
+			value = atoll(pField);
+		}
+
+		return true;
+	}
+
+	bool CDBResultSet::GetField( int fdIndex, std::string & value, std::string & Error )
+	{
+		value = "";
+
+		char* pField = 0;
+		bool IsNull = false;
+		if ( !__GetField( fdIndex, pField, IsNull, Error ) )
+		{
+			return false;
+		}
+
+		if ( !IsNull )
+		{
+			value = pField;
+		}
+
+		return true;
+	}
+
+	bool CDBResultSet::GetField( int fdIndex, char * value, int Length, std::string & Error )
+	{
+		memset( value, 0, Length );
+
+		char* pField = 0;
+		bool IsNull = false;
+		if ( !__GetField( fdIndex, pField, IsNull, Error ) )
+		{
+			return false;
+		}
+
+		if ( !IsNull )
+		{
+			strncpy( value, pField, Length );
+		}
+
+		return true;
+	}
+
+	bool CDBResultSet::GetField( int fdIndex, bool & value, std::string & Error )
+	{
+		value = false;
+		char* pField = 0;
+		bool IsNull = false;
+		if ( !__GetField( fdIndex, pField, IsNull, Error ) )
+		{
+			return false;
+		}
+
+		if ( !IsNull )
+		{
+			value = atoi( pField );
+		}
+
+		return true;
+	}
+
+	bool CDBResultSet::GetField( int fdIndex, float & value, std::string & Error )
+	{
+		value = 0.0;
+		char* pField = 0;
+		bool IsNull = false;
+		if ( !__GetField( fdIndex, pField, IsNull, Error ) )
+		{
+			return false;
+		}
+
+		if ( !IsNull )
+		{
+			value = atof( pField );
+		}
+
+		return true;
+    }
+
+	bool CDBResultSet::GetField( int fdIndex, double & value, std::string & Error )
+	{
+		value = 0.0;
+		char* pField = 0;
+		bool IsNull = false;
+		if (!__GetField( fdIndex, pField, IsNull, Error ) )
+		{
+			return false;
+		}
+
+		if ( !IsNull )
+		{
+			value = std::stod( pField );
+		}
+
+		return true;
+	}
+
+	bool CDBResultSet::GetField( int fdIndex, MYSQL_TIME & value, std::string & Error )
+	{
+		__DBHelper.InitMySQLTime( value );
+		char* pField = 0;
+		bool IsNull = false;
+		if ( !__GetField( fdIndex, pField, IsNull, Error ) )
+		{
+			return false;
+		}
+
+		if ( !IsNull )
+		{
+			__DBHelper.String2DateTime( pField, value );
+		}
+
+		return true;
+	}
+
+	int CDBResultSet::GetFdIndexByName( const char * fdName, std::string & Error )
+	{
+		std::map<std::string,int>::iterator mit_fi;
+		mit_fi = __FNameIndexList.find( fdName );
+		if ( mit_fi != __FNameIndexList.end() )
+		{
+			return mit_fi->second;
+		}
+
+		Error = "Failed to find field: ";
+		Error += fdName;
+
+		return -1;
+	}
+
+	bool CDBResultSet::GetField( const char * fdName, int & value, std::string & Error )
+	{
+		int fdIndex = GetFdIndexByName( fdName, Error );
+
+		if ( fdIndex >= 0 )
+		{
+			return GetField( fdIndex, value, Error );
+		}
+
+		return false;
+	}
+
+	bool CDBResultSet::GetField( const char * fdName, long long & value, std::string & Error )
+	{
+		int fdIndex = GetFdIndexByName( fdName, Error );
+
+		if ( fdIndex >= 0 )
+		{
+			return GetField( fdIndex, value, Error );
+		}
+
+		return false;
+	}
+
+	bool CDBResultSet::GetField( const char * fdName, unsigned int & value, std::string & Error )
+	{
+		int fdIndex = GetFdIndexByName( fdName, Error );
+
+		if ( fdIndex >= 0 )
+		{
+			return GetField( fdIndex, value, Error );
+		}
+
+		return false;
+	}
+
+	bool CDBResultSet::GetField( const char * fdName, std::string & value, std::string & Error )
+	{
+		int fdIndex = GetFdIndexByName( fdName, Error );
+
+		if ( fdIndex >= 0 )
+		{
+			return GetField( fdIndex, value, Error );
+		}
+
+		return false;
+	}
+
+	bool CDBResultSet::GetField( const char * fdName, char * value, int Length, std::string & Error )
+	{
+		int fdIndex = GetFdIndexByName( fdName, Error );
+
+		if ( fdIndex >= 0 )
+		{
+			return GetField( fdIndex, value, Length, Error );
+		}
+
+		return false;
+	}
+
+	bool CDBResultSet::GetField( const char * fdName, bool & value, std::string & Error )
+	{
+		int fdIndex = GetFdIndexByName( fdName, Error );
+
+		if ( fdIndex >= 0 )
+		{
+			return GetField( fdIndex, value, Error );
+		}
+
+		return false;
+	}
+
+	bool CDBResultSet::GetField( const char * fdName, float & value, std::string & Error )
+	{
+		int fdIndex = GetFdIndexByName( fdName, Error );
+
+		if ( fdIndex >= 0 )
+		{
+			return GetField( fdIndex, value, Error );
+		}
+
+		return false;
+	}
+
+	bool CDBResultSet::GetField( const char * fdName, double & value, std::string & Error )
+	{
+		int fdIndex = GetFdIndexByName( fdName, Error );
+
+		if ( fdIndex >= 0 )
+		{
+			return GetField( fdIndex, value, Error );
+		}
+
+		return false;
+	}
+
+	bool CDBResultSet::GetField( const char * fdName, MYSQL_TIME & value, std::string & Error )
+	{
+		int fdIndex = GetFdIndexByName( fdName, Error );
+
+		if ( fdIndex >= 0 )
+		{
+			return GetField( fdIndex, value, Error );
+		}
+
+		return false;
+	}
+
+	int CDBResultSet::IsNull( int fdIndex, std::string& Error )
+	{
+		if (0 == __CurRow)
+		{
+			Error = "Error,__CurRow is not initialized!";
+			return -1;
+		}
+
+		if ( fdIndex < 0 || fdIndex >= __FieldCount )
+		{
+			Error = "fdIndex is invalid!";
+			return -1;
+		}
+
+		char* pField = __CurRow[fdIndex];
+		if ( pField == NULL || pField[0] == 0 )
+		{
+			return 1;
+		}
+
+		return 0;
+	}
+}

File diff suppressed because it is too large
+ 573 - 573
db_api/CDBResultSet.h


+ 28 - 6
main.cpp

@@ -17,8 +17,12 @@
 #include"module_service/module_mgr.h"
 
 #include <config_file.h>
-
+#include "three_rates.h"
 config_file config;
+static void handlereader(uint32_t readerid,bool duration,uint32_t t)
+{
+
+}
 struct Init_Setting
 {
     void init()
@@ -38,7 +42,7 @@ struct Init_Setting
         DBSetting.DBName = config.get("db.dbname","yaxt");
         DBSetting.CharSet = config.get("db.charset","utf8");
         DBSetting.TimeOut = config.get("db.conn_timeout",5);
-        DBSetting.PoolSize = config.get("db.conn_timeout",30);
+        DBSetting.PoolSize = config.get("db.pool_size",30);
         _mysql_init(DBSetting);
 
         sit_list::instance()->load_from_db();
@@ -62,12 +66,29 @@ struct Init_Setting
 
         card_list::instance()->load_his_card_postion_from_db();
         event_list::instance()->load_his_data_from_db();
+		db_para dp;	
+
+        dp.Host=DBSetting.Host;
+        dp.User=DBSetting.User;
+        dp.PWD=DBSetting.PWD ;
+        dp.DBName=DBSetting.DBName;
+        dp.CharSet=DBSetting.CharSet ;
+        dp.TimeOut=DBSetting.TimeOut;
+        dp.PoolSize=DBSetting.PoolSize;
+		init_three_rates(dp);
     }
-
+	void init_three_rates(const db_para& dbs)
+	{
+		std_info("three_rates ...................");
+		init_para ip;
+		ip.send_pt = handlereader;
+		three_rates::get_instance()->init(ip,dbs);
+		three_rates::get_instance()->start();
+	}
     void _mysql_init(YADB::_DB_POOL_SETTING_ &dps)
     {
         std::string Error;
-
+		//std::this_thread::sleep_for(std::chrono::seconds(2));
         //创建连接池
         if ( !sDBConnPool.Create( dps, Error ) )
         {
@@ -149,8 +170,6 @@ int main(int argc ,char * argv[])
         return 0;
     }
 
-    module_mgr::start();
-    atexit(&cleanup);
 
     log_init("../etc/log.ini");
     if(config.open("../etc/config.ini"))
@@ -161,6 +180,9 @@ int main(int argc ,char * argv[])
 	card_path::init();
 	test_find_path(point(4717,209.2),point(4727,206.4));
 
+    module_mgr::start();
+    atexit(&cleanup);
+
 
     net_service mh;
     int port=config.get("service.port",4000);

+ 4 - 1
message.cpp

@@ -31,7 +31,10 @@ void message_locinfo::load(zistream&is,bool tdoa)
 
 	//加速度
 	is>>b;
-	m_acc=((b&0x80)?-1.:1.)*(b&0x7f)*98;
+	if(m_card_type==4 || m_card_type==5)
+		m_acc=b;
+	else
+		m_acc=((b&0x80)?-1.:1.)*(b&0x7f)*98;
 
 	//TOF
 	is>>b>>i;