*** empty log message ***
[csql.git] / test / system / lock / common.h
bloba96442a6bc5d2409dce786a7b4bd03278b0f58b3
1 #include<CSql.h>
2 DbRetVal createIndex(DatabaseManager *dbMgr, char *tblname, char *fldname, char *indname)
4 HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
5 strcpy(idxInfo->tableName, tblname);
6 idxInfo->list.append(fldname);
7 idxInfo->indType = hashIndex;
8 idxInfo->isUnique = true;
9 idxInfo->isPrimary = true;
10 DbRetVal rv = dbMgr->createIndex(indname, idxInfo);
11 if (rv != OK) { printf("Index creation failed\n"); return rv; }
12 printf("Index created for %s\n", fldname);
13 delete idxInfo;
14 return OK;
16 DbRetVal createTable(DatabaseManager *dbMgr, char *tblname)
18 TableDef tabDef;
19 tabDef.addField("f1", typeInt, 0, NULL, true);
20 tabDef.addField("f2", typeInt, 0 ,NULL, true);
21 DbRetVal rv = dbMgr->createTable(tblname, tabDef);
22 if (rv != OK) { printf("Table creation failed\n"); return rv; }
23 printf("Table created\n");
24 return OK;
26 DbRetVal dropTable(DatabaseManager *dbMgr, char *tblname)
28 DbRetVal rv = dbMgr->dropTable(tblname);
29 if (rv != OK) { printf("Table drop failed\n"); return rv; }
30 printf("Table Dropped\n");
31 return OK;
33 int insertTuple(DatabaseManager *dbMgr, Connection &conn, char *tblname, int count)
35 Table *table = dbMgr->openTable(tblname);
36 if (table == NULL)
38 printf("Unable to open table\n");
39 return 1;
41 int id1 = 0;
42 table->bindFld("f1", &id1);
43 table->bindFld("f2", &id1);
44 int icount =0;
45 DbRetVal rv = OK;
46 for (int i = 0 ; i < count ; i++)
48 id1= i;
49 rv = table->insertTuple();
50 if (rv != OK) break;
51 icount++;
54 printf("Total Tuples inserted is %d\n", icount);
55 dbMgr->closeTable(table);
56 return icount;
59 int selectTuple(DatabaseManager *dbMgr, Connection &conn, char *tblname, int count)
61 Table *table = dbMgr->openTable(tblname);
62 if (table == NULL)
64 printf("Unable to open table\n");
65 return 1;
67 DbRetVal rv = OK;
69 Condition p1;
70 int val1 = 0;
71 p1.setTerm("f1", OpEquals, &val1);
72 table->setCondition(&p1);
73 int i =0;
74 void *tuple;
75 for (i = 0 ; i < count ; i++)
77 val1 = i;
78 table->execute();
79 if (rv != OK) break;
80 tuple = (char*)table->fetch() ;
81 if (tuple == NULL) break;
82 table->closeScan();
84 printf("Total Tuples selected is %d\n", i);
85 dbMgr->closeTable(table);
86 return i;
88 int updateTuple(DatabaseManager *dbMgr, Connection &conn, char *tblname, int count)
90 Table *table = dbMgr->openTable(tblname);
91 if (table == NULL)
93 printf("Unable to open table\n");
94 return 1;
96 DbRetVal rv = OK;
98 int id1 = 0, id2 =0;
99 table->bindFld("f1", &id1);
100 table->bindFld("f2", &id2);
101 Condition p1;
102 int val1 = 0;
103 p1.setTerm("f1", OpEquals, &val1);
104 table->setCondition(&p1);
105 int i =0;
106 void *tuple;
107 for (i = 0 ; i < count ; i++)
109 val1 = i;
110 table->execute();
111 if (rv != OK) break;
112 tuple = (char*)table->fetch() ;
113 if (tuple == NULL) break;
114 id2 = 1000 + i;
115 rv = table->updateTuple();
116 if (rv != OK) break;
117 table->closeScan();
119 printf("Total Tuples updated is %d\n", i);
120 dbMgr->closeTable(table);
121 return i;
123 int deleteTuple(DatabaseManager *dbMgr, Connection &conn, char *tblname, int count)
125 Table *table = dbMgr->openTable(tblname);
126 if (table == NULL)
128 printf("Unable to open table\n");
129 return 1;
131 DbRetVal rv = OK;
133 Condition p1;
134 int val1 = 0;
135 p1.setTerm("f1", OpEquals, &val1);
136 table->setCondition(&p1);
137 int i =0;
138 void *tuple;
139 for (i = 0 ; i < count ; i++)
141 val1 = i;
142 table->execute();
143 if (rv != OK) break;
144 tuple = (char*)table->fetch() ;
145 if (tuple == NULL) break;
146 rv = table->deleteTuple();
147 if (rv != OK) break;
148 table->closeScan();
150 printf("Total Tuples deleted is %d\n", i);
151 dbMgr->closeTable(table);
152 return i;