fixing timing issue in test script
[csql.git] / test / dbapi / Connection / common.h
blob20cd603b47ca3c1c8612bbaddbc28ce47a4d6ea8
1 #include<CSql.h>
2 DbRetVal createTable(DatabaseManager *dbMgr)
4 TableDef tabDef;
5 tabDef.addField("f1", typeInt, 0, NULL, true);
6 tabDef.addField("f2", typeString, 196);
7 DbRetVal rv = dbMgr->createTable("t1", tabDef);
8 if (rv != OK) { printf("Table creation failed\n"); return ErrUnknown; }
9 printf("Table created\n");
10 HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
11 strcpy(idxInfo->tableName, "t1");
12 idxInfo->list.append("f1");
13 idxInfo->isUnique = true;
14 idxInfo->isPrimary = true;
15 idxInfo->indType = hashIndex;
16 rv = dbMgr->createIndex("indx1", idxInfo);
17 if (rv != OK) {
18 printf("Index creation failed\n");
19 delete idxInfo;
20 return ErrUnknown;
22 delete idxInfo;
23 return OK;
25 DbRetVal insert(DatabaseManager *dbMgr, int val, bool isSleep)
27 Table *table = dbMgr->openTable("t1");
28 if (table == NULL) { printf("Unable to open table\n"); return ErrUnknown; }
29 int id = val ;
30 char name[196] = "PRABAKARAN";
31 table->bindFld("f1", &id);
32 table->bindFld("f2", name);
33 char *tuple;
34 int ret;
35 ret = table->insertTuple();
36 if (ret != 0) { dbMgr->closeTable(table); return ErrLockTimeOut; }
37 printf("Inserted tuple : %d %s\n", val, name);
38 if (isSleep) os::sleep(5);
39 dbMgr->closeTable(table);
40 return OK;
42 DbRetVal select(DatabaseManager *dbMgr, int val, bool isSleep, bool checkUpd=false)
44 Table *table = dbMgr->openTable("t1");
45 if (table == NULL) { printf("Unable to open table\n"); return ErrUnknown; }
46 int id = 0;
47 char name[196];
48 table->bindFld("f1", &id);
49 table->bindFld("f2", name);
50 Condition p1;
51 int val1 = val;
52 p1.setTerm("f1", OpEquals, &val1);
53 table->setCondition(&p1);
54 char *tuple;
55 DbRetVal rv = table->execute();
56 if (rv != OK) { dbMgr->closeTable(table); return ErrUnknown; }
57 tuple = (char*)table->fetch(rv) ;
58 if (rv != OK) { dbMgr->closeTable(table); return rv; } // dont remove this line
59 if (tuple == NULL) {
60 printf("Tuple not found\n");
61 dbMgr->closeTable(table);
62 return ErrUnknown;
64 printf("ThreadID: %lu Tuple %d %s\n", os::getthrid(), id, name);
65 if (isSleep) os::sleep(5);
66 dbMgr->closeTable(table);
67 if ( checkUpd && strcmp(name, "KANCHANA") != 0) return ErrUnknown;
68 return OK;
70 DbRetVal update(DatabaseManager *dbMgr, int val, bool isSleep, char *updname = NULL)
72 Table *table = dbMgr->openTable("t1");
73 if (table == NULL) { printf("Unable to open table\n"); return ErrUnknown; }
74 int id = 0;
75 char name[196];
76 table->bindFld("f1", &id);
77 table->bindFld("f2", name);
78 Condition p1;
79 int val1 = val;
80 p1.setTerm("f1", OpEquals, &val1);
81 table->setCondition(&p1);
82 char *tuple;
83 DbRetVal rv = table->execute();
84 if (rv != OK) { dbMgr->closeTable(table); return ErrUnknown; }
85 tuple = (char*)table->fetch(rv);
86 if (rv != OK) { dbMgr->closeTable(table); return rv; } // do not remove this line
87 if (tuple == NULL) {dbMgr->closeTable(table); return ErrUnknown; }
88 if (!updname) strcpy(name, "KANCHANA");
89 else strcpy(name, updname);
90 rv = table->updateTuple();
91 if (rv != OK) { dbMgr->closeTable(table); return ErrUnknown; }
92 printf("ThreadID: %lu Updated Tuple %d %s\n", os::getthrid(), id, name);
94 if (isSleep) os::sleep(5);
95 dbMgr->closeTable(table);
96 return OK;
98 DbRetVal remove(DatabaseManager *dbMgr, int val, bool isSleep)
100 Table *table = dbMgr->openTable("t1");
101 if (table == NULL) { printf("Unable to open table\n"); return ErrUnknown; }
102 int id = 0;
103 char name[196];
104 table->bindFld("f1", &id);
105 table->bindFld("f2", name);
106 Condition p1;
107 int val1 = val;
108 p1.setTerm("f1", OpEquals, &val1);
109 table->setCondition(&p1);
110 char *tuple;
111 DbRetVal rv = table->execute();
112 if (rv != OK) { dbMgr->closeTable(table); return ErrUnknown; }
113 tuple = (char*)table->fetch(rv);
114 if (rv != OK) { dbMgr->closeTable(table); return rv; }
115 if (tuple == NULL) {dbMgr->closeTable(table); return ErrUnknown; }
116 rv = table->deleteTuple();
117 if (rv != OK) { dbMgr->closeTable(table); return ErrUnknown; }
118 printf("ThreadID: %lu Deleted Tuple %d %s\n", os::getthrid(), id, name);
120 if (isSleep) os::sleep(5);
121 dbMgr->closeTable(table);
122 return OK;