diff --git a/example/src/main/java/com/example/models/BooleanFieldExtendedModel.java b/example/src/main/java/com/example/models/BooleanFieldExtendedModel.java deleted file mode 100644 index 972855cd..00000000 --- a/example/src/main/java/com/example/models/BooleanFieldExtendedModel.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.example.models; - -import com.orm.SugarRecord; - -public class BooleanFieldExtendedModel extends SugarRecord { - private Boolean objectBoolean; - private boolean rawBoolean; - - public BooleanFieldExtendedModel() {} - - public BooleanFieldExtendedModel(Boolean objectBoolean) { - this.objectBoolean = objectBoolean; - } - - public BooleanFieldExtendedModel(boolean rawBoolean) { - this.rawBoolean = rawBoolean; - } - - public Boolean getBoolean() { - return objectBoolean; - } - - public boolean getRawBoolean() { - return rawBoolean; - } -} diff --git a/example/src/main/java/com/example/models/PrimaryKeyNotationBigDecimalFieldAnnotatedModel.java b/example/src/main/java/com/example/models/PrimaryKeyNotationBigDecimalFieldAnnotatedModel.java new file mode 100644 index 00000000..23788a7a --- /dev/null +++ b/example/src/main/java/com/example/models/PrimaryKeyNotationBigDecimalFieldAnnotatedModel.java @@ -0,0 +1,28 @@ +package com.example.models; + +import com.orm.dsl.PrimaryKey; +import com.orm.dsl.Table; + +import java.math.BigDecimal; + +@Table +public class PrimaryKeyNotationBigDecimalFieldAnnotatedModel{ + + @PrimaryKey + private Long myId; + private BigDecimal decimal; + + public PrimaryKeyNotationBigDecimalFieldAnnotatedModel() {} + + public PrimaryKeyNotationBigDecimalFieldAnnotatedModel(BigDecimal decimal) { + this.decimal = decimal; + } + + public BigDecimal getBigDecimal() { + return decimal; + } + + public Long getMyId() { + return myId; + } +} diff --git a/example/src/main/java/com/example/models/PrimaryKeyNotationBooleanFieldAnnotatedModel.java b/example/src/main/java/com/example/models/PrimaryKeyNotationBooleanFieldAnnotatedModel.java new file mode 100644 index 00000000..1e6dbae8 --- /dev/null +++ b/example/src/main/java/com/example/models/PrimaryKeyNotationBooleanFieldAnnotatedModel.java @@ -0,0 +1,29 @@ +package com.example.models; + +import com.orm.dsl.PrimaryKey; +import com.orm.dsl.Table; + +@Table +public class PrimaryKeyNotationBooleanFieldAnnotatedModel{ + private Boolean objectBoolean; + private boolean rawBoolean; + @PrimaryKey private Long myId; + + public PrimaryKeyNotationBooleanFieldAnnotatedModel(){} + + public PrimaryKeyNotationBooleanFieldAnnotatedModel(Boolean objectBoolean){ + this.objectBoolean = objectBoolean; + } + + public PrimaryKeyNotationBooleanFieldAnnotatedModel(boolean rawBoolean) { + this.rawBoolean = rawBoolean; + } + + public Boolean getBoolean() { + return objectBoolean; + } + + public boolean getRawBoolean() { + return rawBoolean; + } +} diff --git a/example/src/main/java/com/example/models/PrimaryKeyNotationRelationshipAnnotatedModel.java b/example/src/main/java/com/example/models/PrimaryKeyNotationRelationshipAnnotatedModel.java new file mode 100644 index 00000000..7e538d51 --- /dev/null +++ b/example/src/main/java/com/example/models/PrimaryKeyNotationRelationshipAnnotatedModel.java @@ -0,0 +1,24 @@ +package com.example.models; + +import com.orm.dsl.PrimaryKey; +import com.orm.dsl.Table; + +@Table +public class PrimaryKeyNotationRelationshipAnnotatedModel{ + private SimpleAnnotatedModel simple; + @PrimaryKey private Long myId; + + public PrimaryKeyNotationRelationshipAnnotatedModel(){} + + public PrimaryKeyNotationRelationshipAnnotatedModel(SimpleAnnotatedModel simple){ + this.simple = simple; + } + + public SimpleAnnotatedModel getSimple(){ + return simple; + } + + public Long getMyId(){ + return myId; + } +} diff --git a/example/src/main/java/com/example/models/PrimaryKeyNotationRelationshipExtendedModel.java b/example/src/main/java/com/example/models/PrimaryKeyNotationRelationshipExtendedModel.java new file mode 100644 index 00000000..461f37ac --- /dev/null +++ b/example/src/main/java/com/example/models/PrimaryKeyNotationRelationshipExtendedModel.java @@ -0,0 +1,26 @@ +package com.example.models; + + +import com.orm.SugarRecord; +import com.orm.dsl.PrimaryKey; + +public class PrimaryKeyNotationRelationshipExtendedModel extends SugarRecord{ + + @PrimaryKey private Long myId; + + private SimpleExtendedModel simple; + + public PrimaryKeyNotationRelationshipExtendedModel(){} + + public PrimaryKeyNotationRelationshipExtendedModel(SimpleExtendedModel simple){ + this.simple = simple; + } + + public SimpleExtendedModel getSimple(){ + return simple; + } + + public Long getMyId(){ + return myId; + } +} diff --git a/example/src/main/java/com/example/models/PrimaryKeyNotationSimpleAnnotatedModel.java b/example/src/main/java/com/example/models/PrimaryKeyNotationSimpleAnnotatedModel.java new file mode 100644 index 00000000..e539238b --- /dev/null +++ b/example/src/main/java/com/example/models/PrimaryKeyNotationSimpleAnnotatedModel.java @@ -0,0 +1,16 @@ +package com.example.models; + +import com.orm.dsl.PrimaryKey; +import com.orm.dsl.Table; + +@Table +public class PrimaryKeyNotationSimpleAnnotatedModel{ + + @PrimaryKey private Long myId; + + public PrimaryKeyNotationSimpleAnnotatedModel(){} + + public Long getMyId(){ + return myId; + } +} diff --git a/example/src/main/java/com/example/models/PrimaryKeyNotationSimpleModel.java b/example/src/main/java/com/example/models/PrimaryKeyNotationSimpleModel.java new file mode 100644 index 00000000..df2107c6 --- /dev/null +++ b/example/src/main/java/com/example/models/PrimaryKeyNotationSimpleModel.java @@ -0,0 +1,40 @@ +package com.example.models; + +import com.orm.SugarRecord; +import com.orm.dsl.PrimaryKey; + +public class PrimaryKeyNotationSimpleModel extends SugarRecord{ + @PrimaryKey + private Long myId; + private String str; + private int integer; + private boolean bool; + + public String getStr(){ + return str; + } + + public void setStr(String str){ + this.str = str; + } + + public int getInteger(){ + return integer; + } + + public void setInteger(int integer){ + this.integer = integer; + } + + public boolean isBool(){ + return bool; + } + + public void setBool(boolean bool){ + this.bool = bool; + } + + public Long getMyId(){ return myId; } + + public void setMyId(Long myId){ this.myId = myId; } +} diff --git a/example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java b/example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java index 9e4b36bb..b17095d3 100644 --- a/example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java +++ b/example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java @@ -11,31 +11,30 @@ import java.math.BigDecimal; + import static com.orm.SugarRecord.save; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class BigDecimalFieldTests { +@RunWith (RobolectricGradleTestRunner.class) +@Config (sdk = 18) +public class BigDecimalFieldTests{ @Test - public void nullBigDecimalExtendedTest() { + public void nullBigDecimalExtendedTest(){ save(new BigDecimalFieldExtendedModel()); - BigDecimalFieldExtendedModel model = - SugarRecord.findById(BigDecimalFieldExtendedModel.class, 1); + BigDecimalFieldExtendedModel model = SugarRecord.findById(BigDecimalFieldExtendedModel.class, 1); assertNull(model.getBigDecimal()); } @Test - public void nullBigDecimalAnnotatedTest() { + public void nullBigDecimalAnnotatedTest(){ save(new BigDecimalFieldAnnotatedModel()); - BigDecimalFieldAnnotatedModel model = - SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); + BigDecimalFieldAnnotatedModel model = SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); assertNull(model.getBigDecimal()); } @Test - public void bigDecimalExtendedTest() { + public void bigDecimalExtendedTest(){ BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); save(new BigDecimalFieldExtendedModel(decimal)); BigDecimalFieldExtendedModel model = SugarRecord.findById(BigDecimalFieldExtendedModel.class, 1); @@ -43,11 +42,11 @@ public void bigDecimalExtendedTest() { } @Test - public void bigDecimalAnnotatedTest() { + public void bigDecimalAnnotatedTest(){ BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); save(new BigDecimalFieldAnnotatedModel(decimal)); - BigDecimalFieldAnnotatedModel model = - SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); + BigDecimalFieldAnnotatedModel model = SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); assertEquals(decimal, model.getBigDecimal()); } + } diff --git a/example/src/test/java/com/example/sugartest/BooleanFieldTests.java b/example/src/test/java/com/example/sugartest/BooleanFieldTests.java index a117af6c..3625bed3 100644 --- a/example/src/test/java/com/example/sugartest/BooleanFieldTests.java +++ b/example/src/test/java/com/example/sugartest/BooleanFieldTests.java @@ -2,65 +2,65 @@ import com.example.models.BooleanFieldAnnotatedModel; -import com.example.models.BooleanFieldExtendedModel; import com.orm.SugarRecord; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.annotation.Config; + import static com.orm.SugarRecord.save; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class BooleanFieldTests { +@RunWith (RobolectricGradleTestRunner.class) +@Config (sdk = 18) +public class BooleanFieldTests{ @Test - public void nullBooleanExtendedTest() { - save(new BooleanFieldExtendedModel()); - BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); + public void nullBooleanExtendedTest(){ + save(new BooleanFieldAnnotatedModel()); + BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertNull(model.getBoolean()); } @Test - public void nullRawBooleanExtendedTest() { - save(new BooleanFieldExtendedModel()); - BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); + public void nullRawBooleanExtendedTest(){ + save(new BooleanFieldAnnotatedModel()); + BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(false, model.getRawBoolean()); } @Test - public void nullBooleanAnnotatedTest() { + public void nullBooleanAnnotatedTest(){ save(new BooleanFieldAnnotatedModel()); BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertNull(model.getBoolean()); } @Test - public void nullRawBooleanAnnotatedTest() { + public void nullRawBooleanAnnotatedTest(){ save(new BooleanFieldAnnotatedModel()); BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(false, model.getRawBoolean()); } @Test - public void objectBooleanExtendedTest() { + public void objectBooleanExtendedTest(){ Boolean objectBoolean = new Boolean(true); - save(new BooleanFieldExtendedModel(objectBoolean)); - BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); + save(new BooleanFieldAnnotatedModel(objectBoolean)); + BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(objectBoolean, model.getBoolean()); } @Test - public void rawBooleanExtendedTest() { - save(new BooleanFieldExtendedModel(true)); - BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); + public void rawBooleanExtendedTest(){ + save(new BooleanFieldAnnotatedModel(true)); + BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(true, model.getRawBoolean()); } @Test - public void objectBooleanAnnotatedTest() { + public void objectBooleanAnnotatedTest(){ Boolean objectBoolean = new Boolean(true); save(new BooleanFieldAnnotatedModel(objectBoolean)); BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); @@ -68,9 +68,10 @@ public void objectBooleanAnnotatedTest() { } @Test - public void rawBooleanAnnotatedTest() { + public void rawBooleanAnnotatedTest(){ save(new BooleanFieldAnnotatedModel(true)); BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(true, model.getRawBoolean()); } + } diff --git a/example/src/test/java/com/example/sugartest/PrimaryKeyNotationBigDecimalFieldTests.java b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationBigDecimalFieldTests.java new file mode 100644 index 00000000..620ab84c --- /dev/null +++ b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationBigDecimalFieldTests.java @@ -0,0 +1,47 @@ +package com.example.sugartest; + + +import com.example.models.PrimaryKeyNotationBigDecimalFieldAnnotatedModel; +import com.orm.SugarRecord; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +import java.math.BigDecimal; + + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith (RobolectricGradleTestRunner.class) +@Config (sdk = 18) +public class PrimaryKeyNotationBigDecimalFieldTests{ + + @Test + public void primryKeyNotationNullBigDecimalExtendedTest(){ + save(new PrimaryKeyNotationBigDecimalFieldAnnotatedModel()); + PrimaryKeyNotationBigDecimalFieldAnnotatedModel model = + SugarRecord.findById(PrimaryKeyNotationBigDecimalFieldAnnotatedModel.class, 1); + assertNull(model.getBigDecimal()); + } + + @Test + public void primryKeyNotationBigDecimalExtendedTest(){ + BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); + save(new PrimaryKeyNotationBigDecimalFieldAnnotatedModel(decimal)); + PrimaryKeyNotationBigDecimalFieldAnnotatedModel model = + SugarRecord.findById(PrimaryKeyNotationBigDecimalFieldAnnotatedModel.class, 1); + assertEquals(decimal, model.getBigDecimal()); + } + + @Test + public void primryKeyNotationBigDecimalAnnotatedTest(){ + BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); + save(new PrimaryKeyNotationBigDecimalFieldAnnotatedModel(decimal)); + PrimaryKeyNotationBigDecimalFieldAnnotatedModel model = + SugarRecord.findById(PrimaryKeyNotationBigDecimalFieldAnnotatedModel.class, 1); + assertEquals(decimal, model.getBigDecimal()); + } +} diff --git a/example/src/test/java/com/example/sugartest/PrimaryKeyNotationBooleanFieldTests.java b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationBooleanFieldTests.java new file mode 100644 index 00000000..0fc04fce --- /dev/null +++ b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationBooleanFieldTests.java @@ -0,0 +1,77 @@ +package com.example.sugartest; + + +import com.example.models.PrimaryKeyNotationBooleanFieldAnnotatedModel; +import com.orm.SugarRecord; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith (RobolectricGradleTestRunner.class) +@Config (sdk = 18) +public class PrimaryKeyNotationBooleanFieldTests{ + + @Test + public void primaryKeyNullBooleanExtendedTest(){ + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertNull(model.getBoolean()); + } + + @Test + public void primaryKeynullRawBooleanExtendedTest(){ + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(false, model.getRawBoolean()); + } + + @Test + public void primaryKeyNotationNullBooleanAnnotatedTest(){ + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertNull(model.getBoolean()); + } + + @Test + public void primaryKeyNotationNullRawBooleanAnnotatedTest(){ + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(false, model.getRawBoolean()); + } + + @Test + public void primaryKeyNotationObjectBooleanExtendedTest(){ + Boolean objectBoolean = new Boolean(true); + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(objectBoolean)); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(objectBoolean, model.getBoolean()); + } + + @Test + public void primaryKeyNotationRawBooleanExtendedTest(){ + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(true)); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(true, model.getRawBoolean()); + } + + @Test + public void primaryKeyNotationObjectBooleanAnnotatedTest(){ + Boolean objectBoolean = new Boolean(true); + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(objectBoolean)); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(objectBoolean, model.getBoolean()); + } + + @Test + public void primaryKeyNotationRawBooleanAnnotatedTest(){ + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(true)); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(true, model.getRawBoolean()); + } +} diff --git a/example/src/test/java/com/example/sugartest/PrimaryKeyNotationCursorTests.java b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationCursorTests.java new file mode 100644 index 00000000..42d7e48e --- /dev/null +++ b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationCursorTests.java @@ -0,0 +1,72 @@ +package com.example.sugartest; + +import android.annotation.TargetApi; +import android.content.Context; +import android.database.Cursor; +import android.os.Build; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CursorAdapter; +import android.widget.TextView; + +import com.example.models.PrimaryKeyNotationSimpleModel; +import com.orm.query.Select; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + + +import static com.orm.SugarRecord.save; +import static junit.framework.Assert.assertNotSame; +import static junit.framework.Assert.assertSame; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18) +public class PrimaryKeyNotationCursorTests{ + @Test + public void testColumnNames() { + save(new PrimaryKeyNotationSimpleModel()); + Cursor c = Select.from(PrimaryKeyNotationSimpleModel.class).getCursor(); + for (String col : new String[]{"STR", "INTEGER", "BOOL", "MY_ID"}) { + assertNotSame("Missing column for field: " + col, -1, c.getColumnIndex(col)); + } + } + @Test + public void testSugarCursor(){ + save(new PrimaryKeyNotationSimpleModel()); + Cursor cursor = Select.from(PrimaryKeyNotationSimpleModel.class).getCursor(); + assertNotSame("No _MY_ID", -1, cursor.getColumnIndex("MY_ID")); + assertNotSame("_MY_ID != MY_ID", cursor.getColumnIndex("_MY_ID"), cursor.getColumnIndex("MY_ID")); + } + + @Test + public void testNoColumn(){ + save(new PrimaryKeyNotationSimpleModel()); + Cursor cursor = Select.from(PrimaryKeyNotationSimpleModel.class).getCursor(); + assertSame(-1, cursor.getColumnIndex("nonexistent")); + } + + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + @Test + public void testMakeAdapter() { + save(new PrimaryKeyNotationSimpleModel()); + Cursor c = Select.from(PrimaryKeyNotationSimpleModel.class).getCursor(); + CursorAdapter adapter = new CursorAdapter(RuntimeEnvironment.application, c, true) { + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + TextView tv = new TextView(context); + String s = cursor.getString(cursor.getColumnIndex("STR")); + tv.setText(s); + return null; + } + + @Override + public void bindView(View view, Context context, Cursor cursor) { + String s = cursor.getString(cursor.getColumnIndex("STR")); + ((TextView) view).setText(s); + } + }; + } +} diff --git a/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipAnnotatedTests.java b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipAnnotatedTests.java new file mode 100644 index 00000000..0b3d432b --- /dev/null +++ b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipAnnotatedTests.java @@ -0,0 +1,107 @@ +package com.example.sugartest; + +import com.example.models.PrimaryKeyNotationRelationshipAnnotatedModel; +import com.example.models.SimpleAnnotatedModel; +import com.orm.SugarRecord; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +import java.util.List; + + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; + + +@RunWith (RobolectricGradleTestRunner.class) +@Config (sdk = 18) +public class PrimaryKeyNotationRelationshipAnnotatedTests{ + @Test + public void emptyDatabaseTest() throws Exception{ + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationRelationshipAnnotatedModel.class)); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception{ + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationRelationshipAnnotatedModel.class)); + } + + @Test + public void twoSameSaveTest() throws Exception{ + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(2L, SugarRecord.count(PrimaryKeyNotationRelationshipAnnotatedModel.class)); + } + + @Test + public void twoDifferentSaveTest() throws Exception{ + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); + save(another_simple); + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + save(new PrimaryKeyNotationRelationshipAnnotatedModel(another_simple)); + assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(2L, SugarRecord.count(PrimaryKeyNotationRelationshipAnnotatedModel.class)); + } + + @Test + public void manySameSaveTest() throws Exception{ + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + } + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(100L, SugarRecord.count(PrimaryKeyNotationRelationshipAnnotatedModel.class)); + } + + @Test + public void manyDifferentSaveTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + } + assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(100L, SugarRecord.count(PrimaryKeyNotationRelationshipAnnotatedModel.class)); + } + + @Test + public void listAllSameTest() throws Exception{ + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + } + List models = SugarRecord.listAll(PrimaryKeyNotationRelationshipAnnotatedModel.class); + assertEquals(100, models.size()); + for(PrimaryKeyNotationRelationshipAnnotatedModel model : models){ + assertEquals(simple.getId(), model.getSimple().getId()); + } + } + + @Test + public void listAllDifferentTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + } + List models = SugarRecord.listAll(PrimaryKeyNotationRelationshipAnnotatedModel.class); + assertEquals(100, models.size()); + for(PrimaryKeyNotationRelationshipAnnotatedModel model : models){ + assertEquals(model.getMyId(), model.getSimple().getId()); + } + } +} diff --git a/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipExtendedTests.java b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipExtendedTests.java new file mode 100644 index 00000000..fd649422 --- /dev/null +++ b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipExtendedTests.java @@ -0,0 +1,109 @@ +package com.example.sugartest; + +import com.example.models.PrimaryKeyNotationRelationshipExtendedModel; +import com.example.models.SimpleExtendedModel; +import com.orm.SugarRecord; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +import java.util.List; + + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; + + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18) +public class PrimaryKeyNotationRelationshipExtendedTests{ + @Test + public void emptyDatabaseTest() throws Exception { + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationRelationshipExtendedModel.class)); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationRelationshipExtendedModel.class)); + } + + @Test + public void twoSameSaveTest() throws Exception { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(2L, SugarRecord.count(PrimaryKeyNotationRelationshipExtendedModel.class)); + } + + @Test + public void twoDifferentSaveTest() throws Exception { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + SimpleExtendedModel another_simple = new SimpleExtendedModel(); + save(another_simple); + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + save(new PrimaryKeyNotationRelationshipExtendedModel(another_simple)); + assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(2L, SugarRecord.count(PrimaryKeyNotationRelationshipExtendedModel.class)); + } + + @Test + public void manySameSaveTest() throws Exception { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + for (int i = 1; i <= 100; i++) { + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + } + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(100L, SugarRecord.count(PrimaryKeyNotationRelationshipExtendedModel.class)); + } + + @Test + public void manyDifferentSaveTest() throws Exception { + for (int i = 1; i <= 100; i++) { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + } + assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(100L, SugarRecord.count(PrimaryKeyNotationRelationshipExtendedModel.class)); + } + + @Test + public void listAllSameTest() throws Exception { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + for (int i = 1; i <= 100; i++) { + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + } + List models = + SugarRecord.listAll(PrimaryKeyNotationRelationshipExtendedModel.class); + assertEquals(100, models.size()); + for (PrimaryKeyNotationRelationshipExtendedModel model : models) { + assertEquals(simple.getId(), model.getSimple().getId()); + } + } + + @Test + public void listAllDifferentTest() throws Exception { + for (int i = 1; i <= 100; i++) { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + } + List models = + SugarRecord.listAll(PrimaryKeyNotationRelationshipExtendedModel.class); + assertEquals(100, models.size()); + for (PrimaryKeyNotationRelationshipExtendedModel model : models) { + assertEquals(model.getMyId(), model.getSimple().getId()); + } + } +} diff --git a/example/src/test/java/com/example/sugartest/PrimaryKeySimpleAnnotatedModelTests.java b/example/src/test/java/com/example/sugartest/PrimaryKeySimpleAnnotatedModelTests.java new file mode 100644 index 00000000..bdcb1834 --- /dev/null +++ b/example/src/test/java/com/example/sugartest/PrimaryKeySimpleAnnotatedModelTests.java @@ -0,0 +1,356 @@ +package com.example.sugartest; + +import com.example.models.PrimaryKeyNotationSimpleAnnotatedModel; +import com.orm.SugarRecord; +import com.orm.util.NamingHelper; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; + + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + + +@RunWith (RobolectricGradleTestRunner.class) +@Config (sdk = 18) +public class PrimaryKeySimpleAnnotatedModelTests{ + @Test + public void emptyDatabaseTest() throws Exception{ + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void twoSaveTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(2L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void manySaveTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + assertEquals(100L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void defaultIdTest() throws Exception{ + assertEquals(1L, save(new PrimaryKeyNotationSimpleAnnotatedModel())); + } + + @Test + public void whereCountTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id = ?", new String[] {"1"})); + } + + @Test + public void whereNoCountTest() throws Exception{ + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id = ?", new String[] {"1"})); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id = ?", new String[] {"3"})); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id = ?", new String[] {"a"})); + } + + @Test + public void whereBrokenCountTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(-1L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class, "di = ?", new String[] {"1"})); + } + + @Test + public void deleteTest() throws Exception{ + PrimaryKeyNotationSimpleAnnotatedModel model = new PrimaryKeyNotationSimpleAnnotatedModel(); + save(model); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + assertTrue(SugarRecord.delete(model)); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void deleteUnsavedTest() throws Exception{ + PrimaryKeyNotationSimpleAnnotatedModel model = new PrimaryKeyNotationSimpleAnnotatedModel(); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + assertFalse(SugarRecord.delete(model)); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void deleteWrongTest() throws Exception{ + PrimaryKeyNotationSimpleAnnotatedModel model = new PrimaryKeyNotationSimpleAnnotatedModel(); + save(model); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + Field idField = model.getClass().getDeclaredField("myId"); + idField.setAccessible(true); + idField.set(model, Long.MAX_VALUE); + assertFalse(SugarRecord.delete(model)); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void deleteAllTest() throws Exception{ + int elementNumber = 100; + for(int i = 1; i <= elementNumber; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + assertEquals(elementNumber, SugarRecord.deleteAll(PrimaryKeyNotationSimpleAnnotatedModel.class)); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void deleteAllWhereTest() throws Exception{ + int elementNumber = 100; + for(int i = 1; i <= elementNumber; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + assertEquals(elementNumber - 1, SugarRecord.deleteAll(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id > ?", new String[] + {"1"})); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void deleteInTransactionFewTest() throws Exception{ + PrimaryKeyNotationSimpleAnnotatedModel first = new PrimaryKeyNotationSimpleAnnotatedModel(); + PrimaryKeyNotationSimpleAnnotatedModel second = new PrimaryKeyNotationSimpleAnnotatedModel(); + PrimaryKeyNotationSimpleAnnotatedModel third = new PrimaryKeyNotationSimpleAnnotatedModel(); + save(first); + save(second); + // Not saving last model + assertEquals(2L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + assertEquals(2, SugarRecord.deleteInTx(first, second, third)); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void deleteInTransactionManyTest() throws Exception{ + long elementNumber = 100; + List models = new ArrayList<>(); + for(int i = 1; i <= elementNumber; i++){ + PrimaryKeyNotationSimpleAnnotatedModel model = new PrimaryKeyNotationSimpleAnnotatedModel(); + models.add(model); + // Not saving last model + if(i < elementNumber){ + save(model); + } + } + assertEquals(elementNumber - 1, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + assertEquals(elementNumber - 1, SugarRecord.deleteInTx(models)); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void saveInTransactionTest() throws Exception{ + SugarRecord.saveInTx(new PrimaryKeyNotationSimpleAnnotatedModel(), new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(2L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void listAllTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + List models = SugarRecord.listAll(PrimaryKeyNotationSimpleAnnotatedModel.class); + assertEquals(100, models.size()); + for(long i = 1; i <= 100; i++){ + assertEquals(new Long(i), models.get((int) i - 1).getMyId()); + } + } + + @Test + public void findTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + List models = SugarRecord.find(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id = ?", "2"); + assertEquals(1, models.size()); + assertEquals(new Long(2L), models.get(0).getMyId()); + } + + @Test + public void findWithQueryTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + List models = + SugarRecord.findWithQuery(PrimaryKeyNotationSimpleAnnotatedModel.class, "Select * from " + + NamingHelper.toSQLName( + PrimaryKeyNotationSimpleAnnotatedModel.class) + + " where my_id >= ? ", "50"); + for(PrimaryKeyNotationSimpleAnnotatedModel model : models){ + assertEquals(new Long(75), model.getMyId(), 25L); + } + } + + @Test + public void findByIdTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(new Long(1L), SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, 1L).getMyId()); + } + + @Test + public void findByIdIntegerTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(new Long(1L), SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, 1).getMyId()); + } + + @Test + public void findByIdStringsNullTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(0, SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, new String[] {""}).size()); + } + + @Test + public void findByIdStringsOneTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + List models = + SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, new String[] {"1"}); + assertEquals(1, models.size()); + assertEquals(new Long(1L), models.get(0).getMyId()); + } + + @Test + public void findByIdStringsTwoTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + List models = + SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, new String[] {"1", "3"}); + assertEquals(2, models.size()); + assertEquals(new Long(1L), models.get(0).getMyId()); + assertEquals(new Long(3L), models.get(1).getMyId()); + } + + @Test + public void findByIdStringsManyTest() throws Exception{ + for(int i = 1; i <= 10; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + List models = + SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, new String[] {"1", "3", "6", "10"}); + assertEquals(4, models.size()); + assertEquals(new Long(1L), models.get(0).getMyId()); + assertEquals(new Long(3L), models.get(1).getMyId()); + assertEquals(new Long(6L), models.get(2).getMyId()); + assertEquals(new Long(10L), models.get(3).getMyId()); + } + + @Test + public void findByIdStringsOrderTest() throws Exception{ + for(int i = 1; i <= 10; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + List models = + SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, new String[] {"10", "6", "3", "1"}); + assertEquals(4, models.size()); + // The order of the query doesn't matter + assertEquals(new Long(1L), models.get(0).getMyId()); + assertEquals(new Long(3L), models.get(1).getMyId()); + assertEquals(new Long(6L), models.get(2).getMyId()); + assertEquals(new Long(10L), models.get(3).getMyId()); + } + + @Test + public void findByIdNullTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertNull(SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, 2L)); + } + + @Test + public void findAllTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + Iterator cursor = SugarRecord.findAll(PrimaryKeyNotationSimpleAnnotatedModel.class); + for(int i = 1; i <= 100; i++){ + assertTrue(cursor.hasNext()); + PrimaryKeyNotationSimpleAnnotatedModel model = cursor.next(); + assertNotNull(model); + assertEquals(new Long(i), model.getMyId()); + } + } + + @Test + public void findAsIteratorTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + Iterator cursor = + SugarRecord.findAsIterator(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id >= ?", "50"); + for(int i = 50; i <= 100; i++){ + assertTrue(cursor.hasNext()); + PrimaryKeyNotationSimpleAnnotatedModel model = cursor.next(); + assertNotNull(model); + assertEquals(new Long(i), model.getMyId()); + } + } + + @Test + public void findWithQueryAsIteratorTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + Iterator cursor = + SugarRecord.findWithQueryAsIterator(PrimaryKeyNotationSimpleAnnotatedModel.class, "Select * from " + + NamingHelper.toSQLName( + PrimaryKeyNotationSimpleAnnotatedModel.class) + + " where my_id >= ? ", "50"); + for(int i = 50; i <= 100; i++){ + assertTrue(cursor.hasNext()); + PrimaryKeyNotationSimpleAnnotatedModel model = cursor.next(); + assertNotNull(model); + assertEquals(new Long(i), model.getMyId()); + } + } + + @Test (expected = NoSuchElementException.class) + public void findAsIteratorOutOfBoundsTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + Iterator cursor = + SugarRecord.findAsIterator(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id = ?", "1"); + assertTrue(cursor.hasNext()); + PrimaryKeyNotationSimpleAnnotatedModel model = cursor.next(); + assertNotNull(model); + assertEquals(new Long(1), model.getMyId()); + // This should throw a NoSuchElementException + cursor.next(); + } + + @Test (expected = UnsupportedOperationException.class) + public void disallowRemoveCursorTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + Iterator cursor = + SugarRecord.findAsIterator(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id = ?", "1"); + assertTrue(cursor.hasNext()); + PrimaryKeyNotationSimpleAnnotatedModel model = cursor.next(); + assertNotNull(model); + assertEquals(new Long(1), model.getMyId()); + // This should throw a UnsupportedOperationException + cursor.remove(); + } + + @Test + public void vacuumTest() throws Exception{ + SugarRecord.executeQuery("Vacuum"); + } +} \ No newline at end of file diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index f5fd4f2a..ad3b3511 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -9,6 +9,7 @@ import com.orm.dsl.Column; import com.orm.dsl.MultiUnique; import com.orm.dsl.NotNull; +import com.orm.dsl.PrimaryKey; import com.orm.dsl.Unique; import com.orm.util.MigrationFileParser; import com.orm.util.NamingHelper; @@ -181,15 +182,32 @@ protected String createTableSQL(Class table) { List fields = ReflectionUtil.getTableFields(table); String tableName = NamingHelper.toSQLName(table); StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS "); - sb.append(tableName).append(" ( ID INTEGER PRIMARY KEY AUTOINCREMENT "); + + Field idField = null; + for (Field column : fields) { + if (column.isAnnotationPresent(PrimaryKey.class)) { + idField = column; + break; + } + } + + sb.append(tableName).append((idField == null)?" ( ID INTEGER PRIMARY KEY AUTOINCREMENT " + :" ( "+NamingHelper.toSQLName(idField)+" INTEGER PRIMARY KEY AUTOINCREMENT "); for (Field column : fields) { String columnName = NamingHelper.toSQLName(column); String columnType = QueryBuilder.getColumnType(column.getType()); if (columnType != null) { - if (columnName.equalsIgnoreCase("Id")) { - continue; + + if(idField != null){ + if(column.isAnnotationPresent(PrimaryKey.class)){ + continue; + } + }else{ + if (columnName.equalsIgnoreCase("Id")) { + continue; + } } if (column.isAnnotationPresent(Column.class)) { diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 4b9e04ef..20f7754b 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -8,6 +8,9 @@ import android.text.TextUtils; import android.util.Log; +import com.orm.dsl.Ignore; +import com.orm.dsl.NotNull; +import com.orm.dsl.PrimaryKey; import com.orm.dsl.Table; import com.orm.dsl.Unique; import com.orm.util.NamingHelper; @@ -16,6 +19,7 @@ import com.orm.util.SugarCursor; import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -24,6 +28,7 @@ import java.util.Map; import java.util.NoSuchElementException; + import static com.orm.SugarContext.getSugarContext; public class SugarRecord { @@ -131,8 +136,38 @@ public static List listAll(Class type, String orderBy) { return find(type, null, null, null, orderBy, null); } + /** + * Find field of parameter using @PrimaryKey notation + * @return One field of class + */ + private static Field findPrimaryKeyNotationField(@NotNull Class type){ + Field primaryField = null; + Field[] fields = type.getDeclaredFields(); + for(Field field : fields){ + + if(!field.isAnnotationPresent(Ignore.class) + && !Modifier.isStatic(field.getModifiers()) + && !Modifier.isTransient(field.getModifiers())){ + + if(field.isAnnotationPresent(PrimaryKey.class)){ + primaryField = field; + } + + } + } + return primaryField; + } + public static T findById(Class type, Long id) { - List list = find(type, "id=?", new String[]{String.valueOf(id)}, null, null, "1"); + List list; + Field primaryKeyField = findPrimaryKeyNotationField(type); + if (primaryKeyField != null) { + String pk = NamingHelper.toSQLName(primaryKeyField); + list = find(type, pk + "=?", new String[] {String.valueOf(id)}, null, null, "1"); + }else{ + list = find(type, "id=?", new String[]{String.valueOf(id)}, null, null, "1"); + } + if (list.isEmpty()) return null; return list.get(0); } @@ -142,7 +177,15 @@ public static T findById(Class type, Integer id) { } public static List findById(Class type, String[] ids) { - String whereClause = "id IN (" + QueryBuilder.generatePlaceholders(ids.length) + ")"; + String whereClause; + Field primaryKeyField = findPrimaryKeyNotationField(type); + if (primaryKeyField != null) { + String pk = NamingHelper.toSQLName(primaryKeyField); + whereClause = pk+" IN (" + QueryBuilder.generatePlaceholders(ids.length) + ")"; + }else{ + whereClause = "id IN (" + QueryBuilder.generatePlaceholders(ids.length) + ")"; + } + return find(type, whereClause, ids); } @@ -264,19 +307,37 @@ static long save(SQLiteDatabase db, Object object) { Map entitiesMap = getSugarContext().getEntitiesMap(); List columns = ReflectionUtil.getTableFields(object.getClass()); ContentValues values = new ContentValues(columns.size()); - Field idField = null; - for (Field column : columns) { - ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap); - if (column.getName().equals("id")) { - idField = column; + boolean isSugarEntity = isSugarEntity(object.getClass()); + + Field idField = findPrimaryKeyNotationField(object.getClass()); + if(idField != null){ + for(Field column : columns){ + ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap); + + if(!column.isAnnotationPresent(Ignore.class)){ + if(column.isAnnotationPresent(PrimaryKey.class)){ + idField = column; + } + } } - } - boolean isSugarEntity = isSugarEntity(object.getClass()); - if (isSugarEntity && entitiesMap.containsKey(object)) { + if (isSugarEntity && entitiesMap.containsKey(object)) { + values.put(NamingHelper.toSQLName(idField), entitiesMap.get(object)); + } + }else{ + for(Field column : columns){ + ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap); + if(column.getName().equals("id")){ + idField = column; + } + } + + if (isSugarEntity && entitiesMap.containsKey(object)) { values.put("id", entitiesMap.get(object)); + } } + long id = db.insertWithOnConflict(NamingHelper.toSQLName(object.getClass()), null, values, SQLiteDatabase.CONFLICT_REPLACE); @@ -325,8 +386,15 @@ static long update(SQLiteDatabase db, Object object) { e.printStackTrace(); } } else { - if (!column.getName().equals("id")) { - ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap); + Field idField = findPrimaryKeyNotationField(object.getClass()); + if(idField != null){ + if(!column.getName().equals(NamingHelper.toSQLName(idField))){ + ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap); + } + }else{ + if(!column.getName().equals("id")){ + ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap); + } } } } @@ -350,10 +418,20 @@ public static boolean isSugarEntity(Class objectClass) { private static void inflate(Cursor cursor, Object object, Map entitiesMap) { List columns = ReflectionUtil.getTableFields(object.getClass()); - if (!entitiesMap.containsKey(object)) { - entitiesMap.put(object, cursor.getLong(cursor.getColumnIndex(("ID")))); + + Field idField = findPrimaryKeyNotationField(object.getClass()); + if(idField != null){ + if (!entitiesMap.containsKey(object)) { + entitiesMap.put(object, cursor.getLong(cursor.getColumnIndex((NamingHelper.toSQLName(idField))))); + } + }else{ + if (!entitiesMap.containsKey(object)) { + entitiesMap.put(object, cursor.getLong(cursor.getColumnIndex(("ID")))); + } } + + for (Field field : columns) { field.setAccessible(true); Class fieldType = field.getType(); @@ -370,6 +448,8 @@ private static void inflate(Cursor cursor, Object object, Map enti } } + //FIXME I don't know how make this method using PrimaryKey notation without the 'Object'. Suggestions? + @Deprecated public boolean delete() { Long id = getId(); Class type = getClass(); @@ -386,11 +466,20 @@ public static boolean delete(Object object) { Class type = object.getClass(); if (type.isAnnotationPresent(Table.class)) { try { - Field field = type.getDeclaredField("id"); + + Field idField = findPrimaryKeyNotationField(object.getClass()); + Field field = (idField != null)?idField:type.getDeclaredField("id"); + field.setAccessible(true); Long id = (Long) field.get(object); if (id != null && id > 0L) { - boolean deleted = getSugarDataBase().delete(NamingHelper.toSQLName(type), "Id=?", new String[]{id.toString()}) == 1; + boolean deleted; + if(idField != null){ + deleted = getSugarDataBase().delete(NamingHelper.toSQLName(type), NamingHelper.toSQLName(idField) + "=?", + new String[] {id.toString()}) == 1; + }else{ + deleted = getSugarDataBase().delete(NamingHelper.toSQLName(type), "Id=?", new String[] {id.toString()}) == 1; + } Log.i(SUGAR, type.getSimpleName() + " deleted : " + id); return deleted; } else { @@ -398,14 +487,34 @@ public static boolean delete(Object object) { return false; } } catch (NoSuchFieldException e) { - Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - annotated object has no id"); + Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - annotated object has no pk"); return false; } catch (IllegalAccessException e) { - Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - can't access id"); + Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - can't access pk"); return false; } } else if (SugarRecord.class.isAssignableFrom(type)) { - return ((SugarRecord) object).delete(); + Field idField = findPrimaryKeyNotationField(object.getClass()); + if(idField != null){ + idField.setAccessible(true); + Long id = null; + try{ + id = idField.getLong(object); + }catch(IllegalAccessException e){ + Log.i(SUGAR, "Cannot delete object using primaryKey notation " + e.getMessage(),e); + return false; + } + if (id != null && id > 0L) { + Log.i(SUGAR, type.getSimpleName() + " deleted : " + id); + return getSugarDataBase().delete(NamingHelper.toSQLName(type), NamingHelper.toSQLName(idField)+"=?", + new String[]{id.toString()}) == 1; + } else { + Log.i(SUGAR, "Cannot delete object: " + type.getSimpleName() + " - object has not been saved"); + return false; + } + }else{ + return ((SugarRecord) object).delete(); + } } else { Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - not persisted"); return false; @@ -425,10 +534,12 @@ void inflate(Cursor cursor) { inflate(cursor, this, getSugarContext().getEntitiesMap()); } + //FIXME I don't know how make this method using PrimaryKey notation without the 'Object'. Suggestions? public Long getId() { return id; } + //FIXME I don't know how make this method using PrimaryKey notation without the 'Object'. Suggestions? public void setId(Long id) { this.id = id; } diff --git a/library/src/main/java/com/orm/dsl/PrimaryKey.java b/library/src/main/java/com/orm/dsl/PrimaryKey.java new file mode 100644 index 00000000..7ef2a3c1 --- /dev/null +++ b/library/src/main/java/com/orm/dsl/PrimaryKey.java @@ -0,0 +1,7 @@ +package com.orm.dsl; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention (RetentionPolicy.RUNTIME) +public @interface PrimaryKey{} \ No newline at end of file