首页 > 学院 > 开发设计 > 正文

数据库MySql类库系列(六)-PrepareOperatorUpdate

2019-11-06 06:13:22
字体:
来源:转载
供稿:网友

第五个工具类,PRepareOperatorUpdate

负责处理:预处理执行sql方式下的,增、删、改的需求。

用到了之前的DBOperator。

提供主要的接口:

1、BindSql:预处理带占位符的sql语句

2、BindParameter:绑定sql语句中占位符对应的参数,这里使用了不定参数的方式,支持7种格式控制符:

int%i
int value;BindParameter("%i", &value);
unsigned int%u
unsigned int value;BindParameter("%u", &value);
long long%I(大写i)
long long value;BindParameter("%I", &value);
unsigned long long%U
unsigned long long value;BindParameter("%U", &value)
float%f
float value;BindParameter("%f", &value);
char *%s
char value[10];unsigned long valueLen = sizeof(value);BindParameter("%s", value, &valueLen);
void *%B
typedef struct _XXX{    //...}XXX;XXX value;unsigned long valueLen = sizeof(value);BindParameter("%B", value, &valueLen);

那么假设有3个字段:帐号名(字符串),账号密码(字符串),账号id(无符号整数)

则:

char accountName[20];unsigned long accountNameLen = sizeof(accountName);char accountKey[20];unsigned long accountKeyLen = sizeof(accountKey);unsigned int accountId;BindParameter("%s,%s,%u",	accountName, &accountNameLen,	accountKey, &accountKeyLen,	&accountId);

3、DoOperator:给sql语句对应的绑定参数赋值之后,执行sql,得到结果

代码:

PrepareOperatorUpdate.h:

#ifndef __PrepareOperatorUpdate_H__#define	__PrepareOperatorUpdate_H__struct st_MySQL;typedef struct st_mysql MYSQL;struct st_mysql_stmt;typedef struct st_mysql_stmt MYSQL_STMT;struct st_mysql_bind;typedef struct st_mysql_bind MYSQL_BIND;struct st_mysql_res;typedef struct st_mysql_res MYSQL_RES;namespace common{	namespace db{		class PrepareOperatorUpdate		{		public:			PrepareOperatorUpdate();			~PrepareOperatorUpdate();			void Release();			// 设置sql			bool BindSql(MYSQL *connect, const char *sql);			bool BindParameter(const char *format, ...);			// 执行sql			bool DoOperator();		private:			MYSQL_STMT *m_Stmt;			// 预处理对象			MYSQL_BIND *m_Parameter;	// 绑定的更新参数		};	}}#endifPrepareOperatorUpdate.cpp:

#include "PrepareOperatorUpdate.h"#ifdef WIN32#include <winsock2.h>#endif#include <stdio.h>#include <mysql.h>#include <string.h>#include <stdarg.h>#include "DBOperator.h"namespace common{	namespace db{		PrepareOperatorUpdate::PrepareOperatorUpdate()		{			m_Stmt = NULL;			m_Parameter = NULL;		}		PrepareOperatorUpdate::~PrepareOperatorUpdate()		{			Release();		}		void PrepareOperatorUpdate::Release()		{			if (NULL != m_Stmt)			{				DBOperator::FreeStmt(m_Stmt);				m_Stmt = NULL;			}			if (NULL != m_Parameter)			{				delete[] m_Parameter;				m_Parameter = NULL;			}		}		bool PrepareOperatorUpdate::BindSql(MYSQL *connect, const char *sql)		{			Release();			if (NULL != connect)			{				m_Stmt = DBOperator::ExecPrepare(connect, sql);				if (NULL != m_Stmt)				{					return true;				}				else				{					return false;				}			}			else			{				return false;			}		}		bool PrepareOperatorUpdate::BindParameter(const char *format, ...)		{			if (NULL != m_Stmt)			{				size_t len = strlen(format);				unsigned int argc = 0;				for (size_t i = 0; i < len; i++)				{					switch (format[i])					{						case 'i':						case 'u':						case 'I':						case 'U':						case 'f':						case 's':						case 'B':						argc++;						break;						default:						break;					}				}				if (NULL != m_Parameter)				{					delete[] m_Parameter;					m_Parameter = NULL;				}				m_Parameter = new MYSQL_BIND[argc];				if (NULL != m_Parameter)				{					va_list argptr;					va_start(argptr, format);					int curr_arg = 0;					for (size_t i = 0; i < len; i++)					{						switch (format[i])						{							case 'i':							{								int *buff = va_arg(argptr, int *);								DBOperator::BindInt(m_Stmt, &m_Parameter[curr_arg], buff);								curr_arg++;							}							break;							case 'u':							{								unsigned int *buff = va_arg(argptr, unsigned int *);								DBOperator::BindUInt(m_Stmt, &m_Parameter[curr_arg], buff);								curr_arg++;							}							break;							case 'I':							{								long long *buff = va_arg(argptr, long long *);								DBOperator::BindInt64(m_Stmt, &m_Parameter[curr_arg], buff);								curr_arg++;							}							break;							case 'U':							{								unsigned long long *buff = va_arg(argptr, unsigned long long *);								DBOperator::BindUInt64(m_Stmt, &m_Parameter[curr_arg], buff);								curr_arg++;							}							break;							case 'f':							{								float *buff = va_arg(argptr, float *);								DBOperator::BindFloat(m_Stmt, &m_Parameter[curr_arg], buff);								curr_arg++;							}							break;							case 's':							{								char *buff = va_arg(argptr, char *);								unsigned long *bufflen = va_arg(argptr, unsigned long *);								DBOperator::BindString(m_Stmt, &m_Parameter[curr_arg], buff, bufflen);								curr_arg++;							}							break;							case 'B':							{								void *buff = va_arg(argptr, void *);								unsigned long *bufflen = va_arg(argptr, unsigned long *);								DBOperator::BindBlob(m_Stmt, &m_Parameter[curr_arg], buff, bufflen);								curr_arg++;							}							break;							default:							break;						}					}					va_end(argptr);					return DBOperator::BindParameterStmt(m_Stmt, m_Parameter);				}				else				{					return false;				}			}			else			{				return false;			}		}		bool PrepareOperatorUpdate::DoOperator()		{			if (NULL != m_Stmt)			{				return DBOperator::ExecStmt(m_Stmt);			}			else			{				return false;			}		}	}}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表