Oracle C++ Call Interface Programmer's Guide Release 2 (9.2) Part Number A96583-01 |
|
This chapter describes how to retrieve metadata about result sets or the database as a whole.
It includes the following topics:
Database objects have various attributes that describe them, and you obtain information about a particular schema object by performing a DESCRIBE
operation for the object. The result can be accessed as an object of the Metadata
class in that you can use class methods to get the actual values of an object. You accomplish this by passing object attributes as arguments to the various methods of the Metadata
class.
You can perform an explicit DESCRIBE
operation on the database as a whole, on the types and properties of the columns contained in a ResultSet
class or on any of the following schema and subschema objects:
You must specify the type of the attribute you are looking for. By using the getAttributeCount
, getAttributeId
, and getAttributeType
methods of the MetaData
class, you can scan through each available attribute.
All DESCRIBE
information is cached until the last reference to it is deleted. Users are in this way prevented from accidentally trying to access DESCRIBE
information that is already freed.
You obtain metadata by calling the getMetaData
method on the Connection
class in case of an explicit describe, or by calling the getColumnListMetaData
method on the ResultSet
class to get the metadata of the result set columns. Both methods return a MetaData
object with the described information. The MetaData
class provides the get
xxx
methods to access this information.
When performing DESCRIBE
operations, be aware of the following issues:
ATTR_TYPECODE
returns typecodes that represent the type supplied when you created a new type by using the CREATE
TYPE
statement. These typecodes are of the enumerated type OCCITypeCode
, which are represented by OCCI_TYPECODE
constants.
ATTR_DATA_TYPE
returns types that represent the datatypes of the database columns. These values are of enumerated type OCCIType
. For example, LONG
types return OCCI_SQLT_LNG
types.Describing database metadata is equivalent to an explicit DESCRIBE
operation. The object to describe must be an object in the schema. In describing a type, you call the getMetaData
method from the connection, passing the name of the object or a RefAny
object. To do this, you must initialize the environment in the OBJECT
mode. The getMetaData
method returns an object of type MetaData
. Each type of MetaData
object has a list of attributes that are part of the describe tree. The describe tree can then be traversed recursively to point to subtrees containing more information. More information about an object can be obtained by calling the get
xxx
methods.
If you need to construct a browser that describes the database and its objects recursively, then you can access information regarding the number of attributes for each object in the database (including the database), the attribute ID listing, and the attribute types listing. By using this information, you can recursively traverse the describe tree from the top node (the database) to the columns in the tables, the attributes of a type, the parameters of a procedure or function, and so on.
For example, consider the typical case of describing a table and its contents. You call the getMetaData
method from the connection, passing the name of the table to be described. The MetaData
object returned contains the table information. Since you are aware of the type of the object that you want to describe (table, column, type, collection, function, procedure, and so on), you can obtain the attribute list as shown in Table 6-1. You can retrieve the value into a variable of the type specified in the table by calling the corresponding get*()
method.
This section provides code examples for obtaining:
The following code example demonstrates how to obtain metadata about attributes of a simple database table:
/* Create an environment and a connection to the HR database */ . . . /* Call the getMetaData method on the Connection object obtainedv*/ MetaData emptab_metaData = connection->getMetaData("EMPLOYEES", MetaData::PTYPE_TABLE); /* Now that you have the metadata information on the EMPLOYEES table, call the getxxx methods using the appropriate attributes */ /* Call getString */ cout<<"Schema:"<<(emptab_metaData.getString(MetaData::ATTR_OBJ_SCHEMA))<<endl; if(emptab_metaData.getInt(emptab_metaData::ATTR_PTYPE)==MetaData::PTYPE_TABLE) cout<<"EMPLOYEES is a table"<<endl; else cout<<"EMPLOYEES is not a table"<<endl; /* Call getInt to get the number of columns in the table */ int columnCount=emptab_metaData.getInt(MetaData::ATTR_NUM_COLS); cout<<"Number of Columns:"<<columnCount<<endl; /* Call getTimestamp to get the timestamp of the table object */ Timestamp tstamp = emptab_metaData.getTimestamp(MetaData::ATTR_TIMESTAMP); /* Now that you have the value of the attribute as a Timestamp object, you can call methods to obtain the components of the timestamp */ int year; unsigned int month, day; tstamp.getData(year, month, day); /* Call getVector for attributes of list type, for example ATTR_LIST_COLUMNS */ vector<MetaData>listOfColumns; listOfColumns=emptab_metaData.getVector(MetaData::ATTR_LIST_COLUMNS); /* Each of the list elements represents a column metadata, so now you can access the column attributes */ for (int i=0;i<listOfColumns.size();i++ { MetaData columnObj=listOfColumns[i]; cout<<"Column Name:"<<(columnObj.getString(MetaData::ATTR_NAME))<<endl; cout<<"Data Type:"<<(columnObj.getInt(MetaData::ATTR_DATA_TYPE))<<endl; . . . /* and so on to obtain metadata on other column specific attributes */ }
The following code example demonstrates how to obtain metadata about a database table with a column containing a user-defined type:
/* Create an environment and a connection to the HR database */ . . . /* Call the getMetaData method on the Connection object obtained */ MetaData custtab_metaData = connection->getMetaData("CUSTOMERS", MetaData::PTYPE_TABLE); /* Now that you have the metadata information on the CUSTOMERS table, call the getxxx methods using the appropriate attributes */ /* Call getString */ cout<<"Schema:"<<(custtab_metaData.getString(MetaData::ATTR_OBJ_SCHEMA))<<endl; if(custtab_metaData.getInt(custtab_metaData::ATTR_PTYPE)==MetaData::PTYPE_TABLE) cout<<"CUSTOMERS is a table"<<endl; else cout<<"CUSTOMERS is not a table"<<endl; /* Call getVector to obtain a list of columns in the CUSTOMERS table */ vector<MetaData>listOfColumns; listOfColumns=custtab_metaData.getVector(MetaData::ATTR_LIST_COLUMNS); /* Assuming that the metadata for the column cust_address_typ is the fourth element in the list... */ MetaData customer_address=listOfColumns[3]; /* Now you can obtain the metadata for the customer_address attribute */ int typcode = customer_address.getInt(MetaData::ATTR_TYPECODE); if(typcode==OCCI_TYPECODE_OBJECT) cout<<"customer_address is an object type"<<endl; else cout<<"customer_address is not an object type"<<endl; string objectName=customer_address.getString(MetaData::ATTR_OBJ_NAME); /* Now that you have the name of the address object, the metadata of the attributes of the type can be obtained by using getMetaData on the connection by passing the object name */ MetaData address = connection->getMetaData(objectName); /* Call getVector to obtain the list of the address object attributes */ vector<MetaData> attributeList = address.getVector(MetaData::ATT_LIST_TYPE_ATTRS); /* and so on to obtain metadata on other address object specific attributes */
The following code example demonstrates how to obtain metadata about an object when using a reference to it:
Assuming the following schema structure:
Type ADDRESS(street VARCHAR2(50), city VARCHAR2(20)); Table Person(id NUMBER, addr REF ADDRESS);
/* Create an environment and a connection to the HR database */ . . . /* Call the getMetaData method on the Connection object obtained */ MetaData perstab_metaData = connection->getMetaData("Person", MetaData::PTYPE_TABLE); /* Now that you have the metadata information on the Person table, call the getxxx methods using the appropriate attributes */ /* Call getString */ cout<<"Schema:"<<(perstab_metaData.getString(MetaData::ATTR_OBJ_SCHEMA))<<endl; if(perstab_metaData.getInt(perstab_metaData::ATTR_PTYPE)==MetaData::PTYPE_TABLE) cout<<"Person is a table"<<endl; else cout<<"Person is not a table"<<endl; /* Call getVector to obtain the list of columns in the Person table */ vector<MetaData>listOfColumns; listOfColumns=perstab_metaData.getVector(MetaData::ATTR_LIST_COLUMNS); /* Each of the list elements represents a column metadata, so now get the datatype of the column by passing ATTR_DATA_TYPE to getInt */ for(int i=0;i<numCols;i++) { int dataType=colList[i].getInt(MetaData::ATTR_DATA_TYPE); /* If the datatype is a reference, get the Ref and obtain the metadata about the object by passing the Ref to getMetaData */ if(dataType==SQLT_REF) RefAny refTdo=colList[i].getRef(MetaData::ATTR_REF_TDO); /* Now you can obtain the metadata about the object as shown MetaData tdo_metaData=connection->getMetaData(refTdo); /* Now that you have the metadata about the TDO, you can obtain the metadata about the object */
The following code example demonstrates how to obtain metadata about a select list from a ResultSet
object:
/* Create an environment and a connection to the database */ . . . /* Create a statement and associate it with a select clause */ string sqlStmt="SELECT * FROM EMPLOYEES"; Statement *stmt=conn->createStatement(sqlStmt); /* Execute the statement to obtain a ResultSet */ ResultSet *rset=stmt->executeQuery(); /* Obtain the metadata about the select list */ vector<MetaData>cmd=rset->getColumnListMetaData(); /* The metadata is a column list and each element is a column metaData */ int dataType=cmd[i].getInt(MetaData::ATTR_DATA_TYPE); . . .
The getMetaData
method is called for the ATTR_COLLECTION_ELEMENT
attribute only.
This section describes the attributes belonging to schema and subschema objects. The following attribute groupings are presented:
All elements have some attributes specific to that element and some generic attributes. Table 6-2 describes the attributes that belong to all elements:
The sections that follow list attributes specific to different types of elements.
A parameter for a table or view (type PTYPE_TABLE
or PTYPE_VIEW
) has the following type-specific attributes described in Table 6-3:
The additional attributes belonging to tables are described in Table 6-4.
A parameter for a procedure or function (type PTYPE_PROC
or PTYPE_FUNC
) has the type-specific attributes described in Table 6-5.
Attribute | Description | Attribute Datatype |
---|---|---|
|
Refer to List Attributes . |
|
|
Identifies whether the procedure or function has invoker-rights. |
|
The additional attributes belonging to package subprograms are described in Table 6-6.
A parameter for a package (type PTYPE_PKG
) has the type-specific attributes described in Table 6-7.
Attribute | Description | Attribute Datatype |
---|---|---|
|
Refer to List Attributes. |
|
|
Identifies whether the package has invoker-rights |
|
A parameter for a type (type PTYPE_TYPE
) has attributes described in Table 6-8.
Attribute | Description | Attribute Datatype |
---|---|---|
|
Returns the in-memory ref of the type descriptor object for the type, if the column type is an object type. |
|
|
Typecode. Can be Refer to Notes on Types and Attributes. |
|
|
Typecode of collection if type is collection; invalid otherwise. Can be Refer to Notes on Types and Attributes. |
|
|
A null terminated string containing the user-assigned version |
|
|
Identifies whether this is a final type |
bool |
|
Identifies whether this is an instantiable type |
bool |
|
Identifies whether this is a subtype |
bool |
|
Name of the schema containing the supertype |
string |
|
Name of the supertype |
string |
|
Identifies whether this type is invoker-rights |
|
|
Identifies whether this type is incomplete |
|
|
Identifies whether this is a system type |
|
|
Identifies whether this is a predefined type |
|
|
Identifies whether this is a transient type |
|
|
Identifies whether this is a system-generated type |
|
|
Identifies whether this type contains a nested table attribute |
|
|
Identifies whether this type contains a LOB attribute |
|
|
Identifies whether this type contains a FILE attribute |
|
|
Refer to Collection Attributes |
|
|
Number of type attributes |
|
|
Refer to List Attributes |
|
|
Number of type methods |
|
|
Refer to List Attributes |
|
|
Refer to Type Method Attributes |
|
|
Refer to Type Method Attributes |
|
A parameter for an attribute of a type (type PTYPE_TYPE_ATTR) has the attributes described in Table 6-9.
Attribute | Description | Attribute Datatype |
---|---|---|
|
Maximum size of the type attribute. This length is returned in bytes and not characters for strings and raws. Returns 22 for |
|
|
Refer to Notes on Types and Attributes. |
|
|
Datatype of the type attribute Refer to Notes on Types and Attributes. |
|
|
A pointer to a string that is the type attribute name |
|
|
Precision of numeric type attributes. If the precision is nonzero and scale is -127, then it is a FLOAT; otherwise a |
|
|
Scale of numeric type attributes. If the precision is nonzero and scale is -127, then it is a |
|
|
A string that is the type name. The returned value will contain the type name if the datatype is |
|
|
String with the schema name under which the type has been created |
|
|
Returns the in-memory |
|
|
Character set ID, if the type attribute is of a string or character type |
|
|
Character set form, if the type attribute is of a string or character type |
|
|
The fractional seconds precision of a datetime or interval |
|
|
The leading field precision of an interval |
|
A parameter for a method of a type (type PTYPE_TYPE_METHOD) has the attributes described in Table 6-10.
A parameter for a collection type (type PTYPE_COLL
) has the attributes described in Table 6-11.
Attribute | Description | Attribute Datatype |
---|---|---|
|
Maximum size of the type attribute. This length is returned in bytes and not characters for strings and raws. Returns |
|
|
Refer to Notes on Types and Attributes. |
|
|
The datatype of the type attribute Refer to Notes on Types and Attributes. |
|
|
Number of elements in an array. Only valid for collections that are arrays. |
|
|
A pointer to a string that is the type attribute name |
|
|
Precision of numeric type attributes. If the precision is nonzero and scale is |
|
|
Scale of numeric type attributes. If the precision is nonzero and scale is |
|
|
String that is the type name. The returned value will contain the type name if the datatype is |
|
|
String with the schema name under which the type has been created |
|
|
Maximum size of the type attribute. This length is returned in bytes and not characters for strings and raws. Returns |
|
|
Refer to Notes on Types and Attributes. |
|
|
The datatype of the type attribute Refer to Notes on Types and Attributes. |
|
A parameter for a synonym (type PTYPE_SYN
) has the attributes described in Table 6-12.
A parameter for a sequence (type PTYPE_SEQ
) has the attributes described in Table 6-13.
A parameter for a column of a table or view (type PTYPE_COL
) has the attributes described in Table 6-14.
Attribute | Description | Attribute Datatype |
---|---|---|
|
Column length in codepoints. The number of codepoints allowed in the column. |
|
|
Type of length semantics of the column. Valid values are 0 for byte-length semantics and 1 for codepoint-length semantics. |
|
|
Maximum size of the column. This length is returned in bytes and not characters for strings and raws. Returns |
|
|
Refer to Notes on Types and Attributes. |
|
|
Pointer to a string that is the column name |
|
|
The precision of numeric columns. If the precision is nonzero and scale is |
|
|
Scale of numeric columns. If the precision is nonzero and scale is |
|
|
Returns |
|
|
Returns a string that is the type name. The returned value will contain the type name if the datatype is |
|
|
Returns a string with the schema name under which the type has been created |
|
|
|
|
A parameter for an argument or a procedure or function type (type PTYPE_ARG
), for a type method argument (type PTYPE_TYPE_ARG
), or for method results (type PTYPE_TYPE_RESULT
) has the attributes described in Table 6-15.
Attribute | Description | Attribute Datatype |
---|---|---|
|
Returns a pointer to a string which is the argument name |
|
|
Position of the argument in the argument list. Always returns |
|
|
Refer to Notes on Types and Attributes. |
|
|
Refer to Notes on Types and Attributes. |
|
|
Size of the datatype of the argument. This length is returned in bytes and not characters for strings and raws. Returns |
|
|
Precision of numeric arguments. If the precision is nonzero and scale |
|
|
Scale of numeric arguments. If the precision is nonzero and scale |
|
|
Datatype levels. This attribute always returns |
|
|
Indicates whether an argument has a default |
|
|
The list of arguments at the next level (when the argument is of a record or table type) |
|
|
Indicates the argument mode. Valid values are
|
|
|
Returns a radix (if number type) |
|
|
Returns |
|
|
Returns a string that is the type name, or the package name in the case of package local types. The returned value contains the type name if the datatype is |
|
|
For |
|
|
For |
|
|
For |
|
|
Returns the |
|
|
Returns the character set |
|
|
Returns the character set form if the argument is of a string or character type |
|
A list type of attribute can be described for all the elements in the list. In case of a function argument list, position 0 has a parameter for return values (PTYPE_ARG).
The list is described iteratively for all the elements. The results are stored in a C++ vector<MetaData>
. Call the getVector
method to describe list type of attributes. Table 6-16 displays the list attributes.
A parameter for a schema type (type PTYPE_SCHEMA
) has the attributes described in Table 6-17.
Attribute | Description | Attribute Datatype |
---|---|---|
|
List of objects in the schema |
|
A parameter for a database (type PTYPE_DATABASE
) has the attributes described in Table 6-18.
See Also:
|
|
Copyright © 2001, 2002 Oracle Corporation. All Rights Reserved. |
|