1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 **************************************************************************/
17 #include<AggTableImpl.h>
20 //connect to the database first
22 DbRetVal rv
= conn
.open("root", "manager");
25 printf("Error during connection %d\n", rv
);
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
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
);
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();
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
87 p1
.setTerm("f1", OpGreaterThanEquals
, &val1
);
88 table
->setCondition(&p1
);
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();
101 printf("Tuple values:\n");
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);
110 aggTable
.closeScan();
114 dbMgr
->closeTable(table
);
115 // dbMgr->dropTable("t1");
116 printf("Table dropped\n");