// // INFORMIX SOFTWARE, INC. // // PROPRIETARY DATA // // THIS DOCUMENT CONTAINS TRADE SECRET DATA WHICH IS THE PROPERTY OF // INFORMIX SOFTWARE, INC. THIS DOCUMENT IS SUBMITTED TO RECIPIENT IN // CONFIDENCE. INFORMATION CONTAINED HEREIN MAY NOT BE USED, COPIED OR // DISCLOSED IN WHOLE OR IN PART EXCEPT AS PERMITTED BY WRITTEN AGREEMENT // SIGNED BY AN OFFICER OF INFORMIX SOFTWARE, INC. // // THIS MATERIAL IS ALSO COPYRIGHTED AS AN UNPUBLISHED WORK UNDER // SECTIONS 104 AND 408 OF TITLE 17 OF THE UNITED STATES CODE. // UNAUTHORIZED USE, COPYING OR OTHER REPRODUCTION IS PROHIBITED BY LAW. // // Title: cppclient.cpp // // Description: // Example for illustrating the use of UDTs. #include #include #include typedef struct { int complexNum[2]; }ComplexNumber; class cppquery { public: cppquery(); ~cppquery(); int exec_query(ITQuery query, char *qtext); void Connect(char *dbName); void create_RowType_table(ITQuery query); void create_QualifiedType_table(ITQuery query); void create_CollectionType_table(ITQuery query); void create_DistinctType_table(ITQuery query); void create_OpaqueType_table(ITQuery query); void insert_RowType_values(ITQuery query); void insert_QualifiedType_values(ITQuery query); void insert_CollectionType_values(ITQuery query); void insert_DistinctType_values(ITQuery query); void insert_OpaqueType_values(ITQuery query); void query_OpaqueType_values(ITConnection conn); void query_RowType_values(ITConnection conn); void query_DistinctType_values(ITConnection conn); void query_CollectionType_values(ITQuery query); }; cppquery::cppquery() { } cppquery::~cppquery() { } int cppquery::exec_query(ITQuery query, char *qtext) { int errCode; if (!query.ExecForIteration(qtext)) { cout << "Could not execute query: " << qtext << endl; errCode = -1; } else { errCode = 0; if (query.Error()) cout << "Error: " << query.ErrorText() << endl; } return errCode; } /******************************************************************* Method Name : create_RowType_table Parameters : ITQuery Return type : None Description : Creates a table with row types and inserts values into it. ********************************************************************/ void cppquery::create_RowType_table(ITQuery query) { char queryText[200]; int errCode; // drop table if it exists sprintf(queryText, "drop table employee"); errCode = exec_query(query, queryText); if (errCode == 0) cout << "Executed query: " << queryText << endl; // construct the query string for creating table sprintf(queryText, "create table employee ( emp_id integer, emp_name name_t, emp_address address_t)"); errCode = exec_query(query, queryText); if (errCode == 0) cout << "Executed query: " << queryText << endl; //printf("Created row type table\n"); // insert the values into the table insert_RowType_values(query); } /******************************************************************* Method Name : create_QualifiedType_table Parameters : ITQuery Return type : None Description : Creates a table with qualified types. Calls insert_Qualified_values() function to insert values into it. *********************************************************************/ void cppquery::create_QualifiedType_table(ITQuery query) { char queryText[200]; int errCode; // drop table if it exists sprintf(queryText, "drop table teams"); errCode = exec_query(query, queryText); if (errCode == 0) cout << "Executed query: " << queryText << endl; /* This function shows how to create a table with qualified types*/ sprintf(queryText, "create table teams (name varchar(20), city varchar(20), state char(2), zip char(5) )"); errCode = exec_query(query, queryText); if (errCode == 0) cout << "Executed query: " << queryText << endl; insert_QualifiedType_values(query); } /******************************************************************* Method Name : create_CollectionType_table Parameters : ITQuery Return type : None Description : Creates a table with set, multiset, and list as columns. Calls insert_Collection_ Type_values() function to load values. ********************************************************************/ void cppquery::create_CollectionType_table(ITQuery query) { char queryText[200]; int errCode; // drop table if it exists sprintf(queryText, "drop table collections"); errCode = exec_query(query, queryText); if (errCode == 0) cout << "Executed query: " << queryText << endl; sprintf(queryText, "create table collections (names list(name_t not null), age set(integer not null), isStudent multiset(boolean not null) )"); errCode = exec_query(query, queryText); if (errCode == 0) cout << "Executed query: " << queryText << endl; insert_CollectionType_values(query); } /******************************************************************* Method Name : create_DistinctType_table Parameters : ITQuery Return type : None Description : Creates a table with distinct types and inserts values into it. Uses the distinct types dollar and yen created using DBDK. ********************************************************************/ void cppquery::create_DistinctType_table(ITQuery query) { char queryText[200]; int errCode; // drop table if it exists sprintf(queryText, "drop table items"); errCode = exec_query(query, queryText); if (errCode == 0) cout << "Executed query: " << queryText << endl; /* Creating a table containing distinct types. */ sprintf(queryText, "create table items (item_name varchar(20), us_price dollar, japan_price yen)"); errCode = exec_query(query, queryText); if (errCode == 0) cout << "Executed query: " << queryText << endl; insert_DistinctType_values(query); } /******************************************************************* Method Name : create_OpaqueType_table Parameters : ITQuery Return type : None Description : Creates a table with opaque types and inserts values into it. The opaque type used is a complex number. ********************************************************************/ void cppquery::create_OpaqueType_table(ITQuery query) { char queryText[200]; int errCode; // drop table if it exists sprintf(queryText, "drop table complex"); errCode = exec_query(query, queryText); if (errCode == 0) cout << "Executed query: " << queryText << endl; sprintf(queryText, "create table complex ( x integer, number ComplexNumber)"); errCode = exec_query(query, queryText); if (errCode == 0) cout << "Executed query: " << queryText << endl; insert_OpaqueType_values(query); } /******************************************************************* Method Name : insert_RowType_values () Parameters : ITQuery Return type : None Description : This function is called by create_RowType_values(). The function inserts values into a table containing row types. ********************************************************************/ void cppquery::insert_RowType_values(ITQuery query) { char queryText[200]; int errCode; /* Using a Row Type (zip_t) in another Row Type (address_t) */ sprintf(queryText, "insert into employee values( 1, row('Smith', 'John', 'X') ::name_t, row('100 State St.', 'Carmel', 'CA', row('95401')::zip_t)::address_t)"); exec_query(query, queryText); sprintf(queryText, "insert into employee values( 2, row('Clark', 'Eric', 'M') ::name_t, row('2010 Flagstone Dr.', 'Huntsville', 'AL', row('22045')::zip_t)::address_t)"); exec_query(query, queryText); sprintf(queryText, "insert into employee values( 3, row('Jones', 'Jimmy', 'M')::name_t, row('432 Lakeside Dr.', 'Mt. View', 'CA', row('94087')::zip_t)::address_t)"); errCode = exec_query(query,queryText); if (errCode == 0) cout << "Executed query: " << queryText << endl; // printf("Inserted Values into row type table\n"); } /******************************************************************* Method Name : insert_QualifiedType_values() Parameters : ITQuery Return type : None Description : Inserts values into the table containing qualified types. ********************************************************************/ void cppquery::insert_QualifiedType_values(ITQuery query) { char queryText[200]; int errCode; /* Using Qualified Types */ sprintf(queryText, "insert into teams values( 'Jets', 'New York', 'NY', '11021')"); errCode = exec_query(query, queryText); sprintf(queryText, "insert into teams values( 'Niners', 'San Francisco', 'CA', '95040')"); errCode = exec_query(query, queryText); sprintf(queryText, "insert into teams values( 'Packers', 'Green Bay', 'WI', '53120')"); errCode = exec_query(query, queryText); sprintf(queryText, "insert into teams values( 'Raiders', 'Oakland', 'CA', '94056')"); errCode = exec_query(query, queryText); if (errCode == 0) cout << "Executed query: " << queryText << endl; } /******************************************************************* Method Name : insert_DistinctType_values() Parameters : ITQuery Return type : None Description : Called by create_DistinctType_table. The function inserts values into a table containing distinct types. ********************************************************************/ void cppquery::insert_DistinctType_values(ITQuery query) { char queryText[200]; int errCode; /* Using Distinct Types */ sprintf(queryText, "insert into items values( 'Pencil', 1.00::money::dollar, 38.00::money::yen)"); exec_query(query, queryText); sprintf(queryText, "insert into items values( 'Note Book', 1.60::money::dollar, 94.00::money::yen)"); exec_query(query, queryText); sprintf(queryText, "insert into items values( 'Bag',19.60::money::dollar, 2004.00::money::yen)"); exec_query(query, queryText); sprintf(queryText, "insert into items values( 'Eraser', 0.60::money::dollar, 24.00::money::yen)"); errCode = exec_query(query, queryText); if (errCode == 0) cout << "Executed query: " << queryText << endl; } /******************************************************************* Method Name : insert_CollectionType_values() Parameters : ITQuery Return type : None Description : This function inserts values into a table containing collection types. ********************************************************************/ void cppquery::insert_CollectionType_values(ITQuery query) { char queryText[200]; int errCode; /* Using Collection Types */ sprintf(queryText, "insert into collections values( 'list{row(\"Smith\", \"John\", \"X\"), row(\"Jordan\", \"Steve\", \"M\"), row(\"Moore\", \"Eric\", \"T\")}', 'set{22, 35, 17}', 'multiset{\"t\", \"f\", \"t\"}')"); errCode = exec_query(query, queryText); if (errCode == 0) cout << "Executed query: " << queryText << endl; } /******************************************************************* Method Name : insert_OpaqueType_values Parameters : ITQuery Return type : None Description : This function is called by create_Opaque Type_table to insert values into the table containing opaque types. ********************************************************************/ void cppquery::insert_OpaqueType_values(ITQuery query) { char queryText[200]; int errCode; /* Using Opaque Types */ sprintf(queryText, "insert into complex values(1, '1 2' )"); errCode = exec_query(query, queryText); sprintf(queryText, "insert into complex values(2, '5 2' )"); errCode = exec_query(query,queryText); sprintf(queryText, "insert into complex values(3, '3 5' )"); errCode = exec_query(query, queryText); sprintf(queryText, "insert into complex values(4, '8 7' )"); errCode = exec_query(query, queryText); if (errCode == 0) cout << "Executed query: " << queryText << endl; } /****************************************************************** Method Name : query_RowType_values Parameters : None Return type : None Description : Queries for row type values and shows how to display those values to standard output *******************************************************************/ void cppquery::query_RowType_values(ITConnection conn) { char name[20]; char zip[10]; int errCode; ITCursor cursor(conn); if (!cursor.Prepare("select emp_name.first_name, emp_address.zip.code from employee")) cout << "Could not prepare cursor for " << endl; else { if (!cursor.Open(ITCursor::Scrollable+ITCursor::ReadOnly)) { cout << "Could not open cursor " << endl; } else { ITRow *row; int rowcount = 0; while ((row = (ITRow *)cursor.Fetch()) != NULL) { rowcount++; cout << row->Printable() << endl; row->Release(); } } } if (!cursor.Close()) { cout << "Could not close cursor" << endl; } } /******************************************************************* Method Name : query_DistinctType_values() Parameters : ITConnection Return type : None Description : Defines a cursor for fetching distinct type values and fetches those values into host variables which can be used in esql/c programs. ********************************************************************/ void cppquery::query_DistinctType_values(ITConnection conn) { char name[20]; char zip[10]; int errCode; ITCursor cursor(conn); if (!cursor.Prepare("select item_name, us_price from items;")) cout << "Could not prepare cursor for " << endl; else { if (!cursor.Open(ITCursor::Scrollable+ITCursor::ReadOnly)) { cout << "Could not open cursor " << endl; } else { ITRow *row; int rowcount = 0; while (row = cursor.NextRow()) { ITValue *col0 = row->Column(0); ITValue *col1 = row->Column(1); if((!col0 ) && (!col1)) cerr << "Couldn't get the column from the cursor's row" << endl; else cout << "Item Name: " << col0->Printable() << ",\t\tPrice: " << col1->Printable() << endl; } } } if (!cursor.Close()) cout << "Could not close cursor" << endl; } /****************************************************************** Method Name : query_CollectionType_values Parameters : query Return type : None Description : Queries for collection type values and shows how to display those values to standard output *******************************************************************/ void cppquery::query_CollectionType_values(ITQuery query) { char name[20]; char zip[10]; int errCode; ITRow *row = query.ExecOneRow("select names, age from collections"); cout << row->Printable() << endl; row->Release(); } /******************************************************************* Method Name : query_OpaqueType_values Parameters : ITConnection Return type : None Description : Queries for opqaue types and shows how to use those types through a host variable. ********************************************************************/ void cppquery::query_OpaqueType_values(ITConnection conn) { ComplexNumber cpNum; int num; int errCode; char cNum[3]; char message[256]; int msg_len; ITCursor cursor(conn); if (!cursor.Prepare("select x, number from complex")) cout << "Could not prepare cursor for " << endl; else { if (!cursor.Open(ITCursor::Scrollable+ITCursor::ReadOnly)) { cout << "Could not open cursor " << endl; } else { ITRow *row; int rowcount = 0; while ((row = (ITRow *)cursor.Fetch()) != NULL) { rowcount++; cout << row->Printable() << endl; row->Release(); } } } if (!cursor.Close()) { cout << "Could not close cursor" << endl; } } int main(int argc, char *argv[]) { ITDBInfo dbinfo; /* Connect to the database */ if (argc == 2 ) dbinfo.SetDatabase(argv[1]); else { cerr << "Usage: cppclient dbname" << endl; exit(0); } //dbinfo.SetDatabase("udt"); ITConnection conn(dbinfo); cppquery cquery; conn.Open(); // 1 end // Create a query object ITQuery query(conn); // 2 begin // 2 end char qtext[1024]; /* Creates and inserts values into a row type table */ cquery.create_RowType_table(query); /* Creates and inserts values into qualified type table */ cquery.create_QualifiedType_table(query); /* Creates and inserts values into a table containing collection types - list, set, and multiset. */ cquery.create_CollectionType_table(query); /* Creates and inserts values into a table containing distinct types - dollar and yen */ cquery.create_DistinctType_table(query); /* Creates a table with an opaque type and inserts data into the table */ cquery.create_OpaqueType_table(query); cquery.query_RowType_values(conn); cquery.query_DistinctType_values(conn); cquery.query_CollectionType_values(query); cquery.query_OpaqueType_values(conn); conn.Close(); cout << endl; return 0; }