modified to accept hostname and connect to the network
[csql.git] / test / dbapi / Table / insertnulltest8.c
blob12bfe4f7b372589a4a900153faecf555cfb52239
1 #include<CSql.h>
2 int main()
4 Connection conn;
5 DbRetVal rv = conn.open("root", "manager");
6 if (rv != OK) return 1;
7 DatabaseManager *dbMgr = conn.getDatabaseManager();
8 if (dbMgr == NULL) { printf("Auth failed\n"); return 2;}
10 TableDef tabDef;
11 tabDef.addField("f1", typeInt, 0, NULL, true);//NOT NULL
12 tabDef.addField("f2", typeInt, 0, NULL, false);
13 tabDef.addField("f3", typeInt, 0, NULL, false);
14 rv = dbMgr->createTable("t1", tabDef);
15 if (rv != OK) { printf("Table creation failed\n"); conn.close(); return 3; }
16 printf("Table created\n");
18 Table *table = dbMgr->openTable("t1");
19 if (table == NULL)
21 printf("Unable to open table\n");
22 dbMgr->dropTable("t1");
23 conn.close();
24 return -1;
26 int id1=0, id2 = 5, id3=10;
27 table->bindFld("f1", &id1);
28 table->bindFld("f2", &id2);
29 table->bindFld("f3", &id3);
30 int icount=0;
31 for(int i=0;i<6;i++)
33 conn.startTransaction();
34 id1=i;
35 if(i%2==0)
37 if(i!=0)
38 table->markFldNull(2);
40 else table->markFldNull(3);
41 rv = table->insertTuple();
42 if(rv!=OK) break;
43 if(i%2==0) table->clearFldNull(2);
44 else table->clearFldNull(3);
45 icount++;
46 conn.commit();
48 printf("Tuples inserted in 1/txn is %d\n", icount);
49 table->setCondition(NULL);
50 rv = table->execute();
51 if (rv != OK)
53 dbMgr->closeTable(table);
54 dbMgr->dropTable("t1");
55 conn.close();
57 void *tuple = NULL;
58 while(true)
60 tuple = (char*)table->fetch() ;
61 if (tuple == NULL) {break;}
62 if (table->isFldNull(1))
64 printf("Column 1 is null\n");
65 dbMgr->closeTable(table);
66 dbMgr->dropTable("t1");
67 conn.close();
68 return -1;
70 if (table->isFldNull(2)) printf("Column 2 is null\n");
71 if (table->isFldNull(3)) printf("Column 3 is null\n");
72 printf("Binded Tuple value is %d %d %d\n", id1, id2, id3);
74 rv=table->close();
76 Condition p1;
77 int val1=4;
78 p1.setTerm("f1", OpLessThan, &val1);
79 table->setCondition(&p1);
80 conn.startTransaction();
81 rv=table->execute();
82 if (rv != OK)
84 dbMgr->closeTable(table);
85 dbMgr->dropTable("t1");
86 conn.close();
87 return 5;
90 while(true)
92 tuple = (char*)table->fetch();
94 if (tuple == NULL) { break; }
95 table->markFldNull("f1");
96 rv=table->updateTuple();
97 table->clearFldNull("f1");
99 conn.commit();
100 table->close();
102 conn.startTransaction();
103 table->setCondition(NULL);
104 rv = table->execute();
105 if (rv != OK)
107 dbMgr->closeTable(table);
108 dbMgr->dropTable("t1");
109 conn.close();
110 return 4;
112 printf("Scan after updation\n");
113 printf("********************\n");
114 printf("f1 | f2\t| f3\n");
115 printf("--------------------------\n");
117 while(true)
119 tuple = (char*)table->fetch() ;
120 if (tuple == NULL) {break;}
121 if (table->isFldNull(1))
123 printf("Column 1 is null\n");
124 dbMgr->closeTable(table);
125 dbMgr->dropTable("t1");
126 conn.close();
127 return -1;
129 if (table->isFldNull(2)) printf("Column 2 is null\n");
130 if (table->isFldNull(3)) printf("Column 3 is null\n");
131 printf("%d | %d\t| %d\n", id1, id2, id3);
133 table->close();
134 dbMgr->closeTable(table);
135 dbMgr->dropTable("t1");
136 conn.close();
137 return 0;