Skip to content

Commit

Permalink
Merge pull request #1464 from Raizlabs/develop
Browse files Browse the repository at this point in the history
4.1.2
  • Loading branch information
agrosner authored Oct 21, 2017
2 parents 7071f42 + b8e2e92 commit 6ac6930
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 145 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
buildscript {
ext.kotlin_version = '1.1.4-2'
ext.kotlin_version = '1.1.51'
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-beta2'
classpath 'com.android.tools.build:gradle:3.0.0-rc2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.7.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ abstract class BaseTableDefinition(typeElement: Element, processorManager: Proce
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)

for (columnDefinition in packagePrivateList) {
var helperClassName = "${columnDefinition.element.getPackage()}.${columnDefinition.element.enclosingElement.toClassName().simpleName()}${classSeparator}Helper"
var helperClassName = "${columnDefinition.element.getPackage()}.${columnDefinition.element.enclosingElement.toClassName()?.simpleName()}${classSeparator}Helper"
if (columnDefinition is ReferenceColumnDefinition) {
val tableDefinition: TableDefinition? = databaseDefinition?.objectHolder?.tableDefinitionMap?.get(columnDefinition.referencedClassName as TypeName)
if (tableDefinition != null) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ constructor(processorManager: ProcessorManager, element: Element,
var length = -1
var notNull = false
var isNotNullType = false
var isNullableType = true
var onNullConflict: ConflictAction? = null
var onUniqueConflict: ConflictAction? = null
var unique = false
Expand Down Expand Up @@ -113,14 +114,22 @@ constructor(processorManager: ProcessorManager, element: Element,
notNull = true
}

if (elementTypeName?.isPrimitive == true) {
isNullableType = false
isNotNullType = true
}

// if specified, usually from Kotlin targets, we will not set null on the field.
element.annotation<org.jetbrains.annotations.NotNull>()?.let {
isNotNullType = true
isNullableType = false
}

// android support annotation
element.annotationMirrors
.find { it.annotationType.toTypeElement().toClassName() == ClassNames.NON_NULL }?.let {
.find { it.annotationType.toTypeElement().toClassName() == ClassNames.NON_NULL }?.let {
isNotNullType = true
isNullableType = false
}

column?.let {
Expand All @@ -144,13 +153,13 @@ constructor(processorManager: ProcessorManager, element: Element,

val isString = (elementTypeName == ClassName.get(String::class.java))
if (defaultValue != null
&& isString
&& !QUOTE_PATTERN.matcher(defaultValue).find()) {
&& isString
&& !QUOTE_PATTERN.matcher(defaultValue).find()) {
defaultValue = "\"" + defaultValue + "\""
}

if (isNotNullType && defaultValue == null
&& isString) {
&& isString) {
defaultValue = "\"\""
}

Expand All @@ -159,19 +168,19 @@ constructor(processorManager: ProcessorManager, element: Element,

if (isPackagePrivate) {
columnAccessor = PackagePrivateScopeColumnAccessor(elementName, packageName,
baseTableDefinition.databaseDefinition?.classSeparator,
ClassName.get(element.enclosingElement as TypeElement).simpleName())
baseTableDefinition.databaseDefinition?.classSeparator,
ClassName.get(element.enclosingElement as TypeElement).simpleName())

PackagePrivateScopeColumnAccessor.putElement(
(columnAccessor as PackagePrivateScopeColumnAccessor).helperClassName,
columnName)
(columnAccessor as PackagePrivateScopeColumnAccessor).helperClassName,
columnName)

} else {
val isPrivate = element.modifiers.contains(Modifier.PRIVATE)
if (isPrivate) {
val isBoolean = elementTypeName?.box() == TypeName.BOOLEAN.box()
val useIs = isBoolean
&& baseTableDefinition is TableDefinition && (baseTableDefinition as TableDefinition).useIsForPrivateBooleans
&& baseTableDefinition is TableDefinition && (baseTableDefinition as TableDefinition).useIsForPrivateBooleans
columnAccessor = PrivateScopeColumnAccessor(elementName, object : GetterSetter {
override val getterName: String = column?.getterName ?: ""
override val setterName: String = column?.setterName ?: ""
Expand Down Expand Up @@ -220,7 +229,7 @@ constructor(processorManager: ProcessorManager, element: Element,

hasCustomConverter = false
if (typeConverterClassName != null && typeMirror != null &&
typeConverterClassName != ClassNames.TYPE_CONVERTER) {
typeConverterClassName != ClassNames.TYPE_CONVERTER) {
typeConverterDefinition = TypeConverterDefinition(typeConverterClassName, typeMirror, manager)
evaluateTypeConverter(typeConverterDefinition, true)
}
Expand All @@ -234,11 +243,12 @@ constructor(processorManager: ProcessorManager, element: Element,
wrapperAccessor = BlobColumnAccessor()
wrapperTypeName = ArrayTypeName.of(TypeName.BYTE)
} else {
if (elementTypeName is ParameterizedTypeName) {
if (elementTypeName is ParameterizedTypeName ||
elementTypeName == ArrayTypeName.of(TypeName.BYTE.unbox())) {
// do nothing, for now.
} else if (elementTypeName is ArrayTypeName) {
processorManager.messager.printMessage(Diagnostic.Kind.ERROR,
"Columns cannot be of array type.")
"Columns cannot be of array type. Found $elementTypeName")
} else {
if (elementTypeName == TypeName.BOOLEAN) {
wrapperAccessor = BooleanColumnAccessor()
Expand All @@ -258,7 +268,7 @@ constructor(processorManager: ProcessorManager, element: Element,
}

combiner = Combiner(columnAccessor, elementTypeName!!, wrapperAccessor, wrapperTypeName,
subWrapperAccessor)
subWrapperAccessor)
}

private fun evaluateTypeConverter(typeConverterDefinition: TypeConverterDefinition?,
Expand All @@ -268,7 +278,7 @@ constructor(processorManager: ProcessorManager, element: Element,

if (it.modelTypeName != elementTypeName) {
manager.logError("The specified custom TypeConverter's Model Value ${it.modelTypeName}" +
" from ${it.className} must match the type of the column $elementTypeName. ")
" from ${it.className} must match the type of the column $elementTypeName. ")
} else {
hasTypeConverter = true
hasCustomConverter = isCustom
Expand Down Expand Up @@ -312,21 +322,21 @@ constructor(processorManager: ProcessorManager, element: Element,
}

val fieldBuilder = FieldSpec.builder(propParam,
propertyFieldName, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
propertyFieldName, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)

if (isNonPrimitiveTypeConverter) {
val codeBlock = CodeBlock.builder()
codeBlock.add("new \$T(\$T.class, \$S, true,", propParam, tableClass, columnName)
codeBlock.add("\nnew \$T() {" +
"\n@Override" +
"\npublic \$T getTypeConverter(Class<?> modelClass) {" +
"\n \$T adapter = (\$T) \$T.getInstanceAdapter(modelClass);" +
"\nreturn adapter.\$L;" +
"\n}" +
"\n})", ClassNames.TYPE_CONVERTER_GETTER, ClassNames.TYPE_CONVERTER,
baseTableDefinition.outputClassName, baseTableDefinition.outputClassName,
ClassNames.FLOW_MANAGER,
(wrapperAccessor as TypeConverterScopeColumnAccessor).typeConverterFieldName)
"\n@Override" +
"\npublic \$T getTypeConverter(Class<?> modelClass) {" +
"\n \$T adapter = (\$T) \$T.getInstanceAdapter(modelClass);" +
"\nreturn adapter.\$L;" +
"\n}" +
"\n})", ClassNames.TYPE_CONVERTER_GETTER, ClassNames.TYPE_CONVERTER,
baseTableDefinition.outputClassName, baseTableDefinition.outputClassName,
ClassNames.FLOW_MANAGER,
(wrapperAccessor as TypeConverterScopeColumnAccessor).typeConverterFieldName)
fieldBuilder.initializer(codeBlock.build())
} else {
fieldBuilder.initializer("new \$T(\$T.class, \$S)", propParam, tableClass, columnName)
Expand Down Expand Up @@ -375,7 +385,7 @@ constructor(processorManager: ProcessorManager, element: Element,
defineProperty: Boolean = true) = code {
SqliteStatementAccessCombiner(combiner).apply {
addCode(if (useStart) "start" else "", getDefaultValueBlock(), index.get(), modelBlock,
defineProperty)
defineProperty)
}
this
}
Expand All @@ -390,8 +400,8 @@ constructor(processorManager: ProcessorManager, element: Element,
}

LoadFromCursorAccessCombiner(combiner, defaultValue != null,
nameAllocator, baseTableDefinition.orderedCursorLookUp,
assignDefaultValue).apply {
nameAllocator, baseTableDefinition.orderedCursorLookUp,
assignDefaultValue).apply {
addCode(columnName, getDefaultValueBlock(), index.get(), modelBlock)
}
this
Expand Down Expand Up @@ -426,10 +436,10 @@ constructor(processorManager: ProcessorManager, element: Element,

open fun appendExistenceMethod(codeBuilder: CodeBlock.Builder) {
ExistenceAccessCombiner(combiner, isRowId || isPrimaryKeyAutoIncrement,
isQuickCheckPrimaryKeyAutoIncrement, baseTableDefinition.elementClassName!!)
.apply {
codeBuilder.addCode(columnName, getDefaultValueBlock(), 0, modelBlock)
}
isQuickCheckPrimaryKeyAutoIncrement, baseTableDefinition.elementClassName!!)
.apply {
codeBuilder.addCode(columnName, getDefaultValueBlock(), 0, modelBlock)
}
}

open fun appendPropertyComparisonAccessStatement(codeBuilder: CodeBlock.Builder) {
Expand All @@ -446,9 +456,9 @@ constructor(processorManager: ProcessorManager, element: Element,
codeBlockBuilder.add(" PRIMARY KEY ")

if (baseTableDefinition is TableDefinition &&
!(baseTableDefinition as TableDefinition).primaryKeyConflictActionName.isNullOrEmpty()) {
!(baseTableDefinition as TableDefinition).primaryKeyConflictActionName.isNullOrEmpty()) {
codeBlockBuilder.add("ON CONFLICT \$L ",
(baseTableDefinition as TableDefinition).primaryKeyConflictActionName)
(baseTableDefinition as TableDefinition).primaryKeyConflictActionName)
}

codeBlockBuilder.add("AUTOINCREMENT")
Expand Down Expand Up @@ -484,8 +494,8 @@ constructor(processorManager: ProcessorManager, element: Element,
if (elementTypeName == TypeName.BOOLEAN) {
defaultValue = "false"
} else if (elementTypeName == TypeName.BYTE || elementTypeName == TypeName.INT
|| elementTypeName == TypeName.DOUBLE || elementTypeName == TypeName.FLOAT
|| elementTypeName == TypeName.LONG || elementTypeName == TypeName.SHORT) {
|| elementTypeName == TypeName.DOUBLE || elementTypeName == TypeName.FLOAT
|| elementTypeName == TypeName.LONG || elementTypeName == TypeName.SHORT) {
defaultValue = "($elementTypeName) 0"
} else if (elementTypeName == TypeName.CHAR) {
defaultValue = "'\\u0000'"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ inline fun <reified T : Annotation> Element?.annotation() = this?.getAnnotation(

fun Element?.getPackage(manager: ProcessorManager = ProcessorManager.manager) = manager.elements.getPackageOf(this)

fun Element?.toClassName() = ClassName.get(this as TypeElement)
fun Element?.toClassName(): ClassName? = this?.let { ClassName.get(this as TypeElement) }
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ class TypeConverterModel(@PrimaryKey var id: Int = 0,

@Table(database = TestDatabase::class)
class EnumTypeConverterModel(@PrimaryKey var id: Int = 0,
@Column var blob: Blob? = null,
@Column(typeConverter = CustomEnumTypeConverter::class)
var difficulty: Difficulty = Difficulty.EASY)
@Column var blob: Blob? = null,
@Column var byteArray: ByteArray? = null,
@Column(typeConverter = CustomEnumTypeConverter::class)
var difficulty: Difficulty = Difficulty.EASY)

@Table(database = TestDatabase::class, allFields = true)
class FeedEntry(@PrimaryKey var id: Int = 0,
Expand All @@ -96,18 +97,18 @@ class FeedEntry(@PrimaryKey var id: Int = 0,

@Table(database = TestDatabase::class)
@ManyToMany(
generatedTableClassName = "Refund", referencedTable = Transfer::class,
referencedTableColumnName = "refund_in", thisTableColumnName = "refund_out",
saveForeignKeyModels = true
generatedTableClassName = "Refund", referencedTable = Transfer::class,
referencedTableColumnName = "refund_in", thisTableColumnName = "refund_out",
saveForeignKeyModels = true
)
data class Transfer(@PrimaryKey var transfer_id: UUID = UUID.randomUUID())

@Table(database = TestDatabase::class)
data class Transfer2(
@PrimaryKey
var id: UUID = UUID.randomUUID(),
@ForeignKey(stubbedRelationship = true)
var origin: Account? = null
@PrimaryKey
var id: UUID = UUID.randomUUID(),
@ForeignKey(stubbedRelationship = true)
var origin: Account? = null
)

@Table(database = TestDatabase::class)
Expand Down Expand Up @@ -148,7 +149,7 @@ class CustomTypeConverter : TypeConverter<String, CustomType>() {
class CustomEnumTypeConverter : TypeConverter<String, Difficulty>() {
override fun getDBValue(model: Difficulty) = model.name.substring(0..0)

override fun getModelValue(data: String) = when(data) {
override fun getModelValue(data: String) = when (data) {
"E" -> Difficulty.EASY
"M" -> Difficulty.MEDIUM
"H" -> Difficulty.HARD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public Map<Integer, List<Migration>> getMigrations() {
public synchronized OpenHelper getHelper() {
if (openHelper == null) {
DatabaseConfig config = FlowManager.getConfig().databaseConfigMap()
.get(getAssociatedDatabaseClassFile());
.get(getAssociatedDatabaseClassFile());
if (config == null || config.helperCreator() == null) {
openHelper = new FlowSQLiteOpenHelper(this, helperListener);
} else {
Expand All @@ -261,7 +261,7 @@ public DatabaseWrapper getWritableDatabase() {
public ModelNotifier getModelNotifier() {
if (modelNotifier == null) {
DatabaseConfig config = FlowManager.getConfig().databaseConfigMap()
.get(getAssociatedDatabaseClassFile());
.get(getAssociatedDatabaseClassFile());
if (config == null || config.modelNotifier() == null) {
modelNotifier = new ContentResolverNotifier();
} else {
Expand Down Expand Up @@ -375,6 +375,26 @@ public void reset(@Nullable DatabaseConfig databaseConfig) {
}
}

/**
* Reopens the DB with the new {@link DatabaseConfig} specified.
*/
public void reopen(@Nullable DatabaseConfig databaseConfig) {
if (!isResetting) {
close();
openHelper = null;
applyDatabaseConfig(databaseConfig);
getHelper().getDatabase();
isResetting = false;
}
}

/**
* Closes and reopens the database.
*/
public void reopen() {
reopen(databaseConfig);
}

/**
* Deletes the underlying database and destroys it.
*/
Expand Down
Loading

0 comments on commit 6ac6930

Please sign in to comment.