From 9f5973fe8088d6e1d95672ef35caa9f08f210b1e Mon Sep 17 00:00:00 2001
From: znone <glyc@sina.com.cn>
Date: Fri, 23 Apr 2021 14:56:00 +0000
Subject: [PATCH] 1. function query_first return bool 2. fix some bugs.

---
 include/qtl_sqlite.hpp   |    6 +-
 include/qtl_common.hpp   |   49 ++++++++++++++++--------
 include/qtl_postgres.hpp |   49 +++++++++++++++++++++++-
 include/qtl_asio.hpp     |    3 +
 4 files changed, 86 insertions(+), 21 deletions(-)

diff --git a/include/qtl_asio.hpp b/include/qtl_asio.hpp
index 9bfe86a..463a90a 100644
--- a/include/qtl_asio.hpp
+++ b/include/qtl_asio.hpp
@@ -4,6 +4,9 @@
 #include "qtl_async.hpp"
 #include <asio/version.hpp>
 #define ASIO_STANDALONE
+#if defined(_MSC_VER) && _WIN32_WINNT<0x0600
+#define ASIO_ENABLE_CANCELIO 1 
+#endif
 #if ASIO_VERSION < 101200
 #include <asio/io_service.hpp>
 #else
diff --git a/include/qtl_common.hpp b/include/qtl_common.hpp
index e447f37..c6300c6 100644
--- a/include/qtl_common.hpp
+++ b/include/qtl_common.hpp
@@ -1083,55 +1083,67 @@
 	}
 
 	template<typename Params, typename Values>
-	base_database& query_first(const char* query_text, size_t text_length, const Params& params, Values&& values)
+	bool query_first(const char* query_text, size_t text_length, const Params& params, Values&& values)
 	{
-		return query_explicit(query_text, text_length, params, std::forward<Values>(values), first_record());
+		first_record fetcher;
+		query_explicit(query_text, text_length, params, std::forward<Values>(values), std::ref(fetcher));
+		return fetcher;
 	}
 
 	template<typename Params, typename Values>
-	base_database& query_first(const char* query_text, const Params& params, Values&& values)
+	bool query_first(const char* query_text, const Params& params, Values&& values)
 	{
-		return query_explicit(query_text, strlen(query_text), params, std::forward<Values>(values), first_record());
+		first_record fetcher;
+		query_explicit(query_text, strlen(query_text), params, std::forward<Values>(values), std::ref(fetcher));
+		return fetcher;
 	}
 
 	template<typename Params, typename Values>
-	base_database& query_first(const std::string& query_text, const Params& params, Values&& values)
+	bool query_first(const std::string& query_text, const Params& params, Values&& values)
 	{
-		return query_explicit(query_text, params, values, first_record());
+		first_record fetcher;
+		return query_explicit(query_text, params, values, std::ref(fetcher));
+		return fetcher;
 	}
 
 	template<typename Values>
-	base_database& query_first(const char* query_text, size_t text_length, Values&& values)
+	bool query_first(const char* query_text, size_t text_length, Values&& values)
 	{
-		return query_explicit(query_text, text_length, std::make_tuple(), std::forward<Values>(values), first_record());
+		first_record fetcher;
+		return query_explicit(query_text, text_length, std::make_tuple(), std::forward<Values>(values), std::ref(fetcher));
+		return fetcher;
 	}
 
 	template<typename Values>
-	base_database& query_first(const char* query_text, Values&& values)
+	bool query_first(const char* query_text, Values&& values)
 	{
-		return query_explicit(query_text, strlen(query_text), std::make_tuple(), std::forward<Values>(values), first_record());
+		first_record fetcher;
+		return query_explicit(query_text, strlen(query_text), std::make_tuple(), std::forward<Values>(values), std::ref(fetcher));
+		return fetcher;
 	}
 
 	template<typename Values>
-	base_database& query_first(const std::string& query_text, Values&& values)
+	bool query_first(const std::string& query_text, Values&& values)
 	{
-		return query_explicit(query_text, std::make_tuple(), std::forward<Values>(values), first_record());
+		first_record fetcher;
+		return query_explicit(query_text, std::make_tuple(), std::forward<Values>(values), std::ref(fetcher));
+		return fetcher;
 	}
 
 	template<typename... Values>
-	base_database& query_first_direct(const char* query_text, size_t text_length, Values&... values)
+	bool query_first_direct(const char* query_text, size_t text_length, Values&... values)
 	{
 		return query_first(query_text, text_length, std::tie(values...));
 	}
 
 	template<typename... Values>
-	base_database& query_first_direct(const char* query_text, Values&... values)
+	bool query_first_direct(const char* query_text, Values&... values)
 	{
 		return query_first(query_text, std::tie(values...));
 	}
 
 	template<typename... Values>
-	base_database& query_first_direct(const std::string& query_text, Values&... values)
+	bool query_first_direct(const std::string& query_text, Values&... values)
 	{
 		return query_first(query_text, std::tie(values...));
 	}
