-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.c
633 lines (564 loc) · 21.3 KB
/
select.c
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*Laura Stalker
LS451494
lstalker@albany.edu
*/
#include <stdio.h> /*Include the standard input/output library*/
#include <stdlib.h> /*To use exit function*/
#include <string.h> /*String library for string manipulation functions*/
#include "constants.h" /*Header file with constants*/
#include "prototypes.h" /*Header file with prototypes*/
/*A function that takes 6 parameters: a string that is a relation name, a
string that is an attribute name, a string that is a relational operator
(one of: ==, <=, >=, <, >, or !=), an int that is the integer value of an
attribute to be tested, a string that is also from a query and is to be
tested, and a string that is a type (either "S" for string or "I" for int).
The function returns nothing. The function pulls various info from the
schema and data files that correspond to this relation/ attribute combo:
including number of tuples in the relation, size of a tuple in the relation,
the type of the atttribute being compared, and the byte the attribute starts
at in any given tuple. It determines whether or not a tuple in the data file
satisfies the condition given in the query (represented by attribute name,
relational operator, and either string or int value). It assumes that any
strings in the query file that are to be compared, are surrounded by double
quotes. The function will print to stdout, either the tuple that satisfies,
or messages that state any problems with the query, such as: If the query is
asking to compare a string to an int, if the relational operator is invalid
for comparing the types asked to compare, or if there are no tuples in the db
which satisfy the condition.
*/
void select( char *relName, char *attrName, char *relop, int value,
char *string, char *type){
FILE *dataFile; /*Ptr to data file*/
FILE *schemaFile; /*Ptr to schema file*/
int tupleLen; /*Length of a tuple for this relation*/
int numTuples; /*Number of tuples for this relation*/
int startByte; /*Byte to start at for this attribute in tuple*/
int numAttr; /*Number of attributes in schema*/
char tempType[MAX_STRING]; /*Temp for attribute type*/
char tempName[MAX_STRING]; /*Temp for attribute name*/
int bytes; /*Temp storage for bytes of an attribute*/
int bytesArray[MAX_ATTRIBUTES]; /*Array for bytes of each attribute*/
/*Array for type of each attribute
*/
char typeArray[MAX_ATTRIBUTES][MAX_ATTRIBUTES];
int attrBytes; /*Number of bytes for this specific attribute*/
int counter; /*Array index counter*/
int tempInt; /*Temp to store attribute value in*/
char tempString[STRING_LENGTH]; /*Temp to store attr string in*/
char finalString[STRING_LENGTH]; /*string param with "" added*/
int tupleFound; /*Flag to tell if a tuple satisfying condition was found*/
/*Create an array large enough for a string that is the attribute
name plus ".dat\0". This will be the name of the data file.
*/
char fileName[ strlen( relName) + strlen( ".dat\0")];
strcpy( fileName, relName);
strcat( fileName, ".dat\0");
/*Open the data file for reading. If unable to open, print an
error message and exit.
*/
if( (dataFile = ( fopen( fileName, "r"))) == NULL ){
printf("There was an error trying to open data file.\n");
exit(1);
}
/*Copy the attribute name plus ".sch\0" into fileName. This will be the
name of the schema file.
*/
strcpy( fileName, relName);
strcat( fileName, ".sch\0");
/*Open the schema file for reading. If unable to open, print an
error message and exit.
*/
if( (schemaFile = ( fopen( fileName, "r"))) == NULL ){
printf("There was an error trying to open schema file.\n");
exit(1);
}
/////////////FILES ARE OPEN, NOW FILL ARRAY WITH BYTE SIZES/////////////
tupleLen = tuplen( relName, 0); /*Get length of tuple for this rel'n*/
numTuples = count( relName, 0); /*Get num of tuples for this rel'n in db*/
/*Get byte in a tuple where attribute begins
*/
startByte = whatType( relName, attrName, 1);
fscanf( schemaFile, "%d", &numAttr); /*Get numAttr from schema file*/
counter = 0; /*Initialize array counter*/
bytes = 0; /*An initial value for bytes for an attribute*/
/*Go through schema file and get number of bytes for each attribute
and the type, and store the bytes value in a successive spots in an
array and types in another array.
*/
while( numAttr > 0){
fscanf( schemaFile, "%s%s%d", tempName, tempType, &bytes);
if( strcmp( attrName, tempName) == 0){ /*If match attr*/
attrBytes = bytes; /*Store # bytes in attrBytes*/
}
bytesArray[counter] = bytes; /*Add attr length to bytesArray*/
strcpy( typeArray[counter], tempType);
numAttr--; /*To get all attr in schema file*/
/*Move to next position in array to store bytes of each attr
*/
counter++;
}
/////ARRAYS FILLED, LOOP THRU DATA FILE TO FIND TUPLES THAT SATISFY/////
numAttr = counter; /*Reset numAttr to total # of attributes*/
counter = 0; /*Reset array counter*/
tupleFound = 0; /*No tuples satisfying found yet*/
while( numTuples > 0){ /*While there are more tuples to check*/
fseek( dataFile, (long) startByte, SEEK_CUR); /*Get to attr to check*/
if( strcmp( type, "I") == 0){ /*If the type is INT*/
/*If we are going to compare an int, but we were given a string,
then the comparison will fail, print an error message, and end*/
if( value == -1){
printf(" Error: Wrong type of value for attribute %s.\n\n",
attrName);
fflush( stdout);
return;
}
/*Store value to check in tempInt*/
fread( (void *) &tempInt, sizeof(int), 1, dataFile);
if( strcmp( relop, "==") == 0){ /***If relop is EQUALS***/
if( tempInt == value){ /*If condition satisfied*/
tupleFound = 1; /*A tuple satisfying condition was found*/
/*back up to start of tuple in the file
*/
fseek( dataFile, (long) -(sizeof(int) + startByte), SEEK_CUR);
counter = 0; /*Reset loop counter*/
//get each piece of tuple & print it
while( counter < numAttr){
/*If this attribute in tuple is an int, get value and
store it in tempInt. Then print out the value.
*/
if( strcmp( typeArray[counter], "I") == 0 ){
fread( (void *) &tempInt, sizeof(int), 1, dataFile);
printf("%d\t", tempInt);
fflush( stdout);
}
/*If this attribute of the tuple is a string, get the
string and store it. Then print it.
*/
else{
fread( (void *) tempString, bytesArray[counter], 1,
dataFile);
printf("%s\t", tempString);
fflush( stdout);
}
counter++; /*Move to next attr in tuple*/
}
printf("\n"); /*Print a blank line between query results*/
fflush( stdout);
}
else{ /*If condition not satisfied, move to end of tuple*/
fseek( dataFile,
(long) (tupleLen - startByte - sizeof(int)), SEEK_CUR);
}
numTuples--; /*One less tuple to check*/
continue; /*Move to next iteration (next tuple)*/
}
else if( strcmp( relop, ">=") == 0){ /*If relop is GREATER/EQUAL*/
if( tempInt >= value){
tupleFound = 1; /*A tuple satisfying condition was found*/
//back up to start of tuple
fseek( dataFile,
(long) -(sizeof(int) + startByte), SEEK_CUR);
counter = 0; /*Reset loop counter*/
//get each piece of tuple & print it
while( counter < numAttr){
/*If this attribute in tuple is an int, get value and
store it in tempInt. Then print out the value.
*/
if( strcmp( typeArray[counter], "I") == 0 ){
fread( (void *) &tempInt, sizeof(int), 1, dataFile);
printf("%d\t", tempInt);
fflush( stdout);
}
/*If this attribute of the tuple is a string, get the
string and store it. Then print it.
*/
else{
fread( (void *) tempString, bytesArray[counter], 1,
dataFile);
printf("%s\t", tempString);
fflush( stdout);
}
counter++; /*Move to next attr in tuple*/
}
printf("\n"); /*Print a blank line between query results*/
fflush( stdout);
}
else{ /*If condition not satisfied, move to end of tuple*/
fseek( dataFile,
(long) (tupleLen - startByte - sizeof(int)), SEEK_CUR);
}
numTuples--; /*One less tuple to check*/
continue; /*Move to next iteration (next tuple)*/
}
else if( strcmp( relop, "<=") == 0){ /**If relop is LESS/EQUAL**/
if( tempInt <= value){
tupleFound = 1; /*A tuple satisfying condition was found*/
//back up to start of tuple
fseek( dataFile,
(long) -(sizeof(int) + startByte), SEEK_CUR);
counter = 0; /*Reset loop counter*/
//get each piece of tuple & print it
while( counter < numAttr){
/*If this attribute in tuple is an int, get value and
store it in tempInt. Then print out the value.
*/
if( strcmp( typeArray[counter], "I") == 0 ){
fread( (void *) &tempInt, sizeof(int), 1,
dataFile);
printf("%d\t", tempInt);
fflush( stdout);
}
/*If this attribute of the tuple is a string, get the
string and store it. Then print it.
*/
else{
fread( (void *) tempString, bytesArray[counter],
1, dataFile);
printf("%s\t", tempString);
fflush( stdout);
}
counter++; /*Move to next attr in tuple*/
}
printf("\n"); /*Print a blank line between query results*/
fflush( stdout);
}
else{ /*If condition not satisfied, move to end of tuple*/
fseek( dataFile,
(long) (tupleLen - startByte - sizeof(int)), SEEK_CUR);
}
numTuples--; /*One less tuple to check*/
continue; /*Move to next iteration (next tuple)*/
}
else if( strcmp( relop, ">") == 0){ /**If relop is GREATER THAN**/
if( tempInt > value){
tupleFound = 1; /*A tuple satisfying condition was found*/
//back up to start of tuple
fseek( dataFile, (long) -(sizeof(int) + startByte),
SEEK_CUR);
counter = 0; /*Reset loop counter*/
//get each piece of tuple & print it
while( counter < numAttr){
/*If this attribute in tuple is an int, get value and
store it in tempInt. Then print out the value.
*/
if( strcmp( typeArray[counter], "I") == 0 ){
fread( (void *) &tempInt, sizeof(int), 1,
dataFile);
printf("%d\t", tempInt);
fflush( stdout);
}
/*If this attribute of the tuple is a string, get the
string and store it. Then print it.
*/
else{
fread( (void *) tempString, bytesArray[counter],
1, dataFile);
printf("%s\t", tempString);
fflush( stdout);
}
counter++; /*Move to next attr in tuple*/
}
printf("\n"); /*Print a blank line between query results*/
fflush( stdout);
}
else{ /*If condition not satisfied, move to end of tuple*/
fseek( dataFile,
(long) (tupleLen - startByte - sizeof(int)), SEEK_CUR);
}
numTuples--; /*One less tuple to check*/
continue; /*Move to next iteration (next tuple)*/
}
else if( strcmp( relop, "<") == 0){ /***If relop is LESS THAN***/
if( tempInt < value){
tupleFound = 1; /*A tuple satisfying condition was found*/
//back up to start of tuple
fseek( dataFile, (long) -(sizeof(int) + startByte),
SEEK_CUR);
counter = 0; /*Reset loop counter*/
//get each piece of tuple & print it
while( counter < numAttr){
/*If this attribute in tuple is an int, get value and
store it in tempInt. Then print out the value.
*/
if( strcmp( typeArray[counter], "I") == 0 ){
fread( (void *) &tempInt, sizeof(int), 1,
dataFile);
printf("%d\t", tempInt);
fflush( stdout);
}
/*If this attribute of the tuple is a string, get the
string and store it. Then print it.
*/
else{
fread( (void *) tempString, bytesArray[counter],
1, dataFile);
printf("%s\t", tempString);
fflush( stdout);
}
counter++; /*Move to next attr in tuple*/
}
printf("\n"); /*Print a blank line between query results*/
fflush( stdout);
}
else{ /*If condition not satisfied, move to end of tuple*/
fseek( dataFile,
(long) (tupleLen - startByte - sizeof(int)), SEEK_CUR);
}
numTuples--; /*One less tuple to check*/
continue; /*Move to next iteration (next tuple)*/
}
else if( strcmp( relop, "!=") == 0){ /***If relop is NOT EQUAL***/
if( tempInt != value){
tupleFound = 1; /*A tuple satisfying condition was found*/
//back up to start of tuple
fseek( dataFile, (long) -(sizeof(int) + startByte),
SEEK_CUR);
counter = 0; /*Reset loop counter*/
//get each piece of tuple & print it
while( counter < numAttr){
/*If this attribute in tuple is an int, get value and
store it in tempInt. Then print out the value.
*/
if( strcmp( typeArray[counter], "I") == 0 ){
fread( (void *) &tempInt, sizeof(int), 1,
dataFile);
printf("%d\t", tempInt);
fflush( stdout);
}
/*If this attribute of the tuple is a string, get the
string and store it. Then print it.
*/
else{
fread( (void *) tempString, bytesArray[counter],
1, dataFile);
printf("%s\t", tempString);
fflush( stdout);
}
counter++; /*Move to next attr in tuple*/
}
printf("\n"); /*Print a blank line between query results*/
fflush( stdout);
}
else{ /*If condition not satisfied, move to end of tuple*/
fseek( dataFile,
(long) (tupleLen - startByte - sizeof(int)), SEEK_CUR);
}
numTuples--; /*One less tuple to check*/
continue; /*Move to next iteration (next tuple)*/
}
else{ /*If given an invalid relop, print error message*/
printf(" Error: %s is not a valid relational operator\n\n",
relop);
fflush( stdout);
/*So as not to print "no satisfying tuples" message in
addition to this message
*/
tupleFound = 1; /*Set flag for tuple was found*/
break;
}
} ////////END IF TYPE I///////////
else if( strcmp( type, "S") == 0){ /*If attribute is of type STRING*/
/*If we are expecting to compare two strings, but we were given an
int to compare, comparison will fail. Print an error message, and
stop.
*/
if( *(string) != '\"'){
printf(" Error: Wrong type of value for attribute %s.\n\n",
attrName);
fflush( stdout);
return;
}
/*Store value to check in tempInt*/
fread( (void *) tempString, attrBytes, 1, dataFile);
/*Add quotes to string from data file to make comparison with
query string correct
*/
strcpy( finalString, "");
strcpy( finalString, "\"");
strcat( finalString, tempString);
strcat( finalString, "\"");
if( strcmp( relop, "==") == 0){ /*If relop is EQUALS*/
if( strcmp( finalString, string) == 0){
tupleFound = 1; /*A tuple satisfying condition was found*/
//back up to the start of tuple
fseek( dataFile, (long) -(startByte + attrBytes),
SEEK_CUR);
counter = 0; /*Reset loop counter*/
//get each piece of tuple & print it
while( counter < numAttr){
/*If this attribute in tuple is an int, get value and
store it in tempInt. Then print out the value.
*/
if( strcmp( typeArray[counter], "I") == 0 ){
fread( (void *) &tempInt, sizeof(int), 1,
dataFile);
printf("%d\t", tempInt);
fflush( stdout);
}
/*If this attribute of the tuple is a string, get the
string and store it. Then print it.
*/
else{
fread( (void *) tempString, bytesArray[counter],
1, dataFile);
printf("%s\t", tempString);
fflush( stdout);
}
counter++; /*Move to next attr in tuple*/
}
printf("\n"); /*Print a blank line between query results*/
fflush( stdout);
}
else{
//move to end of tuple
fseek( dataFile, (long) (tupleLen - startByte - attrBytes),
SEEK_CUR);
}
numTuples--; /*One less tuple to check*/
continue; /*Move to next iteration (next tuple)*/
}
else if( strcmp( relop, "!=") == 0){ /*If relop is NOT EQUALS*/
if( strcmp( finalString, string) != 0){
tupleFound = 1; /*A tuple satisfying condition was found*/
//back up to start of tuple
fseek( dataFile, (long) -(startByte + attrBytes),
SEEK_CUR);
counter = 0; /*Reset loop counter*/
//get each piece of tuple and print it
while( counter < numAttr){
/*If this attribute in tuple is an int, get value and
store it in tempInt. Then print out the value.
*/
if( strcmp( typeArray[counter], "I") == 0 ){
fread( (void *) &tempInt, sizeof(int), 1,
dataFile);
printf("%d\t", tempInt);
fflush( stdout);
}
/*If this attribute of the tuple is a string, get the
string and store it. Then print it.
*/
else{
fread( (void *) tempString, bytesArray[counter],
1, dataFile);
printf("%s\t", tempString);
fflush( stdout);
}
counter++; /*Move to next attr in tuple*/
}
printf("\n"); /*Print a blank line between query results*/
fflush( stdout);
}
else{
//move to end of tuple
fseek( dataFile, (long) (tupleLen - startByte - attrBytes),
SEEK_CUR);
}
numTuples--; /*One less tuple to check*/
continue; /*Move to next iteration (next tuple)*/
}
else{
printf(" Error: %s is not a valid relop for strings.\n\n",
relop);
fflush( stdout);
/*So as not to print "no satisfying tuples" message in
addition to this message
*/
tupleFound = 1; /*Set flag for tuple was found*/
break;
}
} ////////END IF TYPE S//////////
numTuples--; /*One less tuple to check*/
} //////////END WHILE NUMTUPLES > 0///////////////
/*If no tuple satisfying the condition was found, print a message
*/
if( tupleFound == 0){
printf("No tuples satisfy the specified condition.\n");
fflush( stdout);
}
printf("\n"); /*Print a blank line between different query results*/
fflush( stdout);
/*Close schema file
*/
if( fclose( schemaFile) == EOF){
fprintf( stderr, "Error closing schema file.\n");
exit(1);
}
/*Close data file
*/
if( fclose( dataFile) == EOF){
fprintf( stderr, "Error closing data file.\n");
exit(1);
}
}
/*A function that takes a string that is a relation name, a string
that is an attribute name, and an int that tells whether the user wants
the number of bytes in a tuple before the given attribute returned
(set to 1), or if the user wants the type of this attribute returned.
The function looks through the schema file for the given relation to
determine these pieces of information. The int returned is either the
number of bytes that occur in a tuple before the given attribute (if the
value of wantBytes is 1), or the type of the attribute (1 if an int, and
some other number if a string type).
*/
int whatType( char *relName, char *attrName, int wantBytes){
FILE *schemaFile; /*Ptr to schema file*/
int tupleLen; /*Length of a tuple for relation*/
int numAttr; /*Number of attributes for this relation*/
char name[MAX_STRING]; /*For storing string name in schema file*/
char type[CHAR_LENGTH]; /*For storing char type in schema file*/
int bytes; /*Number of bytes for attr*/
int bytesToAttr; /*Num bytes to start of attr*/
/*Create an array large enough for a string that is the attribute
name plus ".sch\0". This will be the name of the schema file.
*/
char fileName[ strlen( relName) + strlen( ".sch\0")];
strcpy( fileName, relName);
strcat( fileName, ".sch\0");
/*Open the schema file for reading. If unable to open, print an
error message and exit.
*/
if( (schemaFile = ( fopen( fileName, "r"))) == NULL ){
printf("There was an error trying to open schema file.\n");
exit(1);
}
tupleLen = tuplen( relName, 0); /*Get length of tuple for this rel'n*/
bytesToAttr = 0;
fscanf( schemaFile, "%d", &numAttr); /*Get first int in schema file*/
/*Check all attributes in schema file for attrName. If found, stop
looking.
*/
while( numAttr > 0){
fscanf( schemaFile, "%s%s%d", name, type, &bytes);
if( strcmp( attrName, name) == 0){
break;
}
numAttr--;
/*Compute the number of bytes in the tuple before the attribute
we are going to compare
*/
bytesToAttr = bytesToAttr + bytes;
}
/*Close schema file
*/
if( fclose( schemaFile) == EOF){
fprintf( stderr, "Error closing schema file.\n");
exit(1);
}
/*If the user wants the number of bytes in a tuple before the given
attribute returned, rather than they type of the attribute, return that*/
if( wantBytes == 1){
return bytesToAttr;
}
else{
/*If type for this attribute is an int, return 1. If the type is
string, return 0
*/
if( strcmp( type, "I") == 0){
return 1; /*1 for an int type*/
}
else{
return 0; /*0 for a string type*/
}
}
}