-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc_bak
executable file
·209 lines (202 loc) · 4.84 KB
/
main.cc_bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <iostream>
#include "ParseTree.h"
#include "QueryTree.h"
#include <fstream>
using namespace std;
extern "C" {
int yyparse(void); // defined in y.tab.c
}
//extern struct FuncOperator *finalFunction; // the aggregate function (NULL if no agg)
//extern struct TableList *tables; // the list of tables and aliases in the query
//extern struct AndList *boolean; // the predicate in the WHERE clause
//extern struct NameList *groupingAtts; // grouping atts (NULL if no grouping)
//extern struct NameList *attsToSelect; // the set of attributes in the SELECT (NULL if no such atts)
//extern int distinctAtts; // 1 if there is a DISTINCT in a non-aggregate query
//extern int distinctFunc; // 1 if there is a DISTINCT in an aggregate query
extern char *createName; // Create Table name
extern struct AttList *newAttList; // Attribute list for the table to be created
extern int commandType; // Type of the command
extern char* fileName;
extern int outType;
extern char *outFileName;
extern char* fileName;
int update_catalog();
int delete_from_catalog(char *);
int main () {
int ret=0;
bool flag = true;
streambuf *sb,*tmp;
while(flag == true){
cout<<"Enter Query and then press cntrl+D\n";
yyparse();
switch (commandType){
case 1:{
DBFile *myDBFile = new DBFile();
ret = update_catalog();
if (ret ==-1){
cout << "Cannot create new table\n";
break;
}
Schema *mysch = new Schema ("catalog", createName);
strcat(createName,".bin");
cout << "Creating New Table"<<endl;
myDBFile->Create(createName,heap,NULL);
myDBFile->Close();
break;
}
case 2:{
DBFile *myDBFile = new DBFile();
Schema *mysch = new Schema ("catalog", createName);
if (update_catalog()==-1){
cout << "Inserting into DBFile"<<endl;
strcat(createName,".bin");
myDBFile->Open(createName);
//myDBFile->MoveFirst();
myDBFile->Load(*mysch,fileName);
myDBFile->Close();
}
break;
}
case 3:{
ret = delete_from_catalog(createName);
if (ret ==-1)
{
cout<<"delete failed"<<endl;
break;
}
strcat(createName,".bin");
remove(createName);
strcat (createName,".header");
remove(createName);
cout << "Dropeed Table successfully"<<endl;
break;
}
case 4:
/* switch(outType){
case 1: // STDOUT
cout << "Changing to STDOUT\n";
if (tmp!=NULL)
cout.rdbuf(tmp);
queryplan = false;
break;
case 2:{ // FILEOUT
cout<< "redirecting to a file\n";
ofstream outfile(outFileName);
tmp = cout.rdbuf();
sb = outfile.rdbuf();
cout.rdbuf(sb);
queryplan = false;
cout<< "Hello i go the file\n";
outfile.close();
}
break;
case 3: // NONE
cout<< "Display only the query plan\n";
queryplan = true;
break;
default:
break;
}*/
break;
case 5:{
QueryTree Q;
Q.BuildTree();
// Display output of the query
break;
}
case 6:{
flag = false;
break;
}
default:
break;
}
//cout << "checking the cout"<<endl;
}
}
int delete_from_catalog(char* tablename){
if (update_catalog()!=-1){
cout<< "Table not present"<<endl;
return -1;
}
string line;
string tmpline;
int i=0;
bool flag=false;
ifstream infile("catalog");
ofstream outtmpfile("tmp");
while(!infile.eof()){
getline(infile,line);
if (strcmp((char*)line.c_str(),"BEGIN")==0){
tmpline = line;
getline(infile,line);
flag = true;
}
if (strcmp(createName,(char*)line.c_str())==0){
while(strcmp((char*)line.c_str(),"END")){
getline(infile,line);
}
getline(infile,line);
}
else{
if (flag == true){
outtmpfile.write((char*)tmpline.c_str(),tmpline.length());
outtmpfile.put('\n');
flag = false;
}
}
outtmpfile.write((char*)line.c_str(),line.length());
outtmpfile.put('\n');
}
infile.close();
outtmpfile.close();
remove("catalog");
rename("tmp","catalog");
}
int update_catalog(){
string line;
char *tmp = createName;
AttList *tmpAttList = newAttList;
ifstream infile ("catalog");
ofstream outfile("catalog",ios_base::app);
while(!infile.eof()){
getline(infile,line);
if (line.compare((string)createName)==0){
cout << "Table already exists\n";
infile.close();
outfile.close();
return -1;
}
}
outfile.write("BEGIN\n",6);
while(*(tmp)!='\0'){
outfile.put(*(tmp));
tmp++;
}
outfile.put('\n');
tmp = createName;
while(*(tmp)!='\0'){
outfile.put(*(tmp));
tmp++;
}
outfile.write(".tbl\n",5);
while(tmpAttList != NULL){
tmp = tmpAttList->AttInfo->name;
while(*(tmp)!='\0'){
outfile.put(*(tmp));
tmp++;
}
if (tmpAttList->AttInfo->code == INT)
outfile.write(" Int\n",4);
else if (tmpAttList->AttInfo->code == DOUBLE)
outfile.write(" Double\n",7);
else if (tmpAttList->AttInfo->code == STRING)
outfile.write(" String\n",7);
outfile.put('\n');
tmpAttList = tmpAttList->next;
}
outfile.write("END\n",4);
infile.close();
outfile.close();
return 1;
}