-
Notifications
You must be signed in to change notification settings - Fork 1
/
table_fn.cpp
179 lines (157 loc) · 4.8 KB
/
table_fn.cpp
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
#include "table_fn.hh"
#define uint unsigned int
// displays the list of tables stored in the database
void show_tables (vector <table> list_table) {
if (list_table.size() == 0)
cout<<"\nNO TABLES ARE CURRENTLY ADDED TO THE DATABASE.\n";
else {
cout<<"\nTABLES IN THE DATABASE:\n";
for (uint i = 0; i < list_table.size(); i++)
cout<<i+1<<". "<<list_table[i].name<<"\n";
}
}
// prints the table T in a nice and easy to see format
void print_table (table T) {
int width = 15;
cout<<"\n\nTABLE: "<<T.name<<"\n+";
for (uint i = 0; i < T.columns.size(); i++)
cout<<"-----------------+";
cout<<"\n| ";
for (uint i = 0; i < T.columns.size(); i++)
cout<<setw(width)<<T.columns[i].name<<" | ";
cout<<"\n+";
for (uint i = 0; i < T.columns.size(); i++)
cout<<"-----------------+";
cout<<"\n+";
for (uint i = 0; i < T.columns.size(); i++)
cout<<"-----------------+";
cout<<"\n| ";
for (uint j = 0; j < T.columns[0].values.size(); j++) {
for (uint i = 0; i < T.columns.size(); i++)
cout<<setw(width)<<T.columns[i].values[j]<<" | ";
cout<<"\n+";
for (uint i = 0; i < T.columns.size(); i++)
cout<<"-----------------+";
cout<<"\n| ";
}
}
// create a table in the database
int create() {
// SYNTAX:
// CREATE <table name>
// <data type> <attribute1 name>
// <data type> <attribute2 name>
// ...
// checking for repetitive table name
for (uint j = 0;
j < list_table.size();
j++)
if (broken_query[1] ==
list_table[j].name) {
cout<<"\n > ERROR: TABLE NAME \""<<broken_query[1]
<<"\" HAS ALREADY BEEN CREATED!\n"
<<" -----\n";
return -1;
}
// push table in list_table
table temp;
temp.name = broken_query[1];
list_table.push_back(temp);
for (uint i = 2; i < broken_query.size(); i = i+2) {
// push attributes in columns of the table
attribute temp;
temp.type = broken_query[i];
// checking for repetitive attribute name
for (uint j = 0;
j < list_table[list_table.size()-1].columns.size();
j++)
if (broken_query[i+1] ==
list_table[list_table.size()-1].columns[j].name) {
list_table.pop_back();
cout<<"\n > ERROR: ATTRIBUTE NAME \""<<broken_query[i+1]
<<"\" HAS ALREADY BEEN CREATED! ABORTING TABLE CREATION.\n"
<<" -----\n";
return -1;
}
temp.name = broken_query[i+1];
list_table[list_table.size()-1].columns.push_back(temp);
}
cout<<"\n > TABLE \""<<broken_query[1]<<"\" HAS BEEN CREATED!\n";
return 0;
}
// insert records into an existing table
int insert() {
// SYNTAX:
// INSERT <table name>
// <value11> <value12> <value13> ...
// <value11> <value12> <value13> ...
// ...
// check whether any table exists
if (list_table.size() == 0) {
cout<<"\n > TABLE \""<<broken_query[1]<<"\" DOES NOT EXIST!\n";
return -1;
}
// find index of table
int table_index = -1;
for (uint x = 0; x < list_table.size(); x++) {
// cout<<"|"<<list_table[x].name<<"|\n";
if (list_table[x].name == broken_query[1]) {
// cout<<"T "<<list_table[x].name<<" "<<broken_query[1]<<"\n";
table_index = x;
break;
}
}
if (table_index == -1) {
cout<<"\n > TABLE \""<<broken_query[1]<<"\" DOES NOT EXIST!\n";
return -1;
}
uint no_attributes = list_table[table_index].columns.size();
uint no_tuple_add = 0;
// table temp;
// temp.name = broken_query[1];
// list_table.push_back(temp);
for (uint i = 2; i < broken_query.size(); i = i+no_attributes) {
// to check for duplicate tuples
bool flag;
bool flag_add = true;
// for each tuple
if (no_attributes > 0)
for (uint x = 0; x < list_table[table_index].columns[0].values.size(); x++) {
// for each record of that tuple
flag = true;
for (uint y = 0; y < no_attributes; y++)
if (list_table[table_index].columns[y].values[x] != broken_query[i+y]) {
flag = false;
break;
}
// if this is a duplicate tuple
if (flag == true) {
flag_add = false;
break;
}
}
// if no duplicate is found
if (flag_add == true) {
// check if insert data is conforming to the type of the correspoding attribute
flag = true;
for (uint k = 0; k < no_attributes; k++) {
// push values in columns of the table
if (list_table[table_index].columns[k].type == "INT" && is_num(broken_query[i+k]) == false) {
flag = false;
break;
}
}
// if insert data is conforming to the type of the correspoding attribute add it
if (flag == true) {
no_tuple_add++;
for (uint k = 0; k < no_attributes; k++)
// push values in columns of the table
list_table[table_index].columns[k].values.push_back(broken_query[i+k]);
}
}
}
cout<<"\n > "<<no_tuple_add
<<" TUPLE(S) ADDED TO TABLE \""
<<broken_query[1]<<"\"!\n";
return 0;
}