OCILIB Tutorials v5.0.0
Advanceddemo/super_types.cdemo/super_types.cppsuber_types.sql

Object ops — assign, compare, inheritance

Objectives — by the end of this page you will be able to:

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 */

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 */

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   */

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);

Putting it together — the typical sequence

Object operations beyond plain get/set:

  1. ObjectAssign — copy one object's state into another.
  2. ObjectGetType — which TypeInfo backs this instance.
  3. ObjectGetSelfReference — mint a REF pointing at this object, for binding into REF columns.
  4. 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

  1. Assign one sale to another handle, change an attr on the copy, verify the original is untouched.
  2. Build the t_person/t_employee hierarchy; describe t_employee and confirm NAME appears via GetSuperType.
  3. ObjectToText a deeply nested object and compare with the describe tree.

Common pitfalls

Recap

Next: collections — VARRAYs and nested tables.