-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.java
162 lines (139 loc) · 5.34 KB
/
Database.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
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
import java.io.*;
import java.util.*;
public class Database {
private Map<String,Relation> relations;
private FileInputStream fin1 = null;
private BufferedReader infile1 = null;
private FileInputStream fin2 = null;
private BufferedReader infile2 = null;
private int numberOfRelations;
private String nameOfRelation;
private int numberOfColumns;
private int numberOfTuples;
// METHODS
// Constructor; creates the empty HashMap object
public Database() {
relations = new HashMap<String, Relation>();
}
// Create the database object by reading from directory
public void initializeDatabase(String dir) {
try {
fin1 = new FileInputStream(dir);
infile1 = new BufferedReader(new InputStreamReader(fin1));
numberOfRelations = Integer.parseInt(infile1.readLine());
} catch (IOException e) {
System.out.println("Error reading file");
}
// crafting the relation full of tuples
for(int i = 0; i < numberOfRelations; i++) {
ArrayList<String> attr1 = new ArrayList<String>();
ArrayList<String> dom1 = new ArrayList<String>();
// Getting information about the relation from the files
try {
nameOfRelation = infile1.readLine();
numberOfColumns = Integer.parseInt(infile1.readLine());
} catch (IOException e) {
System.out.println("Error reading file");
}
// Get attributes and domains to declare the relation
for(int j = 0; j < numberOfColumns; j++) {
try {
attr1.add(infile1.readLine());
dom1.add(infile1.readLine());
} catch (IOException e) {
System.out.println("Error reading file");
}
}
// relation is declared
Relation rTemp = new Relation(nameOfRelation, attr1, dom1);
// Getting number of Tuples inside other reading file
try {
//Relation rTemp = new Relation(nameOfRelation, attr1, dom1);
fin2 = new FileInputStream(nameOfRelation + ".dat");
infile2 = new BufferedReader(new InputStreamReader(fin2));
numberOfTuples = Integer.parseInt(infile2.readLine());
} catch (IOException e) {
System.out.println("Error reading inner file");
}
// crafting the tuple
for(int j = 0; j < numberOfTuples; j++) {
Tuple tTemp = new Tuple(attr1,dom1);
try {
for(int k = 0; k < dom1.size(); k++){
switch (dom1.get(k)) {
case "VARCHAR": tTemp.addStringComponent((infile2.readLine()));
break;
case "INTEGER": tTemp.addIntegerComponent(Integer.parseInt((infile2.readLine())));
break;
case "DECIMAL": tTemp.addDoubleComponent(Double.parseDouble((infile2.readLine())));
break;
}
}
} catch (IOException c) {
System.out.println("Error reading file");
}
// the finished tuple is added to relation
rTemp.addTuple(tTemp);
}
// the finished relation is added to the relation
this.addRelation(nameOfRelation, rTemp);
}
// files are closed
try {
fin1.close();
fin2.close();
} catch (IOException e) {
System.out.println("Error closing file");
}
}
// Add relation r with name rname to HashMap
// if relation does not already exists.
// Make sure to set the name within r to rname.
// return true on successful add; false otherwise
public boolean addRelation(String rname, Relation r) {
if(relations.containsValue(r)) {
return false;
} else {
relations.put(rname, r);
return true;
}
}
// Delete relation with name rname from HashMap
// if relation exists. return true on successful delete; false otherwise
public boolean deleteRelation(String rname) {
if(!relations.containsKey(rname)) {
return false;
} else {
relations.remove(rname);
return true;
}
}
// Return true if relation with name rname exists in HashMap
// false otherwise
public boolean relationExists(String rname) {
if(relations.containsKey(rname)) {
return true;
} else {
return false;
}
}
// Retrieve and return relation with name rname from HashMap;
// return null if it does not exist.
public Relation getRelation (String rname) {
if(!relations.containsKey(rname)){
return null;
} else {
return relations.get(rname);
}
}
// Print database schema to screen.
public void displaySchema() {
for (Map.Entry<String, Relation> entry : relations.entrySet()) {
System.out.print(entry.getKey() + "(");
entry.getValue().displayRelation();
System.out.print(")");
System.out.println();
}
System.out.println();
}
}