// TestODBC.cpp : Defines the entry point for the console application.
//
#include <sqlext.h>
#include <sql.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <EDBODBC.H>
#define MAX_DATA 100
#define MYSQLSUCCESS(rc) ((rc==SQL_SUCCESS)||(rc==SQL_SUCCESS_WITH_INFO))
unsigned char szData[MAX_DATA];

BOOL Connect(char* strDB,char* strUser, char* strPassword, HENV** hEnv, HDBC** hDBC)
{
	RETCODE hRet = 0;
	hRet = SQLAllocEnv( (*hEnv) );
	
	if( SQL_ERROR == hRet )
	{
		printf("Unable to Initialize the Environment..");
		return 0;
			
	}
	else
	{
		printf("%s\r\n","\r\nEnvironment Initialized..");
		SQLAllocConnect( *(*hEnv),(*hDBC) );
		
		if ( *hDBC != NULL ) 
		{
			printf("Database Name = %s\r\n",strDB);
			printf("User = %s\r\n",strUser);
			printf("Password = %s\r\n",strPassword);
			
		}
		
		
		hRet =SQLConnect( *(*hDBC),(SQLCHAR*)strDB,SQL_NTS,(SQLCHAR*)strUser,SQL_NTS,(SQLCHAR*)strPassword,SQL_NTS);
		
		if(!MYSQLSUCCESS(hRet))
		{
			SQLFreeEnv(*(*hEnv ));
			SQLFreeConnect(*( *hDBC ) );
			printf("%s\r\n","Unable to connect..");
			return 0;
		}
		else
		{
			printf("%s\r\n","Connected to the Database..");
			return 1;
		}

	}

	return 0;
}

void Disconnect(HENV** hEnv, HDBC** hDBC, HSTMT** hStmt)
{
	SQLFreeStmt(*(*hStmt),SQL_DROP);
	SQLDisconnect(*(*hDBC));
    SQLFreeConnect(*(*hDBC));
    SQLFreeEnv(*(*hEnv));
	
}

BOOL ExecuteSimple_Select(HSTMT** hStmt,unsigned char * cmdstr)
{
   SDWORD cbData;
   SQLSMALLINT iTotCols = 0;
   int j;
   RETCODE rc=SQLExecDirect(*(*hStmt),cmdstr,SQL_NTS);
   if (!MYSQLSUCCESS(rc))  //Error
   {
      printf("Unable to execute...\r\n");
      // Deallocate handles and disconnect.
      SQLFreeStmt(hStmt,SQL_DROP);
	  return 0;
   }
   else
   {
      rc=SQLFetch(*(*hStmt));
      	if(SQL_NO_DATA == rc)
      	{
		printf("No Data...\r\n");
		return 0;
       	}
	SQLNumResultCols(*(*hStmt),&iTotCols);
	printf("No of Columns = %d\r\n",iTotCols);
	
	for (;rc == SQL_SUCCESS; rc=SQLFetch( *(*hStmt) ) )
      	{        
		 for(j=1;j<=iTotCols;j++)
		 {
			SQLGetData( *(*hStmt),j,SQL_C_CHAR,szData,sizeof(szData),&cbData);
			printf("Data = %s\t",szData);
		 }
		 printf("\r\n");
      	}
	return 1;
   }
}


SQLCHAR* GetDBName(HDBC** hDBC)
{
	SQLSMALLINT   InfoValueLen;
	static SQLCHAR *     InfoValuePtr;
	InfoValuePtr = (SQLCHAR*)malloc(50);   // Allocate InfoValuePtr.
	SQLGetInfo(*(*hDBC), SQL_PROCEDURES, (SQLPOINTER)InfoValuePtr, 50, &InfoValueLen);
	return InfoValuePtr;
	
}


/*Test SQL
create or replace procedure refcur_inout_callee2(v_refcur IN OUT SYS_REFCURSOR,v_refcur1  IN OUT SYS_REFCURSOR) is
begin
	open v_refcur for select ename from emp;	
	open v_refcur1 for select dname from dept;	
end;
*/	

SQLCHAR strName[30];
SQLCHAR strName1[30];

int main(int argc, char* argv[])
{
	RETCODE rCode;
	SQLUSMALLINT a;
	SQLINTEGER Num1IndOrLen;
	SQLSMALLINT iTotCols = 0;
   	
	int j;
	SDWORD cbData;

	HENV *hEnv = (HENV*)malloc(sizeof(HENV));
	HDBC *hDBC = (HDBC*)malloc(sizeof(HDBC));
	HSTMT *hStmt = (HSTMT*)malloc(sizeof(HSTMT));
	HSTMT *hStmt1 = (HSTMT*)malloc(sizeof(HSTMT));
	
	Connect("EnterpriseDB","enterprisedb","edb",&hEnv,&hDBC);
	rCode = SQLAllocStmt(*hDBC,hStmt); 
	rCode = SQLAllocStmt(*hDBC,hStmt1); 
	
	rCode = SQLAllocHandle(SQL_HANDLE_STMT,*hDBC,hStmt);
	rCode = SQLAllocHandle(SQL_HANDLE_STMT,*hDBC,hStmt1);

	ExecuteSimple_Select(&hStmt1,(UCHAR*) "BEGIN;");
	
	RETCODE	rc = SQLPrepare((*hStmt),(SQLCHAR*)"{ call refcur_inout_callee2(?,?)}",SQL_NTS);
  	
	rc = SQLBindParameter((*hStmt),1, SQL_PARAM_INPUT_OUTPUT, SQL_C_CHAR,SQL_REFCURSOR,0, 31,
				strName, 31, &Num1IndOrLen);
	rc = SQLBindParameter((*hStmt),2, SQL_PARAM_INPUT_OUTPUT, SQL_C_CHAR,SQL_REFCURSOR,0, 31,
					&strName1, 31, &Num1IndOrLen);
	
	Num1IndOrLen=0;
	
	rc = SQLExecute((*hStmt));

	if(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
 	{
 		printf("\nstrName _________ = %s\n",strName);
		printf("\nstrName 1_________ = %s\n",strName1);
	
 		
 	}
	printf("\n First Cursor as OUT Parameter \n")	;
 	
	rc = SQLMoreResults((*hStmt));
 	for (;rc == SQL_SUCCESS; rc=SQLFetch( (*hStmt) ) )
       	{        
 		 	SQLGetData((*hStmt),1,SQL_C_CHAR,szData,sizeof(szData),&cbData);
 			printf("Data of Cursor 1 is = %s\t",szData);
 		 
 		 printf("\r\n");
       	}
	
	printf("\n Second Cursor as OUT Parameter \n");
 
	rc = SQLMoreResults((*hStmt));
 	for (;rc == SQL_SUCCESS; rc=SQLFetch( (*hStmt) ) )
       	{        
 		 	SQLGetData((*hStmt),1,SQL_C_CHAR,szData,sizeof(szData),&cbData);
 			printf("Data of Cursor  2 is = %s\t",szData);
 		 
 		 printf("\r\n");
       	}	

	Disconnect(&hEnv,&hDBC,&hStmt);
	free(hEnv);
	free(hDBC);
	free(hStmt);
	
	return 0;
}
