⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 daocore.cpp

📁 一个集分词、词性标注和格式转换的强大的工具包
💻 CPP
📖 第 1 页 / 共 5 页
字号:
		var.Clear();
	}

	if (dwOptions & AFX_DAO_FETCH_ALL_PROPERTIES)
	{
		DAO_CHECK(pDAOWorkspace->get_IsolateODBCTrans(&nBool));
		wsinfo.m_bIsolateODBCTrans = nBool == AFX_DAO_TRUE;
	}
}

void CDaoWorkspace::InitDatabasesCollection()
{
	ASSERT_VALID(this);

	DAO_CHECK(m_pDAOWorkspace->get_Databases(&m_pDAODatabases));
}

void CDaoWorkspace::FillDatabaseInfo(DAODatabase* pDAODatabase,
	CDaoDatabaseInfo& dbinfo, DWORD dwOptions)
{
	ASSERT_VALID(this);
	ASSERT(pDAODatabase != NULL);
	ASSERT(dwOptions != 0);

	COleVariant var;
	short nBool;

	if (dwOptions & AFX_DAO_FETCH_PRIMARY_PROPERTIES)
	{
		DAO_CHECK(pDAODatabase->get_Name(&V_BSTR(&var)));
		var.vt = VT_BSTR;
		dbinfo.m_strName = V_BSTRT(&var);
		var.Clear();

		DAO_CHECK(pDAODatabase->get_Updatable(&nBool));
		dbinfo.m_bUpdatable = nBool == AFX_DAO_TRUE;

		DAO_CHECK(pDAODatabase->get_Transactions(&nBool));
		dbinfo.m_bTransactions = nBool == AFX_DAO_TRUE;
	}

	if (dwOptions & AFX_DAO_FETCH_SECONDARY_PROPERTIES)
	{
		DAO_CHECK(pDAODatabase->get_Version(
			&V_BSTR(&var)));
		var.vt = VT_BSTR;
		dbinfo.m_strVersion = V_BSTRT(&var);
		var.Clear();

		DAO_CHECK(pDAODatabase->get_CollatingOrder(
			&dbinfo.m_lCollatingOrder));

		DAO_CHECK(pDAODatabase->get_QueryTimeout(
			&dbinfo.m_nQueryTimeout));
	}

	if (dwOptions & AFX_DAO_FETCH_ALL_PROPERTIES)
	{
		DAO_CHECK(pDAODatabase->get_Connect(
			&V_BSTR(&var)));
		var.vt = VT_BSTR;
		dbinfo.m_strConnect = V_BSTRT(&var);
		var.Clear();
	}
}

void CDaoWorkspace::ThrowDaoException(int nAfxDaoError)
{
	ASSERT_VALID(this);

	AfxThrowDaoException(nAfxDaoError);
}

#ifdef _DEBUG
void CDaoWorkspace::AssertValid() const
{
	CObject::AssertValid();
}

void CDaoWorkspace::Dump(CDumpContext& dc) const
{
	ASSERT_VALID(this);

	CObject::Dump(dc);

	dc << "m_bOpen = " << m_bOpen;
	dc << "\nm_bNew = " << m_bNew;
	dc << "\nm_nStatus = " << m_nStatus;

	dc << "\n";
}
#endif //_DEBUG

//////////////////////////////////////////////////////////////////////////
// CDaoDatabase
IMPLEMENT_DYNAMIC(CDaoDatabase, CObject)

CDaoDatabase::CDaoDatabase(CDaoWorkspace* pWorkspace)
{
	m_bOpen = FALSE;

	m_pDAODatabase = NULL;

	m_pDAOTableDefs = NULL;
	m_pDAORelations = NULL;
	m_pDAOQueryDefs = NULL;
	m_pDAORecordsets = NULL;

	m_pWorkspace = pWorkspace;
	m_nStatus = 0;
}

CDaoDatabase::~CDaoDatabase()
{
	if (IsOpen())
		Close();

	// Clean up workspace if necessary
	if (m_pWorkspace != NULL && (m_nStatus & AFX_DAO_IMPLICIT_WS))
	{
		m_pWorkspace->Close();
		delete m_pWorkspace;
		m_pWorkspace = NULL;
	}
}

