From 9b81cdebba95f1b6e687191c630c1f8f6cf0df16 Mon Sep 17 00:00:00 2001
From: znone <glyc@sina.com.cn>
Date: Thu, 25 Mar 2021 12:44:30 +0000
Subject: [PATCH] PostgreSQL: Support asynchronous operation Fix bug in different versions of asio

---
 include/qtl_async.hpp |   41 ++++++++++++-----------------------------
 1 files changed, 12 insertions(+), 29 deletions(-)

diff --git a/include/qtl_async.hpp b/include/qtl_async.hpp
index ec877ea..eeb98d5 100644
--- a/include/qtl_async.hpp
+++ b/include/qtl_async.hpp
@@ -163,24 +163,14 @@
 		pThis->open_command(query_text, text_length, [handler, params](const typename T::exception_type& e, std::shared_ptr<Command>& command) mutable {
 			if(e)
 			{
-				handler(e, 0);
-				command->close([command, handler](const typename T::exception_type& e) mutable {
-					if(e) handler(e, 0);
+				command->close([command, e, handler](const typename T::exception_type& ae) mutable {
+					handler(e ? e : ae, 0);
 				});
 				return;
 			}
 			command->execute(params, [command, handler](const typename T::exception_type& e, uint64_t affected) mutable {
-				if(e)
-				{
-					handler(e, 0);
-					command->close([command, handler](const typename T::exception_type& e) mutable {
-						if(e) handler(e, 0);
-					});
-					return;
-				}
-				handler(typename T::exception_type(), affected);
-				command->close([command, handler](const typename T::exception_type& e) mutable {
-					if(e) handler(e, 0);
+				command->close([command, handler, e, affected](const typename T::exception_type& ae) mutable {
+					handler(e ? e : ae, affected);
 				});
 			});
 		});
@@ -227,25 +217,18 @@
 		pThis->open_command(query_text, text_length, [handler, params](const typename T::exception_type& e, std::shared_ptr<Command>& command) {
 			if(e)
 			{
-				handler(e, 0);
+				command->close([command, e, handler](const typename T::exception_type& ae) mutable {
+					handler(e ? e : ae, 0);
+				});
 			}
 			else
 			{
 				command->execute(params, [command, handler](const typename T::exception_type& e, uint64_t affected) {
-					if(e)
-					{
-						handler(e, 0);
-					}
-					else if (affected > 0)
-					{
-						handler(typename T::exception_type(), command->insert_id());
-					}
-					else
-					{
-						handler(typename T::exception_type(), 0);
-					}
-					command->close([handler](const typename T::exception_type& e) {
-						handler(e, 0);
+					auto insert_id = 0;
+					if(!e && affected>0)
+						insert_id  = command->insert_id();
+					command->close([command, handler, e, insert_id](const typename T::exception_type& ae) mutable {
+						handler(e ? e : ae, insert_id);
 					});
 				});
 			}

--
Gitblit v1.9.3