88import  fr .bastoup .bperipherals .database .DBUtil ;
99import  fr .bastoup .bperipherals .util .peripherals .BPeripheral ;
1010
11- import  java .io .File ;
11+ import  javax .annotation .Nonnull ;
12+ import  java .io .IOException ;
13+ import  java .nio .file .Path ;
1214import  java .util .ArrayList ;
1315import  java .util .HashMap ;
1416import  java .util .List ;
@@ -26,15 +28,15 @@ private TileDatabase getTile() {
2628 return  ((TileDatabase ) tile );
2729 }
2830
31+  @ Nonnull 
2932 @ Override 
3033 public  String  getType () {
3134 return  TYPE ;
3235 }
3336
3437 @ Override 
3538 public  boolean  equals (IPeripheral  other ) {
36-  return  other  instanceof  PeripheralDatabase  && ((TileDatabase ) other .getTarget ()).getWorld ().equals (tile .getWorld ()) &&
37-  ((TileDatabase ) other .getTarget ()).getPos ().equals (tile .getPos ());
39+  return  this  == other  || other  instanceof  PeripheralDatabase  && ((PeripheralDatabase ) other ).tile  == tile ;
3840 }
3941
4042 @ LuaFunction 
@@ -65,19 +67,19 @@ public final void setDatabaseName(String name) throws LuaException {
6567
6668 @ LuaFunction 
6769 public  final  List <Object > executeSQL (String  sql ) throws  LuaException  {
68-  File  file  =  null ;
70+  Path  file ;
6971 try  {
7072 file  = getTile ().getDatabaseFile ();
71-  } catch  (NoSuchFieldException  e ) {
72-  e .printStackTrace ();
73-  } catch  (IllegalAccessException  e ) {
73+  if  (getTile ().isDiskInserted ()) {
74+  return  DBUtil .factorizeResults (BPeripherals .getDBFactory ().executeSQL (file .toString (), sql ));
75+  } else  {
76+  throw  new  LuaException ("There is no disk inserted" );
77+  }
78+  } catch  (IllegalAccessException  | IOException  e ) {
7479 e .printStackTrace ();
80+  throw  new  LuaException ("Internal Error. Please send an issue if the problem persists." );
7581 }
76-  if  (getTile ().isDiskInserted ()) {
77-  return  DBUtil .factorizeResults (BPeripherals .getDBFactory ().executeSQL (file .getPath (), sql ));
78-  } else  {
79-  throw  new  LuaException ("There is no disk inserted" );
80-  }
82+ 
8183 }
8284
8385 @ LuaFunction 
@@ -136,7 +138,7 @@ public static class CCPreparedStatement {
136138
137139 private  void  peripheralStillValid () throws  LuaException  {
138140 TileDatabase  t  = (TileDatabase ) database .getTarget ();
139-  if  (t .isRemoved ())
141+  if  (t  ==  null  ||  t .isRemoved ())
140142 throw  new  LuaException ("The peripheral does not exist." );
141143 }
142144
@@ -160,16 +162,19 @@ public final CCPreparedStatement removeParameter(int index) throws LuaException
160162 public  final  List <Object > execute () throws  LuaException  {
161163 peripheralStillValid ();
162164 TileDatabase  tile  = (TileDatabase ) database .getTarget ();
163-  File  file  = null ;
165+  Path  file ;
166+ 
167+  if  (tile  == null )
168+  throw  new  LuaException ("Internal Error. Please send an issue if the problem persists." );
169+ 
164170 try  {
165171 file  = tile .getDatabaseFile ();
166-  } catch  (NoSuchFieldException  e ) {
167-  e .printStackTrace ();
168-  } catch  (IllegalAccessException  e ) {
172+  } catch  (IllegalAccessException  | IOException  e ) {
169173 e .printStackTrace ();
174+  throw  new  LuaException ("Internal Error. Please send an issue if the problem persists." );
170175 }
171176 if  (tile .isDiskInserted ()) {
172-  return  DBUtil .factorizeResults (BPeripherals .getDBFactory ().executePrepared (file .getPath (), this ));
177+  return  DBUtil .factorizeResults (BPeripherals .getDBFactory ().executePrepared (file .toString (), this ));
173178 } else  {
174179 throw  new  LuaException ("There is no disk inserted" );
175180 }
@@ -198,7 +203,7 @@ public static class CCTableCreator {
198203
199204 private  void  peripheralStillValid () throws  LuaException  {
200205 TileDatabase  t  = (TileDatabase ) database .getTarget ();
201-  if  (t .isRemoved ())
206+  if  (t  ==  null  ||  t .isRemoved ())
202207 throw  new  LuaException ("The peripheral does not exist." );
203208 }
204209
@@ -224,7 +229,7 @@ public final CCTableCreator addColumn(String name, String type, boolean notNull,
224229 }
225230
226231 @ LuaFunction 
227-  public  final  CCTableCreator  removeColumn (String  name ) throws   LuaException   {
232+  public  final  CCTableCreator  removeColumn (String  name ) {
228233 if  (primaryKey .equalsIgnoreCase (name )) {
229234 primaryKey  = null ;
230235 autoIncrement  = false ;
@@ -253,13 +258,16 @@ public final CCTableCreator setPrimaryKey(String name, boolean autoIncrement) th
253258 public  final  List <Object > execute () throws  LuaException  {
254259 peripheralStillValid ();
255260 TileDatabase  tile  = (TileDatabase ) database .getTarget ();
256-  File  file  = null ;
261+ 
262+  if  (tile  == null )
263+  throw  new  LuaException ("Internal Error. Please send an issue if the problem persists." );
264+ 
265+  Path  file ;
257266 try  {
258267 file  = tile .getDatabaseFile ();
259-  } catch  (NoSuchFieldException  e ) {
260-  e .printStackTrace ();
261-  } catch  (IllegalAccessException  e ) {
268+  } catch  (IllegalAccessException  | IOException  e ) {
262269 e .printStackTrace ();
270+  throw  new  LuaException ("Internal Error. Please send an issue if the problem persists." );
263271 }
264272 if  (tile .isDiskInserted ()) {
265273 if  (this .primaryKey  == null ) {
@@ -281,7 +289,7 @@ public final List<Object> execute() throws LuaException {
281289 statements .add (statement );
282290 }
283291 String  sql  = "CREATE TABLE "  + tableName  + " ("  + String .join (", " , statements ) + ");" ;
284-  return  DBUtil .factorizeResults (BPeripherals .getDBFactory ().executeSQL (file .getPath (), sql ));
292+  return  DBUtil .factorizeResults (BPeripherals .getDBFactory ().executeSQL (file .toString (), sql ));
285293 } else  {
286294 throw  new  LuaException ("There is no disk inserted" );
287295 }
@@ -300,18 +308,18 @@ public static class CCInsert {
300308
301309 private  void  peripheralStillValid () throws  LuaException  {
302310 TileDatabase  t  = (TileDatabase ) database .getTarget ();
303-  if  (t .isRemoved ())
311+  if  (t  ==  null  ||  t .isRemoved ())
304312 throw  new  LuaException ("The peripheral does not exist." );
305313 }
306314
307315 @ LuaFunction 
308-  public  final  CCInsert  addValue (String  column , Object  value ) throws   LuaException   {
316+  public  final  CCInsert  addValue (String  column , Object  value ) {
309317 values .put (column .toLowerCase (), value );
310318 return  this ;
311319 }
312320
313321 @ LuaFunction 
314-  public  final  CCInsert  removeValue (String  column ) throws   LuaException   {
322+  public  final  CCInsert  removeValue (String  column ) {
315323 values .remove (column );
316324 return  this ;
317325 }
@@ -321,7 +329,7 @@ public final CCInsert removeValue(String column) throws LuaException {
321329 public  final  List <Object > execute () throws  LuaException  {
322330 peripheralStillValid ();
323331 TileDatabase  tile  = (TileDatabase ) database .getTarget ();
324-  if  (tile .isDiskInserted ()) {
332+  if  (tile  !=  null  &&  tile .isDiskInserted ()) {
325333 List <String > s  = new  ArrayList <>();
326334 Map <Integer , Object > obj  = new  HashMap <>();
327335 Object [] keys  = values .keySet ().toArray ();
@@ -351,18 +359,18 @@ public static class CCSelect {
351359
352360 private  void  peripheralStillValid () throws  LuaException  {
353361 TileDatabase  t  = (TileDatabase ) database .getTarget ();
354-  if  (t .isRemoved ())
362+  if  (t  ==  null  ||  t .isRemoved ())
355363 throw  new  LuaException ("The peripheral does not exist." );
356364 }
357365
358366 @ LuaFunction 
359-  public  final  CCSelect  addCondition (String  column , Object  value ) throws   LuaException   {
367+  public  final  CCSelect  addCondition (String  column , Object  value ) {
360368 conditions .put (column .toLowerCase (), value );
361369 return  this ;
362370 }
363371
364372 @ LuaFunction 
365-  public  final  CCSelect  removeCondition (String  column ) throws   LuaException   {
373+  public  final  CCSelect  removeCondition (String  column ) {
366374 conditions .remove (column );
367375 return  this ;
368376 }
@@ -372,7 +380,7 @@ public final CCSelect removeCondition(String column) throws LuaException {
372380 public  final  List <Object > execute () throws  LuaException  {
373381 peripheralStillValid ();
374382 TileDatabase  tile  = (TileDatabase ) database .getTarget ();
375-  if  (tile .isDiskInserted ()) {
383+  if  (tile  !=  null  &&  tile .isDiskInserted ()) {
376384 Map <Integer , Object > obj  = new  HashMap <>();
377385 List <String > k  = new  ArrayList <>();
378386 Object [] keys  = conditions .keySet ().toArray ();
@@ -400,18 +408,18 @@ public static class CCDelete {
400408
401409 private  void  peripheralStillValid () throws  LuaException  {
402410 TileDatabase  t  = (TileDatabase ) database .getTarget ();
403-  if  (t .isRemoved ())
411+  if  (t  ==  null  ||  t .isRemoved ())
404412 throw  new  LuaException ("The peripheral does not exist." );
405413 }
406414
407415 @ LuaFunction 
408-  public  final  CCDelete  addCondition (String  column , Object  value ) throws   LuaException   {
416+  public  final  CCDelete  addCondition (String  column , Object  value ) {
409417 conditions .put (column .toLowerCase (), value );
410418 return  this ;
411419 }
412420
413421 @ LuaFunction 
414-  public  final  CCDelete  removeCondition (String  column ) throws   LuaException   {
422+  public  final  CCDelete  removeCondition (String  column ) {
415423 conditions .remove (column );
416424 return  this ;
417425 }
@@ -421,7 +429,7 @@ public final CCDelete removeCondition(String column) throws LuaException {
421429 public  final  List <Object > execute () throws  LuaException  {
422430 peripheralStillValid ();
423431 TileDatabase  tile  = (TileDatabase ) database .getTarget ();
424-  if  (tile .isDiskInserted ()) {
432+  if  (tile  !=  null  &&  tile .isDiskInserted ()) {
425433 Map <Integer , Object > obj  = new  HashMap <>();
426434 List <String > k  = new  ArrayList <>();
427435 Object [] keys  = conditions .keySet ().toArray ();
0 commit comments