1
[csql.git] / test / dbapi / Connection / conntest11.c
blobc423b2c69c3cd4936440e99e662b6bf95ded1b1a
1 //Update a tuple in a transation and commit it.
2 // Next scan should display the update tuple.
4 #include<CSql.h>
5 #include<NanoTimer.h>
6 int main()
9 Connection conn;
10 DbRetVal rv = conn.open("root", "manager");
11 if (rv != OK)
13 printf("Error during connection %d\n", rv);
14 return 1;
18 DatabaseManager *dbMgr = conn.getDatabaseManager();
19 if (dbMgr == NULL) { printf("Auth failed\n"); return 2;}
22 TableDef tabDef;
23 tabDef.addField("f1", typeInt);
24 tabDef.addField("f2", typeString, 196);
25 rv = dbMgr->createTable("t1", tabDef);
26 if (rv != OK) { printf("Table creation failed\n"); return 3; }
27 printf("Table created\n");
29 Table *table = dbMgr->openTable("t1");
30 if (table == NULL) { printf("Unable to open table\n"); return 3; }
31 int id;
32 char name[200]="PRABA";
33 table->bindFld("f1", &id);
34 table->bindFld("f2", name);
35 int icount = 0;
36 int i;
38 conn.startTransaction();
39 for (i=0;i<10;i++)
41 id=i;
42 rv=table->insertTuple();
43 if(rv!=OK)
45 printf("Insertion failed at %d th tuple",i);
46 dbMgr->closeTable(table);
47 dbMgr->dropTable("t1");
48 conn.close();
49 return 4;
52 conn.commit();
54 conn.startTransaction();
55 table->setCondition(NULL);
56 rv = table->execute();
57 if (rv != OK)
59 dbMgr->closeTable(table);
60 dbMgr->dropTable("t1");
61 conn.close();
62 return 6;
64 printf("Scan before updation\n");
65 printf("********************\n");
66 void *fld2ptr, *tuple;
67 while(true)
69 tuple = (char*)table->fetch() ;
70 if (tuple == NULL) {break;}
71 printf("Binded Tuple value is %d %s \n", id, name);
74 conn.commit();
75 rv=table->closeScan();
76 if(rv!=OK){ printf("Error in closing the table\n"); return 3;}
78 Condition p1;
79 int val1 = 5;
80 p1.setTerm("f1",OpEquals, &val1);
81 table->setCondition(&p1);
82 conn.startTransaction();
83 rv=table->execute();
84 if (rv != OK)
86 dbMgr->closeTable(table);
87 dbMgr->dropTable("t1");
88 conn.close();
89 return 4;
92 // char *tuple;
93 //printf("Scan After:\n");
94 while(true)
96 tuple = (char*)table->fetch() ;
97 if (tuple == NULL) { break; }
98 strcpy(name,"Kanchana");
99 table->updateTuple();
100 printf(" f1=%d f2=%s\n", id, name);
101 // icount++;
103 conn.commit();
104 table->closeScan();
107 conn.startTransaction();
109 table->setCondition(NULL);
110 rv = table->execute();
111 if (rv != OK)
113 dbMgr->closeTable(table);
114 dbMgr->dropTable("t1");
115 conn.close();
116 return 6;
118 printf("Scan After updation\n");
119 printf("********************\n");
120 while(true)
122 tuple = (char*)table->fetch() ;
123 if (tuple == NULL) {break;}
124 printf("Binded Tuple value is %d %s \n", id, name);
127 conn.commit();
129 //unset the condtion set as we need to delete all rows
130 table->setCondition(NULL);
131 table->closeScan();
132 dbMgr->closeTable(table);
133 dbMgr->dropTable("t1");
134 conn.close();
135 // printf("Test script passed ");
136 return 0;