Addding test cases for connection and datatype module.
[csql.git] / test / dbapi / Index / twoindex1.c
blob2323e1f772f2235d0e2204176ac0a2bb15616b96
1 #include<CSql.h>
2 int main()
5 Connection conn;
6 DbRetVal rv = conn.open("praba", "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, true );
16 tabDef.addField("f2", typeInt);
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");
36 Table *table = dbMgr->openTable("t1");
37 if (table == NULL) { printf("Unable to open table\n"); return -1; }
38 int id1 = 0, id2=0;
39 table->bindFld("f1", &id1);
40 table->bindFld("f2", &id2);
41 char *tuple;
42 int ret;
43 int i;
44 int icount =0;
45 rv = conn.startTransaction();
46 if (rv != OK) exit(1);
47 for(i = 0; i< 10; i++)
49 id1= i;
50 id2= i+100;
51 ret = table->insertTuple();
52 if (ret != 0) break;
53 icount++;
55 conn.commit();
56 printf("Total tuples inserted: %d\n", icount);
57 Condition p1, p2;
58 int val1 = 0;
59 p1.setTerm("f1", OpEquals, &val1);
60 p2.setTerm("f2", OpEquals, &val1);
61 table->setCondition(&p1);
62 rv =conn.startTransaction();
63 if (rv != OK) exit(1);
64 for(i = 0; i< 10; i++)
66 val1 = i;
67 table->execute();
68 tuple = (char*)table->fetch() ;
69 if (tuple == NULL) {printf("loop break in %d\n", i);table->close();break;}
70 printf("I:tuple value is %d %d \n", id1, id2);
71 table->close();
74 table->setCondition(&p2);
75 for(i = 0; i< 10; i++)
77 val1 = i+100;
78 table->execute();
79 tuple = (char*)table->fetch() ;
80 if (tuple == NULL) {printf("loop break in %d\n", i);table->close();break;}
81 printf("II:tuple value is %d %d \n", id1, id2);
82 table->close();
84 conn.commit();
86 rv = conn.startTransaction();
87 if (rv != OK) exit (1);
88 table->setCondition(&p1);
89 val1 = 0;
90 table->execute();
91 tuple = (char*)table->fetch() ;
92 if (tuple != NULL) {
93 id2=99;
94 table->updateTuple();
96 table->close();
97 table->setCondition(&p2);
98 val1 = 109;
99 table->execute();
100 tuple = (char*)table->fetch() ;
101 if (tuple != NULL) {
102 id1=99;
103 table->updateTuple();
105 table->close();
106 conn.commit();
109 rv = conn.startTransaction();
110 if (rv != OK) exit (1);
111 table->setCondition(&p1);
112 val1 = 1;
113 table->execute();
114 tuple = (char*)table->fetch() ;
115 if (tuple != NULL) {
116 table->deleteTuple();
118 table->close();
119 table->setCondition(&p2);
120 val1 = 101;
121 table->execute();
122 tuple = (char*)table->fetch() ;
123 if (tuple != NULL) {
124 table->deleteTuple();
126 table->close();
127 conn.commit();
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->close();
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;