Object ops — assign, compare, inheritance
Objectives — by the end of this page you will be able to:
- copy and compare objects
- work with type hierarchies (
under,not final) - use
GetSelfRefandObjectToTextfor identity and debugging
Assign & compare
Assign deep-copies every attribute — nested objects and collections included:
OCI_Object *copy = OCI_ObjectCreate(OCI_ObjectGetTypeInfo(obj));
OCI_ObjectAssign(copy, obj); /* deep copy — all attrs duplicated */
Object copy = obj; /* copy ctor — deep */
ObjectGetStruct copies attributes into a C struct (page 235 rules: member names match attribute names + indicator struct for NULLs).
Per-attribute IsNull checks are the compare primitive.
Self references
A fetched object's self-REF is what you'd bind into a REF column elsewhere —
the link between object values and object identity (page 640):
OCI_Reference *self = OCI_ReferenceCreate(OCI_ObjectGetTypeInfo(obj));
OCI_ObjectGetSelfReference(obj, self); /* a REF pointing at obj */
Reference self = obj.GetSelfRef();
Inheritance — super types
create type t_person as object (name varchar2(30)) not final;
create type t_employee under t_person (empno number, salary number);
The chain is inspectable through the TypeInfo:
OCI_TypeInfo *ti = OCI_TypeInfoGet(cn, OTEXT("t_employee"), OCI_TIF_TYPE);
OCI_TypeInfo *sup = OCI_TypeInfoGetSuperType(ti); /* → t_person */
OCI_Boolean final = OCI_TypeInfoIsFinalType(ti); /* FALSE here */
TypeInfo sup = obj.GetTypeInfo().GetSuperType(); /* → t_person */
bool final = obj.GetTypeInfo().IsFinalType();
Fetched subtype objects carry all attributes (own + inherited) — attribute
getters read NAME on a t_employee because it's inherited from t_person. The
demos super_types.c/suber_types.sql build a full hierarchy to play with.
Debugging
ToText renders the whole nested structure — the single most useful call when
learning this API:
otext buf[4096];
OCI_ObjectToText(obj, sizeof(buf), buf); /* "t_sale(1, 'x', t_vendor(134,…))" */
printf("%s\n", buf);
std::cout << obj.ToText() << "\n";
Putting it together — the typical sequence
Object operations beyond plain get/set:
ObjectAssign— copy one object's state into another.ObjectGetType— which TypeInfo backs this instance.ObjectGetSelfReference— mint aREFpointing at this object, for binding into REF columns.ObjectIsNull— NULL objects are legal column values too.
Hints: GetSelfReference fills a OCI_Reference you created first — caller
provides the output handle; assigning copies values, not identity — two
objects, one REF each; and a NULL object attribute reads as
IsNullAttribute TRUE — distinguish from a zeroed attribute, which is a real
value.
Try it yourself
- Assign one sale to another handle, change an attr on the copy, verify the original is untouched.
- Build the
t_person/t_employeehierarchy; describet_employeeand confirmNAMEappears viaGetSuperType. ObjectToTexta deeply nested object and compare with the describe tree.
Common pitfalls
- Assuming shallow assign —
Assignis deep; edits on copies don't alias. GetSelfRefon un-fetched objects — a created-not-yet-inserted object has no persistent REF.- Subtype attr not found — inherited attrs are fetched through the declared
column type; check
GetSuperTypewhen an attr looks missing.
Recap
Assigndeep-copies;GetSelfRefyields the REF;GetSuperType/IsFinalTypewalk inheritance;ToTextdumps everything.
Next: collections — VARRAYs and nested tables.