55import java .io .*;
66import java .nio .file .*;
77import java .util .ArrayList ;
8- import java .util .Arrays ;
98import java .util .regex .Matcher ;
109import java .util .regex .Pattern ;
1110
1211public 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
0 commit comments