📄 datalink.cpp
字号:
SqlString_Insert(joined_table, nStart2, strItem);
SqlString_Stuff(*this, nStart1, nCount1, joined_table);
return nIndex;
}
void CSqlString::DeleteJoinedTable(int nIndex)
{
int nStart1, nCount1;
SqlString_JoinedTable(*this, nStart1, nCount1);
CString joined_table = Mid(nStart1, nCount1);
int nStart2, nCount2;
if (!SqlString_JoinedTableItem(joined_table, nIndex, nStart2, nCount2))
return;
joined_table.Delete(nStart2, nCount2 + 1);
if (nStart2 == joined_table.GetLength())
joined_table.Delete(nStart2 - 1, 1);
SqlString_Stuff(*this, nStart1, nCount1, joined_table);
}
// join_type and search_condition
CString CSqlString::GetJoinType(int nIndex)
{
CString joined_table = GetJoinedTable();
int nStart1, nCount1;
SqlString_JoinedTableItem(joined_table, nIndex, nStart1, nCount1);
CString table_item = joined_table.Mid(nStart1, nCount1);
int nStart2, nCount2;
SqlString_JoinType(table_item, nStart2, nCount2);
return table_item.Mid(nStart2, nCount2);
}
CString CSqlString::GetJoinType(LPCTSTR lpszTableAlias)
{
CString joined_table = GetJoinedTable();
int nStart1, nCount1;
CString strTableAlias(lpszTableAlias);
SqlString_Alias(strTableAlias);
int nIndex = SqlString_JoinedTableIndex(joined_table, strTableAlias);
SqlString_JoinedTableItem(joined_table, nIndex, nStart1, nCount1);
CString table_item = joined_table.Mid(nStart1, nCount1);
int nStart2, nCount2;
SqlString_JoinType(table_item, nStart2, nCount2);
return table_item.Mid(nStart2, nCount2);
}
CString CSqlString::GetJoinCondition(int nIndex)
{
CString joined_table = GetJoinedTable();
int nStart1, nCount1;
SqlString_JoinedTableItem(joined_table, nIndex, nStart1, nCount1);
CString table_item = joined_table.Mid(nStart1, nCount1);
int nStart2, nCount2;
SqlString_JoinCondition(table_item, nStart2, nCount2);
return table_item.Mid(nStart2, nCount2);
}
CString CSqlString::GetJoinCondition(LPCTSTR lpszTableAlias)
{
CString joined_table = GetJoinedTable();
int nStart1, nCount1;
CString strTableAlias(lpszTableAlias);
SqlString_Alias(strTableAlias);
int nIndex = SqlString_JoinedTableIndex(joined_table, strTableAlias);
SqlString_JoinedTableItem(joined_table, nIndex, nStart1, nCount1);
CString table_item = joined_table.Mid(nStart1, nCount1);
int nStart2, nCount2;
SqlString_JoinCondition(table_item, nStart2, nCount2);
return table_item.Mid(nStart2, nCount2);
}
void CSqlString::SetJoinType(int nIndex, LPCTSTR lpszType)
{
int nStart1, nCount1;
SqlString_JoinedTable(*this, nStart1, nCount1);
CString joined_table = Mid(nStart1, nCount1);
int nStart2, nCount2;
if (!SqlString_JoinedTableItem(joined_table, nIndex, nStart2, nCount2))
return;
CString table_item = joined_table.Mid(nStart2, nCount2);
int nStart3, nCount3;
SqlString_JoinType(table_item, nStart3, nCount3);
SqlString_Stuff(table_item, nStart3, nCount3, lpszType);
SqlString_Stuff(joined_table, nStart2, nCount2, table_item);
SqlString_Stuff(*this, nStart1, nCount1, joined_table);
}
void CSqlString::SetJoinType(LPCTSTR lpszTableAlias, LPCTSTR lpszType)
{
int nStart1, nCount1;
SqlString_JoinedTable(*this, nStart1, nCount1);
CString joined_table = Mid(nStart1, nCount1);
int nStart2, nCount2;
CString strTableAlias(lpszTableAlias);
SqlString_Alias(strTableAlias);
int nIndex = SqlString_JoinedTableIndex(joined_table, strTableAlias);
if (!SqlString_JoinedTableItem(joined_table, nIndex, nStart2, nCount2))
return;
CString table_item = joined_table.Mid(nStart2, nCount2);
int nStart3, nCount3;
SqlString_JoinType(table_item, nStart3, nCount3);
SqlString_Stuff(table_item, nStart3, nCount3, lpszType);
SqlString_Stuff(joined_table, nStart2, nCount2, table_item);
SqlString_Stuff(*this, nStart1, nCount1, joined_table);
}
void CSqlString::SetJoinCondition(int nIndex, LPCTSTR lpszCondition)
{
int nStart1, nCount1;
SqlString_JoinedTable(*this, nStart1, nCount1);
CString joined_table = Mid(nStart1, nCount1);
int nStart2, nCount2;
if (!SqlString_JoinedTableItem(joined_table, nIndex, nStart2, nCount2))
return;
CString table_item = joined_table.Mid(nStart2, nCount2);
int nStart3, nCount3;
SqlString_JoinCondition(table_item, nStart3, nCount3);
SqlString_Stuff(table_item, nStart3, nCount3, lpszCondition);
SqlString_Stuff(joined_table, nStart2, nCount2, table_item);
SqlString_Stuff(*this, nStart1, nCount1, joined_table);
}
void CSqlString::SetJoinCondition(LPCTSTR lpszTableAlias, LPCTSTR lpszCondition)
{
int nStart1, nCount1;
SqlString_JoinedTable(*this, nStart1, nCount1);
CString joined_table = Mid(nStart1, nCount1);
int nStart2, nCount2;
CString strTableAlias(lpszTableAlias);
SqlString_Alias(strTableAlias);
int nIndex = SqlString_JoinedTableIndex(joined_table, strTableAlias);
if (!SqlString_JoinedTableItem(joined_table, nIndex, nStart2, nCount2))
return;
CString table_item = joined_table.Mid(nStart2, nCount2);
int nStart3, nCount3;
SqlString_JoinCondition(table_item, nStart3, nCount3);
SqlString_Stuff(table_item, nStart3, nCount3, lpszCondition);
SqlString_Stuff(joined_table, nStart2, nCount2, table_item);
SqlString_Stuff(*this, nStart1, nCount1, joined_table);
}
// search_condition, group_by_expression and sort_expression
CString CSqlString::GetSearchCondition(BOOL bCompute /* = FALSE */)
{
int nIndex, nCount;
SqlString_SearchCondition(*this, nIndex, nCount, bCompute);
return Mid(nIndex, nCount);
}
CString CSqlString::GetGroupByExpression()
{
int nIndex, nCount;
SqlString_GroupByExpression(*this, nIndex, nCount);
return Mid(nIndex, nCount);
}
CString CSqlString::GetSortExpression()
{
int nIndex, nCount;
SqlString_SortExpression(*this, nIndex, nCount);
return Mid(nIndex, nCount);
}
void CSqlString::SetSearchCondition(LPCTSTR lpszCondition, BOOL bCompute /* = FALSE */)
{
int nIndex, nCount;
if (!SqlString_SearchCondition(*this, nIndex, nCount, bCompute))
SqlString_Insert(*this, nIndex, bCompute ? _T("having ") : _T("where "));
SqlString_Stuff(*this, nIndex, nCount, lpszCondition);
}
void CSqlString::SetGroupByExpression(LPCTSTR lpszExpression)
{
int nIndex, nCount;
if (!SqlString_GroupByExpression(*this, nIndex, nCount))
SqlString_Insert(*this, nIndex, _T("group by "));
SqlString_Stuff(*this, nIndex, nCount, lpszExpression);
}
void CSqlString::SetSortExpression(LPCTSTR lpszExpression)
{
int nIndex, nCount;
if (!SqlString_SortExpression(*this, nIndex, nCount))
SqlString_Insert(*this, nIndex, _T("order by "));
SqlString_Stuff(*this, nIndex, nCount, lpszExpression);
}
//
//
//
//// CXmlString。
//// 有关类定义的信息,请参阅 DataLink.h
//
//CXmlString::CXmlString()
//{
//}
//
//CXmlString::CXmlString(const CString& stringSrc) : CString(stringSrc)
//{
//}
//
//CXmlString::CXmlString(LPCTSTR lpsz) : CString(lpsz)
//{
//}
//
//CXmlString::CXmlString(LPCTSTR lpch, int nLength) : CString(lpch, nLength)
//{
//}
//
//CXmlString::~CXmlString()
//{
//}
//
//void CXmlString::SetAttribute(LPCTSTR keyName, LPCTSTR value)
//{
// LPCTSTR lpchLeft, lpchRight;
// lpchLeft = lpchRight = LPCTSTR(*this);
// TCHAR szKeyName[_MAX_PATH], szValue[_MAX_PATH];
// while (lpchRight != NULL)
// {
// lpchRight = _tsplitattr(lpchRight, szKeyName, NULL, _T(' '));
// if (_tcsicmp(szKeyName, keyName) == 0)
// break;
// lpchLeft = lpchRight;
// }
//
// _tcscpy2(szKeyName, keyName, _T('='));
// _tcscpy2(szValue, value, _T('\"'));
// CString strItem;
// if (_tcscspn(value, _T(" \'\"")) < (size_t)lstrlen(value))
// strItem.Format(_T(" %s=\"%s\" "), szKeyName, szValue);
// else
// strItem.Format(_T(" %s=%s "), szKeyName, szValue);
//
// int nStart = (lpchLeft == NULL) ? GetLength() : (int)(lpchLeft - LPCTSTR(*this));
// int nEnd = (lpchRight == NULL) ? GetLength() : (int)(lpchRight -LPCTSTR(*this));
// Delete(nStart, nEnd - nStart);
// Insert(nStart, strItem);
//}
//
//CString CXmlString::GetAttribute(LPCTSTR keyName)
//{
// CString value;
// LPCTSTR lpFullString = LPCTSTR(*this);
// TCHAR szKeyName[1024], szValue[1024];
// while (lpFullString != NULL)
// {
// lpFullString = _tsplitattr(lpFullString, szKeyName, szValue, _T(' '));
// if (_tcsicmp(szKeyName, keyName) == 0)
// {
// value.SetString(szValue);
// break;
// }
// }
// return value;
//}
//
//CString CXmlString::GetAttributeName(int nIndex)
//{
// CString keyName;
// LPCTSTR lpFullString = LPCTSTR(*this);
// TCHAR szKeyName[_MAX_PATH];
// while (nIndex >= 0)
// {
// if (lpFullString == NULL)
// return keyName;
// lpFullString = _tsplitattr(lpFullString, szKeyName, NULL, _T(' '));
// nIndex--;
// }
// keyName.SetString(szKeyName);
// return keyName;
//}
// CRecordset class 构造/析构函数
IMPLEMENT_DYNAMIC(CRecordset, CObject)
CRecordset::CRecordset(IDispatch* pConnection /*= NULL*/)
{
m_pRecordset = NULL;
m_pConnection = pConnection;
m_bAutoRelease = FALSE;
// 初始化状态
m_bBOF = m_bEOF = TRUE;
m_bScrollable = m_bAppendable = TRUE;
m_bEOFSeen = m_bDeleted = m_bUpdatable = FALSE;
m_lCurrentRecord = AFX_CURRENT_RECORD_UNDEFINED;
m_lRecordCount = 0L;
m_nFields = m_nParams = 0;
m_dwOptions = 0;
}
CRecordset::~CRecordset()
{
// 关闭数据连接
Close();
ASSERT(m_pRecordset == NULL);
if (m_bAutoRelease)
ASSERT(m_pConnection == NULL);
}
BOOL CRecordset::CanRestart() const
{
return TRUE;
}
BOOL CRecordset::CanTransact() const
{
//ASSERT(m_pDatabase != NULL );
//if( m_pDatabase == NULL )
// AfxThrowUserException();
//ASSERT(m_pDatabase->IsOpen());
//return m_pDatabase->m_bTransactions;
return TRUE;
}
BOOL CRecordset::CanBookmark() const
{
//ASSERT_VALID(this);
//ASSERT(m_pDatabase->IsOpen());
//if (!(m_dwOptions & useBookmarks) ||
// (m_nOpenType == forwardOnly && !(m_dwOptions & useExtendedFetch)))
// return FALSE;
//return m_pDatabase->GetBookmarkPersistence() & SQL_BP_SCROLL;
return TRUE;
}
BOOL CRecordset::IsOpen() const
{
_RecordsetPtr pRecordset = (_RecordsetPtr)m_pRecordset;
ASSERT(pRecordset != NULL);
return pRecordset->GetState() & adStateOpen;
}
BOOL CRecordset::IsFieldNull(int nIndex)
{
_RecordsetPtr pRecordset = (_RecordsetPtr)m_pRecordset;
pRecordset->GetFields()->GetItem(COleVariant((long)nIndex));
_variant_t var = pRecordset->GetCollect(COleVariant((long)nIndex));
return (var.vt == VT_EMPTY);
}
BOOL CRecordset::IsFieldNull(LPCTSTR lpszName)
{
_RecordsetPtr pRecordset = (_RecordsetPtr)m_pRecordset;
pRecordset->GetFields()->GetItem(lpszName);
_variant_t var = pRecordset->GetCollect(_variant_t(lpszName));
return (var.vt == VT_EMPTY);
}
BOOL CRecordset::IsFieldNullable(int nIndex)
{
return TRUE;
}
BOOL CRecordset::IsFieldNullable(LPCTSTR lpszName)
{
return TRUE;
}
BOOL CRecordset::Open(LPCTSTR lpszSQL /* = NULL */, DWORD dwOptions /* = NULL*/)
{
// 打开可用的连接
if (m_pConnection == NULL)
{
_AFX_DATALINK_STATE* pState = _afxDataLinkState.GetData();
CConnectionString connection = pState->m_pszConnectionString;
m_pConnection = connection.OpenConnection();
if (m_pConnection == NULL) // 连接没有打开
return FALSE;
// 数据库连接自动关闭
m_bAutoRelease = TRUE;
}
// 创建记录集对象
if (m_pRecordset == NULL)
{
_RecordsetPtr pRecordset;
pRecordset.CreateInstance("ADODB.Recordset");
if (pRecordset == NULL)
{
ASSERT(FALSE);
return FALSE;
}
m_pRecordset = pRecordset;
m_pRecordset->AddRef();
}
// 如果 lpszSQL is NULL, 重新打开数据集
if (lpszSQL != NULL)
m_strSQL = lpszSQL;
CursorTypeEnum cursorType = adOpenDynamic; // 动态
LockTypeEnum lockType = adLockBatchOptimistic; // 乐观封锁型
_RecordsetPtr pRecordset = (_RecordsetPtr)m_pRecordset;
// 先关闭已经打开的记录集
ASSERT(pRecordset != NULL);
if (pRecordset->GetState() & adStateOpen)
pRecordset->raw_Close();
// 打开记录集
pRecordset->CursorLocation = adUseClient;
HRESULT _hr = pRecordset->raw_Open(COleVariant(m_strSQL),
_variant_t(m_pConnection), cursorType, lockType, adCmdUnknown );
if (FAILED(_hr))
{
_ConnectionPtr pConnection = (_ConnectionPtr)m_pConnection;
_SetErrorInfo(pConnection->Errors);
return FALSE;
}
m_bBOF = pRecordset->BOF;
m_bEOF = pRecordset->IsEOF;
return TRUE;
}
void CRecordset::Cancel()
{
try
{
_RecordsetPtr pRecordset = (_RecordsetPtr)m_pRecordset;
pRecordset->Cancel();
}
catch (_com_error e)
{
TRACE(_T("Warning: Cancel发生异常. 错误信息: %s; 描述: %s\n"), e.ErrorMessage(), (LPCTSTR)e.Description());
}
}
void CRecordset::Close()
{
try
{
// 关闭数据集
_RecordsetPtr pRecordset = (_RecordsetPtr)m_pRecordset;
if (pRecordset != NULL)
{
//if (pRecordset->GetEditMode() == adEditNone)
// pRecordset->CancelUpdate();
if (pRecordset->State != adStateClosed)
pRecordset->Close();
m_pRecordset->Release();
m_pRecordset = NULL;
}
// 关闭数据链接
_ConnectionPtr pConnection = (_ConnectionPtr)m_pConnection;
if (m_bAutoRelease && pConnection != NULL)
{
if (pConnection->State != adStateClosed)
pConnection->Close();
m_pConnection->Release();
m_pConnection = NULL;
}
}
catch (_com_error e)
{
TRACE(_T("Warning: 关闭记录集发生异常. 错误信息: %s; 描述: %s\n"), e.ErrorMessage(), (LPCTSTR)e.Description());
}
}
void CRecordset::Move(long nRows, WORD wFetchType /*= SQL_FETCH_RELATIVE*/)
{
_RecordsetPtr pRecordset = (_RecordsetPtr)m_pRecordset;
if (pRecordset == NULL)
return;
RETCODE nRetCode = 0;
switch(wFetchType)
{
case SQL_FETCH_NEXT:
pRecordset->raw_MoveNext();
_AfxSetCur
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -