home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c10 / schema / schemaview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  7.2 KB  |  314 lines

  1. // schemaView.cpp : implementation of the CSchemaView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "schema.h"
  6.  
  7. #include "schemaDoc.h"
  8. #include "schemaView.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CSchemaView
  18.  
  19. IMPLEMENT_DYNCREATE(CSchemaView, CView)
  20.  
  21. BEGIN_MESSAGE_MAP(CSchemaView, CView)
  22.     //{{AFX_MSG_MAP(CSchemaView)
  23.     ON_COMMAND(ID_SCHEMA_DATABASE, OnSchemaDatabase)
  24.     ON_COMMAND(ID_SCHEMA_TABLE, OnSchemaTable)
  25.     ON_COMMAND(ID_SCHEMA_ADDRECORD, OnSchemaAddrecord)
  26.     //}}AFX_MSG_MAP
  27.     // Standard printing commands
  28.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  29.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  30.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  31. END_MESSAGE_MAP()
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CSchemaView construction/destruction
  35.  
  36. CSchemaView::CSchemaView()
  37. {
  38.     // TODO: add construction code here
  39.  
  40. }
  41.  
  42. CSchemaView::~CSchemaView()
  43. {
  44. }
  45.  
  46. BOOL CSchemaView::PreCreateWindow(CREATESTRUCT& cs)
  47. {
  48.     // TODO: Modify the Window class or styles here by modifying
  49.     //  the CREATESTRUCT cs
  50.  
  51.     return CView::PreCreateWindow(cs);
  52. }
  53.  
  54. /////////////////////////////////////////////////////////////////////////////
  55. // CSchemaView drawing
  56.  
  57. void CSchemaView::OnDraw(CDC* pDC)
  58. {
  59.     CSchemaDoc* pDoc = GetDocument();
  60.     ASSERT_VALID(pDoc);
  61.  
  62.     // TODO: add draw code for native data here
  63. }
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CSchemaView printing
  67.  
  68. BOOL CSchemaView::OnPreparePrinting(CPrintInfo* pInfo)
  69. {
  70.     // default preparation
  71.     return DoPreparePrinting(pInfo);
  72. }
  73.  
  74. void CSchemaView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  75. {
  76.     // TODO: add extra initialization before printing
  77. }
  78.  
  79. void CSchemaView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  80. {
  81.     // TODO: add cleanup after printing
  82. }
  83.  
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CSchemaView diagnostics
  86.  
  87. #ifdef _DEBUG
  88. void CSchemaView::AssertValid() const
  89. {
  90.     CView::AssertValid();
  91. }
  92.  
  93. void CSchemaView::Dump(CDumpContext& dc) const
  94. {
  95.     CView::Dump(dc);
  96. }
  97.  
  98. CSchemaDoc* CSchemaView::GetDocument() // non-debug version is inline
  99. {
  100.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSchemaDoc)));
  101.     return (CSchemaDoc*)m_pDocument;
  102. }
  103. #endif //_DEBUG
  104.  
  105. CString CSchemaView::GetDefaultDBName()
  106. {
  107.     return CString("MyDatabase");
  108. }
  109.  
  110. CString CSchemaView::GetDefaultTableName()
  111. {
  112.     return CString("TestTable");
  113. }
  114. CString CSchemaView::GetField1Name()
  115. {
  116.     return CString("Some Text");
  117. }
  118. CString CSchemaView::GetField2Name()
  119. {
  120.     return CString("A Number");
  121. }
  122. CString CSchemaView::GetField3Name()
  123. {
  124.     return CString("DataField");
  125. }
  126.  
  127. /////////////////////////////////////////////////////////////////////////////
  128. // CSchemaView message handlers
  129.  
  130. void CSchemaView::OnSchemaDatabase() 
  131. {
  132.     // Construct the database.
  133.     CDaoDatabase db;
  134.  
  135.     // Create the database.
  136.     try 
  137.     {
  138.         db.Create(GetDefaultDBName());
  139.     } 
  140.     catch (CDaoException* e)
  141.     {
  142.         e->ReportError();
  143.         e->Delete();
  144.     }
  145. }
  146.  
  147. void CSchemaView::OnSchemaTable() 
  148. {
  149.     try 
  150.     {
  151.         // Open the database.
  152.         CDaoDatabase db;
  153.         db.Open(GetDefaultDBName());
  154.  
  155.         // Attempt to delete one if it exists
  156.         try 
  157.         {
  158.             db.DeleteTableDef(GetDefaultTableName());
  159.         } 
  160.         catch (CDaoException* e) 
  161.         {
  162.             e->Delete();    
  163.         }
  164.  
  165.         // Construct and create the table.
  166.         CDaoTableDef table(&db);
  167.         table.Create(GetDefaultTableName());
  168.  
  169.         // Fields must be defined before table can be appended.
  170.  
  171.         // Create a field using one version of CreateField()
  172.         table.CreateField(GetField1Name(), dbText, 255, dbVariableField);
  173.  
  174.           // Create a field using CDaoFieldInfo version of CreateField().
  175.         CDaoFieldInfo fieldinfo;
  176.         fieldinfo.m_strName = GetField2Name();                    // required
  177.         fieldinfo.m_nType = dbInteger;                    // required
  178.         fieldinfo.m_lSize = 2;                            // required
  179.         fieldinfo.m_lAttributes= dbFixedField;            // required
  180.         fieldinfo.m_nOrdinalPosition= 0;    
  181.         fieldinfo.m_bRequired= FALSE;
  182.         fieldinfo.m_bAllowZeroLength= FALSE;            // required
  183.         fieldinfo.m_lCollatingOrder= 0;
  184.         fieldinfo.m_strForeignName= "";
  185.         fieldinfo.m_strSourceField= "";
  186.         fieldinfo.m_strSourceTable= "";
  187.         fieldinfo.m_strValidationRule= "< 100";
  188.         fieldinfo.m_strValidationText= "Value must be less than 100";
  189.         fieldinfo.m_strDefaultValue= "";
  190.  
  191.         table.CreateField( fieldinfo );
  192.  
  193.         // Long Binary field
  194.         table.CreateField(GetField3Name(), dbLongBinary, 0, 0);
  195.  
  196.         // Append the table to the tables collection.
  197.         table.Append();
  198.     } 
  199.     catch (CDaoException* e)
  200.     {
  201.         e->ReportError();
  202.         e->Delete();
  203.     }
  204.     
  205. }
  206.  
  207. void CSchemaView::OnSchemaAddrecord() 
  208. {
  209.     // Open the database.
  210.     CDaoDatabase db;
  211.     db.Open(GetDefaultDBName());
  212.  
  213.     // Construct and open the table.
  214.     CDaoTableDef table(&db);
  215.     table.Open(GetDefaultTableName());
  216.  
  217.     // Create a recordset based upon the table.
  218.     CDaoRecordset rs(&db);
  219.     rs.Open(&table);
  220.  
  221.     // Add a new record.
  222.     rs.AddNew();
  223.  
  224.     // Set the fields of the new record.
  225.     COleVariant varField1("Some text", VT_BSTRT);
  226.     rs.SetFieldValue(GetField1Name(), varField1);
  227.  
  228.     COleVariant varField2( (short) 99);
  229.     rs.SetFieldValue(GetField2Name(), varField2);
  230.  
  231.     // Create an array to copy to table.
  232.     CByteArray ba;
  233.     ba.SetSize(3);
  234.     ba.SetAt(0,1);
  235.     ba.SetAt(1,2);
  236.     ba.SetAt(2,3);
  237.  
  238.     // Copy the array.
  239.     CopyArrayData(rs, ba, FALSE);
  240.  
  241.     // Update the table.
  242.     rs.Update();
  243.  
  244.     rs.MoveLast();
  245.     CByteArray ba2;
  246.     CopyArrayData(rs, ba2, TRUE);
  247.     CString strData;
  248.     strData.Format("Data: ");
  249.     for (int idx=0; idx<ba2.GetSize(); idx++)
  250.     {
  251.         CString val;
  252.         val.Format(" %d", ba2.GetAt(idx));
  253.         strData += val;
  254.     }
  255.     AfxMessageBox(strData);
  256.  
  257. }
  258. // Copy data To/From a CByteArray object. 
  259. // This can easily be converted to support char arrays as well. 
  260. BOOL CSchemaView::CopyArrayData(CDaoRecordset& rs, CByteArray& ba, BOOL bToArray)
  261. {
  262.  
  263.     COleVariant    var;
  264.     UINT numBytes;        // Number of bytes to transfer.
  265.  
  266.     if (bToArray == FALSE) // Copy TO the recordset.
  267.     {        
  268.         numBytes= ba.GetSize();
  269.  
  270.         // Initialize variant to a byte array.
  271.         var.Clear();
  272.         var.vt= VT_UI1 | VT_ARRAY;
  273.  
  274.         // Initialize a SAFEARRAY bounds.
  275.         SAFEARRAYBOUND bound;
  276.         bound.cElements = numBytes;
  277.         bound.lLbound = 0;        // start with index of 0.
  278.  
  279.         // Create a 1 dimensional SAFEARRAY and assign it to the variant.
  280.         var.parray = ::SafeArrayCreate(VT_UI1, 1, &bound);
  281.         if (var.parray == NULL)
  282.             AfxThrowMemoryException();
  283.  
  284.         // Copy the data to the SAFEARRAY.
  285.         if (numBytes) 
  286.         {
  287.           memcpy(var.parray->pvData, &ba.ElementAt(0), numBytes);
  288.         }
  289.  
  290.         // Now copy the variant to the recordset.
  291.         rs.SetFieldValue(GetField3Name(), var);
  292.  
  293.         SafeArrayDestroy(var.parray);
  294.         return TRUE;
  295.  
  296.     } else {    // Copy FROM the recordset.
  297.         var= rs.GetFieldValue(GetField3Name());
  298.         if (var.vt==VT_NULL || var.vt == (VT_UI1 | VT_ARRAY))
  299.         {
  300.             // If empty, set byte array to zero size.
  301.             if (var.vt == VT_NULL) {
  302.                ba.SetSize(0);
  303.             } else {
  304.                 numBytes= var.parray->rgsabound[0].cElements;
  305.                 ba.SetSize(numBytes);
  306.                 memcpy(&ba.ElementAt(0), var.parray->pvData, numBytes);
  307.             }
  308.             return TRUE;
  309.         }
  310.     }
  311.     return FALSE;
  312. }
  313.  
  314.