Now that you understand the mechanics of creating an OCI XML application, we can examine the Query application’s Main function:
#include "sample.hpp" /*-------------------------Main function --------------------------*/ int main( int argc, char* argv[]) { /* database login information */ text *user=(text *)"HR"; text *password=(text *)"HR"; /* OCI environment variables */ ociapi* ociobjp; /* Miscellaneous */ boolean tab_exists = TRUE; ub4 row = 1; if (argc < 2) { printf( "\nUsage error: \"<SQL-XML Query>\" \"<XPath>\" \n\n"); return OCI_ERROR; } ociobjp = new ociapi(); /* Initialize the environment and allocate handles */ if (ociobjp->init_env_handle()) { printf("FAILED: init_env_handle()!\n"); delete ociobjp; return OCI_ERROR; } /* Log on to the server and begin a session */ if (ociobjp->connect_server( user, password)) { printf("FAILED: connect_server()!\n"); delete ociobjp; return OCI_ERROR; } /* Select an xmltype column by defining it to an xmltype instance, and query the XOB using the Xpath expression with the unified DOM APIs */ if (ociobjp->select_and_query((text *)argv[1], (text *)argv[2]) { printf("FAILED: select_and_query!\n"); ociobjp->disconnect_server( tab_exists); delete ociobjp; return OCI_ERROR; } /* Detach from a server and clean up the environment */ ociobjp->disconnect_server( tab_exists); delete ociobjp; return OCI_SUCCESS; }
The application has the software steps of initialization; connecting to the database; submitting the query; performing the application logic on the results; and cleaning up before exiting. In the following sections, you will see the details of each of these steps as the application is built out. Note that there is a series of if statements to call the code we have discussed that set up the environment, connect to and disconnect from the database, and clean up. What is left is the select_and_query() function that does all the work.