@@ -1143,7 +1155,12 @@
 	};
 	struct first_record 
 	{
-		template<typename... Values> bool operator()(Values&&...) const {  return false; }
+		first_record() : _found(false) { }
+		template<typename... Values> bool operator()(Values&&...) { _found = true; return false; }
+		operator bool() const { return _found; }
+
+	private:
+		bool _found;
 	};
 };
 
diff --git a/include/qtl_postgres.hpp b/include/qtl_postgres.hpp
index 3418c22..067e20e 100644
--- a/include/qtl_postgres.hpp
+++ b/include/qtl_postgres.hpp
@@ -27,7 +27,7 @@
 
 extern "C"
 {
-#include <postgres.h>
+#include <c.h>
 #include <catalog/pg_type.h>
 }
 
@@ -59,6 +59,41 @@
 #endif
 #ifdef unlink
 #undef unlink
+#endif
+
+#if defined(_WIN32) && _WIN32_WINNT < 0x0601
+
+#ifdef _M_IX86
+
+#define _WS2_32_WINSOCK_SWAP_LONGLONG(l)            \
+            ( ( ((l) >> 56) & 0x00000000000000FFLL ) |       \
+              ( ((l) >> 40) & 0x000000000000FF00LL ) |       \
+              ( ((l) >> 24) & 0x0000000000FF0000LL ) |       \
+              ( ((l) >>  8) & 0x00000000FF000000LL ) |       \
+              ( ((l) <<  8) & 0x000000FF00000000LL ) |       \
+              ( ((l) << 24) & 0x0000FF0000000000LL ) |       \
+              ( ((l) << 40) & 0x00FF000000000000LL ) |       \
+              ( ((l) << 56) & 0xFF00000000000000LL ) )
+
+#ifndef htonll
+__inline unsigned __int64 htonll(unsigned __int64 Value)
+{
+	const unsigned __int64 Retval = _WS2_32_WINSOCK_SWAP_LONGLONG(Value);
+	return Retval;
+}
+#endif /* htonll */
+
+#ifndef ntohll
+__inline unsigned __int64 ntohll(unsigned __int64 Value)
+{
+	const unsigned __int64 Retval = _WS2_32_WINSOCK_SWAP_LONGLONG(Value);
+	return Retval;
+}
+
+#endif /* ntohll */
+
+#endif
+
 #endif
 
 namespace qtl
@@ -793,6 +828,10 @@
 {
 };
 
+template<> struct object_traits<Oid> : public integral_traits<Oid, OIDOID, OIDARRAYOID>
+{
+};
+
 template<typename T>
 struct text_traits : public base_object_traits<T, TEXTOID>
 {
@@ -1016,7 +1055,7 @@
 		array_header* header = reinterpret_cast<array_header*>(buffer.data()+n);
 		header->ndim = detail::hton(1);
 		header->flags = detail::hton(0);
-		header->elemtype = detail::hton(static_cast<int32_t>(object_traits<T>::type));
+		header->elemtype = detail::hton(static_cast<int32_t>(object_traits<T>::type_id));
 		header->dims[0].length = detail::hton(static_cast<int32_t>(v.size()));
 		header->dims[0].lower_bound = detail::hton(1);
 
@@ -1854,6 +1893,12 @@
 		return (encoding >= 0) ? pg_encoding_to_char(encoding) : nullptr;
 	}
 
+	void encoding(const char* encoding)
+	{
+		if (PQsetClientEncoding(m_conn, encoding))
+			throw error(m_conn);
+	}
+
 	void trace(FILE* stream)
 	{
 		PQtrace(m_conn, stream);
diff --git a/include/qtl_sqlite.hpp b/include/qtl_sqlite.hpp
index 92c2c43..3a0482f 100644
--- a/include/qtl_sqlite.hpp
+++ b/include/qtl_sqlite.hpp
@@ -976,7 +976,7 @@
 		return const_cast<blobbuf*>(&m_buffer);
 	}
 
-	std::streamoff blob_size() const { return m_buffer.blob_size(); }
+	std::streamoff blob_size() const { return m_buffer.size(); }
 
 private:
 	blobbuf m_buffer;
@@ -1034,7 +1034,7 @@
 		return const_cast<blobbuf*>(&m_buffer);
 	}
 
-	std::streamoff blob_size() const { return m_buffer.blob_size(); }
+	std::streamoff blob_size() const { return m_buffer.size(); }
 
 private:
 	blobbuf m_buffer;
@@ -1092,7 +1092,7 @@
 		return const_cast<blobbuf*>(&m_buffer);
 	}
 
-	std::streamoff blob_size() const { return m_buffer.blob_size(); }
+	std::streamoff blob_size() const { return m_buffer.size(); }
 
 private:
 	blobbuf m_buffer;

--
Gitblit v1.9.3