#include #include #include void create(), list(), insert(), update(); void clrbuf(char*, int), failure(char*), usage(); extern "C"{ void exit(int); } SqlMgr sql("scott/tiger"); main(int argc, char** argv) { cout << "CB++ Test Application\n"; char cmd = argc > 1 ? *argv[1] : '?'; switch(cmd) { case 'c': create(); break; case 'l': list(); break; case 'i': insert(); break; case 'u': update(); break; default: usage(); } cout << "\n\nTest end.\n"; } char crtitem[] = "CREATE TABLE XSALES (COMPANY CHAR(50), PRODUCT CHAR(20), \ QUANTITY NUMBER, PRICE NUMBER)"; void create() { if(!sql.perform(crtitem)) { if(sql.status() == CB_ERR_TBLEXIST) cout << "table already exists.\n"; else failure("cannot create table"); } cout << "Table created.\n"; } void list() { SqlHandle h1; char comp[60]; char prod[20]; int amnt; int price; clrbuf(comp, 60); clrbuf(prod, 20); if(!(h1 = sql.open("SELECT COMPANY, PRODUCT, QUANTITY, PRICE AS \ :STRING, :STRING, :INT, :INT FROM XSALES", comp, prod, &amnt, &price))) failure("cannot open select statement."); if(!sql.execute(h1)) failure("select execute failed."); while(sql.fetch(h1)) { cout << "\n" << comp << " \t" << prod << " \t" << amnt << " \t" << price; cout.flush(); } sql.close(h1); } void insert() { SqlHandle h1; char comp[60]; char prod[20]; int amnt; int price; clrbuf(comp, 60); clrbuf(prod, 20); if(!(h1 = sql.open("INSERT INTO XSALES \ VALUES( :STRING, :STRING, :INT, :INT )", comp, prod, &amnt, &price))) failure("cannot open insert statement."); while(1) { cout << "enter sales data...\n"; cout << "company: "; cin >> comp; if(cin.eof()) break; cout << "product: "; cin >> prod; cout << "amount : "; cin >> amnt; cout << "price : "; cin >> price; if(!sql.execute(h1)) failure("insert execute failed."); } if(!sql.close(h1) || !sql.commit()) failure("close/commit insert failed."); } void update() { SqlHandle h1, h2; char comp[60]; char prod[20]; int price; clrbuf(comp, 60); clrbuf(prod, 20); if( !(h1 = sql.open("SELECT PRODUCT, PRICE AS :STRING, :INT \ FROM XSALES WHERE COMPANY = :STRING", prod, &price, comp)) || !(h2 = sql.open("UPDATE XSALES SET PRICE = :INT \ WHERE PRODUCT = :STRING", &price, prod))) failure("error in select/update statement"); while(1) { cout << "name of company: "; cin >> comp; if(cin.eof()) break; if(!sql.execute(h1)) failure("select execute failed."); if(!sql.fetch(h1)) { cout << "invalid name, try again.\n"; continue; } cout << comp << ": " << prod << ", current price: " << price; cout << " - enter new price: "; cin >> price; if(!sql.execute(h2)) failure("update execute failed."); } if(!sql.close(h1) || !sql.close(h2)) failure("close select or update failed."); sql.commit(); } // utility functions /////////////////////////////////////// void failure(char* msg) { cerr << "CB Test error: " << msg << "\n"; exit(0); } void usage() { cerr << "valid commands are: c(reate), l(ist), i(nsert), u(pdate)\n"; } void clrbuf(char* buf, int sz) { for(int i = 0; i < (sz - 1); i++) *(buf + i) = ' '; *(buf + sz - 1) = '\0'; }