Addding test cases for connection and datatype module.
[csql.git] / test / dbapi / DataType / timestamptest.c
blob72a6041d8e66b6180215fb5e37df4efb1a377465
1 #include<CSql.h>
3 TimeStamp id;
4 char name[196] = "PRABAKARAN";
5 int select(Table *table, ComparisionOp op)
7 printf("Operator test for %d\n", op);
8 Condition p1;
9 TimeStamp val1(1981, 12, 24, 12, 30, 30);
10 p1.setTerm("f1", op, &val1);
11 table->setCondition(&p1);
12 table->execute();
13 void *tuple;
14 while ((tuple = (char*) table->fetch())) {
15 printf("tuple value is ");
16 AllDataType::printVal(&id, typeTimeStamp,0);
17 printf(" %s \n", name);
19 table->close();
20 return 0;
23 int main()
26 Connection conn;
27 DbRetVal rv = conn.open("praba", "manager");
28 if (rv != OK)
30 printf("Error during connection %d\n", rv);
31 return 1;
33 DatabaseManager *dbMgr = conn.getDatabaseManager();
34 if (dbMgr == NULL) { printf("Auth failed\n"); return 2;}
35 TableDef tabDef;
36 tabDef.addField("f1", typeTimeStamp, 0, NULL, true, true );
37 tabDef.addField("f2", typeString, 196);
38 rv = dbMgr->createTable("t1", tabDef);
39 if (rv != OK) { printf("Table creation failed\n"); return 3; }
40 printf("Table created\n");
41 #ifdef WITHINDEX
42 HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
43 strcpy(idxInfo->tableName, "t1");
44 idxInfo->list.append("f1");
45 idxInfo->indType = hashIndex;
46 rv = dbMgr->createIndex("indx1", idxInfo);
47 if (rv != OK) { printf("Index creation failed\n"); return -1; }
48 printf("Index created\n");
49 #endif
51 Table *table = dbMgr->openTable("t1");
52 if (table == NULL) { printf("Unable to open table\n"); return 4; }
53 table->bindFld("f1", &id);
54 table->bindFld("f2", name);
55 char *tuple;
56 int ret;
57 int i;
58 rv =conn.startTransaction();
59 if (rv != OK) exit(5);
60 for(i = 1; i< 10; i++) {
61 id.setDate(1981, 12, 21+i );
62 id.setTime(12, 30, 27 +i);
63 strcpy(name, "PRABAKARAN0123456750590");
64 ret = table->insertTuple();
65 if (ret != 0) break;
66 printf("Inserted ");
67 AllDataType::printVal(&id, typeTimeStamp,0);
68 printf(" %s \n", name);
70 conn.commit();
72 conn.startTransaction();
73 select(table, OpEquals);
74 conn.commit();
76 conn.startTransaction();
77 select(table, OpNotEquals);
78 conn.commit();
80 conn.startTransaction();
81 select(table, OpLessThan);
82 conn.commit();
84 conn.startTransaction();
85 select( table, OpLessThanEquals);
86 conn.commit();
88 conn.startTransaction();
89 select( table, OpGreaterThan);
90 conn.commit();
92 conn.startTransaction();
93 select( table, OpGreaterThanEquals);
94 conn.commit();
96 Condition p1;
97 TimeStamp val1;
98 p1.setTerm("f1", OpEquals, &val1);
99 table->setCondition(&p1);
100 rv = conn.startTransaction();
101 if (rv != OK) exit (1);
102 val1.setDate(1981, 12, 24);
103 val1.setTime(12, 30, 30);
104 table->execute();
105 tuple = (char*)table->fetch() ;
106 if (tuple != NULL) {
107 strcpy(name, "My value is updated");
108 table->updateTuple();
110 table->close();
112 conn.commit();
113 rv = conn.startTransaction();
114 if (rv != OK) exit (1);
115 val1.setDate(1981, 12, 28);
116 val1.setTime(12, 30, 34);
118 //val1 = i;
119 table->execute();
120 tuple = (char*)table->fetch() ;
121 if (tuple != NULL) {
122 printf("deleting ");
123 AllDataType::printVal(&id, typeTimeStamp,0);
124 printf(" %s \n", name);
125 table->deleteTuple();
127 table->close();
128 conn.commit();
130 table->setCondition(NULL);
131 rv = conn.startTransaction();
132 table->execute();
133 while ((tuple = (char*) table->fetch())) {
134 printf("after delete ");
135 AllDataType::printVal(&id, typeTimeStamp,0);
136 printf(" %s \n", name);
138 table->close();
139 conn.commit();
141 dbMgr->closeTable(table);
142 dbMgr->dropTable("t1");
144 conn.close();
145 return 0;