OCILIB (C and C++ Driver for Oracle)  4.7.5
Open source and cross platform Oracle Driver delivering efficient access to Oracle databases.
Loading...
Searching...
No Matches
Some OCILIB C++ sample codes

OcilibCppApiMainDemoApplication

Here are some C++ samples code. More samples can be found under the demo folder of ocilib packages.

Fetching data
#include <iostream>
#include "ocilib.hpp"
/* requires script demo/products.sql */
using namespace ocilib;
int main(void)
{
try
{
Connection con("db", "usr", "pwd");
Statement st(con);
st.Execute("select * from products");
Resultset rs = st.GetResultset();
while (rs++)
{
std::cout << "code:" << rs.Get<ostring>(1) << " name:" << rs.Get<ostring>(2) << std::endl;
}
std::cout << "=> Total fetched rows : " << rs.GetCount() << std::endl;
}
catch (std::exception &ex)
{
std::cout << ex.what() << std::endl;
}
return EXIT_SUCCESS;
}
A connection or session with a specific database.
Definition: types.hpp:1577
static void Initialize(EnvironmentFlags mode=Environment::Default, const ostring &libpath=OTEXT(""))
Initialize the OCILIB environment.
Definition: Environment.hpp:32
static void Cleanup()
Clean up all resources allocated by the environment.
Definition: Environment.hpp:37
Database resultset.
Definition: types.hpp:6505
T Get(unsigned int index) const
Return the current value of the column at the given index in the resultset.
Definition: Resultset.hpp:458
unsigned int GetCount() const
Retrieve the number of rows fetched so far.
Definition: Resultset.hpp:58
Object used for executing SQL or PL/SQL statement and returning the produced results.
Definition: types.hpp:5556
OCILIB ++ Namespace.
std::basic_string< otext, std::char_traits< otext >, std::allocator< otext > > ostring
string class wrapping the OCILIB otext * type and OTEXT() macros ( see Character sets )
Definition: config.hpp:120
Binding vectors
#include <iostream>
#include "ocilib.hpp"
/* requires script demo/products.sql */
using namespace ocilib;
const int ArraySize = 1000;
int main(void)
{
try
{
Connection con("db", "usr", "pwd");
std::vector<int> ints;
std::vector<ostring> strs;
Statement st(con);
st.Prepare("insert into products values(:i, :s)");
st.SetBindArraySize(ArraySize);
st.Bind(":i", ints, BindInfo::In);
st.Bind(":s", strs, 20, BindInfo::In);
for (int i = 0; i< ArraySize; i++)
{
ostring str;
str += "Name";
str += ((i + 1) + '0');
ints.push_back(i + 1);
strs.push_back(str);
}
st.ExecutePrepared();
std::cout << "row processed : " << st.GetAffectedRows() << std::endl;
}
catch (std::exception &ex)
{
std::cout << ex.what() << std::endl;
}
return EXIT_SUCCESS;
}
Using collections
#include <iostream>
#include "ocilib.hpp"
/* requires script demo/collections.sql */
using namespace ocilib;
void print_product(const Object &product)
{
std::cout << "...product: " << product.Get<int>("code") << " - " << product.Get<ostring>("name") << std::endl;
}
void print_coll_by_iterator(Collection<Object> &coll)
{
std::cout << "fetch using iterators" << std::endl;
for (; it1 != it2; ++it1)
{
print_product(static_cast<Object>(*it1));
}
}
void print_coll_by_index(Collection<Object> &coll)
{
std::cout << "fetch using index access" << std::endl;
unsigned int i = 1, n = coll.GetCount();
for (; i <= n; i++)
{
print_product(static_cast<Object>(coll[i]));
}
}
void print_const_coll_by_iterator(const Collection<Object> &coll)
{
std::cout << "fetch using const iterators" << std::endl;
for (; it1 != it2; ++it1)
{
print_product(static_cast<Object>(*it1));
}
}
void print_const_coll_by_index(const Collection<Object> &coll)
{
std::cout << "fetch using const index access" << std::endl;
unsigned int i = 1, n = coll.GetCount();
for (; i <=n ; i++)
{
print_product(static_cast<Object>(coll[i]));
}
}
void bind_coll(Connection &con)
{
Statement st(con);
Collection<Object> coll(TypeInfo(con, "product_varray_t", TypeInfo::Type));
st.Prepare("begin :array := product_varray_t(product_t(123, 'name 123'), product_t(456, 'name 456'), product_t(789, 'name 789')); end;");
st.Bind(":array", coll, BindInfo::In);
st.ExecutePrepared();
print_coll_by_iterator(coll);
print_coll_by_index(coll);
}
template <class T>
void fetch_coll(Connection &con, ostring table_name, T func)
{
Statement st(con);
st.Execute("select * from " + table_name);
Resultset rs = st.GetResultset();
while (rs++)
{
std::cout << "#" << rs.Get<ostring>(1) << std::endl;
func(rs.Get<Collection<Object> >(2));
}
}
int main(void)
{
try
{
Connection con("db", "usr", "pwd");
bind_coll(con);
fetch_coll(con, "products_varray", print_const_coll_by_iterator);
fetch_coll(con, "products_nested_table", print_const_coll_by_index);
}
catch (std::exception &ex)
{
std::cout << ex.what() << std::endl;
}
return EXIT_SUCCESS;
}
Object identifying the SQL data types VARRAY and NESTED TABLE.
Definition: types.hpp:5026
unsigned int GetCount() const
Returns the current number of elements in the collection.
Definition: Collection.hpp:87
iterator end()
Returns an iterator referring to the past-the-end element in the collection.
Definition: Collection.hpp:136
iterator begin()
Returns an iterator pointing to the first element in the collection.
Definition: Collection.hpp:124
Object identifying the SQL data type OBJECT.
Definition: types.hpp:4665
T Get(const ostring &name) const
Return the given object attribute value.
Definition: Object.hpp:233
Provides type information on Oracle Database objects.
Definition: types.hpp:4528
Using connection pools
#include <iostream>
#include "ocilib.hpp"
using namespace ocilib;
const int MaxThreads = 50;
const int MaxConnnections = 10;
void worker(ThreadHandle handle, void *data)
{
Connection con = ((Pool *)data)->GetConnection();
Statement st(con);
st.Execute("select to_char(sysdate, 'YYYYMMDD HH24:MI:SS') from dual");
Resultset rs = st.GetResultset();
rs.Next();
std::cout << handle << " - " << rs.Get<ostring>(1) << std::endl;
con.Close();
}
int main(void)
{
try
{
Pool pool("db", "usr", "pwd", Pool::ConnectionPool, 0, MaxConnnections);
std::vector<ThreadHandle> threads;
for (int i = 0; i < MaxThreads; i++)
{
threads.push_back(th);
Thread::Run(th, worker, &pool);
}
for (int i = 0; i < MaxThreads; i++)
{
Thread::Join(threads[i]);
Thread::Destroy(threads[i]);
}
}
catch (std::exception &ex)
{
std::cout << ex.what() << std::endl;
}
return EXIT_SUCCESS;
}
void Close()
Close the physical connection to the DB server.
Definition: Connection.hpp:57
A connection or session Pool.
Definition: types.hpp:1349
@ ConnectionPool
Definition: types.hpp:1360
bool Next()
Fetch the next row of the resultset.
Definition: Resultset.hpp:33
static ThreadHandle Create()
Create a Thread.
Definition: Thread.hpp:28
static void Join(ThreadHandle handle)
Join the given thread.
Definition: Thread.hpp:43
static void Destroy(ThreadHandle handle)
Destroy a thread.
Definition: Thread.hpp:33
static void Run(ThreadHandle handle, ThreadProc func, AnyPointer arg)
Execute the given routine within the given thread.
Definition: Thread.hpp:38
OCI_Thread * ThreadHandle
Alias for an OCI_Thread pointer.
Definition: config.hpp:156
Oracle 12c Implicit resultsets
#include <iostream>
#include "ocilib.hpp"
using namespace ocilib;
int main(void)
{
try
{
Connection con("db", "usr", "pwd");
Statement st(con);
st.Execute("declare"
" c1 sys_refcursor;"
" c2 sys_refcursor;"
" begin"
" open c1 for select username from all_users;"
" dbms_sql.return_result (c1); "
" open c2 for select table_name from all_tables;"
" dbms_sql.return_result (c2); "
"end;");
Resultset rs = st.GetResultset();
while (rs)
{
while (rs++)
{
std::cout << "Fetch column name:" << rs.GetColumn(1).GetName() << ", Value:" << rs.Get<ostring>(1) << std::endl;
}
std::cout << "=> Total fetched rows : " << rs.GetCount() << std::endl;
rs = st.GetNextResultset();
}
}
catch (std::exception &ex)
{
std::cout << ex.what() << std::endl;
}
return EXIT_SUCCESS;
}
ostring GetName() const
Return the Column name.
Definition: Column.hpp:33
Column GetColumn(unsigned int index) const
Return the column from its index in the resultset.
Definition: Resultset.hpp:78
Using Oracle objects
#include <iostream>
#include "ocilib.hpp"
/* requires script demo/object.sql */
using namespace ocilib;
int main(void)
{
try
{
Connection con("db", "usr", "pwd");
Date date;
date.SysDate();
Object sale(TypeInfo(con, "t_sale", TypeInfo::Type));
Object vendor(TypeInfo(con, "t_vendor", TypeInfo::Type));
vendor.Set<int>("code", 134);
vendor.Set<ostring>("name", "JOHN SMITH");
sale.Set<int>("code", 1);
sale.Set<double>("price", 12.99);
sale.Set<ostring>("name", "USB KEY 2go");
sale.Set<ostring>("ref", "A56547WSAA");
sale.Set<Date>("date_sale", date);
sale.Set<Object>("vendor", vendor);
Statement st(con);
st.Prepare("insert into sales values(:obj)");
st.Bind(":obj", sale, BindInfo::In);
st.ExecutePrepared();
std::cout << "Rows inserted : " << st.GetAffectedRows() << std::endl;
con.Commit();
}
catch (std::exception &ex)
{
std::cout << ex.what() << std::endl;
}
return EXIT_SUCCESS;
}
void Commit()
Commit current pending changes.
Definition: Connection.hpp:62
Object identifying the SQL data type DATE.
Definition: types.hpp:2675
static Date SysDate()
Return the current system date time.
Definition: Date.hpp:64
Database notifications
#include <iostream>
#include "ocilib.hpp"
using namespace ocilib;
#if defined(_WINDOWS)
#define sleep(x) Sleep(x*1000)
#else
#include <unistd.h>
#endif
#define WaitForEvents() sleep(5)
#define WaitForDatabase() sleep(60)
static std::map<unsigned int, ostring> EventTypes;
static std::map<unsigned int, ostring> ObjectEvents;
void EventHandler(Event &evt);
void SetupNames();
int main(int argc, char* argv[])
{
SetupNames();
try
{
Connection con("db", "usr", "pwd");
con.SetAutoCommit(true);
Statement st(con);
st.Execute("create table table1(code number)");
st.Execute("create table table2(str varchar2(10))");
sub.Register(con, "sub-00", Subscription::AllChanges, EventHandler, 5468, 0);
sub.Watch("select * from table1");
sub.Watch("select * from table2");
st.Execute("alter table table1 add price number");
WaitForEvents();
st.Execute("insert into table1 values(1, 10.5)");
st.Execute("insert into table2 values('shoes')");
WaitForEvents();
st.Execute("update table1 set price = 13.5 where code = 1");
st.Execute("delete from table2 ");
WaitForEvents();
st.Execute("drop table table1");
st.Execute("drop table table2");
WaitForEvents();
con.Close();
/* start remote instance */
/* shutdown remote instance */
WaitForDatabase();
sub.Unregister();
}
catch (std::exception &ex)
{
std::cout << ex.what() << std::endl;
}
}
void SetupNames()
{
EventTypes[Event::DatabaseStart] = "Startup";
EventTypes[Event::DatabaseShutdown] = "Shutdown";
EventTypes[Event::DatabaseShutdownAny] = "Shutdown Any";
EventTypes[Event::DatabaseDrop] = "Drop Database";
EventTypes[Event::Unregister] = "Unregister";
EventTypes[Event::ObjectChanged] = "Object Changed";
ObjectEvents[Event::ObjectInserted] = "Insert";
ObjectEvents[Event::ObjectUpdated] = "Update";
ObjectEvents[Event::ObjectDeleted] = "Delete";
ObjectEvents[Event::ObjectAltered] = "Alter";
ObjectEvents[Event::ObjectDropped] = "Drop";
ObjectEvents[Event::ObjectGeneric] = "Generic";
}
void EventHandler(Event &evt)
{
std::cout << "** Notification : " << evt.GetSubscription().GetName() << std::endl;
std::cout << "** Database : " << evt.GetDatabaseName() << std::endl;
std::cout << "** Event : " << EventTypes[evt.GetType()] << std::endl;
{
std::cout << ".... Object : " << evt.GetObjectName() << std::endl;
std::cout << ".... Action : " << ObjectEvents[evt.GetObjectEvent()] << std::endl;
std::cout << ".... RowID : " << evt.GetRowID() << std::endl;
}
std::cout << std::endl;
}
void SetAutoCommit(bool enabled)
Enable or disable auto commit mode (implicit commits after every SQL execution)
Definition: Connection.hpp:77
static void ShutdownDatabase(const ostring &db, const ostring &user, const ostring &pwd, Environment::ShutdownFlags shutdownFlags, Environment::ShutdownMode shutdownMode, Environment::SessionFlags sessionFlags=SessionSysDba)
Shutdown a database instance.
static void StartDatabase(const ostring &db, const ostring &user, const ostring &pwd, Environment::StartFlags startFlags, Environment::StartMode startMode, Environment::SessionFlags sessionFlags=SessionSysDba, const ostring &spfile=OTEXT(""))
Start a database instance.
Subscription Event.
Definition: types.hpp:7202
ostring GetRowID() const
Return the rowid of the altered database object row.
Definition: Event.hpp:53
Subscription GetSubscription() const
Return the subscription that generated this event.
Definition: Event.hpp:58
ostring GetObjectName() const
Return the name of the object that generated the event.
Definition: Event.hpp:48
ObjectEvent GetObjectEvent() const
Return the type of operation reported by a notification.
Definition: Event.hpp:38
@ DatabaseShutdownAny
Definition: types.hpp:7220
@ DatabaseShutdown
Definition: types.hpp:7218
ostring GetDatabaseName() const
Return the name of the database that generated the event.
Definition: Event.hpp:43
EventType GetType() const
Return the type of event reported by a notification.
Definition: Event.hpp:33
Subscription to database or objects changes.
Definition: types.hpp:7068
void Watch(const ostring &sql)
Add a SQL query to monitor.
void Unregister()
Unregister a previously registered notification.
void Register(const Connection &connection, const ostring &name, ChangeTypes changeTypes, NotifyHandlerProc handler, unsigned int port=0, unsigned int timeout=0)
Register a notification against the given database.
ostring GetName() const
Return the name of the given registered subscription.