#include <iostream>
using namespace std;
#include <stdio.h>
#define OTL_ORA10G_R2 // Compile OTL 4.0/OCI10gR2
#include "otlv4.h" // include the OTL 4.0 header file
//Global variables
static const char oracleLogin[] = "user";
static const char oraclePassword[] = "mdp";
static const char oracleAddress[] = "(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521)))";
static const char oracleTable[] = "test_oci";
otl_connect ObjectDatabase; // otl connect object
bool bEraseTableFirst=false;
/**
* function insert
* Insert rows into table
*/
void insert()
{
char cRef[50], cColor[50], cQuery[250];
int iIterator, iTag, iSize;
//prepare insert query string
strcpy(cQuery, "insert into ");
strcat(cQuery, oracleTable);
strcat(cQuery, "values(:cloth_tag<int>, :cloth_reference<char(101)>, :cloth_size<int>, :cloth_color<char(50)>)");
otl_stream otlStreamOut(200, // buffer size
cQuery, // SQL statement
ObjectDatabase // connect object
);
otlStreamOut.set_commit(0); // turn off stream's "auto-commit"
//just create some futuristic rows
for(iIterator = 1 ; iIterator <= 25 ; ++iIterator)
{
iTag = 132456 + iIterator;
if (iIterator < 10)
{
sprintf(cRef,"Ref000%d",iIterator);
strcpy(cColor, "blue");
}
if (iIterator >= 10)
{
sprintf(cRef,"Ref00%d",iIterator);
strcpy(cColor, "red");
}
iSize = (40 + iIterator) /2;
otlStreamOut << iTag << cRef << iSize << cColor;
}
otlStreamOut << 523456 << "ref0026" << 42 << "blue";
otlStreamOut << 378569 << "ref0027" << 72 << "green";
otlStreamOut << 548622 << "ref0028" << 22 << "red";
otlStreamOut.flush(); // flush the stream's dirty buffer:
// execute the INSERT for the rows
// that are still in the stream buffer
ObjectDatabase.commit_nowait(); // commit with no wait (new feature of Oracle 10.2)
}
/**
* function : select
* Send queries to Oracle database and display result in the default output
*/
void select()
{
int iTag, iSize;
char cRef[100], cColor[100], cQuery[250];
strcpy(cQuery, "select * from ");
strcat(cQuery, oracleTable);
strcat(cQuery, " where cloth_tag>=:f<int>");
otl_stream otlStreamIn(200, // buffer size
cQuery, // SELECT statement
ObjectDatabase // connect object
); // create select stream
otlStreamIn << 0; // assigning :f = 0 beginning
// SELECT automatically executes when all input variables are
// assigned. First portion of output rows is fetched to the buffer
while(!otlStreamIn.eof())// while not end-of-data
{
otlStreamIn >> iTag >> cRef >> iSize >> cColor;
cout << "Tag=" << iTag << ", Reference=" << cRef << ", Size=" << iSize << ", Color=" << cColor << endl;
}
}
/**
* function selectTag
* send a query for just one row
* @ int iTagQueried : the tag number
* @ char* cRow : pointer to the char array for the result
*/
void selectTag(int iSearchedTag, char* cRow)
{
int iTag, iSize;
char cRef[100], cColor[100], cQuery[250], cTemp[50];
//prepare request string
strcpy(cQuery, "select * from ");
strcat(cQuery, oracleTable);
strcat(cQuery, " where cloth_tag=:f<int>");
otl_stream otlStreamIn(200, // buffer size
cQuery, // SELECT statement
ObjectDatabase // connect object
); // create select stream
otlStreamIn << iSearchedTag; // assigning :f
//TODO verify if entry is existing before go on and not after
otlStreamIn >> iTag >> cRef >> iSize >> cColor;
if (iTag != iSearchedTag)
{
strcpy(cRow, "Tag not found");
return;
}
strcpy(cRow, "Tag=");
sprintf(cTemp,"%d",iTag);
strcat(cRow, cTemp);
strcat(cRow, ", Reference=");
strcat(cRow, cRef);
strcat(cRow, ", Size=");
sprintf(cTemp,"%d",iSize);
strcat(cRow, cTemp);
strcat(cRow, ", Color=");
strcat(cRow, cColor);
}
/**
* main
* Read all entries of the table (via select function)
* Switch global flag bEraseTableFirst, the function drop the table and launch the
* insert function before all.
*/
int main()
{
char cConnect[250], cEraseTableQuery[250], cCreateTableQuery[250];
char cRow[250];
int iKeyboardEntry;
//prepare drop query string
strcpy(cEraseTableQuery, "drop table ");
strcat(cEraseTableQuery, oracleTable);
//prepare create query string
strcpy(cCreateTableQuery, "create table ");
strcat(cCreateTableQuery, oracleTable);
strcat(cCreateTableQuery, "(cloth_tag number, cloth_reference varchar2(100),");
strcat(cCreateTableQuery, " cloth_size number, cloth_color varchar2(50))");
//prepare login/password@db string
strcpy(cConnect, oracleLogin);
strcat(cConnect, "/");
strcat(cConnect, oraclePassword);
strcat(cConnect, "@");
strcat(cConnect, oracleAddress);
otl_connect::otl_initialize(); // initialize OCI environment
try
{
ObjectDatabase.rlogon(cConnect); // connect to Oracle
if (bEraseTableFirst) //if table must be drop and recreate
{
otl_cursor::direct_exec( ObjectDatabase,
cEraseTableQuery,
otl_exception::disabled // disable OTL exceptions
//not necessary for just droping a table :-)
); // drop table
otl_cursor::direct_exec( ObjectDatabase,
cCreateTableQuery); // create table
insert(); // call to insert function
}
select(); // call to select function
}
catch(otl_exception& pException) // intercept OTL exceptions
{
cerr << pException.msg << endl; // print out error message
cerr << pException.stm_text << endl; // print out SQL that caused the error
cerr << pException.var_info << endl; // print out the variable that caused the error
}
while (iKeyboardEntry != 0)
{
cout << "Enter Tag number (0 to quit): " << endl;
cin >> iKeyboardEntry;
if (iKeyboardEntry != 0)
{
selectTag(iKeyboardEntry, cRow);
cout << cRow << endl;
}
}
ObjectDatabase.logoff(); // disconnect from Oracle
return 0;
}
Partager