/
InsertParser.java
114 lines (98 loc) · 2.36 KB
/
InsertParser.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
113
114
/*
Name: Martin Smith, Eric Anderson
CSCI 330 - Spring 2019
File Name : InsertParser.java
*/
import java.util.*;
public class InsertParser{ // modified from SURLY1, primarily with printing conventiond
private String name;
private int attrNum;
private Relation relation;
private Tuple tuple;
InsertParser(Parser p){
addInfo(p);
}
InsertParser(String command){
Parser p = new Parser(command);
addInfo(p);
}
private void addInfo(Parser p){
Tuple t = new Tuple();
int attrVar = 0;
boolean nameSet = false;
LinkedList<String> commands = p.parseCommandSet();
for(int i = 1; i < commands.size(); i++){
AttributeValue value = new AttributeValue();
String currCommand = commands.get(i);
if(!nameSet){//set name of relation if not set
name = currCommand;
nameSet = true;
}else{
if(!currCommand.equals(";")){
value.setValue(name);
value.setName(currCommand);
t.add(value);
attrVar = attrVar + 1;
}
}
}
tuple = t;
attrNum = attrVar;
}
public boolean hasValidAttNum(int attLimit){
if(attrNum == attLimit){
return true;
}else{
return false;
}
}
private boolean validType(AttributeValue a, String curr){ // typically error handling of valids, if it matches the already established schema
if(curr.equals("NUM")){
try{
Integer.parseInt(a.parseAttName());
}catch(Exception e){
return false;
}
}
return true;
}
private boolean validSize(AttributeValue a, int curr){ // typically error handling of valids, if it matches the already established schema
if(curr < a.parseAttName().length()){
return false;
}else{
return true;
}
}
public boolean isValid(Relation r){
LinkedList<Attribute> schema = r.parseRelationSchema();
if(!r.getTemp()){
int count = 0;
for(int i = 0; i < tuple.parseTupleValues().size(); i++){
AttributeValue a = tuple.parseTupleValues().get(i);
Attribute curr = schema.get(i);
if(validType(a, curr.parseAttributeType()) && validSize(a, curr.parseAttributeLength())){//don't do anything
}else{
break;
}
count++;
}
if(count != tuple.parseTupleValues().size()){
return false;
}else{
return true;
}
}else{
return false;
}
}
public Tuple parseTuple(){
return tuple;
}
public String parseRelationName(){
return this.name;
}
public int parseAttributeCount(){
return this.attrNum;
}
}
// ### END ###