我是靠谱客的博主 凶狠项链,这篇文章主要介绍SQL 参数动态绑定 模板不定长参数展开,现在分享给大家,希望可以做个参考。

#参数占位绑定
std::string strInsert = "insert into track_history_table values(?,?,?,?,?,?)";
std::string strSql = ParamBind::BindParams(strInsert , v1,v2,v3,v4,v5,v6)
#pragma once
#ifndef __C_PARAM_BIND_H__
#define __C_PARAM_BIND_H__
#include <iostream>
#include <string>
using namespace std;
namespace ParamBind
{
//************************************
// Method:
ReplaceSlot 绑定字符串参数到sql中
// FullName:
CMySql::ReplaceSlot
// Access:
public
// Returns:
:type
// Qualifier:
// Parameter: std::string & strStateMent
// Parameter: T t
//************************************
template <typename T>
typename std::enable_if<std::is_same<char*, T>::value || std::is_same<const char*, T>::value>::type
ReplaceSlot(std::string& strStateMent, T t)
{
int nPos = strStateMent.find_first_of('?');
strStateMent.replace(nPos, 1, "'" + std::string(t) + "'");
}
//************************************
// Method:
ReplaceSlot 绑定数值参数到sql中
// FullName:
CMySql::ReplaceSlot
// Access:
public
// Returns:
:type
// Qualifier:
// Parameter: std::string & strStateMent
// Parameter: T t
//************************************
template <typename T>
typename
std::enable_if<std::is_integral<T>::value>::type ReplaceSlot(std::string& strStateMent, T t)
{
int nPos = strStateMent.find_first_of('?');
std::string strValue = std::to_string(t);
strStateMent.replace(nPos, 1, strValue);
}
//************************************
// Method:
BindParams 参数递归展开终止
// FullName:
CMySql::BindParams
// Access:
public
// Returns:
int
// Qualifier:
// Parameter: std::string & strStateMent
//************************************
inline int BindParams(std::string& strStateMent)
{
return 0;
}
//************************************
// Method:
BindParams
递归展开不定长参数,同时进行值绑定
// FullName:
CMySql::BindParams
// Access:
public
// Returns:
int
// Qualifier:
// Parameter: std::string & strStateMent
// Parameter: T & & first
// Parameter: Args & & ... args
//************************************
template <typename T, typename... Args>
inline std::string BindParams(std::string& strStateMent, T&&
first, Args&&... args)
{
//参数展开时替换占位符
ReplaceSlot(strStateMent, first);
BindParams(strStateMent, std::forward<Args>(args)...);
std::string res = strStateMent;
return res;
}
}
#endif

最后

以上就是凶狠项链最近收集整理的关于SQL 参数动态绑定 模板不定长参数展开的全部内容,更多相关SQL内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部