User-defined types — OCI_Object / Object
Objectives — by the end of this page you will be able to:
- describe a schema
CREATE TYPEwithOCI_TypeInfo - create, read and write object attributes
- bind and fetch objects in SQL and PL/SQL
In context — schema objects vs PL/SQL records
Oracle gives you two ways to build a composite type, and they land on different
OCILIB paths. A UDT (CREATE TYPE … AS OBJECT) is a schema-level object —
it lives in the data dictionary, can be a column type in any table, and can be
returned by SQL functions. A PL/SQL RECORD (previous page) is anonymous and
package-scoped — it exists only inside PL/SQL execution. Both map to
OCI_Object, but only the UDT can appear in a SELECT list or a column
definition.
Describing is the entry point for both: OCI_TypeInfoGet walks the data
dictionary and hands you back the attribute layout — names, types, order — which
is what the Get/Set accessors then index. Because describing hits the
dictionary, OCILIB caches type info internally; the TypeInfo object itself is
borrowed (owned by the connection's cache) while each Object you create is
yours to free.
The Object handle
A user-defined type (UDT) is a schema-level CREATE TYPE … AS OBJECT — a
structured row of named attributes, stored as a table column or passed to
PL/SQL.
OCILIB represents every UDT instance as OCI_Object (C) / Object
(C++) — the same handle that backs PL/SQL RECORDs (previous page). Describe
the type once, then create instances, get/set attributes by name, and bind them
into statements.
| C function | C++ method | purpose |
|---|---|---|
OCI_TypeInfoGet (OCI_TIF_TYPE) |
TypeInfo ctor |
describe the UDT |
OCI_ObjectCreate |
Object ctor |
instantiate |
OCI_ObjectFree |
— (RAII) | release |
OCI_ObjectGet{Type} / OCI_ObjectSet{Type} |
Get<T> / Set<T> |
attribute access |
OCI_ObjectGetStruct |
— | all attrs → C struct |
OCI_StatementBindObject |
Bind(":o", obj) |
SQL/PLSQL param |
OCI_ResultsetGetObject |
Get<Object> |
fetch a UDT column |
OCI_TypeInfo *ti = OCI_TypeInfoGet(con, OTEXT("VENDOR"), OCI_TIF_TYPE);
OCI_Object *v = OCI_ObjectCreate(ti);
OCI_ObjectSetInt32 (v, OTEXT("CODE"), 42);
OCI_ObjectSetString(v, OTEXT("NAME"), OTEXT("ACME"));
OCI_StatementPrepare(st,
OTEXT("insert into vendors(id, vendor) values(:i, :v)"));
OCI_StatementBindInt32 (st, OTEXT(":i"), &id);
OCI_StatementBindObject(st, OTEXT(":v"), v);
OCI_StatementExecutePrepared(st);
OCI_ObjectFree(v);
Object v(TypeInfo(con, "VENDOR", TypeInfo::Type));
v.Set<Int32>("CODE", 42);
v.Set<String> ("NAME", "ACME");
st.Prepare("insert into vendors(id, vendor) values(:i, :v)");
st.Bind(":i", id);
st.Bind(":v", v);
st.ExecutePrepared(); /* v is RAII — no free needed */
Fetching a VENDOR-typed column gives a borrowed Object — read it, never
free it:
OCI_Resultset *rs = OCI_StatementGetResultset(st);
while (OCI_ResultsetFetchNext(rs))
{
OCI_Object *v = OCI_ResultsetGetObject(rs, 1); /* borrowed! */
printf("%d %s\n",
OCI_ObjectGetInt32 (v, OTEXT("CODE")),
OCI_ObjectGetString(v, OTEXT("NAME")));
}
Resultset rs = st.GetResultset();
while (rs.Next())
{
Object v = rs.Get<Object>(1); /* borrowed! */
std::cout << v.Get<Int32>("CODE") << ' '
<< v.Get<String>("NAME") << '\n';
}
Beyond plain objects
UDTs compose — an attribute can be another UDT, a VARRAY/nested table, a
REF, or any scalar. That whole family shares the object-relational API:
- Objects & attributes — the full
ObjectAPI - Collections —
OCI_Collection/CollectionforVARRAYand nested-table attributes - REFs —
OCI_Reference/Referencepointers between objects - Describe —
OCI_TypeInfo/TypeInfointrospection
Putting it together — the typical sequence
UDT work always starts with the dictionary, not the data:
- Describe the type with
TypeInfoGet(con, "MY_TYPE", OCI_TIF_TYPE)— fully qualified with the schema when it isn't yours. This one call walks the data dictionary and gives you the attribute layout everything else depends on. - Create the object from that
TypeInfo—ObjectCreatebuilds anOCI_Objectyou own. - Fill the attributes by name —
Set/Set<Type>— then bind it into an INSERT or PL/SQL call withBindObject. - Fetch it back: a UDT column arrives as a borrowed
Object— read the attributes withGet/Get<Type>; never free the handle. - Go deeper:
ObjectGetCollpulls a nested collection attribute,ObjectGetSelfReferencegives aREFyou can bind elsewhere — both come from the same describe-first machinery.
Hints: OCI_TIF_TYPE describes a type, OCI_TIF_TABLE a table — mixing
them is a common source of "type not found"; describe results are cached per
connection, so repeating TypeInfoGet is cheap; and a NULL object attribute
needs ObjectSetNull, not a zeroed Set.
Common pitfalls
- Freeing a fetched
Object— resultset objects are borrowed; only free objects you created withOCI_ObjectCreate. OCI_TypeInfoGeton a table — passOCI_TIF_TYPEfor UDTs; tables/views useOCI_TIF_TABLE/OCI_TIF_VIEW(see describe).Get<int>on aVARCHAR2attr — match the getter to the attribute type; checkOCI_ColumnGetTypeon the type'sOCI_Columnfirst.
Recap
- UDT =
CREATE TYPE … AS OBJECT→OCI_Object/Objecthandle OCI_TypeInfoGet(con, name, OCI_TIF_TYPE)→OCI_ObjectCreate→Get/Setattrs →OCI_StatementBindObject- fetched objects are borrowed; created ones you free
- collections/REFs/describe → Part 6
Next: checkpoint — put every type from Part 2 to work.