/
RelationParser.java
112 lines (98 loc) · 2.39 KB
/
RelationParser.java
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
/*
Name: Martin Smith, Eric Anderson
CSCI 330 - Spring 2019
File Name : RelationParser.java
*/
import java.util.*;
public class RelationParser{ // from SURLY1
private String name;
private int attrNum;
private Relation relation;
RelationParser(){ // constructors
relation = new Relation();
}
RelationParser(String command){
relation = new Relation();
Parser p = new Parser(command);
LinkedList<String> commands = p.parseCommandSet();
createRelation(commands);
}
RelationParser(Parser commands){
relation = new Relation();
createRelation(commands.parseCommandSet());
}
public void createRelation(LinkedList<String> commands){ // numerically builds from scratch a relation, starting with a schema
Attribute attribute = new Attribute(); // assumes proper syntax (i.e. CNUM CHAR 8).
LinkedList<Attribute> schema = new LinkedList<Attribute>();
int attrVar = 0;
int schemaVal = 0;
boolean nameSet = false;
boolean hitComma = false;
boolean evalAttribute = false;
for(int i = 1; i < commands.size(); i++){
String currCommand = commands.get(i);
if(!nameSet){
name = currCommand;
relation.setName(name);
nameSet = true;
}else{
if(evalAttribute){
switch(schemaVal){
case 0:
attribute.setAttributeName(currCommand);
schemaVal++;
break;
case 1:
attribute.setDataType(currCommand);
schemaVal++;
break;
case 2:
try {
attribute.setLength(Integer.parseInt(currCommand));
}
catch(Exception e) {
attribute.setLength(-1);
attrNum = -1;
}
schemaVal++;
break;
case 3:
if(currCommand.equals(",")){
schema.add(attribute);
attribute = new Attribute();
attrVar++;
schemaVal = 0;
}else if(currCommand.equals(")")){
schema.add(attribute);
attribute = new Attribute();
attrVar++;
evalAttribute = false;
}else{
this.attrNum = -1;
}
break;
default:
}
}
if(currCommand.equals("(")){
evalAttribute = true;
}
}
}
if(attrNum != -1){
attrNum = attrVar;
}
relation.setSchema(schema);
this.relation = relation;
}
public String parseRelationName(){
return this.name;
}
public int parseAttributeCount(){
return this.attrNum;
}
public Relation parseRelations(){
return this.relation;
}
}
// ### END ###