From 16519fab3d99ca277bce00d2bcbfd46b33a46391 Mon Sep 17 00:00:00 2001
From: znone <glyc@sina.com.cn>
Date: Sun, 16 Jun 2019 13:27:41 +0000
Subject: [PATCH] 通过 MariaDB 的非阻塞函数访问 MySQL。

---
 include/qtl_mysql_pool.hpp |   43 ++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 42 insertions(+), 1 deletions(-)

diff --git a/include/qtl_mysql_pool.hpp b/include/qtl_mysql_pool.hpp
index 53c5a4e..9dc8948 100644
--- a/include/qtl_mysql_pool.hpp
+++ b/include/qtl_mysql_pool.hpp
@@ -18,11 +18,14 @@
 	virtual database* new_database() throw() override
 	{
 		database* db=new database;
-		db->charset_name("utf8");
 		if(!db->open(m_host.data(), m_user.data(), m_password.data(), m_database.data(), 0, m_port))
 		{
 			delete db;
 			db=NULL;
+		}
+		else
+		{
+			db->charset_name("utf8");
 		}
 		return db;
 	}
@@ -35,6 +38,44 @@
 	std::string m_password;
 };
 
+#if MARIADB_VERSION_ID >= 050500
+
+template<typename EventLoop>
+class async_pool : public qtl::async_pool<async_pool<EventLoop>, EventLoop, async_connection>
+{
+	typedef qtl::async_pool<async_pool<EventLoop>, EventLoop, async_connection> base_class;
+public:
+	async_pool(EventLoop& ev) : base_class(ev), m_port(0) { }
+	virtual ~async_pool() { }
+
+	template<typename Handler>
+	void new_connection(EventLoop& ev, Handler&& handler) throw()
+	{
+		async_connection* db = new async_connection;
+		db->open(ev, [this, handler, db](const mysql::error& e) mutable {
+			if (e)
+			{
+				delete db;
+				db = nullptr;
+			}
+			else
+			{
+				db->charset_name("utf8");
+			}
+			handler(e, db);
+		}, m_host.data(), m_user.data(), m_password.data(), m_database.data(), 0, m_port);
+	}
+
+protected:
+	std::string m_host;
+	unsigned short m_port;
+	std::string m_database;
+	std::string m_user;
+	std::string m_password;
+};
+
+#endif //MariaDB
+
 }
 
 }

--
Gitblit v1.9.3