1
[csql.git] / test / dbapi / Predicate / common.h
blob4b9203bda38b0ce6ff6bfac04a6ff6418e419425
1 #include<CSql.h>
2 DbRetVal createIndex(DatabaseManager *dbMgr, char *fldname, char *indname, bool flag=false)
4 HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
5 strcpy(idxInfo->tableName, "t1");
6 idxInfo->list.append(fldname);
7 if (flag)
8 idxInfo->indType = treeIndex;
9 else
10 idxInfo->indType = hashIndex;
11 DbRetVal rv = dbMgr->createIndex(indname, idxInfo);
12 if (rv != OK) { printf("Index creation failed\n"); return rv; }
13 printf("Index created for %s\n", fldname);
14 delete idxInfo;
15 return OK;
17 DbRetVal createTable(DatabaseManager *dbMgr)
19 TableDef tabDef;
20 tabDef.addField("f1", typeInt, 0, NULL, true);
21 tabDef.addField("f2", typeInt, 0, NULL, true);
22 tabDef.addField("f3", typeInt);
23 tabDef.addField("f4", typeInt);
24 tabDef.addField("f5", typeInt);
25 DbRetVal rv = dbMgr->createTable("t1", tabDef);
26 if (rv != OK) { printf("Table creation failed\n"); return rv; }
27 printf("Table created\n");
28 return OK;
30 int insertTuple(DatabaseManager *dbMgr, Connection &conn)
32 Table *table = dbMgr->openTable("t1");
33 if (table == NULL)
35 printf("Unable to open table\n");
36 return 0;
38 int id1 = 0;
39 table->bindFld("f1", &id1);
40 table->bindFld("f2", &id1);
41 table->bindFld("f3", &id1);
42 table->bindFld("f4", &id1);
43 table->bindFld("f5", &id1);
44 int icount =0;
45 DbRetVal rv = OK;
46 rv = conn.startTransaction();
47 if (rv != OK) { dbMgr->closeTable(table); return 1; }
48 for (int i = 0 ; i < 10 ; i++)
50 id1= i;
51 rv = table->insertTuple();
52 if (rv != OK) break;
53 icount++;
56 conn.commit();
57 printf("Total Tuples inserted is %d\n", icount);
58 dbMgr->closeTable(table);
59 return icount;
61 DbRetVal execAndPrint(Table *table)
63 table->execute();
64 void *tuple;
65 while ((tuple = (char*) table->fetch())) {
66 char *t = (char*) tuple;
67 printf("tuple value is %d %d %d %d %d\n", *(int*)t, *(int*)(t+4),
68 *(int*)(t+8),*(int*)(t+12),*(int*)(t+16));
70 table->closeScan();
71 return OK;