Skip to content

Commit 783e2b2

Browse files
committed
modified README
1 parent 52b00e8 commit 783e2b2

File tree

8 files changed

+326
-121
lines changed

8 files changed

+326
-121
lines changed

ExceptionFramework/src/main/java/edu/upvictoria/poo/Analyzer.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package edu.upvictoria.poo;
22

3+
import edu.upvictoria.poo.SQLProcedures.Insertion;
34
import edu.upvictoria.poo.exceptions.*;
45

56
import java.io.FileNotFoundException;
@@ -17,6 +18,7 @@ public class Analyzer {
1718

1819
private final SQL sql = new SQL();
1920
private Database database = new Database();
21+
private final Insertion insertion = new Insertion();
2022

2123
public Analyzer(){
2224
keywords.add("USE");
@@ -112,7 +114,9 @@ public void analyzeSyntax(String line) throws Exception {
112114
}
113115

114116
refreshDB(this.database.getDbFile());
115-
sql.handleInsertInto(line, keyword, this.database);
117+
insertion.setDatabase(database);
118+
insertion.setQuery(line);
119+
insertion.handle();
116120
refreshDB(this.database.getDbFile());
117121
return;
118122

@@ -127,6 +131,10 @@ public void analyzeSyntax(String line) throws Exception {
127131
return;
128132

129133
case "UPDATE":
134+
if (database.getDbFile() == null) {
135+
throw new DatabaseNotSetException("USE COMMAND NOT EXECUTED");
136+
}
137+
130138
refreshDB(this.database.getDbFile());
131139
sql.handleUpdate(line, keyword, this.database);
132140
refreshDB(this.database.getDbFile());

ExceptionFramework/src/main/java/edu/upvictoria/poo/Database.java

+8
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@ public void printTableNames(){
4242
public void addTable(Table table) {
4343
this.tables.add(table);
4444
}
45+
46+
public Table getTableByName(String tableName){
47+
for(Table table : this.tables){
48+
if(table.getTableName().equals(tableName)){
49+
return table;
50+
}
51+
}
52+
}
4553
}

ExceptionFramework/src/main/java/edu/upvictoria/poo/SQL.java

+25-116
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,12 @@
55
import java.io.*;
66
import java.nio.file.*;
77
import java.util.ArrayList;
8-
import java.util.Arrays;
98
import java.util.regex.Matcher;
109
import java.util.regex.Pattern;
1110

1211
public class SQL {
13-
public String clean(String line, String keyword) throws StringIndexOutOfBoundsException {
14-
int endOfKeyword = line.indexOf(keyword) + keyword.length();
15-
int semicolon = line.indexOf(";");
16-
17-
line = line.substring(endOfKeyword + 1, semicolon);
18-
return line.trim();
19-
}
20-
2112
public File handleUse(String line, String keyword) throws FileSystemException, StringIndexOutOfBoundsException, FileNotFoundException {
22-
String givenPath = clean(line,keyword);
13+
String givenPath = Utils.clean(line,keyword);
2314

2415
File database = new File(Paths.get("").toAbsolutePath().resolve(givenPath).toString());
2516
if(!database.exists()){
@@ -107,7 +98,7 @@ public ArrayList<Column> splitValues(String line) throws DataTypeNotFoundExcepti
10798
}
10899

109100
public void handleCreateTable(String line, String keyword, Database database) throws IOException {
110-
String cleanedLine = clean(line, keyword);
101+
String cleanedLine = Utils.clean(line, keyword);
111102
ArrayList<Column> columns = splitValues(cleanedLine);
112103
ArrayList<String> duplicates = new ArrayList<>();
113104

@@ -162,7 +153,7 @@ private static File getFile(Database database, ArrayList<Column> columns) throws
162153
}
163154

164155
public void handleCreateDatabase(String line, String keyword) throws FileSystemException {
165-
String givenPath = clean(line,keyword);
156+
String givenPath = Utils.clean(line,keyword);
166157
File database = new File(Paths.get("").toAbsolutePath().resolve(givenPath).toString());
167158

168159
if(database.exists()){
@@ -183,7 +174,7 @@ public void handleCreateDatabase(String line, String keyword) throws FileSystemE
183174
}
184175

185176
public void handleDropTable(String line, String keyword, Database database) throws IOException {
186-
String givenName = clean(line, keyword);
177+
String givenName = Utils.clean(line, keyword);
187178

188179
for (Table table : database.getTables()) {
189180
if (table.getTableName().equals(givenName)) {
@@ -212,106 +203,8 @@ public void handleDropTable(String line, String keyword, Database database) thro
212203
throw new FileNotFoundException("TABLE NOT FOUND");
213204
}
214205

215-
public ArrayList<String> splitInsertionColumns (String line, boolean splittingCols) throws SQLSyntaxException {
216-
ArrayList<String> columns = new ArrayList<>();
217-
String tableName;
218-
219-
if(splittingCols){
220-
tableName = line.substring(0, line.indexOf("(")).trim();
221-
line = line.substring(tableName.length()).trim();
222-
223-
if(!(line.startsWith("(") && line.endsWith(")"))){
224-
throw new SQLSyntaxException("SYNTAX ERROR AT INSERTION COLUMNS");
225-
}
226-
227-
line = line.substring(line.indexOf("(")+1,line.indexOf(")"));
228-
columns.add(tableName.trim());
229-
230-
String[] values = line.split(",");
231-
for(int i = 0; i < values.length; i++){
232-
values[i] = values[i].trim();
233-
}
234-
235-
columns.addAll(Arrays.asList(values));
236-
237-
return columns;
238-
}
239-
240-
if(!(line.startsWith("(") && line.endsWith(")"))){
241-
throw new SQLSyntaxException("SYNTAX ERROR AT INSERTION VALUES");
242-
}
243-
244-
line = line.substring(line.indexOf("(") + 1,line.indexOf(")")).trim();
245-
String[] values = line.split(",");
246-
247-
for(int i = 0; i < values.length; i++){
248-
values[i] = values[i].trim();
249-
}
250-
251-
columns.addAll(Arrays.asList(values));
252-
return columns;
253-
}
254-
255-
public void handleInsertInto(String line, String keyword, Database database) throws IOException, StringIndexOutOfBoundsException {
256-
String cleanedLine, cleanedLine_v2, cleanedLine_v3;
257-
ArrayList<String> insertionColumns, insertionData;
258-
ArrayList<Column> tableColumns = new ArrayList<>();
259-
Table table = null;
260-
boolean tableExists = false, columnFound = false;
261-
262-
cleanedLine = clean(line, keyword);
263-
int VALUEIndex = cleanedLine.indexOf("VALUES");
264-
265-
if(VALUEIndex == -1){
266-
throw new StringIndexOutOfBoundsException("MISSING EXPRESSION");
267-
}
268-
269-
cleanedLine_v2 = cleanedLine.substring(0,VALUEIndex-1);
270-
insertionColumns = splitInsertionColumns(cleanedLine_v2, true);
271-
272-
cleanedLine_v3 = cleanedLine.substring(VALUEIndex + "VALUES ".length());
273-
insertionData = splitInsertionColumns(cleanedLine_v3, false);
274-
275-
276-
if(insertionColumns.size()-1 != insertionData.size()){
277-
throw new InsuficientDataProvidedException("INSUFICIENT DATA PROVIDED");
278-
}
279-
280-
ArrayList<Table> tables = database.getTables();
281-
for(Table tableF : tables) {
282-
if(tableF.getTableName().equals(insertionColumns.get(0))){
283-
tableExists = true;
284-
insertionColumns.remove(0);
285-
table = tableF;
286-
tableColumns = tableF.getColumns();
287-
break;
288-
}
289-
}
290-
291-
if(!tableExists){
292-
throw new NoSuchFileException("TABLE DOES NOT EXISTS");
293-
}
294-
295-
for(String insertionColumn : insertionColumns){
296-
for(Column tableColumn : tableColumns){
297-
if(tableColumn.getName().equals(insertionColumn)){
298-
columnFound = true;
299-
break;
300-
}
301-
}
302-
303-
if(!columnFound){
304-
throw new ColumnDoesNotMatch("COLUMN DOES NOT MATCH: " + insertionColumn);
305-
}
306-
307-
columnFound = false;
308-
}
309-
310-
table.appendDataToTable(insertionData,insertionColumns);
311-
}
312-
313206
public void handleDeleteFrom(String line, String keyword, Database database) throws IOException, StringIndexOutOfBoundsException {
314-
String cleanedLine = clean(line, keyword), selectedTable, whereLine = null;
207+
String cleanedLine = Utils.clean(line, keyword), selectedTable, whereLine = null;
315208
boolean tableExists = false;
316209
ArrayList<String> whereTokens;
317210
ArrayList<ArrayList<Object>> wheredData;
@@ -351,7 +244,7 @@ public void handleDeleteFrom(String line, String keyword, Database database) thr
351244
}
352245

353246
public void handleUpdate(String line, String keyword, Database database) throws SQLSyntaxException, NoSuchFileException {
354-
String cleanedLine = clean(line,keyword), selectedTable, whereLine = null, rawUpdateData;
247+
String cleanedLine = Utils.clean(line,keyword), selectedTable, whereLine = null, rawUpdateData;
355248
boolean tableExists = false;
356249
ArrayList<String> whereTokens;
357250
ArrayList<ArrayList<Object>> wheredData;
@@ -384,7 +277,18 @@ public void handleUpdate(String line, String keyword, Database database) throws
384277

385278
ArrayList<Table> tables = database.getTables();
386279
for(Table table : tables){
387-
280+
if(table.getTableName().equals(selectedTable)){
281+
tableExists = true;
282+
283+
if(whereLine != null){
284+
whereTokens = getWhereTokens(whereLine,table);
285+
whereTokens = Where.infixToPostfix(whereTokens);
286+
Tree.Node root = Where.createTree(whereTokens);
287+
wheredData = Where.evaluateTree(root, table.getData(), table);
288+
} else {
289+
wheredData = table.getData();
290+
}
291+
}
388292
}
389293

390294
if(!tableExists){
@@ -396,7 +300,7 @@ public void handleSelect(String line, String keyword, Database database) throws
396300
ArrayList<String> columns = new ArrayList<>();
397301
ArrayList<String> showingCol = new ArrayList<>();
398302

399-
String cleanedLine = clean(line, keyword);
303+
String cleanedLine = Utils.clean(line, keyword);
400304
String selectedColumns = cleanedLine.substring(0,cleanedLine.indexOf("FROM")-1).trim();
401305
String selectedTable, whereLine = null;
402306

@@ -576,9 +480,14 @@ public ArrayList<String> getWhereTokens(String line, Table table) throws SQLSynt
576480
throw new IndexOutOfBoundsException("WHERE STATEMENT MALFORMED AT > " + line);
577481
}
578482

483+
for(String token : whereTokens){
484+
System.out.println(token);
485+
}
486+
579487
return whereTokens;
580488
}
581489
}
582490

583491
// TODO: COMPROBAR QUE SE INGRESA UN TIPO DE DATO CORRECTO AL CREAR UN REGISTRO
584-
// TODO: COMPROBAR QUE NO SE ELIGEN DOS COLUMNAS IGUALES A LAS QUE INGRESAR UN REGISTRO
492+
// TODO: COMPROBAR QUE NO SE ELIGEN DOS COLUMNAS IGUALES A LAS QUE INGRESAR UN REGISTRO
493+
// TODO: EN INSERCION DE DATOS DELIMITAR LOS STRINGS CON COMILLAS SIMPLES
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,128 @@
1-
package edu.upvictoria.poo.SQLProcedures;public class Insertion {
1+
package edu.upvictoria.poo.SQLProcedures;
2+
3+
import edu.upvictoria.poo.Column;
4+
import edu.upvictoria.poo.Database;
5+
import edu.upvictoria.poo.Table;
6+
import edu.upvictoria.poo.Utils;
7+
8+
import edu.upvictoria.poo.exceptions.ColumnDoesNotMatch;
9+
import edu.upvictoria.poo.exceptions.InsuficientDataProvidedException;
10+
import edu.upvictoria.poo.exceptions.SQLSyntaxException;
11+
12+
import java.io.IOException;
13+
import java.nio.file.NoSuchFileException;
14+
import java.util.ArrayList;
15+
import java.util.Arrays;
16+
17+
public class Insertion {
18+
private String query;
19+
private Database database;
20+
private final String keyword = "INSERT INTO";
21+
22+
public void setDatabase(Database database) {
23+
this.database = database;
24+
}
25+
26+
public void setQuery(String query) {
27+
this.query = query;
28+
}
29+
30+
public void handle() throws IOException, StringIndexOutOfBoundsException {
31+
String cleanedLine, cleanedLine_v2, cleanedLine_v3;
32+
ArrayList<String> insertionColumns, insertionData;
33+
ArrayList<Column> tableColumns = new ArrayList<>();
34+
Table table = null;
35+
boolean tableExists = false, columnFound = false;
36+
37+
cleanedLine = Utils.clean(query, keyword);
38+
int VALUEIndex = cleanedLine.indexOf("VALUES");
39+
40+
if(VALUEIndex == -1){
41+
throw new StringIndexOutOfBoundsException("MISSING EXPRESSION");
42+
}
43+
44+
cleanedLine_v2 = cleanedLine.substring(0,VALUEIndex-1);
45+
insertionColumns = splitInsertionColumns(cleanedLine_v2);
46+
47+
cleanedLine_v3 = cleanedLine.substring(VALUEIndex + "VALUES ".length());
48+
insertionData = splitInsertionValues(cleanedLine_v3);
49+
50+
if(insertionColumns.size()-1 != insertionData.size()){
51+
throw new InsuficientDataProvidedException("INSUFICIENT DATA PROVIDED");
52+
}
53+
54+
ArrayList<Table> tables = database.getTables();
55+
for(Table tableF : tables) {
56+
if(tableF.getTableName().equals(insertionColumns.get(0))){
57+
tableExists = true;
58+
insertionColumns.remove(0);
59+
table = tableF;
60+
tableColumns = tableF.getColumns();
61+
break;
62+
}
63+
}
64+
65+
if(!tableExists){
66+
throw new NoSuchFileException("TABLE DOES NOT EXISTS");
67+
}
68+
69+
for(String insertionColumn : insertionColumns){
70+
for(Column tableColumn : tableColumns){
71+
if(tableColumn.getName().equals(insertionColumn)){
72+
columnFound = true;
73+
break;
74+
}
75+
}
76+
77+
if(!columnFound){
78+
throw new ColumnDoesNotMatch("COLUMN DOES NOT MATCH: " + insertionColumn);
79+
}
80+
81+
columnFound = false;
82+
}
83+
84+
table.appendDataToTable(insertionData,insertionColumns);
85+
}
86+
87+
private ArrayList<String> splitInsertionColumns(String query) throws SQLSyntaxException {
88+
ArrayList<String> columns = new ArrayList<>();
89+
String tableName;
90+
91+
tableName = query.substring(0, query.indexOf("(")).trim();
92+
query = query.substring(tableName.length()).trim();
93+
94+
if(!(query.startsWith("(") && query.endsWith(")"))){
95+
throw new SQLSyntaxException("SYNTAX ERROR AT INSERTION COLUMNS");
96+
}
97+
98+
query = query.substring(query.indexOf("(")+1,query.indexOf(")"));
99+
columns.add(tableName.trim());
100+
101+
String[] values = query.split(",");
102+
for(int i = 0; i < values.length; i++){
103+
values[i] = values[i].trim();
104+
}
105+
106+
columns.addAll(Arrays.asList(values));
107+
return columns;
108+
}
109+
110+
private ArrayList<String> splitInsertionValues(String query) throws SQLSyntaxException{
111+
if(!(query.startsWith("(") && query.endsWith(")"))){
112+
throw new SQLSyntaxException("SYNTAX ERROR AT INSERTION VALUES");
113+
}
114+
115+
query = query.substring(query.indexOf("(") + 1,query.indexOf(")")).trim();
116+
String[] values = query.split(",");
117+
118+
for(int i = 0; i < values.length; i++){
119+
if(!(values[i].startsWith("'") && values[i].endsWith("'"))){
120+
throw new SQLSyntaxException("SYNTAX ERROR AT INSERTION VALUES");
121+
}
122+
values[i] = values[i].trim();
123+
values[i] = values[i].replace("'", "");
124+
}
125+
126+
return new ArrayList<>(Arrays.asList(values));
127+
}
2128
}

0 commit comments

Comments
 (0)