typo
[csql.git] / test / dbapi / Index / twoindex2.c
blobf5ce049dfddff59076f5c538b85864cae933110a
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 rv = dbMgr->createIndex("indx1", idxInfo);
25 if (rv != OK) { printf("Index creation failed\n"); return -1; }
26 printf("Index created for f1\n");
27 printf("size of index field list %d\n", idxInfo->list.size());
28 idxInfo->list.remove("f1");
29 printf("size of index field list %d\n", idxInfo->list.size());
30 idxInfo->list.append("f2");
31 printf("size of index field list %d\n", idxInfo->list.size());
32 rv = dbMgr->createIndex("indx2", idxInfo);
33 if (rv != OK) { printf("Index creation failed\n"); return -1; }
34 printf("Index created for f2\n");
35 delete idxInfo;
36 Table *table = dbMgr->openTable("t1");
37 if (table == NULL) { printf("Unable to open table\n"); return -1; }
38 int id1 = 0;
39 char id2[50] ="Aruna";
40 table->bindFld("f1", &id1);
41 table->bindFld("f2", id2);
42 char *tuple;
43 int ret;
44 int i;
45 int icount =0;
46 rv = conn.startTransaction();
47 if (rv != OK) exit(1);
48 for(i = 0; i< 10; i++)
50 id1= i;
51 sprintf(id2, "Aruna:%d", i);
52 ret = table->insertTuple();
53 if (ret != 0) break;
54 icount++;
56 conn.commit();
57 printf("Total tuples inserted: %d\n", icount);
58 Condition p1, p2;
59 int val1 = 0;
60 char val2[50];
61 p1.setTerm("f1", OpEquals, &val1);
62 p2.setTerm("f2", OpEquals, &val2);
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->close();break;}
72 printf("I:tuple value is %d %s \n", id1, id2);
73 table->close();
76 table->setCondition(&p2);
77 for(i = 0; i< 10; i++)
79 sprintf(val2, "Aruna:%d", i);
80 table->execute();
81 tuple = (char*)table->fetch() ;
82 if (tuple == NULL) {printf("loop break in %d\n", i);table->close();break;}
83 printf("II:tuple value is %d %s \n", id1, id2);
84 table->close();
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 sprintf(id2,"Aruna:%d", 99);
96 table->updateTuple();
98 table->close();
99 table->setCondition(&p2);
100 sprintf(val2, "Aruna:%d", 10);
101 table->execute();
102 tuple = (char*)table->fetch() ;
103 if (tuple != NULL) {
104 id1=99;
105 table->updateTuple();
107 table->close();
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->close();
121 table->setCondition(&p2);
122 sprintf(val2, "Aruna:%d", 101);
123 table->execute();
124 tuple = (char*)table->fetch() ;
125 if (tuple != NULL) {
126 table->deleteTuple();
128 table->close();
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 %s \n", id1, id2);
138 count++;
140 table->close();
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;