-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSemiSQL.cpp
329 lines (327 loc) · 6.04 KB
/
SemiSQL.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include<conio.h>
#include<math.h>
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<fstream.h>
#include<DOS.h>
#include<iomanip.h>
/*
create database done
create table done
show tables done
select *table done
select <specific_column>table done
delete table
delete column
add,average,percentage
*/
void syntax(char a[]);
void database(char cj[]);
void createtable(char i[]);
void shreadtable(char gh[], char tt[]);
int linecr(char x[],char j[]);
void main()
{
clrscr();
_setcursortype(_NOCURSOR);
int i=0;
while(1)
{
gotoxy(15,6);
cout << "\t! READ THIS !";
gotoxy(15,7);
if(i==0)
i++;
cprintf("********************************************************");
textcolor(i);
gotoxy(15,8);
cprintf("Type CREATEDB to create a database.");
textcolor(i+1);
gotoxy(15,9);
cprintf("Type OPENDB to open the database.");
textcolor(i+2);
gotoxy(15,10);
cprintf("Type CREATETABLE to create the table.");
textcolor(i+3);
gotoxy(15,11);
cprintf("Type OPENTABLE to open the table.");
textcolor(i+4);
gotoxy(15,12);
cprintf("Type DPTABLE to display the table.");
textcolor(i+5);
gotoxy(15,13);
cprintf("Type SHOWTABLES to display the names of all the tables");
textcolor(i+6);
gotoxy(15,14);
cprintf("Type SELECTTABLE to select a specific table.");
textcolor(i+7);
gotoxy(15,15);
cprintf("Type SELECTCOLUMN to select a column name.");
textcolor(i+8);
gotoxy(15,16);
cprintf("Type EXIT to exit.");
textcolor(i);
gotoxy(15,17);
i++;
cprintf("********************************************************");
delay(2000);
if(i > 5 ) break;
}
cout << "\nNow start entering data\n";
char a[10];
gets(a);
syntax(a);
getch();
}
void database(char db[])
{
char z[20];
int j=strlen(db);
for (int k=0,a=k+1;k<j,a<=j+1;k++,a++)
{
z[a]=db[k];
}
z[0]='@';
int i=strlen(db);
z[i+1]='@';
z[i+2]='\0';
strcpy(db,z); // to be changed
}
void shreadtable(char db[],char table[])
{
/*
user enters tablename ...table comparison done ....check for which column it is
.....display the subsequent data..of that particular column...
till it detects another @....
*/
char myline[100],line[100],ch;
cout <<"\n Enter column name\n";
gets(myline);
ifstream file(db);
while(1)
{
file.get(line,100);
file.get(ch);
if(linecr(line,table) == 1)
{
file.get(line,100);
file.get(ch);
break;
}
}
delay(2000);
int j=0,k=0,n=0,i=0;
cas:
k=0; j=0;
while(line[i] != ',')
{
if(line[i] == ','|| line[i] == ';') break;
if(line[i] == myline[k])
{ k++;
j++;
} i++;
}
if(line[i] == ',' || line[i] == ';') {n++;}
if(j != strlen(myline)){i++;goto cas;}
j=n-1;
while(1)
{ i=0;
k=0;
file.get(line,100);file.get(ch);
if(line[0] == '}') break;
while(line[i] != ';')
{
if(line[i] == ',')
{k++;i++;}
if(k>j || line[i] == ';') break;
if(k == j)cout << line[i];
i++;
}i=0;
cout << "\n" ;
delay(2000);
}
file.close();
gets(line);
syntax(line);
}
void createtable(char db[])
{
char a[10];
int i=0,n=0,k=0;
cout << "\nEnter table name\n";
gets(a);
ofstream fout;
fout.open(db,ios::app|ios::nocreate);
if(!fout)
{cout<<"\nNO dB OPENED!!\n";}; //syntax
fout << "@"<< a << "@\n";
cout<<"\nEnter column names\n"; //excess data warning;
gets(a);
while(a[i] != '\0')
{
if(a[i] == ',') n++;
i++;
}
cout << endl;
fout << a<< "\n";
cout <<"\nEnter column data\n";
i=0;
do
{
maaza:
k= 0;
gets(a);
while(a[i] != '\0')
{
if(a[i] == ',') k++;
if(k > n){ cout <<"\nYou're entering excess data!"
<< "\n Recheck your data\n"; goto maaza;}
i++;
}i=0;
fout << a;
while(a[i] != '\0')
{
if(a[i] == '}'){fout << '\n' ;goto cas;}
i++;
}
if(a[i] == '\0') fout << '\n';
i=0;
}while(1);
cas:
fout.close();
gets(a);
syntax(a);
}
void linecr(char line[])
{
char trans[10];
int n,i=0;
strcpy(trans,line);
n = strlen(trans);
trans[n-1] = '\0';
while(trans[i] != '\0')
{
trans[i] = trans[i+1];
i++;
}
strcpy(line,trans);
}
int linecr(char line[],char line2[])
{
int j=0,i=0;
while(line2[i] != '\0')
{ j++;
i++;
if(line2[i] != line[i+1])goto fun;
}
fun:
if(j == strlen(line2))return 1;
else return 0;
}
void syntax(char a[])
{
char syn[10],var[10],ch,line[50], line2[50];
static char db[20],table[30];
int i=0,k=0;
fstream infile,tout;
if(strcmp("CREATEDB",a) == 0) // done
{
cout << "\nCREATING" ;
cout << "\nEnter database name-\n";
gets(db);
database(db);
puts(db);
infile.open(db,ios::out|ios::noreplace);
cout << "\nDatabase created!\n";
gets(syn);
syntax(syn);
}
else if(strcmp("OPENDB",a) ==0) //done
{
cout << "\nEnter database name-\n ";
gets(db);
database(db);
tout.open(db,ios::in);
if(!tout)
{
cout << "\nDB not created!\n ";
}
else cout << "\nDatabase opened!\n";
tout.close();
gets(syn);
syntax(syn);
}
if(strcmp("CREATETABLE",a) ==0)
{
createtable(db);
}
if(strcmp("DPTABLE",a) == 0)
{
ifstream file(db);
if(!file) cout << "\nDB not entered!\n";
while(1)
{
while(k != 1)
{
file.get(line,100);
file.get(ch);
if(line[0] == '@'){k=linecr(line,table);}
}
if(k==1 )
{
file.get(line,100);
file.get(ch);
i=0;
while(line[i] != ';' )
{
if(line[i] == ',') {cout << "\t\t"; i++;}
if(line[i] == '}') goto jojol;
cout << line[i];
i++;
delay(100);
}
if(line[i] == ';')cout << ch;
delay(1000);
}
if(file.eof() ==1) break;
}
jojol:
gets(syn);
syntax(syn);
}
if(strcmp("SHOWTABLES",a) == 0)
{
ifstream tout(db,ios::nocreate);
tout.seekg(0);
while(1)
{
tout.get(var,100); // add variable
tout.get(ch);
if(var[0] == '@')
{ linecr(var);
cout << var << ",";
}
if(tout.eof()) break;
}
tout.close();
gets(syn);
syntax(syn);
}
if(strcmp("SELECTTABLE",a) == 0)
{
cout << "\nEnter table name-\n";
gets(table);
gets(syn);
syntax(syn);
}
if(strcmp("SELECTCOLUMN",a) == 0)
{
shreadtable(db,table); //enter any column name and it will display the column name
}
if(strcmp("EXIT",a) == 0) exit(1);
else cout << "\t\t --WRONG SYNTAX!\n";
gets(syn);
syntax(syn);
}