Skip to content

Commit 6ac6930

Browse files
authored
Merge pull request #1464 from Raizlabs/develop
4.1.2
2 parents 7071f42 + b8e2e92 commit 6ac6930

12 files changed

Lines changed: 215 additions & 145 deletions

File tree

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
buildscript {
2-
ext.kotlin_version = '1.1.4-2'
2+
ext.kotlin_version = '1.1.51'
33
repositories {
44
jcenter()
55
google()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:3.0.0-beta2'
8+
classpath 'com.android.tools.build:gradle:3.0.0-rc2'
99
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
1010
classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.7.3'
1111
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

dbflow-processor/src/main/java/com/raizlabs/android/dbflow/processor/definition/BaseTableDefinition.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ abstract class BaseTableDefinition(typeElement: Element, processorManager: Proce
134134
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
135135

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

dbflow-processor/src/main/java/com/raizlabs/android/dbflow/processor/definition/TableDefinition.kt

Lines changed: 70 additions & 62 deletions
Large diffs are not rendered by default.

dbflow-processor/src/main/java/com/raizlabs/android/dbflow/processor/definition/column/ColumnDefinition.kt

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ constructor(processorManager: ProcessorManager, element: Element,
6565
var length = -1
6666
var notNull = false
6767
var isNotNullType = false
68+
var isNullableType = true
6869
var onNullConflict: ConflictAction? = null
6970
var onUniqueConflict: ConflictAction? = null
7071
var unique = false
@@ -113,14 +114,22 @@ constructor(processorManager: ProcessorManager, element: Element,
113114
notNull = true
114115
}
115116

117+
if (elementTypeName?.isPrimitive == true) {
118+
isNullableType = false
119+
isNotNullType = true
120+
}
121+
116122
// if specified, usually from Kotlin targets, we will not set null on the field.
117123
element.annotation<org.jetbrains.annotations.NotNull>()?.let {
118124
isNotNullType = true
125+
isNullableType = false
119126
}
120127

128+
// android support annotation
121129
element.annotationMirrors
122-
.find { it.annotationType.toTypeElement().toClassName() == ClassNames.NON_NULL }?.let {
130+
.find { it.annotationType.toTypeElement().toClassName() == ClassNames.NON_NULL }?.let {
123131
isNotNullType = true
132+
isNullableType = false
124133
}
125134

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

145154
val isString = (elementTypeName == ClassName.get(String::class.java))
146155
if (defaultValue != null
147-
&& isString
148-
&& !QUOTE_PATTERN.matcher(defaultValue).find()) {
156+
&& isString
157+
&& !QUOTE_PATTERN.matcher(defaultValue).find()) {
149158
defaultValue = "\"" + defaultValue + "\""
150159
}
151160

152161
if (isNotNullType && defaultValue == null
153-
&& isString) {
162+
&& isString) {
154163
defaultValue = "\"\""
155164
}
156165

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

160169
if (isPackagePrivate) {
161170
columnAccessor = PackagePrivateScopeColumnAccessor(elementName, packageName,
162-
baseTableDefinition.databaseDefinition?.classSeparator,
163-
ClassName.get(element.enclosingElement as TypeElement).simpleName())
171+
baseTableDefinition.databaseDefinition?.classSeparator,
172+
ClassName.get(element.enclosingElement as TypeElement).simpleName())
164173

165174
PackagePrivateScopeColumnAccessor.putElement(
166-
(columnAccessor as PackagePrivateScopeColumnAccessor).helperClassName,
167-
columnName)
175+
(columnAccessor as PackagePrivateScopeColumnAccessor).helperClassName,
176+
columnName)
168177

169178
} else {
170179
val isPrivate = element.modifiers.contains(Modifier.PRIVATE)
171180
if (isPrivate) {
172181
val isBoolean = elementTypeName?.box() == TypeName.BOOLEAN.box()
173182
val useIs = isBoolean
174-
&& baseTableDefinition is TableDefinition && (baseTableDefinition as TableDefinition).useIsForPrivateBooleans
183+
&& baseTableDefinition is TableDefinition && (baseTableDefinition as TableDefinition).useIsForPrivateBooleans
175184
columnAccessor = PrivateScopeColumnAccessor(elementName, object : GetterSetter {
176185
override val getterName: String = column?.getterName ?: ""
177186
override val setterName: String = column?.setterName ?: ""
@@ -220,7 +229,7 @@ constructor(processorManager: ProcessorManager, element: Element,
220229

221230
hasCustomConverter = false
222231
if (typeConverterClassName != null && typeMirror != null &&
223-
typeConverterClassName != ClassNames.TYPE_CONVERTER) {
232+
typeConverterClassName != ClassNames.TYPE_CONVERTER) {
224233
typeConverterDefinition = TypeConverterDefinition(typeConverterClassName, typeMirror, manager)
225234
evaluateTypeConverter(typeConverterDefinition, true)
226235
}
@@ -234,11 +243,12 @@ constructor(processorManager: ProcessorManager, element: Element,
234243
wrapperAccessor = BlobColumnAccessor()
235244
wrapperTypeName = ArrayTypeName.of(TypeName.BYTE)
236245
} else {
237-
if (elementTypeName is ParameterizedTypeName) {
246+
if (elementTypeName is ParameterizedTypeName ||
247+
elementTypeName == ArrayTypeName.of(TypeName.BYTE.unbox())) {
238248
// do nothing, for now.
239249
} else if (elementTypeName is ArrayTypeName) {
240250
processorManager.messager.printMessage(Diagnostic.Kind.ERROR,
241-
"Columns cannot be of array type.")
251+
"Columns cannot be of array type. Found $elementTypeName")
242252
} else {
243253
if (elementTypeName == TypeName.BOOLEAN) {
244254
wrapperAccessor = BooleanColumnAccessor()
@@ -258,7 +268,7 @@ constructor(processorManager: ProcessorManager, element: Element,
258268
}
259269

260270
combiner = Combiner(columnAccessor, elementTypeName!!, wrapperAccessor, wrapperTypeName,
261-
subWrapperAccessor)
271+
subWrapperAccessor)
262272
}
263273

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

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

314324
val fieldBuilder = FieldSpec.builder(propParam,
315-
propertyFieldName, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
325+
propertyFieldName, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
316326

317327
if (isNonPrimitiveTypeConverter) {
318328
val codeBlock = CodeBlock.builder()
319329
codeBlock.add("new \$T(\$T.class, \$S, true,", propParam, tableClass, columnName)
320330
codeBlock.add("\nnew \$T() {" +
321-
"\n@Override" +
322-
"\npublic \$T getTypeConverter(Class<?> modelClass) {" +
323-
"\n \$T adapter = (\$T) \$T.getInstanceAdapter(modelClass);" +
324-
"\nreturn adapter.\$L;" +
325-
"\n}" +
326-
"\n})", ClassNames.TYPE_CONVERTER_GETTER, ClassNames.TYPE_CONVERTER,
327-
baseTableDefinition.outputClassName, baseTableDefinition.outputClassName,
328-
ClassNames.FLOW_MANAGER,
329-
(wrapperAccessor as TypeConverterScopeColumnAccessor).typeConverterFieldName)
331+
"\n@Override" +
332+
"\npublic \$T getTypeConverter(Class<?> modelClass) {" +
333+
"\n \$T adapter = (\$T) \$T.getInstanceAdapter(modelClass);" +
334+
"\nreturn adapter.\$L;" +
335+
"\n}" +
336+
"\n})", ClassNames.TYPE_CONVERTER_GETTER, ClassNames.TYPE_CONVERTER,
337+
baseTableDefinition.outputClassName, baseTableDefinition.outputClassName,
338+
ClassNames.FLOW_MANAGER,
339+
(wrapperAccessor as TypeConverterScopeColumnAccessor).typeConverterFieldName)
330340
fieldBuilder.initializer(codeBlock.build())
331341
} else {
332342
fieldBuilder.initializer("new \$T(\$T.class, \$S)", propParam, tableClass, columnName)
@@ -375,7 +385,7 @@ constructor(processorManager: ProcessorManager, element: Element,
375385
defineProperty: Boolean = true) = code {
376386
SqliteStatementAccessCombiner(combiner).apply {
377387
addCode(if (useStart) "start" else "", getDefaultValueBlock(), index.get(), modelBlock,
378-
defineProperty)
388+
defineProperty)
379389
}
380390
this
381391
}
@@ -390,8 +400,8 @@ constructor(processorManager: ProcessorManager, element: Element,
390400
}
391401

392402
LoadFromCursorAccessCombiner(combiner, defaultValue != null,
393-
nameAllocator, baseTableDefinition.orderedCursorLookUp,
394-
assignDefaultValue).apply {
403+
nameAllocator, baseTableDefinition.orderedCursorLookUp,
404+
assignDefaultValue).apply {
395405
addCode(columnName, getDefaultValueBlock(), index.get(), modelBlock)
396406
}
397407
this
@@ -426,10 +436,10 @@ constructor(processorManager: ProcessorManager, element: Element,
426436

427437
open fun appendExistenceMethod(codeBuilder: CodeBlock.Builder) {
428438
ExistenceAccessCombiner(combiner, isRowId || isPrimaryKeyAutoIncrement,
429-
isQuickCheckPrimaryKeyAutoIncrement, baseTableDefinition.elementClassName!!)
430-
.apply {
431-
codeBuilder.addCode(columnName, getDefaultValueBlock(), 0, modelBlock)
432-
}
439+
isQuickCheckPrimaryKeyAutoIncrement, baseTableDefinition.elementClassName!!)
440+
.apply {
441+
codeBuilder.addCode(columnName, getDefaultValueBlock(), 0, modelBlock)
442+
}
433443
}
434444

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

448458
if (baseTableDefinition is TableDefinition &&
449-
!(baseTableDefinition as TableDefinition).primaryKeyConflictActionName.isNullOrEmpty()) {
459+
!(baseTableDefinition as TableDefinition).primaryKeyConflictActionName.isNullOrEmpty()) {
450460
codeBlockBuilder.add("ON CONFLICT \$L ",
451-
(baseTableDefinition as TableDefinition).primaryKeyConflictActionName)
461+
(baseTableDefinition as TableDefinition).primaryKeyConflictActionName)
452462
}
453463

454464
codeBlockBuilder.add("AUTOINCREMENT")
@@ -484,8 +494,8 @@ constructor(processorManager: ProcessorManager, element: Element,
484494
if (elementTypeName == TypeName.BOOLEAN) {
485495
defaultValue = "false"
486496
} else if (elementTypeName == TypeName.BYTE || elementTypeName == TypeName.INT
487-
|| elementTypeName == TypeName.DOUBLE || elementTypeName == TypeName.FLOAT
488-
|| elementTypeName == TypeName.LONG || elementTypeName == TypeName.SHORT) {
497+
|| elementTypeName == TypeName.DOUBLE || elementTypeName == TypeName.FLOAT
498+
|| elementTypeName == TypeName.LONG || elementTypeName == TypeName.SHORT) {
489499
defaultValue = "($elementTypeName) 0"
490500
} else if (elementTypeName == TypeName.CHAR) {
491501
defaultValue = "'\\u0000'"

dbflow-processor/src/main/java/com/raizlabs/android/dbflow/processor/utils/ElementExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ inline fun <reified T : Annotation> Element?.annotation() = this?.getAnnotation(
3232

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

35-
fun Element?.toClassName() = ClassName.get(this as TypeElement)
35+
fun Element?.toClassName(): ClassName? = this?.let { ClassName.get(this as TypeElement) }

dbflow-tests/src/test/java/com/raizlabs/android/dbflow/models/SimpleTestModels.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ class TypeConverterModel(@PrimaryKey var id: Int = 0,
8585

8686
@Table(database = TestDatabase::class)
8787
class EnumTypeConverterModel(@PrimaryKey var id: Int = 0,
88-
@Column var blob: Blob? = null,
89-
@Column(typeConverter = CustomEnumTypeConverter::class)
90-
var difficulty: Difficulty = Difficulty.EASY)
88+
@Column var blob: Blob? = null,
89+
@Column var byteArray: ByteArray? = null,
90+
@Column(typeConverter = CustomEnumTypeConverter::class)
91+
var difficulty: Difficulty = Difficulty.EASY)
9192

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

9798
@Table(database = TestDatabase::class)
9899
@ManyToMany(
99-
generatedTableClassName = "Refund", referencedTable = Transfer::class,
100-
referencedTableColumnName = "refund_in", thisTableColumnName = "refund_out",
101-
saveForeignKeyModels = true
100+
generatedTableClassName = "Refund", referencedTable = Transfer::class,
101+
referencedTableColumnName = "refund_in", thisTableColumnName = "refund_out",
102+
saveForeignKeyModels = true
102103
)
103104
data class Transfer(@PrimaryKey var transfer_id: UUID = UUID.randomUUID())
104105

105106
@Table(database = TestDatabase::class)
106107
data class Transfer2(
107-
@PrimaryKey
108-
var id: UUID = UUID.randomUUID(),
109-
@ForeignKey(stubbedRelationship = true)
110-
var origin: Account? = null
108+
@PrimaryKey
109+
var id: UUID = UUID.randomUUID(),
110+
@ForeignKey(stubbedRelationship = true)
111+
var origin: Account? = null
111112
)
112113

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

151-
override fun getModelValue(data: String) = when(data) {
152+
override fun getModelValue(data: String) = when (data) {
152153
"E" -> Difficulty.EASY
153154
"M" -> Difficulty.MEDIUM
154155
"H" -> Difficulty.HARD

dbflow/src/main/java/com/raizlabs/android/dbflow/config/DatabaseDefinition.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public Map<Integer, List<Migration>> getMigrations() {
241241
public synchronized OpenHelper getHelper() {
242242
if (openHelper == null) {
243243
DatabaseConfig config = FlowManager.getConfig().databaseConfigMap()
244-
.get(getAssociatedDatabaseClassFile());
244+
.get(getAssociatedDatabaseClassFile());
245245
if (config == null || config.helperCreator() == null) {
246246
openHelper = new FlowSQLiteOpenHelper(this, helperListener);
247247
} else {
@@ -261,7 +261,7 @@ public DatabaseWrapper getWritableDatabase() {
261261
public ModelNotifier getModelNotifier() {
262262
if (modelNotifier == null) {
263263
DatabaseConfig config = FlowManager.getConfig().databaseConfigMap()
264-
.get(getAssociatedDatabaseClassFile());
264+
.get(getAssociatedDatabaseClassFile());
265265
if (config == null || config.modelNotifier() == null) {
266266
modelNotifier = new ContentResolverNotifier();
267267
} else {
@@ -375,6 +375,26 @@ public void reset(@Nullable DatabaseConfig databaseConfig) {
375375
}
376376
}
377377

378+
/**
379+
* Reopens the DB with the new {@link DatabaseConfig} specified.
380+
*/
381+
public void reopen(@Nullable DatabaseConfig databaseConfig) {
382+
if (!isResetting) {
383+
close();
384+
openHelper = null;
385+
applyDatabaseConfig(databaseConfig);
386+
getHelper().getDatabase();
387+
isResetting = false;
388+
}
389+
}
390+
391+
/**
392+
* Closes and reopens the database.
393+
*/
394+
public void reopen() {
395+
reopen(databaseConfig);
396+
}
397+
378398
/**
379399
* Deletes the underlying database and destroys it.
380400
*/

0 commit comments

Comments
 (0)