adding test scripts
[csql.git] / test / sqlgw / Select / singleconngateway.c
blobafa444b9613a246df912ab598f0d0c22fb1003f2
1 /*One connection working with multiple statements
2 *create table T1 with two fields,
3 *One connection working with multiple statements simultaneously for csql,
4 *Create conn
5 *create 3 stmts for inserting into 5 tables and set the same connection
6 *insert into all 3 tables and commit the transaction
7 *free all stmts
8 *select the records from 3 tables in same connection
9 *free all stmt;
10 *Author : Nihar Paital
13 #include"common.h"
15 int main()
17 DbRetVal rv = OK;
18 AbsSqlConnection *con = createConnection();
19 rv = con->connect("root","manager");
20 if(rv!=OK)return 1;
21 AbsSqlStatement *stmt1 = createStatement();
22 AbsSqlStatement *stmt2 = createStatement();
23 AbsSqlStatement *stmt3 = createStatement();
24 char statement[200];
26 stmt1->setConnection(con);
27 stmt2->setConnection(con);
28 stmt3->setConnection(con);
29 int rows=0;
30 void *f1varT1 = NULL;
31 void *f1varT2 = NULL;
32 void *f1varT3 = NULL;
33 // insert records into T1
34 if(strcmp(getenv("DSN"),"oracle")==0) {
35 f1varT1 = malloc(sizeof(long long));
36 f1varT2 = malloc(sizeof(long long));
37 f1varT3 = malloc(sizeof(long long));
38 } else {
39 f1varT1 = malloc(sizeof(int));
40 f1varT2 = malloc(sizeof(int));
41 f1varT3 = malloc(sizeof(int));
43 char f2varT1[36] = "lakshya";
44 printf("Inserting records into table T1\n");
45 strcpy(statement,"INSERT INTO T1 VALUES(?,?);");
46 rv = stmt1->prepare(statement);
47 if(rv!=OK) { delete stmt1; delete con; return 3; }
48 int count=0;
49 rv = con->beginTrans();
50 for(int j=0;j<5;j++) {
51 if(strcmp(getenv("DSN"),"oracle")==0) {
52 *(long long *) f1varT1 = j;
53 stmt1->setLongLongParam(1,*(long long *)f1varT1);
54 } else {
55 *(int *) f1varT1 = j;
56 stmt1->setIntParam(1,*(int *) f1varT1);
58 stmt1->setStringParam(2,f2varT1);
59 rv = stmt1->execute(rows);
60 if(rv!=OK)break;
61 count++;
63 printf("%d rows inserted into table T1\n",count);
65 // insert records into T2
66 char f2varT2[36] = "lakshya";
67 printf("Inserting records into table T2\n");
68 strcpy(statement,"INSERT INTO T2 VALUES(?,?);");
69 rv = stmt2->prepare(statement);
70 if(rv!=OK) { delete stmt2; delete con; return 3; }
71 count=0;
72 for(int j=5;j<10;j++) {
73 if(strcmp(getenv("DSN"),"oracle")==0) {
74 *(long long *) f1varT2 = j;
75 stmt2->setLongLongParam(1,*(long long *)f1varT2);
76 } else {
77 *(int *) f1varT2 = j;
78 stmt2->setIntParam(1,*(int *) f1varT2);
80 stmt2->setStringParam(2,f2varT2);
81 rv = stmt2->execute(rows);
82 if(rv!=OK)break;
83 count++;
85 printf("%d rows inserted into table T2\n",count);
87 // insert records into T3
88 char f2varT3[36] = "lakshya";
89 printf("Inserting records into table T3\n");
90 strcpy(statement,"INSERT INTO T3 VALUES(?,?);");
91 rv = stmt3->prepare(statement);
92 if(rv!=OK) { delete stmt3; delete con; return 3; }
93 count=0;
94 for(int j=10;j<15;j++) {
95 if(strcmp(getenv("DSN"),"oracle")==0) {
96 *(long long *) f1varT3 = j;
97 stmt3->setLongLongParam(1,*(long long *)f1varT3);
98 } else {
99 *(int *) f1varT3 = j;
100 stmt3->setIntParam(1,*(int *) f1varT3);
102 stmt3->setStringParam(2,f2varT3);
103 rv = stmt3->execute(rows);
104 if(rv!=OK)break;
105 count++;
107 printf("%d rows inserted into table T3\n",count);
108 rv = con->commit();
110 free(f1varT1); free(f1varT2); free(f1varT3);
111 stmt1->free(); delete stmt1;
112 stmt2->free(); delete stmt2;
113 stmt3->free(); delete stmt3;
114 delete con;
115 return 0;