void CDaoDatabase::Create(LPCTSTR lpszName, LPCTSTR lpszLocale,
	int nOptions)
{
	ASSERT_VALID(this);
	ASSERT(!IsOpen());

	// Allocate and maintain workspace if necessary
	InitWorkspace();

	COleVariant varName(lpszName, VT_BSTRT);
	COleVariant varLocale(lpszLocale, VT_BSTRT);

	DAO_CHECK(m_pWorkspace->m_pDAOWorkspace->CreateDatabase(V_BSTR(&varName),
		V_BSTR(&varLocale), COleVariant((long)nOptions),
		&m_pDAODatabase));

	m_bOpen = TRUE;

	// Add the database to map of Open CDaoDatabases
	m_pWorkspace->m_mapDatabases.SetAt(this, this);
}

void CDaoDatabase::Open(LPCTSTR lpszName, BOOL bExclusive,
	BOOL bReadOnly, LPCTSTR lpszConnect)
{
	ASSERT_VALID(this);

	// Re-Opening is invalid.
	if (IsOpen())
	{
		ASSERT(FALSE);
		return;
	}

	// Allocate, maintain and/or open workspace if necessary
	InitWorkspace();

	COleVariant var(lpszName, VT_BSTRT);

	DAO_CHECK(m_pWorkspace->m_pDAOWorkspace->OpenDatabase(
		V_BSTR(&var),
		COleVariant((long)bExclusive, VT_BOOL),
		COleVariant((long)bReadOnly, VT_BOOL),
		COleVariant(lpszConnect, VT_BSTRT),
		&m_pDAODatabase));

	m_bOpen = TRUE;

	// Add the database to map of Open CDaoDatabases
	m_pWorkspace->m_mapDatabases.SetAt(this, this);
}

// Disconnect connection
void CDaoDatabase::Close()
{
	ASSERT_VALID(this);

	if (m_pDAORecordsets != NULL)
	{
		m_pDAORecordsets->Release();
		m_pDAORecordsets = NULL;
	}

	if (m_pDAOQueryDefs != NULL)
	{
		m_pDAOQueryDefs->Release();
		m_pDAOQueryDefs = NULL;
	}

	if (m_pDAORelations != NULL)
	{
		m_pDAORelations->Release();
		m_pDAORelations = NULL;
	}

	if (m_pDAOTableDefs != NULL)
	{
		m_pDAOTableDefs->Release();
		m_pDAOTableDefs = NULL;
	}

	// Close any Open CDaoRecordsets
	void* pvKey;
	void* pvObject;
	POSITION pos = m_mapRecordsets.GetStartPosition();
	while (pos != NULL)
	{
		m_mapRecordsets.GetNextAssoc(pos, pvKey, pvObject);
		((CDaoRecordset*)pvObject)->Close();
	}
	m_mapRecordsets.RemoveAll();

	// Close any Open CDaoQueryDefs
	pos = m_mapQueryDefs.GetStartPosition();
	while (pos != NULL)
	{
		m_mapQueryDefs.GetNextAssoc(pos, pvKey, pvObject);
		((CDaoQueryDef*)pvObject)->Close();
	}
	m_mapQueryDefs.RemoveAll();

	// Close any Open CDaoTableDefs
	pos = m_mapTableDefs.GetStartPosition();
	while (pos != NULL)
	{
		m_mapTableDefs.GetNextAssoc(pos, pvKey, pvObject);
		((CDaoTableDef*)pvObject)->Close();
	}
	m_mapTableDefs.RemoveAll();

	if (m_pDAODatabase != NULL)
	{
		// If implicit database, don't close.
		// It will be automatically closed when ref count 0.
		if (!(m_nStatus & AFX_DAO_IMPLICIT_DB))
			DAO_TRACE(m_pDAODatabase->Close());

		m_pDAODatabase->Release();
		m_pDAODatabase = NULL;
	}

	m_bOpen = FALSE;

	// Remove the CDaoDatabase from the CDaoWorkspace's map
	m_pWorkspace->m_mapDatabases.RemoveKey(this);
}

BOOL CDaoDatabase::CanUpdate()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());
	ASSERT(m_pDAODatabase != NULL);

	short nUpdatable;
	DAO_CHECK(m_pDAODatabase->get_Updatable(&nUpdatable));
	return nUpdatable == AFX_DAO_TRUE;
}

BOOL CDaoDatabase::CanTransact()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());
	ASSERT(m_pDAODatabase != NULL);

	short nTransactable;
	DAO_CHECK(m_pDAODatabase->get_Transactions(&nTransactable));
	return nTransactable == AFX_DAO_TRUE;
}

