#include #include #include #include extern "C"{ void exit(int); } struct item { char descr[26]; int price; } tabdata[] = { "CINNAMON", 10, "SAGE", 95, "BASIL", 89, "OREGANO", 89, "SAFFRON", 30, "GINSENG", 49, "", 0 }; static char note[] = "sample.txt"; #ifdef GUPTA static char dbnam[] = "DEMO/SYSADM/SYSADM"; #endif #ifdef ORACLE static char dbnam[] = "SCOTT/TIGER"; #endif #ifdef OS2 static char dbnam[] = "SAMPLE/BS/TEST"; #endif static char crtitem[] = "CREATE TABLE ITEM (ITEM_NO SMALLINT NOT NULL,\ ITEM_IDX INTEGER, ITEM_DESCR CHAR(25), ITEM_PRICE INTEGER, ITEM_NOTE LONG VARCHAR)"; char notebuf[200]; char itembuf[26]; char pricebuf[25]; unsigned int itemno; SqlHandle h1, h2; SqlMgr sql(dbnam); void failure(char*), itemins(), priceupd(), clrbuf(char*, int); main() { char c; cout << "CB++ test start...\n"; if(sql.perform(crtitem) == FALSE) { if(sql.status() == CB_ERR_TBLEXIST) { cout << "table exists, append?\n"; cin.get(c); if(c != 'y' && c != 'Y') { if(sql.perform("DELETE FROM ITEM") == FALSE) failure("cannot delete table contents"); } } else failure("cannot create table"); } if(sql.commit() == FALSE) failure("cannot commit after create"); itemins(); if(sql.commit() == FALSE) failure("cannot commit after insert"); priceupd(); if(sql.commit() == FALSE) failure("cannot commit after price update"); cout << "Test end.\n"; } void itemins() { unsigned int maxitem = 50; int itemndx; int price = 0; struct item* datap = &tabdata[0]; clrbuf(itembuf, 26); clrbuf(notebuf, 200); if(!(h1 = sql.open("INSERT INTO ITEM VALUES( :UINT, :INT, :STRING, :INT, \ :LSTRING )", (void* )&maxitem, (void* )&itemndx, itembuf, (void* )&price, notebuf))) failure("cannot open insert statement."); while(*(datap->descr) != '\0') { maxitem++; strcpy(itembuf, datap->descr); price = datap->price; strcpy(notebuf, "this should be a very long string..."); if(sql.execute(h1) == FALSE) failure("insert execute failed."); datap++; } if(sql.close(h1) == FALSE) failure("close insert failed."); cout << "all items inserted.\n"; } void priceupd() { int itemndx = 0; int price = 0, nprice = 0; char line[80]; clrbuf(itembuf, 26); clrbuf(notebuf, 200); if(!(h1 = sql.open("SELECT * AS :UINT, :INT, :STRING, :INT, :LSTRING FROM ITEM", &itemno, &itemndx, itembuf, &price, notebuf))) failure("cannot open select statement."); if(!(h2 = sql.open("UPDATE ITEM SET ITEM_PRICE = :INT WHERE ITEM_NO = :UINT", &nprice, &itemno))) failure("cannot open update statement."); if(sql.execute(h1) == FALSE) failure("select execute failed."); while(sql.fetch(h1) == TRUE) { cout << "\n" << itemno << " " << itemndx << " " << itembuf << " " << price << "\n" << notebuf << "\n\n"; cout.flush(); for(;;) { cout << "Enter new price for " << itembuf << " or . for no price change: "; cout.flush(); nprice = 0; cin >> nprice; char c; cin.get(c); if(!nprice) break; cout << "\nnew price = " << nprice << "\n"; if(sql.execute(h2) == FALSE) failure("update execute failed."); } } if(sql.status(h1) != CB_ERR_EOF) failure("fetch data failed."); if(sql.close(h1) == FALSE || sql.close(h2) == FALSE) failure("close select or update failed."); else cout << "all items updated.\n"; } void failure(char* msg) { cerr << "Test error: " << msg << "\ntrying cleanup....\n"; if((h1 && sql.close(h1) == FALSE) || (h2 && sql.close(h2) == FALSE)) cerr << "cannot close all cursors.\n"; if(sql.perform("DROP TABLE ITEM") == TRUE && sql.commit() == TRUE) cerr << "cleanup successful.\n"; else cerr << "cleanup failed.\n"; cerr << "\nexiting.\n"; exit(1); } void clrbuf(char* buf, int sz) { for(int i = 0; i < (sz - 1); i++) *(buf + i) = ' '; *(buf + sz - 1) = '\0'; }