Oracle® C++ Call Interface Programmer's Guide, 11g Release 2 (11.2) Part Number E10764-03 |
|
|
PDF · Mobi · ePub |
This chapter describes how to retrieve metadata about result sets or the database as a whole.
This chapter contains these topics:
Database objects have various attributes that describe them; you can obtain information about a particular schema object by performing a DESCRIBE
operation. The result can be accessed as an object of the Metadata
class 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, such as tables, types, sequences, views, type attributes, columns, procedures, type methods, arguments, functions, collections, results, packages, synonyms, and lists
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 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 describing information. The MetaData
class provides the get
xxx
()
methods to access this information.
When performing DESCRIBE
operations, be aware of the following issues:
The ATTR_TYPECODE
returns type codes that represent the type supplied when you created a new type by using the CREATE
TYPE
statement. These type codes are of the enumerated type TypeCode
, which are represented by OCCI_TYPECODE
constants. Internal PL/SQL types (boolean, indexed table) are not supported
The ATTR_DATA_TYPE
returns types that represent the data types of the database columns. These values are of enumerated type Type
. 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. You must first 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 that contain more information. More information about an object can be obtained by calling the get
xxx
()
methods.
If you must 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. Beacuse you are aware of the type of the object you want to describe (table, column, type, collection, function, procedure, and so on), you can obtain the attribute list. You can retrieve the value into a variable of the type specified in the table by calling the corresponding get
xxx
()
method.
This section provides code examples for using metadata:
Example 6-1, "How to Obtain Metadata About Attributes of a Simple Database Table"
Example 6-2, "How to Obtain Metadata from a Column Containing User-Defined Types"
Example 6-3, "How to Obtain Object Metadata from a Reference"
Example 6-4, "How to Obtain Metadata About a Select List from a ResultSet Object"
Example 6-1 How to Obtain Metadata About Attributes of a Simple Database Table
This 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, such as 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 */ }
Example 6-2 How to Obtain Metadata from a Column Containing User-Defined Types
This example demonstrates how to obtain metadata from a column that contains user-defined types database table.
/* 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); /* Have metadata information on CUSTOMERS table; call the getxxx methods */ /* 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 list of columns in the CUSTOMERS table */ vector<MetaData>listOfColumns; listOfColumns=custtab_metaData.getVector(MetaData::ATTR_LIST_COLUMNS); /* Assuming metadata for column cust_address_typ is fourth element in list*/ MetaData customer_address=listOfColumns[3]; /* 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 */
Example 6-3 How to Obtain Object Metadata from a Reference
This example demonstrates how to obtain metadata about an object when using a reference to it:
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 data type 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 data type 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 */ }
Example 6-4 How to Obtain Metadata About a Select List from a ResultSet Object
This example demonstrates how to obtain metadata about a select list from a ResultSet
.
/* 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.
All elements have some attributes specific to that element and some generic attributes. Table 6-2 describes the attributes that belong to all elements:
Table 6-2 Attributes that Belong to All Elements
Attribute | Description | Attribute Data Type |
---|---|---|
|
Object or schema ID |
|
|
Object, schema, or database name |
|
|
Schema where object is located |
|
|
Type of information described by the parameter. Possible values are:
|
|
|
The |
|
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:
Table 6-3 Attributes that Belong to Tables or Views
Attribute | Description | Attribute Data Type |
---|---|---|
|
Object ID |
|
|
Number of columns |
|
|
Column list (type |
|
|
|
|
|
Identifies whether the table or view is temporary |
|
|
Identifies whether the table or view is typed |
|
|
Duration of a temporary table. Values can be:
|
|
The additional attributes belonging to tables are described in Table 6-4.
Table 6-4 Attributes Specific to Tables
Attribute | Description | Attribute Data Type |
---|---|---|
|
Data block address of the segment header |
|
|
Tablespace the table resides on |
|
|
Identifies whether the table is clustered |
|
|
Identifies whether the table is partitioned |
|
|
Identifies whether the table is index only |
|
A parameter for a procedure or function (type PTYPE_PROC
or PTYPE_FUNC
) has the type-specific attributes described in Table 6-5.
Table 6-5 Attributes that Belong to Procedures or Functions
Attribute | Description | Attribute Data Type |
---|---|---|
|
Argument list; refer to List Attributes . |
|
|
Identifies whether the procedure or function has invoker's rights. |
|
The additional attributes belonging to package subprograms are described in Table 6-6.
Table 6-6 Attributes that Belong to Package Subprograms
Attribute | Description | Attribute Data Type |
---|---|---|
|
Name of procedure or function |
|
|
Overloading ID number (relevant in case the procedure or function is part of a package and is overloaded). Values returned may be different from direct query of a PL/SQL function or procedure. |
|
A parameter for a package (type PTYPE_PKG
) has the type-specific attributes described in Table 6-7.
Table 6-7 Attributes that Belong to Packages
Attribute | Description | Attribute Data Type |
---|---|---|
|
Subprogram list; refer to List Attributes. |
|
|
Identifies whether the package has invoker's rights |
|
A parameter for a type (type PTYPE_TYPE
) has attributes described in Table 6-8.
Table 6-8 Attributes that Belong to Types
Attribute | Description | Attribute Data Type |
---|---|---|
|
Returns the in-memory ref of the type descriptor object for the type, if the column type is an object type. |
|
|
Type code. Can be:
|
|
|
Type code of collection if type is collection; invalid otherwise. Can be:
|
|
|
A |
|
|
Identifies whether this is a final type |
|
|
Identifies whether this is an instantiable type |
|
|
Identifies whether this is a subtype |
|
|
Name of the schema containing the supertype |
|
|
Name of the supertype |
|
|
Identifies whether this type is invoker's 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 |
|
|
Identifies whether this type contains a |
|
|
Handle to collection element Refer to Collection Attributes |
|
|
Number of type attributes |
|
|
List of type attributes Refer to List Attributes |
|
|
Number of type methods |
|
|
List of type methods Refer to List Attributes |
|
|
Map method of type Refer to Type Method Attributes |
|
|
Order method of type; 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.
Table 6-9 Attributes that Belong to Type Attributes
Attribute | Description | Attribute Data Type |
---|---|---|
|
Maximum size of the type attribute. This length is returned in bytes and not characters for strings and raws. Returns |
|
|
Type code |
|
|
Data type of the type attribute |
|
|
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 |
|
|
A string that is the type name. The returned value contains the type name if the data type 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.
Table 6-10 Attributes that Belong to Type Methods
Attribute | Description | Attribute Data Type |
---|---|---|
|
Name of method (procedure or function) |
|
|
Encapsulation level of the method; can be:
|
|
|
Argument list |
|
|
Identifies whether the method is a constructor |
|
|
Identifies whether the method is a destructor |
|
|
Identifies whether the method is an operator |
|
|
Identifies whether the method is selfish |
|
|
Identifies whether the method is a map method |
|
|
Identifies whether the method is an order method |
|
|
Identifies whether "Read No Data State" is set for the method |
|
|
Identifies whether "Read No Process State" is set for the method |
|
|
Identifies whether "Write No Data State" is set for the method |
|
|
Identifies whether "Write No Process State" is set for the method |
|
|
Identifies whether this is a final method |
|
|
Identifies whether this is an instantiable method |
|
|
Identifies whether this is an overriding method |
|
A parameter for a collection type (type PTYPE_COLL
) has the attributes described in Table 6-11.
Table 6-11 Attributes that Belong to Collection Types
Attribute | Description | Attribute Data Type |
---|---|---|
|
Maximum size of the type attribute. This length is returned in bytes and not characters for strings and raws. Returns |
|
|
Typecode. |
|
|
The data type of the type attribute. |
|
|
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 contains the type name if the data type is |
|
|
String with the schema name under which the type has been created |
|
|
Returns the in memory |
|
|
Typecode. |
|
|
The data type of the type attribute. |
|
A parameter for a synonym (type PTYPE_SYN
) has the attributes described in Table 6-12.
Table 6-12 Attributes that Belong to Synonyms
Attribute | Description | Attribute Data Type |
---|---|---|
|
Object ID |
|
|
Null-terminated string containing the schema name of the synonym translation |
|
|
Null-terminated string containing the object name of the synonym translation |
|
|
Null-terminated string containing the database link name of the synonym translation |
|
A parameter for a sequence (type PTYPE_SEQ
) has the attributes described in Table 6-13.
Table 6-13 Attributes that Belong to Sequences
Attribute | Description | Attribute Data Type |
---|---|---|
|
Object ID |
|
|
Minimum value (in Oracle number format) |
|
|
Maximum value (in Oracle number format) |
|
|
Increment (in Oracle number format) |
|
|
Number of sequence numbers cached; zero if the sequence is not a cached sequence (in Oracle number format) |
|
|
Identifies whether the sequence is ordered |
|
|
High-water mark (in Oracle number format) |
|
A parameter for a column of a table or view (type PTYPE_COL
) has the attributes described in Table 6-14.
Table 6-14 Attributes that Belong to Columns of Tables or Views
Attribute | Description | Attribute Data Type |
---|---|---|
|
Maximum size of the column. This length is returned in bytes and not characters for strings and raws. Returns |
|
|
The data type of the column. |
|
|
Pointer to a string that is the column name. |
|
|
Returns the precision. |
|
|
Scale of numeric columns. If the precision is nonzero and scale is |
|
|
Returns |
|
|
Returns a string that is the type name. The returned value contains the type name if the data type is |
|
|
Returns a string with the schema name under which the type has been created. |
|
|
The |
|
|
Character set ID for character column. If not set, the character set ID defaults to the character set ID set in the direct path context. |
|
|
Character set form of the column. Setting this attribute specifies the use of the database or national character set on the client side. |
|
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.
Table 6-15 Attributes that Belong to Arguments / Results
Attribute | Description | Attribute Data Type |
---|---|---|
|
Returns a pointer to a string which is the argument name |
|
|
Position of the argument in the argument list. Always returns |
|
|
Typecode. |
|
|
Data type of the argument. |
|
|
Size of the data type 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 is |
|
|
Scale of numeric arguments. If the precision is nonzero and scale is |
|
|
Data type 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 |
bool |
|
Returns a string that is the type name (or the package name for local package types). The returned value contains the type name if the data type is |
|
|
For |
|
|
For |
|
|
For |
|
|
Returns the |
|
|
Returns the character set ID if the argument is of a string or character type. |
|
|
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.
Table 6-16 Values for ATTR_LIST_TYPE
Possible Values | Description |
---|---|
|
Column list |
|
Procedure or function arguments list |
|
Subprogram list |
|
Type attribute list |
|
Type method list |
|
Object list within a schema |
|
Schema list within a database |
A parameter for a schema type (type PTYPE_SCHEMA
) has the attributes described in Table 6-17.
A parameter for a database (type PTYPE_DATABASE
) has the attributes described in Table 6-18.
Table 6-18 Attributes Specific to Databases
Attribute | Description | Attribute Data Type |
---|---|---|
|
Database version |
|
|
Database character set ID from the server handle |
|
|
Database native character set ID from the server handle |
|
|
List of schemas (type |
|
|
Maximum length of a procedure name |
|
|
Maximum length of a column name |
|
|
How a
|
|
|
Maximum length of a catalog (database) name |
|
|
Position of the catalog in a qualified table; values are:
|
|
|
Identifies whether the database supports savepoints; values are:
|
|
|
Identifies whether the database supports the nowait clause; values are:
|
|
|
Identifies whether the autocommit mode is required for DDL statements; values are:
|
|
|
Locking mode for the database; values are:
|
|