Removing large file for data load. Modified README to generate mysql and psql input...
[csql.git] / test / sqlapi / Parameter / paratest4.c
blobd73747575ff2db86041147e37d3a2bbd4d9d23b8
1 /* create table T1 with 10 fields.
2 * insert some records into it.
3 * update T1 WITH ALL 8 PARAMETERS.
4 noOfParamFields() should return 8.
5 * select * from T1;
7 * Author : Jitendra Lenka.
8 */
10 #include<AbsSqlStatement.h>
11 #include<SqlFactory.h>
12 #include<CSql.h>
13 #include<Info.h>
14 int main()
16 DbRetVal rv = OK;
17 AbsSqlConnection *con = SqlFactory :: createConnection(CSql);
18 rv = con->connect("root","manager");
19 if(rv!=OK)return 1;
21 AbsSqlStatement *stmt = SqlFactory :: createStatement(CSql);
22 stmt->setConnection(con);
23 char statement[200];
24 strcpy(statement,"CREATE TABLE T1(F1 INT,F2 SMALLINT,F3 CHAR(30),F4 FLOAT,F5 FLOAT,F6 DATE,F7 TIME,F8 TIMESTAMP,F9 INT,F10 BIGINT, F11 BINARY(4));");
26 int rows = 0;
27 rv = stmt->prepare(statement);
28 if(rv!=OK)
30 delete stmt;
31 delete con;
32 return 1;
35 rv = stmt->execute(rows);
36 if(rv!=OK)
38 delete stmt;
39 delete con;
40 return 1;
43 stmt->free();
44 printf("Table created\n");
46 // insert into table
48 strcpy(statement,"INSERT INTO T1 VALUES(?,?,?,?,?,?,?,?,?,?,?);");
50 int f1var = 0;
51 short int f2var = 10;
52 char f3var[20]="jitendra";
53 float f4var = 5.5;
54 float f5var = 10.50;
55 Date f6var;
56 f6var.set(2008,01,21);
58 Time f7var;
59 f7var.set(12,29,30);
61 TimeStamp f8var;
62 f8var.setDate(2008,01,21);
63 f8var.setTime(12,29,30);
65 int f9var = 20;
66 long long f10var = 12000;
67 char f11var[8]="23fe";
68 rv = stmt->prepare(statement);
69 if(rv!=OK)
71 delete stmt;
72 delete con;
73 return 1;
76 int count=0;
78 for(int i=0;i<10;i++)
80 rv = con->beginTrans();
81 if(rv!=OK) break;
82 f1var=i;
83 f2var++;
84 stmt->setIntParam(1,f1var);
85 stmt->setShortParam(2,f2var);
86 stmt->setStringParam(3,f3var);
87 stmt->setFloatParam(4,f4var);
88 stmt->setFloatParam(5,f5var);
89 stmt->setDateParam(6,f6var);
90 stmt->setTimeParam(7,f7var);
91 stmt->setTimeStampParam(8,f8var);
92 stmt->setIntParam(9,f9var);
93 stmt->setLongLongParam(10,f10var);
94 stmt->setBinaryParam(11,f11var);
95 rv = stmt->execute(rows);
96 if(rv!=OK)break;
97 rv = con->commit();
98 if(rv!=OK)break;
99 count++;
101 printf("%d Rows inserted \n",count);
104 stmt->free();
106 //**********************************************************
107 // UPDATE TABLE
109 strcpy(statement,"UPDATE T1 SET F10=?,F8=?,F6=?,F5=?,F4=?,F3=?,F11=? WHERE F2=?;");
110 rv = stmt->prepare(statement);
112 int nop;
113 nop = stmt->noOfParamFields();
114 printf("noOfParamFields return=%d\n",nop);
115 if(nop!=8)return 1;
117 long long f10var1=15000;
118 TimeStamp f8var1;
119 f8var1.setDate(2007,3,30);
120 f8var1.setTime(10,25,55);
122 Date f6var1;
123 f6var1.set(2007,3,30);
125 float f5var1=20.25;
126 float f4var1=11.22;
127 char f3var1[10]="lakshya";
128 char f11var1[8]="ab12";
129 short int f2var1=10;
130 count=0;
132 for(int i=0;i<5;i++)
135 rv = con->beginTrans();
136 if(rv!=OK)break;
138 f2var1++;
139 stmt->setShortParam(8,f2var1);
140 stmt->setLongLongParam(1,f10var1);
141 stmt->setTimeStampParam(2,f8var1);
142 stmt->setDateParam(3,f6var1);
143 stmt->setFloatParam(4,f5var1);
144 stmt->setFloatParam(5,f4var1);
145 stmt->setStringParam(6,f3var1);
146 stmt->setBinaryParam(7,f11var1);
148 //stmt->setShortParam(7,f2var1);
150 rv = stmt->execute(rows);
151 if(rv!=OK)break;
152 rv = con->commit();
153 if(rv!=OK)break;
154 count++;
157 stmt->free();
159 printf("%d Rows updated\n",count);
161 //**********************************************************
162 // SELECT * FROM T1;
164 strcpy(statement,"SELECT * FROM T1;");
165 rv = stmt->prepare(statement);
166 if(rv!=OK)
168 delete stmt;
169 delete con;
170 return 5;
173 stmt->bindField(1,&f1var);
174 stmt->bindField(2,&f2var);
175 stmt->bindField(3,f3var);
176 stmt->bindField(4,&f4var);
177 stmt->bindField(5,&f5var);
178 stmt->bindField(6,&f6var);
179 stmt->bindField(7,&f7var);
180 stmt->bindField(8,&f8var);
181 stmt->bindField(9,&f9var);
182 stmt->bindField(10,&f10var);
183 stmt->bindField(11,f11var);
184 count=0;
185 rv = con->beginTrans();
186 if(rv!=OK)return 6;
187 stmt->execute(rows);
188 while(stmt->fetch() !=NULL)
190 if(f1var==5)printf("\n");
191 printf("F1=%d | F2=%hd | F3=%s | F4=%f | F5=%f | DATE=%d-%d-%d | TIME=%d:%d:%d | TIMESTAMP=%d-%d-%d %d:%d:%d | F9=%d | F10=%lld",f1var,f2var,f3var,f4var,f5var,f6var.year(),f6var.month(),f6var.dayOfMonth(),f7var.hours(),f7var.minutes(),f7var.seconds(),f8var.year(),f8var.month(),f8var.dayOfMonth(),f8var.hours(),f8var.minutes(),f8var.seconds(),f9var,f10var);
192 printf(" | ");
193 AllDataType::printVal(f11var,typeBinary,4);
194 printf("\n");
197 count++;
200 stmt->close();
201 rv = con->commit();
202 if(rv !=OK)
204 delete stmt;
205 delete con;
206 return 7;
209 printf("%d Rows fetched=%d\n",count);
210 stmt->free();
213 strcpy(statement,"DROP TABLE T1;");
214 rv = stmt->prepare(statement);
215 if(rv!=OK)
217 delete stmt;
218 delete con;
219 return 8;
221 rv = stmt->execute(rows);
222 if(rv!=OK)
224 delete stmt;
225 delete con;
226 return 9;
228 stmt->free();
229 printf("Table dropped\n");
231 delete stmt;
232 delete con;
233 return 0;