📄 sqldata.cpp
字号:
ASSERT(m_pData->nBufferSize >= sizeof(TIMESTAMP_STRUCT));
TIMESTAMP_STRUCT* pData = (TIMESTAMP_STRUCT*) GetBuffer();
::sscanf(pszValue, "%d-%d-%d %d:%d:%d.%d",
&pData->year, &pData->month, &pData->day,
&pData->hour, &pData->minute, &pData->second,
&pData->fraction);
if (!m_pData->nScale)
pData->fraction = 0;
}
m_pData->nIndicator = sizeof(TIMESTAMP_STRUCT);
break;
default:
return SetValue(pszValue);
}
return TRUE;
}
// Converts the value to string
int CSqlVariant::ToString(std::string& strValue)
{
strValue.resize(0);
if (IsNull())
return 0;
int nSize = GetDisplaySize();
if (nSize <= 0)
return 0;
strValue.resize(nSize+1);
char* pszBuff = &strValue[0];
switch (m_pData->nDataType)
{
case SQL_C_SHORT:
::itoa((int)m_pData->iVal, pszBuff, 10);
break;
case SQL_C_LONG:
::itoa(m_pData->nVal, pszBuff, 10);
break;
case SQL_C_SBIGINT:
#if defined(__xlC__) || defined(__IBMC__) || defined(__IBMCPP__)
::lltoa(m_pData->lVal, pszBuff, 10);
#else
::_i64toa(m_pData->lVal, pszBuff, 10);
#endif
break;
case SQL_C_FLOAT:
::sprintf(pszBuff, "%*f", m_pData->fltVal);
break;
case SQL_C_DOUBLE:
::sprintf(pszBuff, "%*lf", m_pData->dbVal);
break;
case SQL_C_TYPE_DATE:
::sprintf(pszBuff, "%d-%02d-%02d", m_pData->dtVal.year,
m_pData->dtVal.month, m_pData->dtVal.day);
break;
case SQL_C_TYPE_TIME:
::sprintf(pszBuff, "%02d:%02d:%02d", m_pData->tmVal.hour,
m_pData->tmVal.minute, m_pData->tmVal.second);
break;
case SQL_C_TYPE_TIMESTAMP:
{
const TIMESTAMP_STRUCT* pData = GetTimeStamp();
::sprintf(pszBuff, "%d-%d-%d %d:%d:%d.%d",
pData->year, pData->month, pData->day,
pData->hour, pData->minute, pData->second,
pData->fraction);
}
break;
default:
::memcpy(pszBuff, m_pData->pVal, nSize);
pszBuff[nSize] = 0;
// remove the trailing spaces, added May 20, 2002
nSize = (int)::strlen(pszBuff);
while (nSize > 0 && pszBuff[nSize-1] == ' ')
nSize--;
pszBuff[nSize] = 0;
break;
}
//strValue.resize(string::traits_type::length(strValue.c_str()));
strValue.resize(::strlen(strValue.c_str()));
return (int)strValue.size();
}
//////////////////////////////////////////////////////////////////////
// class CSqlParameter - Represents a parameter or argument
// associated with a SQL statement or stored procedure
//////////////////////////////////////////////////////////////////////
CSqlParameter::CSqlParameter() :
m_bBindFile(FALSE),
m_nFileOptions(SQL_FILE_READ),
m_nSqlType(SQL_UNKNOWN_TYPE),
m_nIoType(SQL_PARAM_INPUT) // SQL_PARAM_TYPE_UNKNOWN
{
}
CSqlParameter::CSqlParameter(const CSqlParameter& rData)
{
*this = rData;
}
CSqlParameter::~CSqlParameter()
{
}
// Initializes a parameter
// Arguments:
// nSqlType - Specifies the SQL data type of the parameter.
// nIoType - Specifies the input/output type of the parameter.
// nPrecision - Specifies the precision of the parameter.
// - For a binary or single byte string, it is in unit of byte
// - For a double byte string, it is in unit of double-byte
// - For NUMERIC or DECIMAL, it is the decimal precision.
// - For fixed-length data type, this argument is ignored
// nScale - Specifies the scale if nSqlType is SQL_NUMERIC, SQL_ECIMAL, or SQL_TIMESTAMP
BOOL CSqlParameter::CreateParameter(SQLSMALLINT nSqlType, SQLSMALLINT nIoType,
SQLUINTEGER nPrecision, SQLSMALLINT nScale)
{
if (!CreateBuffer(nSqlType, nPrecision))
return FALSE;
m_pData->nScale = nScale;
m_nIoType = nIoType;
m_bBindFile = FALSE;
return TRUE;
}
// Initializes a file-binding parameter
// Arguments:
// nSqlType - Specifies the SQL data type of the parameter. It can only be BLOB, CLOB, or DBCLOB.
BOOL CSqlParameter::CreateFileParam(SQLSMALLINT nSqlType)
{
if (!AllocBuffer(MAX_PATH))
return FALSE;
m_pData->nDataType = SQL_C_CHAR;
m_pData->nSqlType = SQL_CHAR;
ASSERT(nSqlType == SQL_BLOB ||
nSqlType == SQL_CLOB ||
nSqlType == SQL_DBCLOB);
m_nSqlType = nSqlType;
m_bBindFile = TRUE;
return TRUE;
}
// Binds the parameter to a command object
// This function should be called before SQLExecute()
BOOL CSqlParameter::Bind(CSqlCommand* pStmt, int nOrdinal)
{
SQLRETURN nRet;
ASSERT(pStmt != NULL);
ASSERT(pStmt->IsOpen());
if (!m_bBindFile)
{
nRet = ::SQLBindParameter(pStmt->GetHandle(), nOrdinal, m_nIoType,
m_pData->nDataType, m_pData->nSqlType, m_pData->nPrecision, m_pData->nScale,
(SQLPOINTER) (IsDefault() ? NULL : m_pData->pVal),
GetMaxSize(), &m_pData->nIndicator);
}
else
{
ASSERT(!IsDefault());
m_pData->nIndicator = 0; // must be set to 0
nRet = ::SQLBindFileToParam(pStmt->GetHandle(), nOrdinal, m_nSqlType,
GetBuffer(), NULL, &m_nFileOptions, (SQLSMALLINT)GetMaxSize(), &m_pData->nIndicator);
}
return pStmt->SqlCheck(nRet);
}
// Unbinds the parameter
BOOL CSqlParameter::Unbind(CSqlCommand* pStmt, int nOrdinal)
{
BOOL bOK = CSqlVariant::Unbind(pStmt, nOrdinal);
if (bOK && pStmt != NULL && pStmt->IsOpen() && !m_bBindFile)
bOK = pStmt->SqlCheck(::SQLBindParameter(pStmt->GetHandle(), nOrdinal,
0, SQL_C_DEFAULT, SQL_DEFAULT, 0, 0, NULL, 0, NULL));
return bOK;
}
CSqlParameter& CSqlParameter::operator=(const CSqlParameter& rValue)
{
if (this != &rValue)
{
CopyData(rValue);
m_bBindFile = rValue.m_bBindFile;
m_nFileOptions = rValue.m_nFileOptions;
m_nSqlType = rValue.m_nSqlType;
m_nIoType = rValue.m_nIoType;
}
return *this;
}
//////////////////////////////////////////////////////////////////////
// class CSqlField - Represents a column of data in a recordset
//////////////////////////////////////////////////////////////////////
CSqlField::CSqlField() :
m_bBindFile(FALSE),
m_nFileOptions(SQL_FILE_OVERWRITE),
m_nOrdinal(0),
m_bNullable(FALSE)
{
}
CSqlField::CSqlField(const CSqlField& rData)
{
*this = rData;
}
CSqlField::~CSqlField()
{
}
// Initializes a field object
// Application call this function when the column size and type is known
// and auto-binding is not wanted.
// Arguments:
// pszFieldName - Specifies the column name. It can be an SQL expression
// nDataType - Specifies the C data type of the field
// nDataSize - Specifies the maximum length in bytes of the field.
// This argument is ignored if nDataType specifies a fixed-length data type.
BOOL CSqlField::CreateField(PCSTR pszFieldName, SQLSMALLINT nDataType, SQLINTEGER nDataSize)
{
for (int n=0; n<MAX_SQL_TYPE; n++)
if (nDataType == m_tblDataType[n].nDataType)
break;
if (n < MAX_SQL_TYPE) // it's a valid data type
{
if (m_tblDataType[n].nDataSize != 0) // fixed-length data type
nDataSize = m_tblDataType[n].nDataSize;
}
if (!AllocBuffer(nDataSize))
return FALSE;
m_pData->nDataType = nDataType;
m_pData->nIndicator = 0;
m_strName = pszFieldName;
m_nOrdinal = 0;
m_bBindFile = FALSE;
return TRUE;
}
// Initializes a field according to the attribute of the specified column in a recordset.
BOOL CSqlField::CreateField(CSqlCommand* pStmt, int nOrdinal)
{
// get the column information
SQLCHAR szName[256];
SQLSMALLINT nNameSize, nSqlType, nScale, nNullable;
SQLUINTEGER nPrecision;
if (!pStmt->SqlCheck(::SQLDescribeCol(pStmt->GetHandle(), nOrdinal, szName, sizeof(szName),
&nNameSize, &nSqlType, &nPrecision, &nScale, &nNullable)))
return FALSE;
if (!CreateBuffer(nSqlType, nPrecision))
return FALSE;
// calls SQLColAttribute(SQL_DESC_UPDATABLE) to check if the column is updatable.
// if the column should not be updated, places SQL_ROW_IGNORE in StrLen_or_IndPtr
// buffer when SQLSetPos() for SQL_UPDATE is called.
m_pData->nScale = nScale;
m_strName.assign((const char*) szName);
m_nOrdinal = nOrdinal;
m_bBindFile = FALSE;
m_bNullable = (SQL_NULLABLE == nNullable);
return TRUE;
}
// Initializes a file-binding field
// Arguments:
// pszFieldName - Specifies the column name.
// nFileOption - Specifies the file access option.
// SQL_FILE_CREATE - create a new file.
// SQL_FILE_OVERWRITE - overwrite existing file, or create a new file
// SQL_FILE_APPEND - append to existing file, or create a new file
BOOL CSqlField::CreateFileField(PCSTR pszFieldName, SQLUINTEGER nFileOption)
{
if (!AllocBuffer(MAX_PATH))
return FALSE;
m_pData->nSqlType = SQL_CHAR;
m_pData->nDataType = SQL_C_CHAR;
m_pData->nIndicator = 0;
m_nFileOptions = nFileOption;
m_strName = pszFieldName;
m_nOrdinal = 0;
m_bBindFile = TRUE;
return TRUE;
}
// Binds a field to a column in the recordset
BOOL CSqlField::Bind(CSqlCommand* pStmt, int nOrdinal)
{
ASSERT(!IsDefault());
ASSERT(pStmt != NULL);
ASSERT(pStmt->IsOpen());
m_pData->nIndicator = 0;
if (m_nOrdinal != 0)
nOrdinal = m_nOrdinal;
SQLRETURN nRet;
if (!m_bBindFile)
{
nRet = ::SQLBindCol(pStmt->GetHandle(), nOrdinal, m_pData->nDataType,
GetBuffer(), GetMaxSize(), &m_pData->nIndicator);
}
else
{
nRet = ::SQLBindFileToCol(pStmt->GetHandle(), nOrdinal, GetBuffer(),
NULL, &m_nFileOptions, (SQLSMALLINT)GetMaxSize(), NULL, &m_pData->nIndicator);
}
return pStmt->SqlCheck(nRet);
}
// Unbinds a field from the recordset
BOOL CSqlField::Unbind(CSqlCommand* pStmt, int nOrdinal)
{
BOOL bOK = CSqlVariant::Unbind(pStmt, nOrdinal);
if (bOK && pStmt != NULL && pStmt->IsOpen())
{
if (!m_bBindFile)
bOK = pStmt->SqlCheck(::SQLBindCol(pStmt->GetHandle(), nOrdinal,
SQL_C_DEFAULT, NULL, 0, NULL));
else
bOK = pStmt->SqlCheck(::SQLBindFileToCol(pStmt->GetHandle(), nOrdinal,
NULL, NULL, NULL, 0, NULL, NULL));
}
return bOK;
}
CSqlField& CSqlField::operator=(const CSqlField& rValue)
{
if (this != &rValue)
{
CopyData(rValue);
m_bBindFile = rValue.m_bBindFile;
m_nFileOptions = rValue.m_nFileOptions;
m_strName = rValue.m_strName;
m_bNullable = rValue.m_bNullable;
m_nOrdinal = rValue.m_nOrdinal;
}
return *this;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -