5
5
import java .io .*;
6
6
import java .nio .file .*;
7
7
import java .util .ArrayList ;
8
- import java .util .Arrays ;
9
8
import java .util .regex .Matcher ;
10
9
import java .util .regex .Pattern ;
11
10
12
11
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
-
21
12
public File handleUse (String line , String keyword ) throws FileSystemException , StringIndexOutOfBoundsException , FileNotFoundException {
22
- String givenPath = clean (line ,keyword );
13
+ String givenPath = Utils . clean (line ,keyword );
23
14
24
15
File database = new File (Paths .get ("" ).toAbsolutePath ().resolve (givenPath ).toString ());
25
16
if (!database .exists ()){
@@ -107,7 +98,7 @@ public ArrayList<Column> splitValues(String line) throws DataTypeNotFoundExcepti
107
98
}
108
99
109
100
public void handleCreateTable (String line , String keyword , Database database ) throws IOException {
110
- String cleanedLine = clean (line , keyword );
101
+ String cleanedLine = Utils . clean (line , keyword );
111
102
ArrayList <Column > columns = splitValues (cleanedLine );
112
103
ArrayList <String > duplicates = new ArrayList <>();
113
104
@@ -162,7 +153,7 @@ private static File getFile(Database database, ArrayList<Column> columns) throws
162
153
}
163
154
164
155
public void handleCreateDatabase (String line , String keyword ) throws FileSystemException {
165
- String givenPath = clean (line ,keyword );
156
+ String givenPath = Utils . clean (line ,keyword );
166
157
File database = new File (Paths .get ("" ).toAbsolutePath ().resolve (givenPath ).toString ());
167
158
168
159
if (database .exists ()){
@@ -183,7 +174,7 @@ public void handleCreateDatabase(String line, String keyword) throws FileSystemE
183
174
}
184
175
185
176
public void handleDropTable (String line , String keyword , Database database ) throws IOException {
186
- String givenName = clean (line , keyword );
177
+ String givenName = Utils . clean (line , keyword );
187
178
188
179
for (Table table : database .getTables ()) {
189
180
if (table .getTableName ().equals (givenName )) {
@@ -212,106 +203,8 @@ public void handleDropTable(String line, String keyword, Database database) thro
212
203
throw new FileNotFoundException ("TABLE NOT FOUND" );
213
204
}
214
205
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
-
313
206
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 ;
315
208
boolean tableExists = false ;
316
209
ArrayList <String > whereTokens ;
317
210
ArrayList <ArrayList <Object >> wheredData ;
@@ -351,7 +244,7 @@ public void handleDeleteFrom(String line, String keyword, Database database) thr
351
244
}
352
245
353
246
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 ;
355
248
boolean tableExists = false ;
356
249
ArrayList <String > whereTokens ;
357
250
ArrayList <ArrayList <Object >> wheredData ;
@@ -384,7 +277,18 @@ public void handleUpdate(String line, String keyword, Database database) throws
384
277
385
278
ArrayList <Table > tables = database .getTables ();
386
279
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
+ }
388
292
}
389
293
390
294
if (!tableExists ){
@@ -396,7 +300,7 @@ public void handleSelect(String line, String keyword, Database database) throws
396
300
ArrayList <String > columns = new ArrayList <>();
397
301
ArrayList <String > showingCol = new ArrayList <>();
398
302
399
- String cleanedLine = clean (line , keyword );
303
+ String cleanedLine = Utils . clean (line , keyword );
400
304
String selectedColumns = cleanedLine .substring (0 ,cleanedLine .indexOf ("FROM" )-1 ).trim ();
401
305
String selectedTable , whereLine = null ;
402
306
@@ -576,9 +480,14 @@ public ArrayList<String> getWhereTokens(String line, Table table) throws SQLSynt
576
480
throw new IndexOutOfBoundsException ("WHERE STATEMENT MALFORMED AT > " + line );
577
481
}
578
482
483
+ for (String token : whereTokens ){
484
+ System .out .println (token );
485
+ }
486
+
579
487
return whereTokens ;
580
488
}
581
489
}
582
490
583
491
// 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