-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdInsert.java
More file actions
85 lines (80 loc) · 2.02 KB
/
cmdInsert.java
File metadata and controls
85 lines (80 loc) · 2.02 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
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.util.*;
public class cmdInsert extends base{
public cmdInsert(Scanner line, Database db, LinkedList<String> errorsList){
run(line, db, errorsList);
}
public boolean typematch(String a, LinkedList<String> dataTypes, int i){
boolean match = true;
try{
int title = Integer.parseInt(dataTypes.get(i));
try{
int x = Integer.parseInt(a);
}
catch(Exception NumberFormatException){
match = false;
}
}
catch(Exception NumberFormatException){
}
return match;
}
public void run(Scanner line, Database db, LinkedList<String> errorsList){
boolean errors = false;
String rValue = nextSymbol(line);
int rIndex[] = findRelation(rValue, db, false);
if (rIndex[0] == -1) {
errorsList.add("ERROR: RELATION \"" + rValue + "\" DOESN'T EXIST");
} else {
Relation r = db.getRelations().get(rIndex[0]);
Tuple newTuple = new Tuple();
int i = 0;
while (line.hasNext()) {
String nextAttr = nextSymbol(line);
if (!typematch(nextAttr, r.getdataTypes(), i)) {
errorsList.add("ERROR: TYPES DON'T MATCH");
errors = true;
break;
}
if (i >= r.getmaxSize().size()) {
errorsList.add("ERROR: TOO MANY ATTRIBUTES FOR RELATION \"" + rValue + "\"");
errors = true;
break;
}
String better = format(nextAttr, r.getmaxSize().get(i));
newTuple.attribute().add(better);
i++;
}
if (newTuple.attribute().size() != r.getCategories().size()) {
errorsList.add("ERROR: TOO FEW ATTRIBUTES FOR RELATION \"" + rValue + "\"");
errors = true;
}
if (!errors) {
r.getTuples().add(newTuple);
}
}
}
public String format(String a, String line){
String b = "";
try{
int x = Integer.parseInt(line);
for(int j = 0; j < a.length(); j++){
if(j < x){
b += a.charAt(j);
}
else{
b = b.substring(1);
b += a.charAt(j);
}
}
}
catch(Exception NumberFormatException){
int top = Integer.parseInt(line);
for(int i = 0; i < a.length(); i++){
if( i < top ){
b += a.charAt(i);
}
}
}
return b;
}
}