table->close is changed to call table->closeScan
[csql.git] / test / dbapi / Index / twoindex1.c
blob964fbe7e08984e60d8a45327222d99034e5a0d18
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 rv = dbMgr->createIndex("indx1", idxInfo);
27 if (rv != OK) { printf("Index creation failed\n"); return -1; }
28 printf("Index created for f1\n");
29 printf("size of index field list %d\n", idxInfo->list.size());
30 idxInfo->list.remove("f1");
31 printf("size of index field list %d\n", idxInfo->list.size());
32 idxInfo->list.append("f2");
33 printf("size of index field list %d\n", idxInfo->list.size());
34 rv = dbMgr->createIndex("indx2", idxInfo);
35 if (rv != OK) { printf("Index creation failed\n"); return -1; }
36 printf("Index created for f2\n");
37 delete idxInfo;
38 Table *table = dbMgr->openTable("t1");
39 if (table == NULL) { printf("Unable to open table\n"); return -1; }
40 int id1 = 0, id2=0;
41 table->bindFld("f1", &id1);
42 table->bindFld("f2", &id2);
43 char *tuple;
44 int ret;
45 int i;
46 int icount =0;
47 rv = conn.startTransaction();
48 if (rv != OK) exit(1);
49 for(i = 0; i< 10; i++)
51 id1= i;
52 id2= i+100;
53 ret = table->insertTuple();
54 if (ret != 0) break;
55 icount++;
57 conn.commit();
58 printf("Total tuples inserted: %d\n", icount);
59 Condition p1, p2;
60 int val1 = 0;
61 p1.setTerm("f1", OpEquals, &val1);
62 p2.setTerm("f2", OpEquals, &val1);
63 table->setCondition(&p1);
64 rv =conn.startTransaction();
65 if (rv != OK) exit(1);
66 for(i = 0; i< 10; i++)
68 val1 = i;
69 table->execute();
70 tuple = (char*)table->fetch() ;
71 if (tuple == NULL) {printf("loop break in %d\n", i);table->closeScan();break;}
72 printf("I:tuple value is %d %d \n", id1, id2);
73 table->closeScan();
76 table->setCondition(&p2);
77 for(i = 0; i< 10; i++)
79 val1 = i+100;
80 table->execute();
81 tuple = (char*)table->fetch() ;
82 if (tuple == NULL) {printf("loop break in %d\n", i);table->closeScan();break;}
83 printf("II:tuple value is %d %d \n", id1, id2);
84 table->closeScan();
86 conn.commit();
88 rv = conn.startTransaction();
89 if (rv != OK) exit (1);
90 table->setCondition(&p1);
91 val1 = 0;
92 table->execute();
93 tuple = (char*)table->fetch() ;
94 if (tuple != NULL) {
95 id2=99;
96 table->updateTuple();
98 table->closeScan();
99 table->setCondition(&p2);
100 val1 = 109;
101 table->execute();
102 tuple = (char*)table->fetch() ;
103 if (tuple != NULL) {
104 id1=99;
105 table->updateTuple();
107 table->closeScan();
108 conn.commit();
111 rv = conn.startTransaction();
112 if (rv != OK) exit (1);
113 table->setCondition(&p1);
114 val1 = 1;
115 table->execute();
116 tuple = (char*)table->fetch() ;
117 if (tuple != NULL) {
118 table->deleteTuple();
120 table->closeScan();
121 table->setCondition(&p2);
122 val1 = 101;
123 table->execute();
124 tuple = (char*)table->fetch() ;
125 if (tuple != NULL) {
126 table->deleteTuple();
128 table->closeScan();
129 conn.commit();
131 int count =0;
132 rv = conn.startTransaction();
133 table->setCondition(NULL);
134 if (rv != OK) exit (1);
135 table->execute();
136 while((tuple = (char*)table->fetch())!= NULL) {
137 printf("tuple value is %d %d \n", id1, id2);
138 count++;
140 table->closeScan();
141 conn.commit();
142 printf("Total rows selected %d\n", count);
143 dbMgr->closeTable(table);
144 dbMgr->dropTable("t1");
146 conn.close();
147 return 0;