1
[csql.git] / test / dbapi / Table / insertnulltest8.c
blob50efac4472575849401d7cae5018cfb3e2d370d5
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->closeScan();
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;
89 while(true)
91 tuple = (char*)table->fetch();
92 if (tuple == NULL) { break; }
93 rv = table->markFldNull("f1");
94 if (rv == ErrNullViolation) {
95 printf("NULL Violation\n");
96 continue;
98 rv=table->updateTuple();
99 if(rv==OK)
101 table->clearFldNull("f1");
102 break;
104 table->clearFldNull("f1");
106 conn.commit();
107 table->closeScan();
109 conn.startTransaction();
110 table->setCondition(NULL);
111 rv = table->execute();
112 if (rv != OK)
114 dbMgr->closeTable(table);
115 dbMgr->dropTable("t1");
116 conn.close();
117 return 4;
119 printf("Scan after updation\n");
120 printf("********************\n");
121 printf("f1 | f2\t| f3\n");
122 printf("--------------------------\n");
124 while(true)
126 tuple = (char*)table->fetch() ;
127 if (tuple == NULL) {break;}
128 if (table->isFldNull(1))
130 printf("Column 1 is null\n");
131 dbMgr->closeTable(table);
132 dbMgr->dropTable("t1");
133 conn.close();
134 return -1;
136 if (table->isFldNull(2)) printf("Column 2 is null\n");
137 if (table->isFldNull(3)) printf("Column 3 is null\n");
138 printf("%d | %d\t| %d\n", id1, id2, id3);
140 table->closeScan();
141 dbMgr->closeTable(table);
142 dbMgr->dropTable("t1");
143 conn.close();
144 return 0;