*** empty log message ***
[csql.git] / test / dbapi / Index / twoindex2.c
blobf0a0d7a60cb6bc018563a98ad64c345cb5c0988d
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 #ifdef F2TRIE
28 idxInfo->indType = trieIndex;
29 #endif
30 rv = dbMgr->createIndex("indx1", idxInfo);
31 if (rv != OK) { printf("Index creation failed\n"); return -1; }
32 printf("Index created for f1\n");
33 printf("size of index field list %d\n", idxInfo->list.size());
34 idxInfo->list.remove("f1");
35 printf("size of index field list %d\n", idxInfo->list.size());
36 idxInfo->list.append("f2");
37 printf("size of index field list %d\n", idxInfo->list.size());
38 idxInfo->indType = hashIndex;
39 #ifdef F2TREE
40 idxInfo->indType = treeIndex;
41 #endif
42 #ifdef F2TRIE
43 idxInfo->indType = trieIndex;
44 #endif
45 rv = dbMgr->createIndex("indx2", idxInfo);
46 if (rv != OK) { printf("Index creation failed\n"); return -1; }
47 printf("Index created for f2\n");
48 delete idxInfo;
49 Table *table = dbMgr->openTable("t1");
50 if (table == NULL) { printf("Unable to open table\n"); return -1; }
51 int id1 = 0;
52 char id2[50] ="Aruna";
53 table->bindFld("f1", &id1);
54 table->bindFld("f2", id2);
55 char *tuple;
56 int ret;
57 int i;
58 int icount =0;
59 rv = conn.startTransaction();
60 if (rv != OK) exit(1);
61 for(i = 0; i< 10; i++)
63 id1= i;
64 sprintf(id2, "Aruna:%d", i);
65 ret = table->insertTuple();
66 if (ret != 0) break;
67 icount++;
69 conn.commit();
70 printf("Total tuples inserted: %d\n", icount);
71 Condition p1, p2;
72 int val1 = 0;
73 char val2[50];
74 p1.setTerm("f1", OpEquals, &val1);
75 p2.setTerm("f2", OpEquals, &val2);
76 table->setCondition(&p1);
77 rv =conn.startTransaction();
78 if (rv != OK) exit(1);
79 for(i = 0; i< 10; i++)
81 val1 = i;
82 table->execute();
83 tuple = (char*)table->fetch() ;
84 if (tuple == NULL) {printf("loop break in %d\n", i);table->closeScan();break;}
85 printf("I:tuple value is %d %s \n", id1, id2);
86 table->closeScan();
89 table->setCondition(&p2);
90 for(i = 0; i< 10; i++)
92 sprintf(val2, "Aruna:%d", i);
93 table->execute();
94 tuple = (char*)table->fetch() ;
95 if (tuple == NULL) {printf("loop break in %d\n", i);table->closeScan();break;}
96 printf("II:tuple value is %d %s \n", id1, id2);
97 table->closeScan();
99 conn.commit();
101 rv =conn.startTransaction();
102 if (rv != OK) exit(1);
103 table->setCondition(&p1);
104 val1 = 1;
105 table->execute();
106 tuple = (char*)table->fetch() ;
107 if (tuple != NULL) {
108 table->deleteTuple();
110 table->closeScan();
111 table->setCondition(&p2);
112 sprintf(val2, "Aruna:%d", 2);
113 table->execute();
114 tuple = (char*)table->fetch() ;
115 if (tuple != NULL) {
116 table->deleteTuple();
118 table->closeScan();
119 conn.commit();
121 int count =0;
122 rv = conn.startTransaction();
123 table->setCondition(NULL);
124 if (rv != OK) exit (1);
125 table->execute();
126 while((tuple = (char*)table->fetch())!= NULL) {
127 printf("tuple value is %d %s \n", id1, id2);
128 count++;
130 table->closeScan();
131 conn.commit();
132 printf("Total rows selected %d\n", count);
133 dbMgr->closeTable(table);
134 dbMgr->dropTable("t1");
136 conn.close();
137 return 0;