*** empty log message ***
[csql.git] / test / adapter / Select / selecttest3.c
blob478891cc03d9d25a4d19edb49d5a05889e8127fb
1 /* create table t1 with two fields,
2 * insert 10 rows into the table ,
3 * select from nonexisting table.
4 */
6 #include "common.h"
7 int main()
9 DbRetVal rv = OK;
10 AbsSqlConnection *con = createConnection();
11 rv = con->connect("root","manager");
12 if(rv!=OK)return 1;
14 AbsSqlStatement *stmt = createStatement();
15 stmt->setConnection(con);
16 char statement[200];
18 strcpy(statement,"CREATE TABLE t1(f1 INT,f2 CHAR(20));");
19 if(strcmp(getenv("DSN"),"db2")==0)
20 strcpy(statement,"CREATE TABLE t1(f1 INT,f2 CHAR(20))");
22 int rows=0;
23 rv = stmt->prepare(statement);
24 if(rv!=OK)
26 delete stmt;
27 delete con;
28 return 1;
31 rv = stmt->execute(rows);
32 if(rv!=OK)
34 delete stmt;
35 delete con;
36 return 2;
38 stmt->free();
39 printf("Table created\n");
40 con->commit();
41 // insert records
43 strcpy(statement,"INSERT INTO t1 VALUES(?,?);");
44 if(strcmp(getenv("DSN"),"db2")==0)
45 strcpy(statement,"INSERT INTO t1 VALUES(?,?)");
47 int f1var = 1;
48 char f2var[20] = "lakshya";
51 rv = stmt->prepare(statement);
52 if(rv!=OK)
54 delete stmt;
55 delete con;
56 return 3;
59 int count=0;
61 for(int i=0;i<2;i++)
64 for(int j=0;j<5;j++)
66 f1var = j;
67 rv = con->beginTrans();
68 if(rv!=OK)break;
71 stmt->setIntParam(1,f1var);
72 stmt->setStringParam(2,f2var);
74 rv = stmt->execute(rows);
75 if(rv!=OK)break;
76 rv = con->commit();
77 if(rv!=OK)break;
78 count++;
80 printf("%d rows inserted\n",count);
82 stmt->free();
84 //*******************************************
86 strcpy(statement,"SELECT * FROM t11;");
87 if(strcmp(getenv("DSN"),"db2")==0)
88 strcpy(statement,"SELECT * FROM t11");
89 rv = stmt->prepare(statement);
91 if(rv !=OK)
93 printf("test script passed\n");
94 strcpy(statement,"DROP TABLE t1;");
95 if(strcmp(getenv("DSN"),"db2")==0)
96 strcpy(statement,"DROP TABLE t1");
97 rv = stmt->prepare(statement);
98 rv = stmt->execute(rows);
99 if(rv==OK){printf("Table dropped\n");}
100 stmt->free();
101 con->commit();
102 delete stmt;
103 delete con;
104 return 0;
107 else if(rv==OK)
109 if(strcmp(getenv("DSN"),"oracle")==0 || strcmp(getenv("DSN"),"psql")==0 || strcmp(getenv("DSN"),"sybase")==0)
111 printf("Test script Passed\n");
112 strcpy(statement,"DROP TABLE t1;");
113 if(strcmp(getenv("DSN"),"db2")==0)
114 strcpy(statement,"DROP TABLE t1");
115 rv = stmt->prepare(statement);
116 rv = stmt->execute(rows);
117 if(rv==OK){printf("Table dropped\n");}
118 stmt->free();
119 con->commit();
120 delete stmt;
121 delete con;
122 return 0;
124 else
126 printf("Test script failed\n");
127 strcpy(statement,"DROP TABLE t1;");
128 if(strcmp(getenv("DSN"),"db2")==0)
129 strcpy(statement,"DROP TABLE t1");
130 rv = stmt->prepare(statement);
131 rv = stmt->execute(rows);
132 if(rv==OK){printf("Table dropped\n");}
133 stmt->free();
134 con->commit();
135 delete stmt;
136 delete con;
137 return 5;
140 else
142 stmt->bindField(1,&f1var);
143 stmt->bindField(2,f2var);
146 count = 0;
147 rv = con->beginTrans();
148 if(rv!=OK)return 6;
149 stmt->execute(rows);
150 while(stmt->fetch()!=NULL)
152 count++;
155 stmt->close();
156 rv = con->commit();
157 if(rv!=OK)
159 delete stmt;
160 delete con;
161 return 7;
164 printf("Total row fetched=%d\n",count);
167 stmt->free();
168 con->commit();
170 delete stmt;
171 delete con;
172 return 0;