CString CDaoDatabase::GetName()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());
	ASSERT(m_pDAODatabase != NULL);

	COleVariant var;
	DAO_CHECK(m_pDAODatabase->get_Name(&V_BSTR(&var)));
	var.vt = VT_BSTR;
	return V_BSTRT(&var);
}

CString CDaoDatabase::GetConnect()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());
	ASSERT(m_pDAODatabase != NULL);

	COleVariant var;
	DAO_CHECK(m_pDAODatabase->get_Connect(&V_BSTR(&var)));
	var.vt = VT_BSTR;
	return V_BSTRT(&var);
}

CString CDaoDatabase::GetVersion()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());
	ASSERT(m_pDAODatabase != NULL);

	COleVariant var;
	DAO_CHECK(m_pDAODatabase->get_Version(&V_BSTR(&var)));
	var.vt = VT_BSTR;
	return V_BSTRT(&var);
}

short CDaoDatabase::GetQueryTimeout()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());
	ASSERT(m_pDAODatabase != NULL);

	short nQueryTimeout;
	DAO_CHECK(m_pDAODatabase->get_QueryTimeout(&nQueryTimeout));
	return nQueryTimeout;
}

void CDaoDatabase::SetQueryTimeout(short nSeconds)
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());
	ASSERT(m_pDAODatabase != NULL);

	DAO_CHECK(m_pDAODatabase->put_QueryTimeout(nSeconds));
}

long CDaoDatabase::GetRecordsAffected()
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());
	ASSERT(m_pDAODatabase != NULL);

	long lRecordsAffected;
	DAO_CHECK(m_pDAODatabase->get_RecordsAffected(&lRecordsAffected));
	return lRecordsAffected;
}

void CDaoDatabase::DeleteTableDef(LPCTSTR lpszName)
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	if (m_pDAOTableDefs == NULL)
		InitTableDefsCollection();

	COleVariant var(lpszName, VT_BSTRT);
	DAO_CHECK(m_pDAOTableDefs->Delete(V_BSTR(&var)));
}

void CDaoDatabase::DeleteQueryDef(LPCTSTR lpszName)
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	if (m_pDAOQueryDefs == NULL)
		InitQueryDefsCollection();

	COleVariant var(lpszName, VT_BSTRT);
	DAO_CHECK(m_pDAOQueryDefs->Delete(V_BSTR(&var)));
}

void CDaoDatabase::CreateRelation(LPCTSTR lpszName, LPCTSTR lpszTable,
	LPCTSTR lpszForeignTable, long lAttributes, LPCTSTR lpszField,
	LPCTSTR lpszForeignField)
{
	ASSERT_VALID(this);

	CDaoRelationInfo relinfo;
	CDaoRelationFieldInfo fieldinfo;

	relinfo.m_strName = lpszName;
	relinfo.m_strTable = lpszTable;
	relinfo.m_strForeignTable = lpszForeignTable;
	relinfo.m_lAttributes = lAttributes;
	relinfo.m_nFields = 1;

	relinfo.m_pFieldInfos = &fieldinfo;
	relinfo.m_pFieldInfos->m_strName = lpszField;
	relinfo.m_pFieldInfos->m_strForeignName = lpszForeignField;

	CreateRelation(relinfo);
}

void CDaoDatabase::CreateRelation(CDaoRelationInfo& relinfo)
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());
	ASSERT(relinfo.m_nFields > 0);

	// Initialize relations collection so that relation can be appended later
	if (m_pDAORelations == NULL)
		InitRelationsCollection();

	DAORelation* pDAORelation = NULL;
	DAOFields* pDAOFields = NULL;
	DAOField* pDAOField = NULL;

	// Create the relation
	DAO_CHECK(m_pDAODatabase->CreateRelation(
		COleVariant(relinfo.m_strName, VT_BSTRT),
		COleVariant(relinfo.m_strTable, VT_BSTRT),
		COleVariant(relinfo.m_strForeignTable, VT_BSTRT),
		COleVariant(relinfo.m_lAttributes), &pDAORelation));

	TRY
	{
		// Get the fields collection for later append of created field
		DAO_CHECK(pDAORelation->get_Fields(&pDAOFields));

		// Create field(s) and set the name and foreign name
		for (int nIndex = 0; nIndex < relinfo.m_nFields; nIndex++)
		{
			DAO_CHECK(pDAORelation->CreateField(
				COleVariant(relinfo.m_pFieldInfos[nIndex].m_strName, VT_BSTRT),
				_afxOptionalVariant, _afxOptionalVariant, &pDAOField));

			COleVariant var(relinfo.m_pFieldInfos[nIndex].m_strForeignName, VT_BSTRT);
			DAO_CHECK(pDAOField->put_ForeignName(V_BSTR(&var)));

			// Append the field to relation fields collection and release
			DAO_CHECK(pDAOFields->Append(pDAOField));
			pDAOField->Release();
		}

		DAO_CHECK(m_pDAORelations->Append(pDAORelation));
	}
	CATCH_ALL(e)
	{
		// Clean up before throw
		if (pDAOField != NULL)
			pDAOField->Release();

		if (pDAOFields != NULL)
			pDAOFields->Release();

		pDAORelation->Release();
		THROW_LAST();
	}
	END_CATCH_ALL

	// Clean up
	if (pDAOField != NULL)
		pDAOField->Release();

	pDAOFields->Release();
	pDAORelation->Release();
}

