uint32 CSqlDataBase::QueryTable() { if(!m_oSelect.m_pAccess) return 0; CMdbFilter* pFilter = m_oSelect.m_pAccess->GetResultFilter(); uint32 nSize = m_oSelect.m_oFieldTable.GetSize(); for(uint32 i=0; i<nSize; ++i) pFilter->SetField(m_oSelect.m_pAccess->GetFieldNo(m_oSelect.m_oFieldTable[i].GetStr())); uint32 nRet; if(m_oSelect.m_bError) return 0; uint32 nTotal = 0; CString oList = GetFieldList(); while(true) { nRet = m_oSelect.m_pAccess->Query(m_nResultSize, nTotal); m_oSelect.m_pRecordSet = m_oSelect.m_pAccess->GetResultSet(); if(nRet) { FocpCmdLogEx("SQL", FOCP_LOG_ERROR, ("CSqlDataBase::QueryTable(%s.%s) failure: %s", m_pDb->GetDbName(), m_oSelect.m_oName.GetStr(), GetMdbError(nRet))); m_pEnv->Flush(false); m_oSelect.UnBind(); return 0; } else { if(!nTotal) { FocpCmdLogEx("SQL", FOCP_LOG_CLOSE, ("%s", oList.GetStr())); m_pEnv->Flush(false); } uint32 nCount = m_oSelect.GetResultCount(); for(uint32 i=0; i<nCount; ++i) { CMdbResult* pRecord = m_oSelect.GetRecord(i); FocpCmdLogEx("SQL", FOCP_LOG_CLOSE, ("%s", GetRecordText(pRecord, oList).GetStr())); m_pEnv->Flush(false); } nTotal += nCount; if(nCount < m_nResultSize) break; } } FocpCmdLogEx("SQL", FOCP_LOG_CLOSE, ("\r\ntotal %u records in %s.%s", nTotal, m_pDb->GetDbName(), m_oSelect.m_oName.GetStr())); m_oSelect.UnBind(); return 0; }
BOOL CBCGPODBCGridCtrl::OpenSQL (LPCTSTR lpszSQL) { ASSERT (lpszSQL != NULL); m_strSQL.Empty (); RemoveAll (); if (!m_bIsSorting) { DeleteAllColumns (); } if (m_pDataBase == NULL) { ASSERT (FALSE); return FALSE; } try { ASSERT_VALID (m_pDataBase); if (!m_pDataBase->IsOpen ()) { ASSERT (FALSE); return FALSE; } if (m_pRecordSet != NULL) { ASSERT_VALID (m_pRecordSet); if (m_pRecordSet->IsOpen ()) { m_pRecordSet->Close (); } delete m_pRecordSet; m_pRecordSet = NULL; } //--------------------------------------------------------- // Create a new record set and open it using SQL statement: //--------------------------------------------------------- m_pRecordSet = new CRecordset (m_pDataBase); if (!m_pRecordSet->Open (CRecordset::dynaset, lpszSQL)) { return FALSE; } int nColumns = 0; if (!m_bIsSorting) { //------------- // Add columns: //------------- CStringList lstField; if (!GetFieldList (lstField)) { return FALSE; } int nColumn = 0; for (POSITION pos = lstField.GetHeadPosition (); pos != NULL; nColumn++) { InsertColumn (nColumn, lstField.GetNext (pos), 50); } nColumns = (int) lstField.GetCount (); } else { nColumns = GetColumnCount (); } if (nColumns == 0) { // No columns AdjustLayout (); return TRUE; } //------------- // Add records: //------------- if (m_pRecordSet->IsEOF () && m_pRecordSet->IsBOF ()) { // The table is empty AdjustLayout (); return TRUE; } if (m_bVirtualMode) { while (!m_pRecordSet->IsEOF ()) { m_pRecordSet->MoveNext (); } SetVirtualRows (max (0, m_pRecordSet->GetRecordCount ())); } else { for (int nRow = 0; !m_pRecordSet->IsEOF (); m_pRecordSet->MoveNext (), nRow++) { CBCGPGridRow* pRow = CreateRow (nColumns); ASSERT_VALID (pRow); for (int nColumn = 0; nColumn < nColumns; nColumn++) { OnAddData (pRow, nColumn, nRow); } if (OnBeforeAddRow (pRow, nRow)) { AddRow (pRow, FALSE); } else { delete pRow; } } } m_strSQL = lpszSQL; AdjustLayout (); if (!m_pRecordSet->CanUpdate ()) { SetReadOnly (); } } catch (CDBException* pEx) { OnODBCException (pEx); pEx->Delete (); return FALSE; } return TRUE; }