📄 wtrecordset.cpp
字号:
/*
* 文件名: WTRecordSet.cpp
*
* 作者: 施立虹
*
* 描述: 用来查询和读取 oracle 数据库中的数据
*
* 全局变量: 无
*
* 修订记录:
* 日期 修订者 修订描述
* 2004-06-16 施立虹 创建本文件
*/
// WTRecordSet.cpp: implementation of the CWTRecordSet class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "WTRecordSet.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#include "WTCommand.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
long CWTRecordSet::m_RecordCount = 0;
CWTRecordSet::CWTRecordSet()
{
m_FieldCount = 0;
m_nIndex = 0;
m_strSort = _T("");
m_strFilter = _T("");
m_phCol = NULL; //参数句柄
m_phdef[1024] = 0; //定义句柄
m_colBuf[1024] = 0; //存放select语句选中的列数据
m_ind[1024] = 0; //指示符变量
m_colLen[1024] = 0; //列长度
m_colType[1024] = 0; //列类型
m_colPrecision[1024] =0; //精度
m_pRecord = NULL;
}
CWTRecordSet::~CWTRecordSet()
{
}
/*
* 函数名称: GetRecord
* 编写者: 施立虹
* 功能: 获取一条记录
* 输入参数: nIndex
* 输出参数:
* 返回值: CWTRecord *
* 注意事项:
* 修订记录:
* 日期 修订者 修订描述
* 2004-06-16 施立虹 创建
*/
CWTRecord *CWTRecordSet::GetRecord(long nIndex)
{
return Records().GetAt(nIndex);
}
CWTParameter *CWTRecordSet::GetParameter(long nIndex)
{
return Parameters().GetAt(nIndex);
}
void CWTRecordSet::Close()
{
Release();
for(int i = 0;i < Records().GetSize();i++)
{
CWTRecord *pRecord = NULL;
pRecord = (CWTRecord *)Records().GetAt(i);
for(int j = 0;j < pRecord->Fields().GetSize();j++)
{
CWTField *pField = (CWTField *)pRecord->Fields().GetAt(j);
delete pField;
pField = NULL;
}
delete pRecord;
pRecord = NULL;
}
}
/*
* 函数名称: GetRecordCount
* 编写者: 施立虹
* 功能: 返回记录条数
* 输入参数:
* 输出参数:
* 返回值: m_RecordCount
* 注意事项:
* 修订记录:
* 日期 修订者 修订描述
* 2004-06-16 施立虹 创建
*/
long CWTRecordSet::GetRecordCount()
{
return CWTRecordSet::m_RecordCount;
}
/*
void CWTRecordSet::MoveTo(long nIndex)
{
m_nIndex = nIndex;
}
void CWTRecordSet::MoveFirst()
{
m_nIndex = 0;
}
void CWTRecordSet::MoveLast()
{
m_nIndex = m_RecordCount - 1;
}
void CWTRecordSet::MovePrevious()
{
m_nIndex -= 1;
}
void CWTRecordSet::MoveNext()
{
m_nIndex += 1;
}
BOOL CWTRecordSet::IsError()
{
if(m_nIndex < 0 || m_nIndex > m_RecordCount)
return FALSE;
return TRUE;
};
*/
/*
* 函数名称: BuildSQL
* 编写者: 施立虹
* 功能: 组织 sql 语句
* 输入参数: strTableName
* 输出参数:
* 返回值: strSQL
* 注意事项:
* 修订记录:
* 日期 修订者 修订描述
* 2004-06-16 施立虹 创建
*/
CString CWTRecordSet::BuildSQL(CString strTableName)
{
CString strSQL = _T("");
strSQL = _T("SELECT ");
if(m_listFields.GetSize() > 0)
{
for (int n=0; n<m_listFields.GetSize(); n++)
{
CWTField * pField = (CWTField *) m_listFields.GetAt(n);
strSQL += pField->GetFieldName();
if (n < m_listFields.GetSize()-1)
strSQL += ',';
}
strSQL += _T(" ");
}
else strSQL += _T("* ");
strSQL += _T("FROM ");
strSQL += strTableName;
if (!m_strFilter.IsEmpty())
{
strSQL += " WHERE ";
strSQL += m_strFilter;
}
if (!m_strSort.IsEmpty())
{
strSQL += " ORDER BY ";
strSQL += m_strSort;
}
return strSQL;
}
BOOL CWTRecordSet::SaveRecords()
{
if(SaveMsg() == FALSE)
return FALSE;
if(SaveData() == FALSE)
return FALSE;
return TRUE;
}
/*
* 函数名称: Open
* 编写者: 施立虹
* 功能: 读取数据
* 输入参数: strTableName,m_pError
* 输出参数:
* 返回值: TRUE 或 FLASE
* 注意事项:
* 修订记录:
* 日期 修订者 修订描述
* 2004-06-16 施立虹 创建
*/
BOOL CWTRecordSet::Open(CString strTableName)
{
if(m_pError == NULL)
return FALSE;
// Release();
sword status = 0;
m_strSQLCommand = BuildSQL(strTableName);//组织 SQL 语句:m_strSQLCommand
text textSQL[1024];
wsprintf((char *)textSQL,"%s",m_strSQLCommand);
//准备 sql
status = OCIStmtPrepare(m_phstmt,
m_pError->m_pherr,
textSQL,strlen((char*)textSQL),
OCI_NTV_SYNTAX,
OCI_DEFAULT);
if(status == OCI_ERROR)
{
m_pError->m_status = status;
return FALSE;
}
if(m_listParams.GetSize() > 0)
{
//获取输入参数
CWTParameter *pParamter = NULL;
for(int i = 0;i < m_listParams.GetSize();i++)
{
pParamter = m_listParams.GetAt(i);
text TextMrak[512] = {0};
wsprintf((char *)TextMrak,"%s",pParamter->GetMark());
CWTValue::ValType valType = pParamter->GetValueType();
switch(valType)
{
case CWTValue::WTINT:
case CWTValue::WTLONG:
case CWTValue::WTFLOAT:
case CWTValue::WTDOUBLE:
{
sword status = 0;
double dlVal = 0;
OCINumber OCINum;
dlVal = pParamter->m_dlVal;
int nVal = (int)dlVal;
status = OCINumberFromReal(m_pError->m_pherr,
(double *)&dlVal,
sizeof(double),
&OCINum);
if(status != OCI_SUCCESS)
{
m_pError->m_status = status;
return FALSE;
}
//绑定
status = OCIBindByName(m_phstmt,
&pParamter->m_phbid,
m_pError->m_pherr,
(text *)TextMrak,
-1,
(int *)&nVal,
sizeof(nVal),
SQLT_INT,
0, 0, 0, 0, 0,
OCI_DEFAULT);
if(status != OCI_SUCCESS)
{
m_pError->m_status = status;
return FALSE;
}
}
break;
case CWTValue::WTDATE:
case CWTValue::WTCSTRING:
{
int nLen = 0;
sword status = 0;
text TextVal[512] = {0};
nLen = pParamter->m_strVal.GetLength();
wsprintf((char *)TextVal,"%s",pParamter->m_strVal);
TextVal[nLen] = '\0';
nLen += 1;
//绑定
status = OCIBindByName(m_phstmt, &pParamter->m_phbid, m_pError->m_pherr,
(text *)TextMrak, -1,
(ub1 *)TextVal, nLen + 1,
SQLT_STR,
0, 0, 0, 0, 0,
OCI_DEFAULT);
if(status != OCI_SUCCESS)
{
m_pError->m_status = status;
return FALSE;
}
}
break;
default:
{
}
break;
}
}
}
//执行 sql 语句
status = OCIStmtExecute(m_pDataBase->m_phsvc,m_phstmt,m_pError->m_pherr,
(ub4)0,0,NULL,NULL,OCI_DEFAULT);
if(status == OCI_ERROR)
{
m_pError->m_status = status;
return FALSE;
}
//保存记录
if(SaveRecords() == FALSE)
{
return FALSE;
};
return TRUE;
}
BOOL CWTRecordSet::SaveMsg()
{
sword status = 0; //
int nColumnNum = 0; //字段数目
//读取字段数
OCIAttrGet(m_phstmt,OCI_HTYPE_STMT,&nColumnNum,0,
OCI_ATTR_PARAM_COUNT,m_pError->m_pherr);
if(nColumnNum <= 0)
{
return FALSE;
}
m_FieldCount = nColumnNum;
text *pFieldName = NULL;//字段名
ub4 size = 0;
text tempText[1024] = {0};
ub2 colLen[1024] = {0}; //列长度
for(int i = 0;i < nColumnNum;i++)
{
//读取某一字段(第 i + 1 字段)的参数描述符
status = OCIParamGet(m_phstmt,OCI_HTYPE_STMT,m_pError->m_pherr,
(void **)&m_phCol,(ub4)(i + 1));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -