1
[csql.git] / test / dbapi / Index / twoindex1.c
bloba71e0f8c28f5325171742fe555455e7cf7ff2835
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 rv = dbMgr->createIndex("indx1", idxInfo);
30 if (rv != OK) { printf("Index creation failed\n"); return -1; }
31 printf("Index created for f1\n");
32 printf("size of index field list %d\n", idxInfo->list.size());
33 idxInfo->list.remove("f1");
34 printf("size of index field list %d\n", idxInfo->list.size());
35 idxInfo->list.append("f2");
36 printf("size of index field list %d\n", idxInfo->list.size());
37 idxInfo->indType = hashIndex;
38 #ifdef F2TREE
39 idxInfo->indType = treeIndex;
40 #endif
41 rv = dbMgr->createIndex("indx2", idxInfo);
42 if (rv != OK) { printf("Index creation failed\n"); return -1; }
43 printf("Index created for f2\n");
44 delete idxInfo;
45 Table *table = dbMgr->openTable("t1");
46 if (table == NULL) { printf("Unable to open table\n"); return -1; }
47 int id1 = 0, id2=0;
48 table->bindFld("f1", &id1);
49 table->bindFld("f2", &id2);
50 char *tuple;
51 int ret;
52 int i;
53 int icount =0;
54 rv = conn.startTransaction();
55 if (rv != OK) exit(1);
56 for(i = 0; i< 10; i++)
58 id1= i;
59 id2= i+100;
60 ret = table->insertTuple();
61 if (ret != 0) break;
62 icount++;
64 conn.commit();
65 printf("Total tuples inserted: %d\n", icount);
66 Condition p1, p2;
67 int val1 = 0;
68 p1.setTerm("f1", OpEquals, &val1);
69 p2.setTerm("f2", OpEquals, &val1);
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 %d \n", id1, id2);
80 table->closeScan();
83 table->setCondition(&p2);
84 for(i = 0; i< 10; i++)
86 val1 = i+100;
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 %d \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 printf("deleted record\n");
108 table->setCondition(&p2);
109 val1 = 102;
110 table->execute();
111 tuple = (char*)table->fetch() ;
112 if (tuple != NULL) {
113 table->deleteTuple();
115 table->closeScan();
116 conn.commit();
118 dbMgr->closeTable(table);
119 table = dbMgr->openTable("t1");
120 if (table == NULL) { printf("Unable to open table\n"); return -1; }
121 table->bindFld("f1", &id1);
122 table->bindFld("f2", &id2);
123 int count =0;
124 rv = conn.startTransaction();
125 table->setCondition(NULL);
126 if (rv != OK) exit (1);
127 table->execute();
128 while((tuple = (char*)table->fetch())!= NULL) {
129 printf("tuple value is %d %d \n", id1, id2);
130 count++;
132 table->closeScan();
133 conn.commit();
134 printf("Total rows selected %d\n", count);
135 dbMgr->closeTable(table);
136 dbMgr->dropTable("t1");
138 conn.close();
139 return 0;