Skip to content

Commit

Permalink
Merge pull request #1505 from Raizlabs/develop
Browse files Browse the repository at this point in the history
4.2.4
  • Loading branch information
agrosner committed Dec 28, 2017
2 parents eb5ed84 + e8330fc commit 0e06ea8
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 78 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Add the library to the project-level build.gradle, using the apt plugin to enabl
apply plugin: 'kotlin-kapt' // required for kotlin.
def dbflow_version = "4.2.3"
def dbflow_version = "4.2.4"
// or dbflow_version = "develop-SNAPSHOT" for grabbing latest dependency in your project on the develop branch
// or 10-digit short-hash of a specific commit. (Useful for bugs fixed in develop, but not in a release yet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.squareup.javapoet.NameAllocator
import com.squareup.javapoet.ParameterizedTypeName
import com.squareup.javapoet.TypeName
import com.squareup.javapoet.TypeSpec
import java.util.*
import java.util.ArrayList
import java.util.concurrent.atomic.AtomicInteger
import javax.lang.model.element.Element
import javax.lang.model.element.Modifier
Expand Down Expand Up @@ -345,11 +345,13 @@ class ReferenceColumnDefinition(manager: ProcessorManager, tableDefinition: Base
}

override fun appendPropertyComparisonAccessStatement(codeBuilder: CodeBlock.Builder) {
if (nonModelColumn || columnAccessor is TypeConverterScopeColumnAccessor) {
super.appendPropertyComparisonAccessStatement(codeBuilder)
} else {

referencedClassName?.let {
when {
nonModelColumn -> PrimaryReferenceAccessCombiner(combiner).apply {
checkNeedsReferences()
codeBuilder.addCode(references!![0].columnName, getDefaultValueBlock(), 0, modelBlock)
}
columnAccessor is TypeConverterScopeColumnAccessor -> super.appendPropertyComparisonAccessStatement(codeBuilder)
else -> referencedClassName?.let {
val foreignKeyCombiner = ForeignKeyAccessCombiner(columnAccessor)
_referenceDefinitionList.forEach {
foreignKeyCombiner.fieldAccesses += it.primaryReferenceField
Expand Down Expand Up @@ -402,7 +404,7 @@ class ReferenceColumnDefinition(manager: ProcessorManager, tableDefinition: Base
primaryColumns.forEach {
val foreignKeyReferenceDefinition = ReferenceDefinition(manager,
elementName, it.elementName, it, this, primaryColumns.size,
if (isColumnMap) it.elementName else "")
if (isColumnMap) it.elementName else "")
_referenceDefinitionList.add(foreignKeyReferenceDefinition)
}
needsReferences = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.raizlabs.android.dbflow.processor.test

import com.raizlabs.android.dbflow.processor.definition.column.*
import com.raizlabs.android.dbflow.processor.definition.column.PrimaryReferenceAccessCombiner
import com.squareup.javapoet.CodeBlock
import com.squareup.javapoet.NameAllocator
import com.squareup.javapoet.TypeName
import org.junit.Assert.assertEquals
import org.junit.Test
Expand All @@ -21,9 +21,11 @@ class ContentValuesCombinerTest {
TypeName.get(String::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "columnName", CodeBlock.of("\$S", "nonNull"), -1)
combiner.apply {
codeBuilder.addCode("columnName", CodeBlock.of("\$S", "nonNull"), -1)
}

assertEquals("values.put(\"columnName\", model.name != null ? model.name : \"nonNull\");",
assertEquals("values.put(\"`columnName`\", model.name != null ? model.name : \"nonNull\");",
codeBuilder.build().toString().trim())
}

Expand All @@ -33,9 +35,11 @@ class ContentValuesCombinerTest {
TypeName.get(Boolean::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "columnName", index = -1)
combiner.apply {
codeBuilder.addCode("columnName", index = -1)
}

assertEquals("values.put(\"columnName\", model.name);",
assertEquals("values.put(\"`columnName`\", model.name);",
codeBuilder.build().toString().trim())
}

Expand All @@ -48,10 +52,12 @@ class ContentValuesCombinerTest {
TypeName.get(String::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "columnName", CodeBlock.of("\$S", "nonNull"), -1)
combiner.apply {
codeBuilder.addCode("columnName", CodeBlock.of("\$S", "nonNull"), -1)
}

assertEquals("java.lang.String refname = com.fuzz.android.TestType_Helper.getName(model) != null ? global_converter.getDBValue(com.fuzz.android.TestType_Helper.getName(model)) : null;"
+ "\nvalues.put(\"columnName\", refname != null ? refname : \"nonNull\");",
+ "\nvalues.put(\"`columnName`\", refname != null ? refname : \"nonNull\");",
codeBuilder.build().toString().trim())
}

Expand All @@ -64,10 +70,12 @@ class ContentValuesCombinerTest {
TypeName.get(String::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "columnName", CodeBlock.of("\$S", "nonNull"), -1)
combiner.apply {
codeBuilder.addCode("columnName", CodeBlock.of("\$S", "nonNull"), -1)
}

assertEquals("java.lang.String refname = model.getName() != null ? global_converter.getDBValue(model.getName()) : null;"
+ "\nvalues.put(\"columnName\", refname != null ? refname : \"nonNull\");",
assertEquals("java.lang.String refname = model.getName() != null ? global_converter.getDBValue(model.getName()) : null;"
+ "\nvalues.put(\"`columnName`\", refname != null ? refname : \"nonNull\");",
codeBuilder.build().toString().trim())
}

Expand All @@ -82,9 +90,11 @@ class SqliteStatementAccessCombinerTest {
Combiner(VisibleScopeColumnAccessor("name"), TypeName.get(String::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "start", CodeBlock.of("\$S", "nonNull"), 0)
combiner.apply {
codeBuilder.addCode("start", CodeBlock.of("\$S", "nonNull"), 0)
}

assertEquals("if (model.name != null) {" +
assertEquals("if (model.name != null) {" +
"\n statement.bindString(0 + start, model.name);" +
"\n} else {" +
"\n statement.bindString(0 + start, \"nonNull\");" +
Expand All @@ -100,9 +110,11 @@ class SqliteStatementAccessCombinerTest {
TypeName.get(Boolean::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "start", index = 0)
combiner.apply {
codeBuilder.addCode("start", index = 0)
}

assertEquals("statement.bindLong(0 + start, model.name ? 1 : 0);",
assertEquals("statement.bindLong(0 + start, model.name ? 1 : 0);",
codeBuilder.build().toString().trim())
}

Expand All @@ -115,10 +127,12 @@ class SqliteStatementAccessCombinerTest {
TypeName.get(String::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "start", CodeBlock.of("\$S", "nonNull"), 1)
combiner.apply {
codeBuilder.addCode("start", CodeBlock.of("\$S", "nonNull"), 1)
}

assertEquals("java.lang.String refname = com.fuzz.android.TestType_Helper.getName(model) != null ? global_converter.getDBValue(com.fuzz.android.TestType_Helper.getName(model)) : null;" +
"\nif (refname != null) {" +
assertEquals("java.lang.String refname = com.fuzz.android.TestType_Helper.getName(model) != null ? global_converter.getDBValue(com.fuzz.android.TestType_Helper.getName(model)) : null;" +
"\nif (refname != null) {" +
"\n statement.bindString(1 + start, refname);" +
"\n} else {" +
"\n statement.bindString(1 + start, \"nonNull\");" +
Expand All @@ -134,10 +148,11 @@ class SqliteStatementAccessCombinerTest {
TypeName.get(String::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "start", CodeBlock.of("\$S", "nonNull"), 1)

assertEquals("java.lang.String refname = model.getName() != null ? global_converter.getDBValue(model.getName()) : null;" +
"\nif (refname != null) {" +
combiner.apply {
codeBuilder.addCode("start", CodeBlock.of("\$S", "nonNull"), 1)
}
assertEquals("java.lang.String refname = model.getName() != null ? global_converter.getDBValue(model.getName()) : null;" +
"\nif (refname != null) {" +
"\n statement.bindString(1 + start, refname);" +
"\n} else {" +
"\n statement.bindString(1 + start, \"nonNull\");" +
Expand All @@ -151,16 +166,13 @@ class LoadFromCursorAccessCombinerTest {
@Test
fun test_simpleCase() {
val combiner = LoadFromCursorAccessCombiner(
Combiner(VisibleScopeColumnAccessor("name"), TypeName.get(String::class.java)))
Combiner(VisibleScopeColumnAccessor("name"), TypeName.get(String::class.java)), false, NameAllocator())
val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "columnName", CodeBlock.of("\$S", "nonNull"))
combiner.apply {
codeBuilder.addCode("columnName", CodeBlock.of("\$S", "nonNull"))
}

assertEquals("int index_columnName = cursor.getColumnIndex(\"columnName\");" +
"\nif (index_columnName != -1 && !cursor.isNull(index_columnName)) {" +
"\n model.name = cursor.getString(index_columnName);" +
"\n} else {" +
"\n model.name = \"nonNull\";" +
"\n}", codeBuilder.build().toString().trim())
assertEquals("model.name = cursor.getStringOrDefault(\"columnName\");", codeBuilder.build().toString().trim())
}

@Test
Expand All @@ -169,9 +181,11 @@ class LoadFromCursorAccessCombinerTest {
Combiner(VisibleScopeColumnAccessor("name"),
TypeName.get(Date::class.java),
wrapperLevelAccessor = TypeConverterScopeColumnAccessor("global_converter"),
wrapperFieldTypeName = TypeName.get(String::class.java)))
wrapperFieldTypeName = TypeName.get(String::class.java)),false, NameAllocator())
val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "columnName", CodeBlock.of("\$S", "nonNull"))
combiner.apply {
codeBuilder.addCode("columnName", CodeBlock.of("\$S", "nonNull"))
}

assertEquals("int index_columnName = cursor.getColumnIndex(\"columnName\");" +
"\nif (index_columnName != -1 && !cursor.isNull(index_columnName)) {" +
Expand All @@ -191,7 +205,9 @@ class PrimaryReferenceAccessCombiner {
Combiner(VisibleScopeColumnAccessor("id"), TypeName.get(Long::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "id")
combiner.apply {
codeBuilder.addCode("id")
}

assertEquals("clause.and(id.eq(model.id));", codeBuilder.build().toString().trim())
}
Expand All @@ -205,7 +221,9 @@ class PrimaryReferenceAccessCombiner {
TypeName.get(Date::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "id")
combiner.apply {
codeBuilder.addCode("id")
}

assertEquals("java.util.Date refid = model.id != null ? global_converter.getDBValue(model.id) : null;\n" +
"clause.and(id.invertProperty().eq(refid));",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class ForeignKeyAccessCombinerTest {
foreignKeyAccessCombiner.addCode(builder, AtomicInteger(4))

assertEquals("if (model.name != null) {" +
"\n values.put(\"test\", model.name.test);" +
"\n values.put(\"test2\", model.name.getTest2());" +
"\n values.put(\"`test`\", model.name.test);" +
"\n values.put(\"`test2`\", model.name.getTest2());" +
"\n} else {" +
"\n values.putNull(\"test\");" +
"\n values.putNull(\"test2\");" +
"\n values.putNull(\"`test`\");" +
"\n values.putNull(\"`test2`\");" +
"\n}",
builder.build().toString().trim())
}
Expand All @@ -51,7 +51,7 @@ class ForeignKeyAccessCombinerTest {
foreignKeyAccessCombiner.addCode(builder, AtomicInteger(4))

assertEquals("if (model.getName() != null) {" +
"\n statement.bindString(4 + start, model.getName().test);" +
"\n statement.bindStringOrNull(4 + start, model.getName().test);" +
"\n} else {" +
"\n statement.bindNull(4 + start);" +
"\n}",
Expand Down Expand Up @@ -93,11 +93,11 @@ class ForeignKeyAccessCombinerTest {
foreignKeyAccessCombiner.addCode(builder, AtomicInteger(1))

assertEquals("if (model.modem != null) {" +
"\n values.put(\"number\", com.fuzz.AnotherHelper\$Helper.getNumber(model.modem));" +
"\n values.put(\"date\", global_converter.getDBValue(model.modem.date));" +
"\n values.put(\"`number`\", com.fuzz.AnotherHelper\$Helper.getNumber(model.modem));" +
"\n values.put(\"`date`\", global_converter.getDBValue(model.modem.date));" +
"\n} else {" +
"\n values.putNull(\"number\");" +
"\n values.putNull(\"date\");" +
"\n values.putNull(\"`number`\");" +
"\n values.putNull(\"`date`\");" +
"\n}",
builder.build().toString().trim())
}
Expand All @@ -116,12 +116,12 @@ class ForeignKeyAccessCombinerTest {
val builder = CodeBlock.builder()
foreignKeyAccessCombiner.addCode(builder, AtomicInteger(0))

assertEquals("int index_testmodel_id = cursor.getColumnIndex(\"testmodel_id\");" +
"\nint index_testmodel_type = cursor.getColumnIndex(\"testmodel_type\");" +
"\nif (index_testmodel_id != -1 && !cursor.isNull(index_testmodel_id) && index_testmodel_type != -1 && !cursor.isNull(index_testmodel_type)) {" +
assertEquals("int index_testmodel_id_ParentModel_Table = cursor.getColumnIndex(\"testmodel_id\");" +
"\nint index_testmodel_type_ParentModel_Table = cursor.getColumnIndex(\"testmodel_type\");" +
"\nif (index_testmodel_id_ParentModel_Table != -1 && !cursor.isNull(index_testmodel_id_ParentModel_Table) && index_testmodel_type_ParentModel_Table != -1 && !cursor.isNull(index_testmodel_type_ParentModel_Table)) {" +
"\n model.testModel1 = com.raizlabs.android.dbflow.sql.language.SQLite.select().from(com.raizlabs.android.dbflow.test.container.ParentModel.class).where()" +
"\n .and(com.raizlabs.android.dbflow.test.container.ParentModel_Table.name.eq(cursor.getString(index_testmodel_id)))" +
"\n .and(com.raizlabs.android.dbflow.test.container.ParentModel_Table.type.eq(cursor.getString(index_testmodel_type)))" +
"\n .and(com.raizlabs.android.dbflow.test.container.ParentModel_Table.name.eq(cursor.getString(index_testmodel_id_ParentModel_Table)))" +
"\n .and(com.raizlabs.android.dbflow.test.container.ParentModel_Table.type.eq(cursor.getString(index_testmodel_type_ParentModel_Table)))" +
"\n .querySingle();" +
"\n} else {" +
"\n model.testModel1 = null;" +
Expand All @@ -142,12 +142,12 @@ class ForeignKeyAccessCombinerTest {
val builder = CodeBlock.builder()
foreignKeyAccessCombiner.addCode(builder, AtomicInteger(0))

assertEquals("int index_testmodel_id = cursor.getColumnIndex(\"testmodel_id\");" +
"\nint index_testmodel_type = cursor.getColumnIndex(\"testmodel_type\");" +
"\nif (index_testmodel_id != -1 && !cursor.isNull(index_testmodel_id) && index_testmodel_type != -1 && !cursor.isNull(index_testmodel_type)) {" +
assertEquals("int index_testmodel_id_ParentModel_Table = cursor.getColumnIndex(\"testmodel_id\");" +
"\nint index_testmodel_type_ParentModel_Table = cursor.getColumnIndex(\"testmodel_type\");" +
"\nif (index_testmodel_id_ParentModel_Table != -1 && !cursor.isNull(index_testmodel_id_ParentModel_Table) && index_testmodel_type_ParentModel_Table != -1 && !cursor.isNull(index_testmodel_type_ParentModel_Table)) {" +
"\n model.testModel1 = new com.raizlabs.android.dbflow.test.container.ParentModel();" +
"\n model.testModel1.name = cursor.getString(index_testmodel_id);" +
"\n model.testModel1.type = cursor.getString(index_testmodel_type);" +
"\n model.testModel1.name = cursor.getString(index_testmodel_id_ParentModel_Table);" +
"\n model.testModel1.type = cursor.getString(index_testmodel_type_ParentModel_Table);" +
"\n} else {" +
"\n model.testModel1 = null;" +
"\n}", builder.build().toString().trim())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ class BooleanTypeColumnAccessorTest() {
@Test
fun test_canSetBoolean() {
val access = BooleanColumnAccessor()
assertEquals("cursor.getInt(index) == 1 ? true : false",
access.set(CodeBlock.of("cursor.getInt(index)")).toString())
assertEquals("cursor.getBoolean(index)",
access.set(CodeBlock.of("cursor.getBoolean(index)")).toString())
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.raizlabs.android.dbflow.models.ants;

import com.raizlabs.android.dbflow.TestDatabase;
import com.raizlabs.android.dbflow.annotation.PrimaryKey;
import com.raizlabs.android.dbflow.annotation.Table;
import com.raizlabs.android.dbflow.structure.BaseModel;

@Table(database = TestDatabase.class)
public class AntHill extends BaseModel {
@PrimaryKey
public String hillId;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.raizlabs.android.dbflow.models.ants;

import com.raizlabs.android.dbflow.TestDatabase;
import com.raizlabs.android.dbflow.annotation.ForeignKey;
import com.raizlabs.android.dbflow.annotation.ForeignKeyReference;
import com.raizlabs.android.dbflow.annotation.PrimaryKey;
import com.raizlabs.android.dbflow.annotation.Table;
import com.raizlabs.android.dbflow.structure.BaseModel;

@Table(database = TestDatabase.class)
public class Ants extends BaseModel {
@PrimaryKey
public String antId;

@PrimaryKey
@ForeignKey(tableClass = AntHill.class, references = {
@ForeignKeyReference(columnName = "hillIdRef", foreignKeyColumnName = "hillId")
})
public String hillId;
}
Loading

0 comments on commit 0e06ea8

Please sign in to comment.