/**
* @file:    $RCSfile: oracle_process.cpp,v $
* @brief:   $IEC 61850 Protocol  
*
* @version: $Revision: 1.1 $
* @date:    $Date: 2018/11/24 06:54:51 $
* @author:  $Author: lizhongming $
* @state:   $State: Exp $
*
* @latest:  $Id: oracle_process.cpp,v 1.1 2018/11/24 06:54:51 lizhongming Exp $
*
*/



#include <iostream>
using namespace std;
#include <stdio.h>

#define OTL_ORA11G 
#include "otlv4.h" // include the OTL 4 header file

otl_connect db; // connect object

int OTLConnect (const char* pszConnStr)
{
	try
	{
		otl_connect::otl_initialize(); // initialize OCI environment
		db.rlogon(pszConnStr);
		//db.auto_commit_off ( );
		printf ( "CONNECT: OK!\n" );
	}
	catch(otl_exception& p)
	{   // intercept OTL exceptions
		printf ( "Connect Error: (%s) (%s) (%s)\n",p.msg, p.stm_text, p.var_info );
		return -1;
	}
	return 0;
}

int OTLDisconnect()
{
	//db.commit ( );
	db.logoff();
	printf ( "DISCONNECT: OK!\n" );
	return 0;
}



void insert()
	// insert rows into table
{ 
	otl_stream o(50, // buffer size
		"insert into test_tab values(:f1<int>,:f2<char[31]>)", 
		// SQL statement
		db // connect object
		);
	char tmp[32];
	for(int i=1;i<=100;++i){
		sprintf(tmp,"Name%d",i);
		// the old way (operators >>() / <<()) is available as always:
		o<<i<<tmp;
	}
}



void select()
{ 
	otl_stream i(50, // buffer size
		"select * from test_tab where f1>=:f<int> and f1<=:f*2",
		// SELECT statement
		db // connect object
		); 
	// create select stream

	float f1=0;
	char f2[31];
	// the old way (operators >>() / <<()) is available as always:
	i<<8; // assigning :f = 8
	// C++98/03 compiler
	while(!i.eof()){ // while not end-of-data
		i>>f1>>f2;
		cout<<"f1="<<f1<<", f2="<<f2<<endl;
	}
 
	i<<4; // assigning :f = 4
	while(!i.eof()){ // while not end-of-data
		i>>f1>>f2;
		cout<<"f1="<<f1<<", f2="<<f2<<endl;
	}
}

int main2()
{
	OTLConnect("pqsadmin/pqsadmin@PQSBASE"); // connect to Oracle
	try{
		otl_cursor::direct_exec
			(
			db,
			"drop table test_tab",
			otl_exception::disabled // disable OTL exceptions
			); // drop table

		otl_cursor::direct_exec
			(
			db,
			"create table test_tab(f1 number, f2 varchar2(30))"
			);  // create table
		insert(); // insert records into table
		select(); // select records from table

	}
	catch(otl_exception& p){ // intercept OTL exceptions
		cerr<<p.msg<<endl; // print out error message
		cerr<<p.stm_text<<endl; // print out SQL that caused the error
		cerr<<p.var_info<<endl; // print out the variable that caused the error
	}
	
	//OTLDisconnect();
	return 0;
}
