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

数据库MySql类库系列(七)-PrepareOperatorSelect

2019-11-06 06:12:38
字体:
来源:转载
供稿:网友

第六个工具类:PRepareOperatorSelect

负责处理:预处理执行sql方式下的,查找,以及获取查找结果的需求。

用到了之前的DBOperator。

提供主要的接口:

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

2、BindParameter,BindResult:绑定sql语句中占位符对应的参数,

BindParameter用于绑定查询参数

BindResult用于绑定查询结果

这里使用了不定参数的方式,支持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,得到结果

4、FetchResult:得到结果之后,获取一条结果数据。多于有多行结果的,则前一条结果处理完之后,再调用则获取下一行结果

5、FreeResult:所有结果全部处理完之后,释放结果集(如果不释放,在下次执行查询之前,会强制释放,对象析构时,会强制释放)。

代码:

PrepareOperatorSelect.h:

#ifndef __PrepareOperatorSelect_H__#define	__PrepareOperatorSelect_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 PrepareOperatorSelect		{		public:			PrepareOperatorSelect();			~PrepareOperatorSelect();			void Release();			// 设置sql			bool BindSql(MYSQL *connect, const char *sql);			bool BindParameter(const char *format, ...);			bool BindResult(const char *format, ...);			// 执行sql			bool DoOperator();			// 获取一条结果			bool FetchResult();			// 释放结果			bool FreeResult();		private:			MYSQL_STMT *m_Stmt;			// 预处理对象			MYSQL_BIND *m_Parameter;	// 绑定的查询参数			MYSQL_BIND *m_Result;		// 绑定的查询结果			MYSQL_RES *m_Data;			// 结果集数据		};	}}#endifPrepareOperatorSelect.cpp:

#include "PrepareOperatorSelect.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{		PrepareOperatorSelect::PrepareOperatorSelect()		{			m_Stmt = NULL;			m_Parameter = NULL;			m_Result = NULL;			m_Data = NULL;		}		PrepareOperatorSelect::~PrepareOperatorSelect()		{			Release();		}		void PrepareOperatorSelect::Release()		{			if (NULL != m_Data)			{				DBOperator::FreeResult(m_Data);				m_Data = NULL;			}			if (NULL != m_Stmt)			{				DBOperator::FreeStmt(m_Stmt);				m_Stmt = NULL;			}			if (NULL != m_Parameter)			{				delete[] m_Parameter;				m_Parameter = NULL;			}			if (NULL != m_Result)			{				delete[] m_Result;				m_Result = NULL;			}		}		bool PrepareOperatorSelect::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 PrepareOperatorSelect::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 PrepareOperatorSelect::BindResult(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_Result)				{					delete[] m_Result;					m_Result = NULL;				}				m_Result = new MYSQL_BIND[argc];				if (NULL != m_Result)				{					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_Result[curr_arg], buff);								curr_arg++;							}							break;							case 'u':							{								unsigned int *buff = va_arg(argptr, unsigned int *);								DBOperator::BindUInt(m_Stmt, &m_Result[curr_arg], buff);								curr_arg++;							}							break;							case 'I':							{								long long *buff = va_arg(argptr, long long *);								DBOperator::BindInt64(m_Stmt, &m_Result[curr_arg], buff);								curr_arg++;							}							break;							case 'U':							{								unsigned long long *buff = va_arg(argptr, unsigned long long *);								DBOperator::BindUInt64(m_Stmt, &m_Result[curr_arg], buff);								curr_arg++;							}							break;							case 'f':							{								float *buff = va_arg(argptr, float *);								DBOperator::BindFloat(m_Stmt, &m_Result[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_Result[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_Result[curr_arg], buff, bufflen);								curr_arg++;							}							break;							default:							break;						}					}					va_end(argptr);					return DBOperator::BindResultStmt(m_Stmt, m_Result);				}				else				{					return false;				}			}			else			{				return false;			}		}		bool PrepareOperatorSelect::DoOperator()		{			if (NULL != m_Stmt)			{				if (NULL != m_Data)				{					DBOperator::FreeResult(m_Data);					m_Data = NULL;				}				if (DBOperator::ExecStmt(m_Stmt))				{					m_Data = DBOperator::SelectResult(m_Stmt);					if (NULL != m_Data)					{						return true;					}					else					{						return false;					}				}				else				{					return false;				}			}			else			{				return false;			}		}		bool PrepareOperatorSelect::FetchResult()		{			if (NULL != m_Stmt)			{				if (NULL != m_Data)				{					if (DBOperator::GetNextResult(m_Stmt))					{						return true;					}					else					{						DBOperator::FreeResult(m_Data);						m_Data = NULL;						return false;					}				}				else				{					return false;				}			}			else			{				return false;			}		}		bool PrepareOperatorSelect::FreeResult()		{			if (NULL != m_Stmt)			{				if (NULL != m_Data)				{					DBOperator::FreeResult(m_Data);					m_Data = NULL;					return true;				}				else				{					return false;				}			}			else			{				return false;			}		}	}}


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