void CDaoDatabase::DeleteRelation(LPCTSTR lpszName)
{
	ASSERT_VALID(this);
	ASSERT(IsOpen());

	if (m_pDAORelations == NULL)
		InitRelationsCollection();

	COleVariant var(lpszName, VT_BSTRT);
	DAO_CHECK(m_pDAORelations->Delete(V_BSTR(&var)));
}

void CDaoDatabase::Execute(LPCTSTR lpszSQL, int nOptions)
{
	ASSERT_VALID(this);
	ASSERT(m_pDAODatabase != NULL);

	COleVariant var(lpszSQL, VT_BSTRT);
	DAO_CHECK(m_pDAODatabase->Execute(
		V_BSTR(&var), COleVariant((long)nOptions)));
}

short CDaoDatabase::GetTableDefCount()
{
	ASSERT_VALID(this);

	short nTables;

	if (m_pDAOTableDefs == NULL)
		InitTableDefsCollection();

	DAO_CHECK(m_pDAOTableDefs->get_Count(&nTables));
	return nTables;
}

void CDaoDatabase::GetTableDefInfo(int nIndex, CDaoTableDefInfo& tabledefinfo,
	DWORD dwInfoOptions)
{
	ASSERT_VALID(this);

	if (m_pDAOTableDefs == NULL)
		InitTableDefsCollection();

	// Get DAOTableDef object and fill in table info struct
	DAOTableDef* pDAOTableDef;
	DAO_CHECK(m_pDAOTableDefs->get_Item(
		COleVariant((long)nIndex), &pDAOTableDef));
	FillTableDefInfo(pDAOTableDef, tabledefinfo, dwInfoOptions);

	// Clean up
	pDAOTableDef->Release();
}

void CDaoDatabase::GetTableDefInfo(LPCTSTR lpszName,
	CDaoTableDefInfo&  tabledefinfo, DWORD dwInfoOptions)
{
	ASSERT_VALID(this);

	if (m_pDAOTableDefs == NULL)
		InitTableDefsCollection();

	// Get DAOTableDef object and fill in table info struct
	DAOTableDef* pDAOTableDef;
	DAO_CHECK(m_pDAOTableDefs->get_Item(
		COleVariant(lpszName, VT_BSTRT), &pDAOTableDef));
	FillTableDefInfo(pDAOTableDef, tabledefinfo, dwInfoOptions);

	// Clean up
	pDAOTableDef->Release();
}

short CDaoDatabase::GetRelationCount()
{
	ASSERT_VALID(this);

	short nRelations;

	if (m_pDAORelations == NULL)
		InitRelationsCollection();

	DAO_CHECK(m_pDAORelations->get_Count(&nRelations));
	return nRelations;
}

void CDaoDatabase::GetRelationInfo(int nIndex, CDaoRelationInfo& relinfo,
	DWORD dwInfoOptions)
{
	ASSERT_VALID(this);

	if (m_pDAORelations == NULL)
		InitRelationsCollection();

	// Get DAORelation object and fill in relation info struct
	DAORelation* pDAORelation;
	DAO_CHECK(m_pDAORelations->get_Item(
		COleVariant((long)nIndex), &pDAORelation));
	FillRelationInfo(pDAORelation, relinfo, dwInfoOptions);

	// Clean up
	pDAORelation->Release();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -