-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.c
More file actions
73 lines (58 loc) · 1.62 KB
/
select.c
File metadata and controls
73 lines (58 loc) · 1.62 KB
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
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
// select.c ... run queries
// part of Multi-attribute linear-hashed files
// Ask a query on a named relation
// Usage: ./select [-v] RelName v1,v2,v3,v4,...
// where any of the vi's can be "?" (unknown)
#include "defs.h"
#include "query.h"
#include "tuple.h"
#include "reln.h"
#include "chvec.h"
#define USAGE "./select [-v] RelName v1,v2,v3,v4,..."
// Main ... process args, run query
int main(int argc, char **argv)
{
Reln r; // handle on the open relation
Query q; // processed version of query string
Tuple t; // tuple pointer
char err[MAXERRMSG]; // buffer for error messages
int verbose; // show extra info on query progress
char *rname; // name of table/file
char *qstr; // query string
// process command-line args
if (argc < 3) fatal(USAGE);
if (strcmp(argv[1], "-v") == 0) {
verbose = 1; rname = argv[2]; qstr = argv[3];
}
else {
verbose = 0; rname = argv[1]; qstr = argv[2];
}
if (verbose) { /* keeps compiler quiet */ }
// initialise relation and scanning structure
if (!existsRelation(rname)) {
sprintf(err, "No such relation: %s",rname);
fatal(err);
}
if ((r = openRelation(rname,"r")) == NULL) {
sprintf(err, "Can't open relation: %s",rname);
fatal(err);
}
if ((q = startQuery(r, qstr)) == NULL) {
sprintf(err, "Invalid query: %s",qstr);
fatal(err);
}
// execute the query (find matching tuples)
char tup[MAXTUPLEN];
while ((t = getNextTuple(q)) != NULL) {
tupleString(t,tup);
printf("%s\n",tup);
}
// clean up
closeQuery(q);
closeRelation(r);
return 0;
}