1
[csql.git] / test / dbapi / Index / twoindex2.c
bloba4c5fa47ca249cf86e199783fcbdf692284e424f
1 #include<CSql.h>
2 int main()
5 Connection conn;
6 DbRetVal rv = conn.open("root", "manager");
7 if (rv != OK)
9 printf("Error during connection %d\n", rv);
10 return -1;
12 DatabaseManager *dbMgr = conn.getDatabaseManager();
13 if (dbMgr == NULL) { printf("Auth failed\n"); return -1;}
14 TableDef tabDef;
15 tabDef.addField("f1", typeInt, 0, NULL, true);
16 tabDef.addField("f2", typeString, 50);
17 rv = dbMgr->createTable("t1", tabDef);
18 if (rv != OK) { printf("Table creation failed\n"); return -1; }
19 printf("Table created\n");
20 HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
21 strcpy(idxInfo->tableName, "t1");
22 idxInfo->list.append("f1");
23 idxInfo->indType = hashIndex;
24 #ifdef F1TREE
25 idxInfo->indType = treeIndex;
26 #endif
27 rv = dbMgr->createIndex("indx1", idxInfo);
28 if (rv != OK) { printf("Index creation failed\n"); return -1; }
29 printf("Index created for f1\n");
30 printf("size of index field list %d\n", idxInfo->list.size());
31 idxInfo->list.remove("f1");
32 printf("size of index field list %d\n", idxInfo->list.size());
33 idxInfo->list.append("f2");
34 printf("size of index field list %d\n", idxInfo->list.size());
35 idxInfo->indType = hashIndex;
36 #ifdef F2TREE
37 idxInfo->indType = treeIndex;
38 #endif
39 rv = dbMgr->createIndex("indx2", idxInfo);
40 if (rv != OK) { printf("Index creation failed\n"); return -1; }
41 printf("Index created for f2\n");
42 delete idxInfo;
43 Table *table = dbMgr->openTable("t1");
44 if (table == NULL) { printf("Unable to open table\n"); return -1; }
45 int id1 = 0;
46 char id2[50] ="Aruna";
47 table->bindFld("f1", &id1);
48 table->bindFld("f2", id2);
49 char *tuple;
50 int ret;
51 int i;
52 int icount =0;
53 rv = conn.startTransaction();
54 if (rv != OK) exit(1);
55 for(i = 0; i< 10; i++)
57 id1= i;
58 sprintf(id2, "Aruna:%d", i);
59 ret = table->insertTuple();
60 if (ret != 0) break;
61 icount++;
63 conn.commit();
64 printf("Total tuples inserted: %d\n", icount);
65 Condition p1, p2;
66 int val1 = 0;
67 char val2[50];
68 p1.setTerm("f1", OpEquals, &val1);
69 p2.setTerm("f2", OpEquals, &val2);
70 table->setCondition(&p1);
71 rv =conn.startTransaction();
72 if (rv != OK) exit(1);
73 for(i = 0; i< 10; i++)
75 val1 = i;
76 table->execute();
77 tuple = (char*)table->fetch() ;
78 if (tuple == NULL) {printf("loop break in %d\n", i);table->closeScan();break;}
79 printf("I:tuple value is %d %s \n", id1, id2);
80 table->closeScan();
83 table->setCondition(&p2);
84 for(i = 0; i< 10; i++)
86 sprintf(val2, "Aruna:%d", i);
87 table->execute();
88 tuple = (char*)table->fetch() ;
89 if (tuple == NULL) {printf("loop break in %d\n", i);table->closeScan();break;}
90 printf("II:tuple value is %d %s \n", id1, id2);
91 table->closeScan();
93 conn.commit();
95 rv =conn.startTransaction();
96 if (rv != OK) exit(1);
97 table->setCondition(&p1);
98 val1 = 1;
99 table->execute();
100 tuple = (char*)table->fetch() ;
101 if (tuple != NULL) {
102 table->deleteTuple();
104 table->closeScan();
105 table->setCondition(&p2);
106 sprintf(val2, "Aruna:%d", 2);
107 table->execute();
108 tuple = (char*)table->fetch() ;
109 if (tuple != NULL) {
110 table->deleteTuple();
112 table->closeScan();
113 conn.commit();
115 int count =0;
116 rv = conn.startTransaction();
117 table->setCondition(NULL);
118 if (rv != OK) exit (1);
119 table->execute();
120 while((tuple = (char*)table->fetch())!= NULL) {
121 printf("tuple value is %d %s \n", id1, id2);
122 count++;
124 table->closeScan();
125 conn.commit();
126 printf("Total rows selected %d\n", count);
127 dbMgr->closeTable(table);
128 dbMgr->dropTable("t1");
130 conn.close();
131 return 0;