Fixing failing tests in join test module
[csql.git] / tmptest / test.c
blob714a0e237dcaa2e7e530b33225147cdabe2af594
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 **************************************************************************/
16 #include<CSql.h>
17 #include<AggTableImpl.h>
18 int main()
20 //connect to the database first
21 Connection conn;
22 char *tuple;
23 int ret;
24 int i;
25 int icount =0;
26 DbRetVal rv = conn.open("root", "manager");
27 if (rv != OK)
29 printf("Error during connection %d\n", rv);
30 return -1;
33 //get dbmgr to create table and index
34 DatabaseManager *dbMgr = conn.getDatabaseManager();
35 if (dbMgr == NULL) { printf("Auth failed\n"); return -1;}
37 //create table with two fields, f1 integer and f2 string
38 TableDef tabDef;
39 tabDef.addField("f1", typeInt, 0, NULL, true);
40 tabDef.addField("f2", typeInt);
41 rv = dbMgr->createTable("t1", tabDef);
42 if (rv != OK) { printf("Table creation failed\n"); return -1; }
43 printf("Table created\n");
44 rv = dbMgr->createTable("t2", tabDef);
45 if (rv != OK) { printf("Table creation failed\n"); return -1; }
47 //open the table handle for doing DML operations
48 Table *table = dbMgr->openTable("t1");
49 Table *table1 = dbMgr->openTable("t2");
51 if (table == NULL) { printf("Unable to open table\n"); return -1; }
52 int id1 = 0, id2=0;
53 table->bindFld("f1", &id1);
54 table1->bindFld("f1", &id1);
55 table->bindFld("f2", &id2);
56 table1->bindFld("f2", &id2);
58 //insert 10 tuples into the table t1
59 for(i = 0; i< 5; i++)
61 conn.startTransaction();
62 id1= i; id2 = i %3;
63 ret = table->insertTuple();
64 if (ret != 0) break;
65 ret = table1->insertTuple();
66 if (ret != 0) break;
67 icount++;
68 conn.commit();
70 char msgBuf[1024];
71 sprintf(msgBuf,"Total rows inserted %d \n",icount);
72 os::write(1,msgBuf,strlen(msgBuf));
73 dbMgr->closeTable(table);
74 dbMgr->closeTable(table1);
76 table = dbMgr->openTable("t1");
77 if (table == NULL) { printf("Unable to open table\n"); return -1; }
78 table1 = dbMgr->openTable("t2");
79 if (table1 == NULL) { printf("Unable to open table\n"); return -1; }
82 JoinTableImpl joinTable;
83 joinTable.setTable(table, table1);
84 int out1, out2, out3;
85 joinTable.bindFld("t1.f1", &out1);
86 joinTable.bindFld("t1.f2", &out2);
87 joinTable.bindFld("t2.f1", &out3);
89 //joinTable.setJoinCondition("t1.f1", OpEquals, "t2.f1");
91 conn.startTransaction();
92 joinTable.execute();
93 printf("Tuple values:\n");
94 int cnt=0;
95 while(true)
97 tuple = (char*)joinTable.fetch() ;
98 if (tuple == NULL) { break; }
99 printf("Tuple %d: %d %d %d\n", cnt++, out1, out2, out3);
100 icount++;
102 joinTable.closeScan();
103 conn.commit();
104 joinTable.close();
106 dbMgr->closeTable(table);
107 dbMgr->closeTable(table1);
108 // dbMgr->dropTable("t1");
109 printf("Table dropped\n");
110 conn.close();
111 return 0;