modified to accept hostname and connect to the network
[csql.git] / test / sqlnetwork / Parameter / paratest3.c
blob4a5fd1735b1e4914206a03525558491dbab47d93
1 /* create table T1 with 10 fields.
2 * insert some records into it.
3 * delete records from T1 with where clause
4 * select * from T1;
6 * Author : Jitendra Lenka.
7 */
9 #include<SqlNwConnection.h>
10 #include<SqlNwStatement.h>
11 #include<SqlFactory.h>
13 int main()
15 DbRetVal rv = OK;
16 AbsSqlConnection *con = new SqlNwConnection();
17 con->setInnerConnection(NULL);
18 SqlNwConnection *conn = (SqlNwConnection *)con;
19 conn->setHost("localhost", 5678);
20 rv = con->connect("root","manager");
21 if(rv!=OK)return 1;
23 AbsSqlStatement *stmt = new SqlNwStatement();
24 stmt->setInnerStatement(NULL);
25 stmt->setConnection(con);
26 char statement[200];
27 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));");
29 int rows = 0;
30 rv = stmt->prepare(statement);
31 if(rv!=OK) { delete stmt; delete con; return 2; }
33 rv = stmt->execute(rows);
34 if(rv!=OK) { delete stmt; delete con; return 3; }
35 printf("Table T1 created\n");
37 // insert into table
39 strcpy(statement,"INSERT INTO T1 VALUES(?,?,?,?,?,?,?,?,?,?,?);");
41 int f1var = 0;
42 short int f2var = 1;
43 char f3var[31]="jitendra";
44 float f4var = 5.5;
45 float f5var = 10.50;
46 Date f6var;
47 f6var.set(2007,01,21);
49 Time f7var;
50 f7var.set(12,29,30);
52 TimeStamp f8var;
53 f8var.setDate(2007,01,21);
54 f8var.setTime(12,29,30);
56 int f9var = 20;
57 long long f10var = 12000;
58 char f11var[8]="23fe";
59 rv = stmt->prepare(statement);
60 if(rv!=OK) { delete stmt; delete con; return 4; }
62 int count=0;
64 for(int i=0;i<10;i++)
66 rv = con->beginTrans();
67 if(rv!=OK) break;
68 f1var=i;
69 stmt->setIntParam(1,f1var);
70 stmt->setShortParam(2,f2var);
71 stmt->setStringParam(3,f3var);
72 stmt->setFloatParam(4,f4var);
73 stmt->setFloatParam(5,f5var);
74 stmt->setDateParam(6,f6var);
75 stmt->setTimeParam(7,f7var);
76 stmt->setTimeStampParam(8,f8var);
77 stmt->setIntParam(9,f9var);
78 stmt->setLongLongParam(10,f10var);
79 stmt->setBinaryParam(11,f11var);
80 rv = stmt->execute(rows);
81 if(rv!=OK)break;
82 rv = con->commit();
83 if(rv!=OK)break;
84 count++;
86 printf("Total rows inserted %d\n",count);
89 //*********************************************************
90 // delete records from the T1
91 strcpy(statement,"DELETE FROM T1 WHERE F10=? AND F9=? AND F8=? AND F7=? AND F6=? AND F5=? AND F4=? AND F3=? AND F2=? AND F1=? AND F11=? ;");
93 rv = stmt->prepare(statement);
94 if(rv!=OK){delete stmt;delete con;return -1;}
96 short int f2var1=1;
97 char f3var1[20] ="jitendra";
98 float f4var1 = 5.5;
99 float f5var1 = 10.50;
100 Date f6var1;
101 f6var1.set(2007,1,21);
103 Time f7var1;
104 f7var1.set(12,29,30);
106 TimeStamp f8var1;
107 f8var1.setDate(2007,1,21);
108 f8var1.setTime(12,29,30);
110 int f9var1=20;
111 long long f10var1=12000;
112 char f11var1[8]="23fe";
113 count=0;
115 for(int i=5;i<10;i++)
117 rv = con->beginTrans();
118 if(rv!=OK)break;
120 stmt->setLongLongParam(1,f10var1);
121 stmt->setIntParam(2,f9var1);
122 stmt->setTimeStampParam(3,f8var1);
123 stmt->setTimeParam(4,f7var1);
124 stmt->setDateParam(5,f6var1);
125 stmt->setFloatParam(6,f5var1);
126 stmt->setFloatParam(7,f4var1);
127 stmt->setStringParam(8,f3var1);
128 stmt->setShortParam(9,f2var1);
129 stmt->setIntParam(10,i);
130 stmt->setBinaryParam(11,f11var);
131 rv = stmt->execute(rows);
132 if(rv!=OK)break;
133 rv = con->commit();
134 if(rv!=OK)break;
135 count++;
137 printf("Total row deleted=%d\n",count);
142 //**********************************************************
144 // SELECT * FROM T1;
145 strcpy(statement,"SELECT * FROM T1;");
146 rv = stmt->prepare(statement);
147 if(rv!=OK) { delete stmt; delete con; return 5; }
149 stmt->bindField(1,&f1var);
150 stmt->bindField(2,&f2var);
151 stmt->bindField(3,f3var);
152 stmt->bindField(4,&f4var);
153 stmt->bindField(5,&f5var);
154 stmt->bindField(6,&f6var);
155 stmt->bindField(7,&f7var);
156 stmt->bindField(8,&f8var);
157 stmt->bindField(9,&f9var);
158 stmt->bindField(10,&f10var);
159 stmt->bindField(11,f11var);
160 count=0;
161 rv = con->beginTrans();
162 if(rv!=OK)return 6;
163 stmt->execute(rows);
164 while(stmt->fetch() !=NULL)
167 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);
168 printf(" | ");
169 AllDataType::printVal(f11var,typeBinary,4);
170 printf("\n");
171 count++;
174 stmt->close();
175 rv = con->commit();
176 if(rv !=OK)
177 { delete stmt; delete con; return 7; }
179 printf("Total row fetched=%d\n",count);
181 strcpy(statement,"DROP TABLE T1;");
182 rv = stmt->prepare(statement);
183 if(rv!=OK) { delete stmt; delete con; return 8; }
184 rv = stmt->execute(rows);
185 if(rv!=OK)
186 { delete stmt; delete con; return 9; }
187 printf("Table dropped\n");
189 delete stmt;
190 delete con;
191 return 0;