OCILIB Tutorials v5.0.0
Intermediatedemo/desc.cdemo/meta.c

Describing schema objects — the TypeInfo descriptor

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

The TypeInfo

A TypeInfo is a server-side description of a schema object — the same info describe gives you in SQL*Plus.

It does double duty: pure introspection, and as the type descriptor every object-API constructor needs — objects, collections, REFs and collection elements are all created from a TypeInfo, not from a connection directly.

OCI_TypeInfo *ti = OCI_TypeInfoGet(cn, OTEXT("employees"), OCI_TIF_TABLE);
/* … use it … */
OCI_TypeInfoFree(ti);

Kinds — what to describe

C enum C++ enum Describes
OCI_TIF_TABLE TypeInfo::Table a table — columns via GetColumn{,Count}
OCI_TIF_VIEW TypeInfo::View a view
OCI_TIF_TYPE TypeInfo::Type an object/collection type — attributes become column descriptors

The metadata surface

C function C++ method Returns
OCI_TypeInfoGetName GetName() object/type name
OCI_TypeInfoGetType GetType() kind detail (varies by kind flag)
OCI_TypeInfoGetColumnCount GetColumnCount() number of columns/attributes
OCI_TypeInfoGetColumn GetColumn(i) column descriptor (page 240)
OCI_TypeInfoGetSuperType GetSuperType() parent type for inherited types
OCI_TypeInfoIsFinalType IsFinalType() whether the type can be extended
OCI_TypeInfoGetConnection GetConnection() owning connection

Describe a table

OCI_TypeInfo *ti = OCI_TypeInfoGet(cn, OTEXT("employees"), OCI_TIF_TABLE);
printf("%s has %u columns\n",
       OCI_TypeInfoGetName(ti), OCI_TypeInfoGetColumnCount(ti));

for (OCI_UInt32 i = 0; i < OCI_TypeInfoGetColumnCount(ti); ++i)
{
    OCI_Column *col = OCI_TypeInfoGetColumn(ti, i);
    printf("  %-12s %s\n", OCI_ColumnGetName(col), OCI_ColumnGetSqlType(col));
}
OCI_TypeInfoFree(ti);

TypeInfo as factory input

OCI_TypeInfo *tt = OCI_TypeInfoGet(cn, OTEXT("t_vendor"), OCI_TIF_TYPE);
OCI_Object *obj = OCI_ObjectCreate(tt);      /* new t_vendor instance */
OCI_TypeInfoFree(tt);                        /* obj keeps what it needs */

Putting it together — the typical sequence

Schema describe is the dictionary-introspection entry point:

  1. Call OCI_Describe on the object name — table, type, procedure.
  2. Get the OCI_TypeInfo — the dictionary-level description.
  3. Walk it — TypeInfoGet* gives column/type lists, ColumnCount, per-attribute info.
  4. Use it to drive code — describe-once, generate or adapt at runtime.

Hints: TypeInfoGet results are cached per-connection — the second describe of the same object is cheap; fully-qualify with schema.object when the object isn't in your schema — unqualified misses are silent; and describe a type (OCI_TIF_TYPE) when you want the UDT layout, a table (OCI_TIF_TABLE) when you want the column list — easy to mix up.

Try it yourself

  1. Describe employees and departments — compare with describe in SQL*Plus.
  2. Describe an object type (create type t_point as object(x number, y number)) and print its attribute list.
  3. Walk GetSuperType on a type created under another.

Common pitfalls

Recap

Next: objects — create them from a TypeInfo and fill their attributes.