Fixing failing tests in join test module
[csql.git] / tmptest / create.c
blob4282bd5b52cc3d8592ae83cb2c076633278e27db
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 DbRetVal rv = conn.open("root", "manager");
23 if (rv != OK)
25 printf("Error during connection %d\n", rv);
26 return -1;
29 //get dbmgr to create table and index
30 DatabaseManager *dbMgr = conn.getDatabaseManager();
31 if (dbMgr == NULL) { printf("Auth failed\n"); return -1;}
33 //create table with two fields, f1 integer and f2 string
34 TableDef tabDef;
35 tabDef.addField("f1", typeInt, 0, NULL, true);
36 tabDef.addField("f2", typeInt);
37 tabDef.addField("f3", typeInt);
38 tabDef.addField("f4", typeInt);
39 tabDef.addField("f5", typeInt);
40 rv = dbMgr->createTable("t1", tabDef);
41 if (rv != OK) { printf("Table creation failed\n"); return -1; }
42 printf("Table created\n");
44 //Creating hash index on field f1 of table t1
45 HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
46 strcpy(idxInfo->tableName, "t1");
47 idxInfo->list.append("f1");
48 idxInfo->indType = hashIndex;
49 rv = dbMgr->createIndex("indx1", idxInfo);
50 if (rv != OK) { printf("Index creation failed\n"); return -1; }
51 printf("Index created\n");
53 //open the table handle for doing DML operations
54 Table *table = dbMgr->openTable("t1");
55 if (table == NULL) { printf("Unable to open table\n"); return -1; }
56 int id1 = 0, id2=0, id3=0, id4=0, id5=0;
57 table->bindFld("f1", &id1);
58 table->bindFld("f2", &id2);
59 table->bindFld("f3", &id3);
60 table->bindFld("f4", &id4);
61 table->bindFld("f5", &id5);
62 char *tuple;
63 int ret;
64 int i;
65 int icount =0;
67 //insert 10 tuples into the table t1
68 for(i = 0; i< 10; i++)
70 conn.startTransaction();
71 id1= i; id2 = i +100; id3 = i+ 1000; id4=i%2; id5=i%3;
72 ret = table->insertTuple();
73 if (ret != 0) break;
74 icount++;
75 conn.commit();
77 char msgBuf[1024];
78 sprintf(msgBuf,"Total rows inserted %d \n",icount);
79 os::write(1,msgBuf,strlen(msgBuf));
80 dbMgr->closeTable(table);
81 table = dbMgr->openTable("t1");
82 if (table == NULL) { printf("Unable to open table\n"); return -1; }
84 //set the condition f1 >=5
85 Condition p1;
86 int val1 = 2;
87 p1.setTerm("f1", OpGreaterThanEquals, &val1);
88 table->setCondition(&p1);
89 icount=0;
90 printf("Selection f1 >= 2 starts on table t1\n");
91 AggTableImpl aggTable;
92 int group1=0, sum1=0, max1=0, cnt1=0;
93 aggTable.setTable(table);
94 //aggTable.setGroup("f4", &group1);
95 aggTable.bindFld("f1", AGG_AVG, &sum1);
96 aggTable.bindFld("f2", AGG_MAX, &max1);
97 aggTable.bindFld("f3", AGG_COUNT, &cnt1);
99 conn.startTransaction();
100 aggTable.execute();
101 printf("Tuple values:\n");
102 while(true)
104 tuple = (char*)aggTable.fetch() ;
105 if (tuple == NULL) { break; }
106 printf("grpfld=%d sumfld=%d maxfld=%d cntfld=%d\n", group1, sum1,max1,cnt1);
107 // printf("grpfld=%d sumfld=%d \n", group1, sum1);
108 icount++;
110 aggTable.closeScan();
111 conn.commit();
112 aggTable.close();
114 dbMgr->closeTable(table);
115 // dbMgr->dropTable("t1");
116 printf("Table dropped\n");
117 conn.close();
118 return 0;