第四个工具类,QueryOperatorSelect
负责处理:直接执行sql方式下的,查找,以及获取查找结果的需求。
用到了之前的DBOperator,主要是封装其ExecQuery接口。
另外提供获取查询结果的相应接口:
从结果集中取出一行数据:FetchResult。
取出一行数据之后,在这一行数据中,取每个字段的值:GetColumn。(由于本人的业务需要,目前仅提供支持7种数据类型:int,unsigned int,long long,unsigned long long,float,字符串,Blob)。
每一行数据都处理完之后,释放结果集:FreeResult。(如果不释放,在下次执行查询之前,会强制释放,对象析构时,会强制释放)。
QueryOperatorSelect.h
#ifndef __QueryOperatorSelect_H__#define __QueryOperatorSelect_H__struct st_MySQL;typedef struct st_mysql MYSQL;struct st_mysql_res;typedef struct st_mysql_res MYSQL_RES;typedef char** MYSQL_ROW;namespace common{ namespace db{ class QueryOperatorSelect { public: QueryOperatorSelect(); ~QueryOperatorSelect(); void Release(); // 执行sql bool DoOperator(MYSQL *connect, const char *sql); // 获取一条结果 bool FetchResult(); // 获取一条结果一列的值 bool GetColumn(int& value, unsigned int index); bool GetColumn(unsigned int& value, unsigned int index); bool GetColumn(long long& value, unsigned int index); bool GetColumn(unsigned long long& value, unsigned int index); bool GetColumn(float& value, unsigned int index); bool GetColumn(void* value, unsigned int index); // 释放结果 bool FreeResult(); PRivate: MYSQL_RES *m_Data; // 结果集数据 unsigned int m_FieldCount; // 结果集列数 MYSQL_ROW m_Columns; // 数据每一列的值 unsigned long *m_ColumnLens; // 数据每一列的长度 }; }}#endifQueryOperatorSelect.cpp#include "QueryOperatorSelect.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{ QueryOperatorSelect::QueryOperatorSelect() { m_Data = NULL; m_FieldCount = 0; m_Columns = NULL; m_ColumnLens = NULL; } QueryOperatorSelect::~QueryOperatorSelect() { Release(); } void QueryOperatorSelect::Release() { if (NULL != m_Data) { DBOperator::FreeResult(m_Data); m_Data = NULL; } if (NULL != m_Columns) { m_Columns = NULL; } } bool QueryOperatorSelect::DoOperator(MYSQL *connect, const char *sql) { if (NULL != connect && NULL != sql) { if (NULL != m_Data) { DBOperator::FreeResult(m_Data); m_Data = NULL; } if (DBOperator::ExecQuery(connect, sql)) { m_Data = DBOperator::SelectResult(connect); if (NULL != m_Data) { m_FieldCount = DBOperator::GetResultFields(m_Data); return true; } else { m_FieldCount = 0; return false; } } else { return false; } } else { return false; } } bool QueryOperatorSelect::FetchResult() { if (NULL != m_Data) { m_Columns = DBOperator::GetNextResult(m_Data); m_ColumnLens = DBOperator::GetResultLens(m_Data); if (NULL != m_Columns && NULL != m_ColumnLens) { return true; } else { DBOperator::FreeResult(m_Data); m_Data = NULL; m_ColumnLens = NULL; return false; } } else { return false; } } bool QueryOperatorSelect::GetColumn(int& value, unsigned int index) { if (NULL != m_Columns && index < m_FieldCount) { sscanf(m_Columns[index], "%d", &value); return true; } else { return false; } } bool QueryOperatorSelect::GetColumn(unsigned int& value, unsigned int index) { if (NULL != m_Columns && index < m_FieldCount) { sscanf(m_Columns[index], "%u", &value); return true; } else { return false; } } bool QueryOperatorSelect::GetColumn(long long& value, unsigned int index) { if (NULL != m_Columns && index < m_FieldCount) { sscanf(m_Columns[index], "%lld", &value); return true; } else { return false; } } bool QueryOperatorSelect::GetColumn(unsigned long long& value, unsigned int index) { if (NULL != m_Columns && index < m_FieldCount) { sscanf(m_Columns[index], "%llu", &value); return true; } else { return false; } } bool QueryOperatorSelect::GetColumn(float& value, unsigned int index) { if (NULL != m_Columns && index < m_FieldCount) { sscanf(m_Columns[index], "%f", &value); return true; } else { return false; } } bool QueryOperatorSelect::GetColumn(void* value, unsigned int index) { if (NULL != m_Columns && NULL != m_ColumnLens && index < m_FieldCount) { memcpy(value, m_Columns[index], m_ColumnLens[index]); return true; } else { return false; } } bool QueryOperatorSelect::FreeResult() { if (NULL != m_Data) { DBOperator::FreeResult(m_Data); m_Data = NULL; return true; } else { return false; } } }}
新闻热点
疑难解答