home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CATALG.PAK / TABLESET.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  3.5 KB  |  139 lines

  1. // sqltable.cpp : implementation of the CTables class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13.  
  14. #include "stdafx.h"
  15. #include "tableset.h"
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CTables implementation
  19.  
  20. IMPLEMENT_DYNAMIC(CTables, CRecordset)
  21.  
  22. CTables::CTables(CDatabase* pDatabase)
  23.     : CRecordset(pDatabase)
  24. {
  25.     //{{AFX_FIELD_INIT(CTables)
  26.     m_strQualifier = "";
  27.     m_strOwner = "";
  28.     m_strName = "";
  29.     m_strType = "";
  30.     m_strRemarks = "";
  31.     m_nFields = 5;
  32.     //}}AFX_FIELD_INIT
  33.     m_strQualifierParam = "";
  34.     m_strOwnerParam = "";
  35.     m_strNameParam = "";
  36.     m_strTypeParam = "";
  37. }
  38.  
  39. BOOL CTables::Open(UINT nOpenType /* = snapshot */,
  40.     LPCSTR lpszSQL /* = NULL */, DWORD dwOptions /* = none */)
  41. {
  42.     RETCODE nRetCode;
  43.     ASSERT(lpszSQL == NULL);
  44.  
  45.     dwOptions;  // not used in release build
  46.     nOpenType;
  47.     lpszSQL;
  48.  
  49.     // Allocation and opening of database not supported
  50.     if (m_hstmt == SQL_NULL_HSTMT)
  51.     {
  52.         CString strDefaultConnect;
  53.         TRY
  54.         {
  55.             if (m_pDatabase == NULL)
  56.             {
  57.                 m_pDatabase = new CDatabase();
  58.                 m_bRecordsetDb = TRUE;
  59.             }
  60.  
  61.             strDefaultConnect = GetDefaultConnect();
  62.             // If not already opened, attempt to open
  63.             if (!m_pDatabase->IsOpen() &&
  64.                 !m_pDatabase->Open("", FALSE, FALSE, strDefaultConnect))
  65.                 return FALSE;
  66.  
  67.             AFX_SQL_SYNC(::SQLAllocStmt(m_pDatabase->m_hdbc, &m_hstmt));
  68.             if (!Check(nRetCode))
  69.                 ThrowDBException(SQL_INVALID_HANDLE);
  70.         }
  71.         CATCH_ALL(e)
  72.         {
  73. #ifdef _DEBUG
  74.             if (afxTraceFlags & 0x20)
  75.                 TRACE0("Error: CDatabase create for CRecordset failed\n");
  76. #endif // _DEBUG
  77.             strDefaultConnect.Empty();
  78.             if (m_bRecordsetDb)
  79.             {
  80.                 delete m_pDatabase;
  81.                 m_pDatabase = NULL;
  82.             }
  83.             ASSERT(m_hstmt == SQL_NULL_HSTMT);
  84.             THROW_LAST();
  85.         }
  86.         END_CATCH_ALL
  87.     }
  88.  
  89.     TRY
  90.     {
  91.         // set any options, like timeouts, scrolling options
  92.         OnSetOptions(m_hstmt);
  93.  
  94.         // call the ODBC catalog function with data member params
  95.         AFX_SQL_ASYNC(this, (::SQLTables)(m_hstmt,
  96.             (m_strQualifierParam.IsEmpty()? (UCHAR FAR *)NULL: (UCHAR FAR *)(const char*)m_strQualifierParam), SQL_NTS,
  97.             (m_strOwnerParam.IsEmpty()? (UCHAR FAR *)NULL: (UCHAR FAR *)(const char*)m_strOwnerParam), SQL_NTS,
  98.             (m_strNameParam.IsEmpty()? (UCHAR FAR *)NULL: (UCHAR FAR *)(const char*)m_strNameParam), SQL_NTS,
  99.             (m_strTypeParam.IsEmpty()? (UCHAR FAR *)NULL: (UCHAR FAR *)(const char*)m_strTypeParam), SQL_NTS));
  100.         if (!Check(nRetCode))
  101.         {
  102.             AfxThrowDBException(nRetCode, m_pDatabase, m_hstmt);
  103.         }
  104.         // load first record
  105.         MoveFirst();
  106.     }
  107.     CATCH_ALL(e)
  108.     {
  109.         Close();
  110.         THROW_LAST();
  111.     }
  112.     END_CATCH_ALL
  113.     return TRUE;
  114. }
  115.  
  116. CString CTables::GetDefaultConnect()
  117. {
  118.     return "ODBC;";
  119. }
  120.  
  121. CString CTables::GetDefaultSQL()
  122. {
  123.     // should SQLTables directly, so GetSQL should never be called
  124.     ASSERT(FALSE);
  125.     return "!";
  126. }
  127.  
  128. void CTables::DoFieldExchange(CFieldExchange* pFX)
  129. {
  130.     //{{AFX_FIELD_MAP(CTables)
  131.     pFX->SetFieldType(CFieldExchange::outputColumn);
  132.     RFX_Text(pFX, "table_qualifier", m_strQualifier);
  133.     RFX_Text(pFX, "table_owner", m_strOwner);
  134.     RFX_Text(pFX, "table_name", m_strName);
  135.     RFX_Text(pFX, "table_type", m_strType);
  136.     RFX_Text(pFX, "remarks", m_strRemarks);
  137.     //}}AFX_FIELD_MAP
  138. }
  139.