55import android .database .sqlite .SQLiteDatabase ;
66import android .database .sqlite .SQLiteOpenHelper ;
77import android .os .Build ;
8+ import android .util .Log ;
89
910import com .google .gson .Gson ;
1011
2122import com .simplesql .simplesql .annotations .ForeignKey ;
2223import com .simplesql .simplesql .annotations .Key ;
2324import com .simplesql .simplesql .annotations .Table ;
25+ import com .simplesql .simplesql .annotations .Unique ;
2426
2527public class SimpleSQL {
2628 private SQLiteOpenHelper helperBD ;
@@ -249,7 +251,7 @@ public List execute() {
249251 if (functionParameter ) {
250252 if (fields [0 ] == null || fields [0 ].equals ("" ))
251253 columnFunction = "*" ;
252- SQLString = SQLString .replace ("*" ,"" ).replace (KEY_FUNCTION_PARAMETER , fields [0 ]);
254+ SQLString = SQLString .replace ("*" , "" ).replace (KEY_FUNCTION_PARAMETER , fields [0 ]);
253255 }
254256 SQLString = SQLString + ";" ;
255257 List lstClasses = new ArrayList <>();
@@ -266,6 +268,7 @@ public List execute() {
266268 String hashJson = new Gson ().toJson (hashMap );
267269 lstClasses .add (new Gson ().fromJson (hashJson , (Type ) typeObject .getClass ()));
268270 }
271+ cursor .close ();
269272 } catch (Exception e ) {
270273 e .printStackTrace ();
271274 return new ArrayList ();
@@ -486,12 +489,10 @@ public String create(Object obj, SQLiteDatabase db) {
486489 Column column =
487490 field .getAnnotation (Column .class );
488491 if (column != null ) {
489- if (count == 0 ) {
492+ if (count == 0 )
490493 columns += field .getName () + " " + column .type ();
491- } else {
494+ else
492495 columns += " , " + field .getName () + " " + column .type ();
493-
494- }
495496 columns += checkAnnotations (field , column .non_null ());
496497 if (field .isAnnotationPresent (ForeignKey .class ))
497498 foreignKeys .add (field );
@@ -508,6 +509,7 @@ public String create(Object obj, SQLiteDatabase db) {
508509 " REFERENCES " + foreignKey .table ().getSimpleName () + "(" + foreignKey .column () + ")" ;
509510 }
510511 sql += ");" ;
512+ foreignKeys .clear ();
511513 db .execSQL (sql );
512514 return "Table create success" ;
513515 } else
@@ -528,16 +530,20 @@ public String create(Object obj, SQLiteDatabase db) {
528530 public class Insert {
529531 private Object obj ;
530532 private ContentValues values ;
533+ private SQLiteDatabase write ;
534+ private SQLiteDatabase read ;
535+ private String table ;
531536
532537 public Insert (Object obj ) {
533538 this .obj = obj ;
539+ table = obj .getClass ().getSimpleName ();
534540 values = new ContentValues ();
541+ write = helperBD .getWritableDatabase ();
542+ read = helperBD .getReadableDatabase ();
535543 }
536544
537545 public boolean execute () {
538-
539546 try {
540- SQLiteDatabase write = helperBD .getReadableDatabase ();
541547 Table persistable =
542548 obj .getClass ().getAnnotation (Table .class );
543549 if (persistable != null ) {
@@ -551,21 +557,24 @@ public boolean execute() {
551557 if (column != null ) {
552558 if (!field .isAnnotationPresent (AutoIncrement .class ) && field .get (obj ) != null )
553559 checkObject (field , obj );
554- } else
560+ if (field .get (obj ) == null && column .non_null ())
561+ throw new SQLException ("This " + field .getName () + " is not null but is empty" );
562+ } else
555563 throw new SQLException ("The " + field .getName () + "attribute did not have the column annotation" );
556564 }
557- long result = write .insert (obj . getClass (). getSimpleName () , null , values );
565+ long result = write .insert (table , null , values );
558566 return result > -1 ;
559567 } else
560568 throw new SQLException ("This class does not have the table annotation" );
561569 } catch (SQLException e ) {
570+ Log .e ("Insert" , e .getMessage ());
562571 e .printStackTrace ();
563572 return false ;
564573 } catch (Exception e ) {
574+ Log .e ("Insert" , e .getMessage ());
565575 e .printStackTrace ();
566576 return false ;
567577 }
568-
569578 }
570579
571580 private void checkObject (Field field , Object obj ) {
@@ -586,6 +595,16 @@ else if (field.get(obj).getClass() == Short.class)
586595 e .printStackTrace ();
587596 }
588597 }
598+
599+ /* private void getCount(String f) {
600+ String sql = "SELECT IFNULL(MAX(" + f + "),0) + 1 AS id FROM " + table + ";";
601+ Cursor cursor = read.rawQuery(sql, null);
602+ if (cursor.moveToFirst())
603+ values.put(f, cursor.getInt(cursor.getColumnIndex("id")));
604+ else
605+ values.put(f, 1);
606+ }*/
607+
589608 }
590609
591610
@@ -597,7 +616,8 @@ private String checkAnnotations(Field c, boolean not_null) {
597616 annotations += " AUTOINCREMENT" ;
598617 if (not_null && !c .isAnnotationPresent (Key .class ))
599618 annotations += " NOT NULL" ;
600-
619+ if (c .isAnnotationPresent (Unique .class ))
620+ annotations += " UNIQUE" ;
601621 return annotations ;
602622 }
603623
0 commit comments