From 9d6e4318302b5967227ab8899bf29eb44873610d Mon Sep 17 00:00:00 2001
From: znone <glyc@sina.com.cn>
Date: Thu, 07 Dec 2017 06:58:11 +0000
Subject: [PATCH] 修正GCC下的编译错误
---
include/qtl_sqlite.hpp | 1
include/qtl_common.hpp | 57 ++++++++++++++----
include/qtl_mysql.hpp | 63 ++++++++++++---------
3 files changed, 81 insertions(+), 40 deletions(-)
diff --git a/include/qtl_common.hpp b/include/qtl_common.hpp
index 6966a07..df0c144 100644
--- a/include/qtl_common.hpp
+++ b/include/qtl_common.hpp
@@ -96,23 +96,26 @@
return bind_string_helper<string_type>(std::forward<string_type>(value));
}
+template<typename Command>
+inline void bind_param(Command& command, size_t index, const std::string& param)
+{
+ command.bind_param(index, param.data(), param.size());
+}
+
+template<typename Command>
+inline void bind_param(Command& command, size_t index, std::istream& param)
+{
+ command.bind_param(index, param);
+}
+
template<typename Command, typename T>
inline void bind_param(Command& command, size_t index, const T& param)
{
command.bind_param(index, param);
}
-template<typename Command, typename T>
-inline void bind_field(Command& command, size_t index, T& value)
-{
- bind_field(command, index, std::forward<T>(value));
-}
-
-template<typename Command, typename T>
-inline void bind_field(Command& command, size_t index, T&& value)
-{
- command.bind_field(index, std::forward<typename std::remove_reference<T>::type>(value));
-}
+// The order of the overloaded functions 'bind_field' is very important
+// The version with the most generic parameters is at the end
template<typename Command, size_t N>
inline void bind_field(Command& command, size_t index, char (&value)[N])
@@ -160,6 +163,34 @@
inline void bind_field(Command& command, size_t index, std::vector<wchar_t>&& value)
{
command.bind_field(index, bind_string(std::forward<std::vector<wchar_t>>(value)));
+}
+
+template<typename Command, typename T, typename=typename std::enable_if<!std::is_reference<T>::value>::type>
+inline void bind_field(Command& command, size_t index, T&& value)
+{
+ command.bind_field(index, std::forward<T>(value));
+}
+
+template<typename Command, typename T>
+inline void bind_field(Command& command, size_t index, T& value)
+{
+ bind_field(command, index, std::forward<T>(value));
+}
+
+template<typename FieldType, typename Command, typename BindType>
+inline void bind_field(Command& command, size_t index, BindType& value)
+{
+ FieldType temp=FieldType();
+ bind_field(command, index, temp);
+ value=static_cast<BindType>(temp);
+}
+
+template<typename FieldType, typename Command, typename BindType, typename CastFun>
+inline void bind_field(Command& command, size_t index, BindType& value, CastFun&& fun)
+{
+ FieldType temp=FieldType();
+ bind_field(command, index, temp);
+ fun(value, temp);
}
namespace detail
@@ -322,7 +353,7 @@
explicit bind_helper(Command& command) : m_command(command) { }
void operator()(const std::tuple<Types...>& params) const
{
- m_command.bind_param(N-1, std::get<N-1>(params));
+ bind_param(m_command, N-1, std::get<N-1>(params));
(bind_helper<Command, N-1, Types...>(m_command))(params);
}
void operator()(std::tuple<Types...>&& params) const
@@ -343,7 +374,7 @@
explicit bind_helper(Command& command) : m_command(command) { }
void operator()(const std::tuple<Types...>& params) const
{
- m_command.bind_param(0, std::get<0>(params));
+ bind_param(m_command, 0, std::get<0>(params));
}
void operator()(std::tuple<Types...>&& params) const
{
diff --git a/include/qtl_mysql.hpp b/include/qtl_mysql.hpp
index c853a32..3d59a8b 100644
--- a/include/qtl_mysql.hpp
+++ b/include/qtl_mysql.hpp
@@ -176,9 +176,10 @@
binder.bind(v);
}
-inline void bind(binder& binder, const char* str)
+inline void bind(binder& binder, const char* str, size_t length=0)
{
- binder.bind((char*)str, (unsigned long)strlen(str));
+ if(length==0) length=strlen(str);
+ binder.bind(const_cast<char*>(str), static_cast<unsigned long>(length), MYSQL_TYPE_VAR_STRING);
}
class statement;
@@ -288,6 +289,35 @@
return fetch();
}
+ void bind_param(size_t index, const char* param, size_t length)
+ {
+ bind(m_binders[index], param, length);
+ }
+
+ void bind_param(size_t index, const std::nullptr_t&)
+ {
+ m_binders[index].bind();
+ }
+
+ void bind_param(size_t index, std::istream& param)
+ {
+ m_binders[index].bind(NULL, 0, MYSQL_TYPE_LONG_BLOB);
+ m_binderAddins[index].m_after_fetch=[this, index, ¶m](const binder&) {
+ std::array<char, blob_buffer_size> buffer;
+ unsigned long readed=0;
+ while(!param.eof() && !param.fail())
+ {
+ param.read(buffer.data(), buffer.size());
+ readed=(unsigned long)param.gcount();
+ if(readed>0)
+ {
+ if(mysql_stmt_send_long_data(m_stmt, index, buffer.data(), readed)!=0)
+ throw_exception();
+ }
+ }
+ };
+ }
+
template<class Param>
void bind_param(size_t index, const Param& param)
{
@@ -302,11 +332,6 @@
bind(m_binders[index], std::forward<Type>(value));
m_binderAddins[index].m_after_fetch=if_null<typename std::remove_reference<Type>::type>(value);
}
- }
-
- void bind_param(size_t index, const std::nullptr_t&)
- {
- m_binders[index].bind();
}
void bind_field(size_t index, char* value, size_t length)
@@ -345,24 +370,7 @@
m_binders[index].bind(data, field->length, field->type);
}
}
- void bind_param(size_t index, std::istream& param)
- {
- m_binders[index].bind(NULL, 0, MYSQL_TYPE_LONG_BLOB);
- m_binderAddins[index].m_after_fetch=[this, index, ¶m](const binder&) {
- std::array<char, blob_buffer_size> buffer;
- unsigned long readed=0;
- while(!param.eof() && !param.fail())
- {
- param.read(buffer.data(), buffer.size());
- readed=(unsigned long)param.gcount();
- if(readed>0)
- {
- if(mysql_stmt_send_long_data(m_stmt, index, buffer.data(), readed)!=0)
- throw_exception();
- }
- }
- };
- }
+
void bind_field(size_t index, std::ostream&& value)
{
if(m_result)
@@ -558,8 +566,8 @@
m_mysql=src.m_mysql;
src.m_mysql=NULL;
}
- database& operator=(const database&) = delete;
- database& operator=(database&& src)
+ database& operator==(const database&) = delete;
+ database& operator==(database&& src)
{
if(this!=&src)
{
@@ -567,6 +575,7 @@
m_mysql=src.m_mysql;
src.m_mysql=NULL;
}
+ return *this;
}
MYSQL* handle() { return m_mysql; }
diff --git a/include/qtl_sqlite.hpp b/include/qtl_sqlite.hpp
index f316bed..66cfb0d 100644
--- a/include/qtl_sqlite.hpp
+++ b/include/qtl_sqlite.hpp
@@ -391,6 +391,7 @@
m_db=src.m_db;
src.m_db=NULL;
}
+ return *this;
}
void open(const char *filename, int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)
--
Gitblit v1.9.3