test cases for trie index
[csql.git] / test / dbapi / Index / twoindex1.c
blob188abe556f13264bf111b7e3c829d8ec00e09ccb
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", typeInt, 0, NULL, true);
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->isUnique = true;
24 idxInfo->isPrimary = true;
25 idxInfo->indType = hashIndex;
26 #ifdef F1TREE
27 idxInfo->indType = treeIndex;
28 #endif
29 #ifdef F1TRIE
30 idxInfo->indType = trieIndex;
31 #endif
32 rv = dbMgr->createIndex("indx1", idxInfo);
33 if (rv != OK) { printf("Index creation failed\n"); return -1; }
34 printf("Index created for f1\n");
35 printf("size of index field list %d\n", idxInfo->list.size());
36 idxInfo->list.remove("f1");
37 printf("size of index field list %d\n", idxInfo->list.size());
38 idxInfo->list.append("f2");
39 printf("size of index field list %d\n", idxInfo->list.size());
40 idxInfo->indType = hashIndex;
41 #ifdef F2TREE
42 idxInfo->indType = treeIndex;
43 #endif
44 #ifdef F2TRIE
45 idxInfo->indType = trieIndex;
46 #endif
47 rv = dbMgr->createIndex("indx2", idxInfo);
48 if (rv != OK) { printf("Index creation failed\n"); return -1; }
49 printf("Index created for f2\n");
50 delete idxInfo;
51 Table *table = dbMgr->openTable("t1");
52 if (table == NULL) { printf("Unable to open table\n"); return -1; }
53 int id1 = 0, id2=0;
54 table->bindFld("f1", &id1);
55 table->bindFld("f2", &id2);
56 char *tuple;
57 int ret;
58 int i;
59 int icount =0;
60 rv = conn.startTransaction();
61 if (rv != OK) exit(1);
62 for(i = 0; i< 10; i++)
64 id1= i;
65 id2= i+100;
66 ret = table->insertTuple();
67 if (ret != 0) break;
68 icount++;
70 conn.commit();
71 printf("Total tuples inserted: %d\n", icount);
72 Condition p1, p2;
73 int val1 = 0;
74 p1.setTerm("f1", OpEquals, &val1);
75 p2.setTerm("f2", OpEquals, &val1);
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 %d \n", id1, id2);
86 table->closeScan();
89 table->setCondition(&p2);
90 for(i = 0; i< 10; i++)
92 val1 = i+100;
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 %d \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 printf("deleted record\n");
114 table->setCondition(&p2);
115 val1 = 102;
116 table->execute();
117 tuple = (char*)table->fetch() ;
118 if (tuple != NULL) {
119 table->deleteTuple();
121 table->closeScan();
122 conn.commit();
124 dbMgr->closeTable(table);
125 table = dbMgr->openTable("t1");
126 if (table == NULL) { printf("Unable to open table\n"); return -1; }
127 table->bindFld("f1", &id1);
128 table->bindFld("f2", &id2);
129 int count =0;
130 rv = conn.startTransaction();
131 table->setCondition(NULL);
132 if (rv != OK) exit (1);
133 table->execute();
134 while((tuple = (char*)table->fetch())!= NULL) {
135 printf("tuple value is %d %d \n", id1, id2);
136 count++;
138 table->closeScan();
139 conn.commit();
140 printf("Total rows selected %d\n", count);
141 dbMgr->closeTable(table);
142 dbMgr->dropTable("t1");
144 conn.close();
145 return 0;