home *** CD-ROM | disk | FTP | other *** search
- // schemaView.cpp : implementation of the CSchemaView class
- //
-
- #include "stdafx.h"
- #include "schema.h"
-
- #include "schemaDoc.h"
- #include "schemaView.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CSchemaView
-
- IMPLEMENT_DYNCREATE(CSchemaView, CView)
-
- BEGIN_MESSAGE_MAP(CSchemaView, CView)
- //{{AFX_MSG_MAP(CSchemaView)
- ON_COMMAND(ID_SCHEMA_DATABASE, OnSchemaDatabase)
- ON_COMMAND(ID_SCHEMA_TABLE, OnSchemaTable)
- ON_COMMAND(ID_SCHEMA_ADDRECORD, OnSchemaAddrecord)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CSchemaView construction/destruction
-
- CSchemaView::CSchemaView()
- {
- // TODO: add construction code here
-
- }
-
- CSchemaView::~CSchemaView()
- {
- }
-
- BOOL CSchemaView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
-
- return CView::PreCreateWindow(cs);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CSchemaView drawing
-
- void CSchemaView::OnDraw(CDC* pDC)
- {
- CSchemaDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
-
- // TODO: add draw code for native data here
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CSchemaView printing
-
- BOOL CSchemaView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
-
- void CSchemaView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
-
- void CSchemaView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CSchemaView diagnostics
-
- #ifdef _DEBUG
- void CSchemaView::AssertValid() const
- {
- CView::AssertValid();
- }
-
- void CSchemaView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
-
- CSchemaDoc* CSchemaView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSchemaDoc)));
- return (CSchemaDoc*)m_pDocument;
- }
- #endif //_DEBUG
-
- CString CSchemaView::GetDefaultDBName()
- {
- return CString("MyDatabase");
- }
-
- CString CSchemaView::GetDefaultTableName()
- {
- return CString("TestTable");
- }
- CString CSchemaView::GetField1Name()
- {
- return CString("Some Text");
- }
- CString CSchemaView::GetField2Name()
- {
- return CString("A Number");
- }
- CString CSchemaView::GetField3Name()
- {
- return CString("DataField");
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CSchemaView message handlers
-
- void CSchemaView::OnSchemaDatabase()
- {
- // Construct the database.
- CDaoDatabase db;
-
- // Create the database.
- try
- {
- db.Create(GetDefaultDBName());
- }
- catch (CDaoException* e)
- {
- e->ReportError();
- e->Delete();
- }
- }
-
- void CSchemaView::OnSchemaTable()
- {
- try
- {
- // Open the database.
- CDaoDatabase db;
- db.Open(GetDefaultDBName());
-
- // Attempt to delete one if it exists
- try
- {
- db.DeleteTableDef(GetDefaultTableName());
- }
- catch (CDaoException* e)
- {
- e->Delete();
- }
-
- // Construct and create the table.
- CDaoTableDef table(&db);
- table.Create(GetDefaultTableName());
-
- // Fields must be defined before table can be appended.
-
- // Create a field using one version of CreateField()
- table.CreateField(GetField1Name(), dbText, 255, dbVariableField);
-
- // Create a field using CDaoFieldInfo version of CreateField().
- CDaoFieldInfo fieldinfo;
- fieldinfo.m_strName = GetField2Name(); // required
- fieldinfo.m_nType = dbInteger; // required
- fieldinfo.m_lSize = 2; // required
- fieldinfo.m_lAttributes= dbFixedField; // required
- fieldinfo.m_nOrdinalPosition= 0;
- fieldinfo.m_bRequired= FALSE;
- fieldinfo.m_bAllowZeroLength= FALSE; // required
- fieldinfo.m_lCollatingOrder= 0;
- fieldinfo.m_strForeignName= "";
- fieldinfo.m_strSourceField= "";
- fieldinfo.m_strSourceTable= "";
- fieldinfo.m_strValidationRule= "< 100";
- fieldinfo.m_strValidationText= "Value must be less than 100";
- fieldinfo.m_strDefaultValue= "";
-
- table.CreateField( fieldinfo );
-
- // Long Binary field
- table.CreateField(GetField3Name(), dbLongBinary, 0, 0);
-
- // Append the table to the tables collection.
- table.Append();
- }
- catch (CDaoException* e)
- {
- e->ReportError();
- e->Delete();
- }
-
- }
-
- void CSchemaView::OnSchemaAddrecord()
- {
- // Open the database.
- CDaoDatabase db;
- db.Open(GetDefaultDBName());
-
- // Construct and open the table.
- CDaoTableDef table(&db);
- table.Open(GetDefaultTableName());
-
- // Create a recordset based upon the table.
- CDaoRecordset rs(&db);
- rs.Open(&table);
-
- // Add a new record.
- rs.AddNew();
-
- // Set the fields of the new record.
- COleVariant varField1("Some text", VT_BSTRT);
- rs.SetFieldValue(GetField1Name(), varField1);
-
- COleVariant varField2( (short) 99);
- rs.SetFieldValue(GetField2Name(), varField2);
-
- // Create an array to copy to table.
- CByteArray ba;
- ba.SetSize(3);
- ba.SetAt(0,1);
- ba.SetAt(1,2);
- ba.SetAt(2,3);
-
- // Copy the array.
- CopyArrayData(rs, ba, FALSE);
-
- // Update the table.
- rs.Update();
-
- rs.MoveLast();
- CByteArray ba2;
- CopyArrayData(rs, ba2, TRUE);
- CString strData;
- strData.Format("Data: ");
- for (int idx=0; idx<ba2.GetSize(); idx++)
- {
- CString val;
- val.Format(" %d", ba2.GetAt(idx));
- strData += val;
- }
- AfxMessageBox(strData);
-
- }
- // Copy data To/From a CByteArray object.
- // This can easily be converted to support char arrays as well.
- BOOL CSchemaView::CopyArrayData(CDaoRecordset& rs, CByteArray& ba, BOOL bToArray)
- {
-
- COleVariant var;
- UINT numBytes; // Number of bytes to transfer.
-
- if (bToArray == FALSE) // Copy TO the recordset.
- {
- numBytes= ba.GetSize();
-
- // Initialize variant to a byte array.
- var.Clear();
- var.vt= VT_UI1 | VT_ARRAY;
-
- // Initialize a SAFEARRAY bounds.
- SAFEARRAYBOUND bound;
- bound.cElements = numBytes;
- bound.lLbound = 0; // start with index of 0.
-
- // Create a 1 dimensional SAFEARRAY and assign it to the variant.
- var.parray = ::SafeArrayCreate(VT_UI1, 1, &bound);
- if (var.parray == NULL)
- AfxThrowMemoryException();
-
- // Copy the data to the SAFEARRAY.
- if (numBytes)
- {
- memcpy(var.parray->pvData, &ba.ElementAt(0), numBytes);
- }
-
- // Now copy the variant to the recordset.
- rs.SetFieldValue(GetField3Name(), var);
-
- SafeArrayDestroy(var.parray);
- return TRUE;
-
- } else { // Copy FROM the recordset.
- var= rs.GetFieldValue(GetField3Name());
- if (var.vt==VT_NULL || var.vt == (VT_UI1 | VT_ARRAY))
- {
- // If empty, set byte array to zero size.
- if (var.vt == VT_NULL) {
- ba.SetSize(0);
- } else {
- numBytes= var.parray->rgsabound[0].cElements;
- ba.SetSize(numBytes);
- memcpy(&ba.ElementAt(0), var.parray->pvData, numBytes);
- }
- return TRUE;
- }
- }
- return FALSE;
- }
-
-