//
//
//                                GBase.
//
//                           PROPRIETARY DATA
//
// Licensed Material - Property Of GBase
//
// "Restricted Materails of GBase"
//
// GBase Client SDK
//
// (c)  Copyright GBASE  1997, 2004. All rights reserved.
///////////////////////////////////////////////////////////////////////////////

/*
///////////////////// Description //////////////////////////////////////////////
This demo is an introduction to how to use OLEDB Interfaces in a C++ application.
Here the provider being used is IBM/GBasedbt OLEDB Provider

This is a simple demo that do the following task.
-- Connect to the IBM/GBasedbt Database Server by creating a DataSource Object 
-- Create a Session Object
-- Create a Command Object 
-- Execute SQL Statements 
 	Drop the Table (MyTable) if it exist 
	Create the Table (MyTable)
	Insert Records
-- Delete Command Object
-- Delete Session Object
-- Disconnect Database/Server Connection and Delete DataSource Object

//////////////////////////////////////////////////////////////////////////////////
*/


#include "Demo1.h"
#include "DbConnect.h"

#define MAX_DATA	1024


int main()
{
    /////////////////////////////////////////////////////
    // User has to modify according to his server setting 
    const char  *DsnName = "Database@Server";
    const char  *UserName = "UserID";
    const char  *PassWord = "Password";
    //////////////////////////////////////////////////////
    
    DbConnect   MyDb1;
    HRESULT     hr = S_OK;
    int         tmp  = 0;

    WCHAR wSQLcmd[MAX_DATA];

    
    
    CoInitialize( NULL );
    
    // Create DataSouce Object and Opent A Database Connection
    if (FAILED(hr = MyDb1.MyOpenDataSource( (REFCLSID) CLSID_IFXOLEDBC, 
    			DsnName, UserName, PassWord )) )
    {
    	printf( "\nMyOpenDataSource() failed");
    	return( hr );
    }
    
    // Create Session Object
    if (FAILED( hr = MyDb1.MyCreateSession() ) )
    {
    	printf( "\nMyCreateSession Failed" );
    	return( hr );
    }
    
    
    // Create Command Object
    if (FAILED( hr = MyDb1.MyCreateCmd() ) )
    {
    	printf( "\nMyCreateCmd Failed" );
    	return( hr );
    }

    ///////// Drop the Table if it exist /////
    swprintf( wSQLcmd, L"DROP TABLE MyTable" );
    MyDb1.MyExecuteImmediateCommandText( wSQLcmd );


    ///////// Create A Table (MyTable)  /////////
    swprintf( wSQLcmd, 
            L"CREATE TABLE MyTable              \
            (                                   \
                AcNum         INTEGER NOT NULL, \
                Name          CHAR(20),         \
                Balance       MONEY(8,2),       \
                PRIMARY KEY   (AcNum)           \
            );" );

    if (FAILED( hr = MyDb1.MyExecuteImmediateCommandText( wSQLcmd ) ) )
    {
    	printf( "\nMyExecuteImmediateCommandText Failed" );
    	return( hr );
    }
    

    ///////// Insert A Record into the Table /////
    swprintf( wSQLcmd, 
        L"INSERT INTO MyTable   VALUES ( 100, \'John\', 150.75 );" );

    if (FAILED( hr = MyDb1.MyExecuteImmediateCommandText( wSQLcmd ) ) )
    {
    	printf( "\nMyExecuteImmediateCommandText Failed" );
    	return( hr );
    }

    swprintf( wSQLcmd, 
        L"INSERT INTO MyTable  VALUES ( 101, \'Tom\', 225.75 );" );

    if (FAILED( hr = MyDb1.MyExecuteImmediateCommandText( wSQLcmd ) ) )
    {
    	printf( "\nMyExecuteImmediateCommandText Failed" );
    	return( hr );
    }


    tmp = MyDb1.MyDeleteCmd();
    tmp = MyDb1.MyDeleteSession();
    tmp = MyDb1.MyCloseDataSource();
    
    CoUninitialize();
    
    return(0);
}


