*** empty log message ***
[csql.git] / test / system / Allocator / common.h
blob32b61b64a19ec802378b9c637382d9580023c0e2
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 DbRetVal rv = dbMgr->createIndex(indname, idxInfo);
10 if (rv != OK) {
11 printf("Index creation failed\n");
12 delete idxInfo;
13 return rv;
15 printf("Index created for %s\n", fldname);
16 delete idxInfo;
17 return OK;
19 DbRetVal createTable(DatabaseManager *dbMgr, char *tblname)
21 TableDef tabDef;
22 tabDef.addField("f1", typeInt, 0, NULL, true);
23 tabDef.addField("f2", typeInt);
24 DbRetVal rv = dbMgr->createTable(tblname, tabDef);
25 if (rv != OK) { printf("Table creation failed\n"); return rv; }
26 printf("Table created\n");
27 return OK;
29 DbRetVal dropTable(DatabaseManager *dbMgr, char *tblname)
31 DbRetVal rv = dbMgr->dropTable(tblname);
32 if (rv != OK) { printf("Table drop failed\n"); return rv; }
33 printf("Table Dropped\n");
34 return OK;
36 int insertTuple(DatabaseManager *dbMgr, Connection &conn, char *tblname, int count)
38 Table *table = dbMgr->openTable(tblname);
39 if (table == NULL)
41 printf("Unable to open table\n");
42 return 1;
44 int id1 = 0;
45 table->bindFld("f1", &id1);
46 table->bindFld("f2", &id1);
47 int icount =0;
48 DbRetVal rv = OK;
49 rv = conn.startTransaction();
50 if (rv != OK) { dbMgr->closeTable(table); return 1; }
51 for (int i = 0 ; i < count ; i++)
53 id1= i;
54 rv = table->insertTuple();
55 if (rv != OK) break;
56 icount++;
59 conn.commit();
60 printf("Total Tuples inserted is %d\n", icount);
61 dbMgr->closeTable(table);
62 return icount;
64 int deleteTuple(DatabaseManager *dbMgr, Connection &conn, char *tblname, int count)
66 Table *table = dbMgr->openTable(tblname);
67 if (table == NULL)
69 printf("Unable to open table\n");
70 return 1;
72 DbRetVal rv = OK;
74 Condition p1;
75 int val1 = 0;
76 p1.setTerm("f1", OpEquals, &val1);
77 table->setCondition(&p1);
79 rv = conn.startTransaction();
80 if (rv != OK) { dbMgr->closeTable(table); return 1; }
81 int i =0;
82 void *tuple;
83 for (i = 0 ; i < count ; i++)
85 val1 = i;
86 table->execute();
87 if (rv != OK) break;
88 tuple = (char*)table->fetch() ;
89 if (tuple == NULL) break;
90 rv = table->deleteTuple();
91 if (rv != OK) break;
92 table->closeScan();
94 conn.commit();
95 printf("Total Tuples deleted is %d\n", i);
96 dbMgr->closeTable(table);
97 return i;
99 int deleteAllTuple(DatabaseManager *dbMgr, Connection &conn, char *tblname)
101 Table *table = dbMgr->openTable(tblname);
102 if (table == NULL)
104 printf("Unable to open table\n");
105 return 1;
107 DbRetVal rv = OK;
109 table->setCondition(NULL);
111 rv = conn.startTransaction();
112 if (rv != OK) { dbMgr->closeTable(table); return 1; }
113 int count =0;
114 table->execute();
115 void *tuple;
116 while ((tuple = (char*) table->fetch())) {
117 rv = table->deleteTuple();
118 if (rv != OK) break;
119 count++;
121 table->closeScan();
123 conn.commit();
124 printf("Total Tuples deleted is %d\n", count);
125 dbMgr->closeTable(table);
126 return count;
128 int totalTuples(DatabaseManager *dbMgr, Connection &conn, char *tblname)
130 Table *table = dbMgr->openTable(tblname);
131 if (table == NULL)
133 printf("Unable to open table\n");
134 return 1;
136 DbRetVal rv = OK;
138 table->setCondition(NULL);
140 rv = conn.startTransaction();
141 if (rv != OK) { dbMgr->closeTable(table); return 1; }
142 int count =0;
143 table->execute();
144 void *tuple;
145 while ((tuple = (char*) table->fetch())) {
146 count++;
148 table->closeScan();
150 conn.commit();
151 dbMgr->closeTable(table);
152 return count;