Yigit Boyar | 2259e4d | 2016-11-25 18:26:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Yigit Boyar | 64db0cc | 2017-04-17 13:18:56 -0700 | [diff] [blame] | 17 | package android.arch.persistence.room.solver |
Yigit Boyar | 2259e4d | 2016-11-25 18:26:10 -0800 | [diff] [blame] | 18 | |
Yigit Boyar | 64db0cc | 2017-04-17 13:18:56 -0700 | [diff] [blame] | 19 | import android.arch.persistence.room.Entity |
Yigit Boyar | 64db0cc | 2017-04-17 13:18:56 -0700 | [diff] [blame] | 20 | import android.arch.persistence.room.ext.hasAnnotation |
| 21 | import android.arch.persistence.room.parser.ParsedQuery |
| 22 | import android.arch.persistence.room.parser.SQLTypeAffinity |
| 23 | import android.arch.persistence.room.processor.Context |
| 24 | import android.arch.persistence.room.processor.EntityProcessor |
| 25 | import android.arch.persistence.room.processor.FieldProcessor |
| 26 | import android.arch.persistence.room.processor.PojoProcessor |
Chris Craik | 9fd8e61 | 2017-06-23 14:07:04 -0700 | [diff] [blame] | 27 | import android.arch.persistence.room.solver.binderprovider.CursorQueryResultBinderProvider |
Yigit Boyar | de23b91 | 2018-01-03 17:36:40 -0800 | [diff] [blame] | 28 | import android.arch.persistence.room.solver.binderprovider.DataSourceQueryResultBinderProvider |
Chris Craik | 9fd8e61 | 2017-06-23 14:07:04 -0700 | [diff] [blame] | 29 | import android.arch.persistence.room.solver.binderprovider.FlowableQueryResultBinderProvider |
| 30 | import android.arch.persistence.room.solver.binderprovider.InstantQueryResultBinderProvider |
| 31 | import android.arch.persistence.room.solver.binderprovider.LiveDataQueryResultBinderProvider |
Chris Craik | dc60cac | 2018-01-10 10:19:39 -0800 | [diff] [blame] | 32 | import android.arch.persistence.room.solver.binderprovider.DataSourceFactoryQueryResultBinderProvider |
Yigit Boyar | 0cf0bfe | 2017-07-16 18:21:55 -0700 | [diff] [blame] | 33 | import android.arch.persistence.room.solver.binderprovider.RxMaybeQueryResultBinderProvider |
| 34 | import android.arch.persistence.room.solver.binderprovider.RxSingleQueryResultBinderProvider |
Yigit Boyar | 64db0cc | 2017-04-17 13:18:56 -0700 | [diff] [blame] | 35 | import android.arch.persistence.room.solver.query.parameter.ArrayQueryParameterAdapter |
| 36 | import android.arch.persistence.room.solver.query.parameter.BasicQueryParameterAdapter |
| 37 | import android.arch.persistence.room.solver.query.parameter.CollectionQueryParameterAdapter |
| 38 | import android.arch.persistence.room.solver.query.parameter.QueryParameterAdapter |
| 39 | import android.arch.persistence.room.solver.query.result.ArrayQueryResultAdapter |
Yigit Boyar | 64db0cc | 2017-04-17 13:18:56 -0700 | [diff] [blame] | 40 | import android.arch.persistence.room.solver.query.result.EntityRowAdapter |
Yigit Boyar | 64db0cc | 2017-04-17 13:18:56 -0700 | [diff] [blame] | 41 | import android.arch.persistence.room.solver.query.result.InstantQueryResultBinder |
| 42 | import android.arch.persistence.room.solver.query.result.ListQueryResultAdapter |
Yigit Boyar | 64db0cc | 2017-04-17 13:18:56 -0700 | [diff] [blame] | 43 | import android.arch.persistence.room.solver.query.result.PojoRowAdapter |
| 44 | import android.arch.persistence.room.solver.query.result.QueryResultAdapter |
| 45 | import android.arch.persistence.room.solver.query.result.QueryResultBinder |
| 46 | import android.arch.persistence.room.solver.query.result.RowAdapter |
| 47 | import android.arch.persistence.room.solver.query.result.SingleColumnRowAdapter |
| 48 | import android.arch.persistence.room.solver.query.result.SingleEntityQueryResultAdapter |
| 49 | import android.arch.persistence.room.solver.types.BoxedBooleanToBoxedIntConverter |
| 50 | import android.arch.persistence.room.solver.types.BoxedPrimitiveColumnTypeAdapter |
Yigit Boyar | 64db0cc | 2017-04-17 13:18:56 -0700 | [diff] [blame] | 51 | import android.arch.persistence.room.solver.types.ByteArrayColumnTypeAdapter |
| 52 | import android.arch.persistence.room.solver.types.ColumnTypeAdapter |
| 53 | import android.arch.persistence.room.solver.types.CompositeAdapter |
| 54 | import android.arch.persistence.room.solver.types.CompositeTypeConverter |
| 55 | import android.arch.persistence.room.solver.types.CursorValueReader |
| 56 | import android.arch.persistence.room.solver.types.NoOpConverter |
| 57 | import android.arch.persistence.room.solver.types.PrimitiveBooleanToIntConverter |
| 58 | import android.arch.persistence.room.solver.types.PrimitiveColumnTypeAdapter |
Yigit Boyar | 64db0cc | 2017-04-17 13:18:56 -0700 | [diff] [blame] | 59 | import android.arch.persistence.room.solver.types.StatementValueBinder |
| 60 | import android.arch.persistence.room.solver.types.StringColumnTypeAdapter |
| 61 | import android.arch.persistence.room.solver.types.TypeConverter |
Yigit Boyar | 846dfcf | 2017-01-16 18:05:20 -0800 | [diff] [blame] | 62 | import com.google.auto.common.MoreElements |
Yigit Boyar | 250a3e6 | 2016-11-29 18:06:52 -0800 | [diff] [blame] | 63 | import com.google.auto.common.MoreTypes |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 64 | import com.google.common.annotations.VisibleForTesting |
Aurimas Liutikas | 6f1f556 | 2017-11-17 10:05:57 -0800 | [diff] [blame] | 65 | import java.util.LinkedList |
Yigit Boyar | 250a3e6 | 2016-11-29 18:06:52 -0800 | [diff] [blame] | 66 | import javax.lang.model.type.ArrayType |
Yigit Boyar | efaf86a | 2016-12-02 15:16:35 -0800 | [diff] [blame] | 67 | import javax.lang.model.type.TypeKind |
Yigit Boyar | 2259e4d | 2016-11-25 18:26:10 -0800 | [diff] [blame] | 68 | import javax.lang.model.type.TypeMirror |
| 69 | |
Yigit Boyar | 250a3e6 | 2016-11-29 18:06:52 -0800 | [diff] [blame] | 70 | @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") |
Yigit Boyar | 2259e4d | 2016-11-25 18:26:10 -0800 | [diff] [blame] | 71 | /** |
| 72 | * Holds all type adapters and can create on demand composite type adapters to convert a type into a |
| 73 | * database column. |
| 74 | */ |
Aurimas Liutikas | 6f1f556 | 2017-11-17 10:05:57 -0800 | [diff] [blame] | 75 | class TypeAdapterStore private constructor( |
| 76 | val context: Context, |
| 77 | /** |
| 78 | * first type adapter has the highest priority |
| 79 | */ |
| 80 | private val columnTypeAdapters: List<ColumnTypeAdapter>, |
| 81 | /** |
| 82 | * first converter has the highest priority |
| 83 | */ |
| 84 | private val typeConverters: List<TypeConverter>) { |
Yigit Boyar | aa82fce | 2017-02-02 00:08:36 -0800 | [diff] [blame] | 85 | |
Yigit Boyar | 7a705bc | 2017-04-10 09:32:55 -0700 | [diff] [blame] | 86 | companion object { |
Chris Craik | 9fd8e61 | 2017-06-23 14:07:04 -0700 | [diff] [blame] | 87 | fun copy(context: Context, store: TypeAdapterStore): TypeAdapterStore { |
Yigit Boyar | 7a705bc | 2017-04-10 09:32:55 -0700 | [diff] [blame] | 88 | return TypeAdapterStore(context = context, |
| 89 | columnTypeAdapters = store.columnTypeAdapters, |
| 90 | typeConverters = store.typeConverters) |
| 91 | } |
| 92 | |
Chris Craik | 9fd8e61 | 2017-06-23 14:07:04 -0700 | [diff] [blame] | 93 | fun create(context: Context, @VisibleForTesting vararg extras: Any): TypeAdapterStore { |
Yigit Boyar | 7a705bc | 2017-04-10 09:32:55 -0700 | [diff] [blame] | 94 | val adapters = arrayListOf<ColumnTypeAdapter>() |
| 95 | val converters = arrayListOf<TypeConverter>() |
| 96 | |
| 97 | fun addAny(extra: Any?) { |
| 98 | when (extra) { |
| 99 | is TypeConverter -> converters.add(extra) |
| 100 | is ColumnTypeAdapter -> adapters.add(extra) |
| 101 | is List<*> -> extra.forEach(::addAny) |
| 102 | else -> throw IllegalArgumentException("unknown extra $extra") |
| 103 | } |
Yigit Boyar | 2259e4d | 2016-11-25 18:26:10 -0800 | [diff] [blame] | 104 | } |
Yigit Boyar | aa82fce | 2017-02-02 00:08:36 -0800 | [diff] [blame] | 105 | |
Yigit Boyar | 7a705bc | 2017-04-10 09:32:55 -0700 | [diff] [blame] | 106 | extras.forEach(::addAny) |
| 107 | fun addTypeConverter(converter: TypeConverter) { |
| 108 | converters.add(converter) |
| 109 | } |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 110 | |
Yigit Boyar | 7a705bc | 2017-04-10 09:32:55 -0700 | [diff] [blame] | 111 | fun addColumnAdapter(adapter: ColumnTypeAdapter) { |
| 112 | adapters.add(adapter) |
| 113 | } |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 114 | |
Yigit Boyar | 7a705bc | 2017-04-10 09:32:55 -0700 | [diff] [blame] | 115 | val primitives = PrimitiveColumnTypeAdapter |
| 116 | .createPrimitiveAdapters(context.processingEnv) |
| 117 | primitives.forEach(::addColumnAdapter) |
| 118 | BoxedPrimitiveColumnTypeAdapter |
| 119 | .createBoxedPrimitiveAdapters(context.processingEnv, primitives) |
| 120 | .forEach(::addColumnAdapter) |
| 121 | addColumnAdapter(StringColumnTypeAdapter(context.processingEnv)) |
| 122 | addColumnAdapter(ByteArrayColumnTypeAdapter(context.processingEnv)) |
| 123 | PrimitiveBooleanToIntConverter.create(context.processingEnv).forEach(::addTypeConverter) |
Yigit Boyar | 7a705bc | 2017-04-10 09:32:55 -0700 | [diff] [blame] | 124 | BoxedBooleanToBoxedIntConverter.create(context.processingEnv) |
| 125 | .forEach(::addTypeConverter) |
| 126 | return TypeAdapterStore(context = context, columnTypeAdapters = adapters, |
| 127 | typeConverters = converters) |
| 128 | } |
Yigit Boyar | 2259e4d | 2016-11-25 18:26:10 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Chris Craik | 9fd8e61 | 2017-06-23 14:07:04 -0700 | [diff] [blame] | 131 | val queryResultBinderProviders = listOf( |
| 132 | CursorQueryResultBinderProvider(context), |
| 133 | LiveDataQueryResultBinderProvider(context), |
| 134 | FlowableQueryResultBinderProvider(context), |
Yigit Boyar | 0cf0bfe | 2017-07-16 18:21:55 -0700 | [diff] [blame] | 135 | RxMaybeQueryResultBinderProvider(context), |
| 136 | RxSingleQueryResultBinderProvider(context), |
Chris Craik | 24418e9 | 2017-08-18 10:25:44 -0700 | [diff] [blame] | 137 | DataSourceQueryResultBinderProvider(context), |
Chris Craik | dc60cac | 2018-01-10 10:19:39 -0800 | [diff] [blame] | 138 | DataSourceFactoryQueryResultBinderProvider(context), |
Chris Craik | 9fd8e61 | 2017-06-23 14:07:04 -0700 | [diff] [blame] | 139 | InstantQueryResultBinderProvider(context) |
| 140 | ) |
Yigit Boyar | 3c592c4 | 2017-04-04 14:53:04 -0700 | [diff] [blame] | 141 | |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 142 | // type mirrors that be converted into columns w/o an extra converter |
| 143 | private val knownColumnTypeMirrors by lazy { |
| 144 | columnTypeAdapters.map { it.out } |
| 145 | } |
| 146 | |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 147 | /** |
| 148 | * Searches 1 way to bind a value into a statement. |
| 149 | */ |
Aurimas Liutikas | 6f1f556 | 2017-11-17 10:05:57 -0800 | [diff] [blame] | 150 | fun findStatementValueBinder( |
| 151 | input: TypeMirror, |
| 152 | affinity: SQLTypeAffinity? |
| 153 | ): StatementValueBinder? { |
Yigit Boyar | 8a9e8c0 | 2017-02-28 15:55:33 -0800 | [diff] [blame] | 154 | if (input.kind == TypeKind.ERROR) { |
| 155 | return null |
| 156 | } |
Yigit Boyar | 275e708 | 2017-02-02 10:00:11 -0800 | [diff] [blame] | 157 | val adapter = findDirectAdapterFor(input, affinity) |
| 158 | if (adapter != null) { |
| 159 | return adapter |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 160 | } |
Yigit Boyar | 275e708 | 2017-02-02 10:00:11 -0800 | [diff] [blame] | 161 | val targetTypes = targetTypeMirrorsFor(affinity) |
| 162 | val binder = findTypeConverter(input, targetTypes) ?: return null |
Yigit Boyar | 8af88c1 | 2018-01-11 11:09:19 -0800 | [diff] [blame] | 163 | // columnAdapter should not be null but we are receiving errors on crash in `first()` so |
| 164 | // this safeguard allows us to dispatch the real problem to the user (e.g. why we couldn't |
| 165 | // find the right adapter) |
| 166 | val columnAdapter = getAllColumnAdapters(binder.to).firstOrNull() ?: return null |
| 167 | return CompositeAdapter(input, columnAdapter, binder, null) |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
Yigit Boyar | 275e708 | 2017-02-02 10:00:11 -0800 | [diff] [blame] | 171 | * Returns which entities targets the given affinity. |
| 172 | */ |
Chris Craik | 9fd8e61 | 2017-06-23 14:07:04 -0700 | [diff] [blame] | 173 | private fun targetTypeMirrorsFor(affinity: SQLTypeAffinity?): List<TypeMirror> { |
Yigit Boyar | 275e708 | 2017-02-02 10:00:11 -0800 | [diff] [blame] | 174 | val specifiedTargets = affinity?.getTypeMirrors(context.processingEnv) |
Chris Craik | 9fd8e61 | 2017-06-23 14:07:04 -0700 | [diff] [blame] | 175 | return if (specifiedTargets == null || specifiedTargets.isEmpty()) { |
Yigit Boyar | 275e708 | 2017-02-02 10:00:11 -0800 | [diff] [blame] | 176 | knownColumnTypeMirrors |
| 177 | } else { |
| 178 | specifiedTargets |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Searches 1 way to read it from cursor |
| 184 | */ |
Chris Craik | 9fd8e61 | 2017-06-23 14:07:04 -0700 | [diff] [blame] | 185 | fun findCursorValueReader(output: TypeMirror, affinity: SQLTypeAffinity?): CursorValueReader? { |
Yigit Boyar | 8a9e8c0 | 2017-02-28 15:55:33 -0800 | [diff] [blame] | 186 | if (output.kind == TypeKind.ERROR) { |
| 187 | return null |
| 188 | } |
Yigit Boyar | 275e708 | 2017-02-02 10:00:11 -0800 | [diff] [blame] | 189 | val adapter = findColumnTypeAdapter(output, affinity) |
| 190 | if (adapter != null) { |
| 191 | // two way is better |
| 192 | return adapter |
| 193 | } |
| 194 | // we could not find a two way version, search for anything |
| 195 | val targetTypes = targetTypeMirrorsFor(affinity) |
| 196 | val converter = findTypeConverter(targetTypes, output) ?: return null |
| 197 | return CompositeAdapter(output, |
| 198 | getAllColumnAdapters(converter.from).first(), null, converter) |
| 199 | } |
| 200 | |
| 201 | /** |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 202 | * Tries to reverse the converter going through the same nodes, if possible. |
| 203 | */ |
| 204 | @VisibleForTesting |
Chris Craik | 9fd8e61 | 2017-06-23 14:07:04 -0700 | [diff] [blame] | 205 | fun reverse(converter: TypeConverter): TypeConverter? { |
| 206 | return when (converter) { |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 207 | is NoOpConverter -> converter |
Chris Craik | 9fd8e61 | 2017-06-23 14:07:04 -0700 | [diff] [blame] | 208 | is CompositeTypeConverter -> { |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 209 | val r1 = reverse(converter.conv1) ?: return null |
| 210 | val r2 = reverse(converter.conv2) ?: return null |
| 211 | CompositeTypeConverter(r2, r1) |
| 212 | } |
| 213 | else -> { |
| 214 | val types = context.processingEnv.typeUtils |
| 215 | typeConverters.firstOrNull { |
| 216 | types.isSameType(it.from, converter.to) && types |
| 217 | .isSameType(it.to, converter.from) |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Finds a two way converter, if you need 1 way, use findStatementValueBinder or |
| 225 | * findCursorValueReader. |
| 226 | */ |
Aurimas Liutikas | 6f1f556 | 2017-11-17 10:05:57 -0800 | [diff] [blame] | 227 | fun findColumnTypeAdapter(out: TypeMirror, affinity: SQLTypeAffinity?): ColumnTypeAdapter? { |
Yigit Boyar | 8a9e8c0 | 2017-02-28 15:55:33 -0800 | [diff] [blame] | 228 | if (out.kind == TypeKind.ERROR) { |
| 229 | return null |
| 230 | } |
Yigit Boyar | 275e708 | 2017-02-02 10:00:11 -0800 | [diff] [blame] | 231 | val adapter = findDirectAdapterFor(out, affinity) |
| 232 | if (adapter != null) { |
| 233 | return adapter |
Yigit Boyar | 2259e4d | 2016-11-25 18:26:10 -0800 | [diff] [blame] | 234 | } |
Yigit Boyar | 275e708 | 2017-02-02 10:00:11 -0800 | [diff] [blame] | 235 | val targetTypes = targetTypeMirrorsFor(affinity) |
| 236 | val intoStatement = findTypeConverter(out, targetTypes) ?: return null |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 237 | // ok found a converter, try the reverse now |
| 238 | val fromCursor = reverse(intoStatement) ?: findTypeConverter(intoStatement.to, out) |
| 239 | ?: return null |
| 240 | return CompositeAdapter(out, getAllColumnAdapters(intoStatement.to).first(), intoStatement, |
| 241 | fromCursor) |
Yigit Boyar | 2259e4d | 2016-11-25 18:26:10 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Aurimas Liutikas | 6f1f556 | 2017-11-17 10:05:57 -0800 | [diff] [blame] | 244 | private fun findDirectAdapterFor( |
| 245 | out: TypeMirror, affinity: SQLTypeAffinity?): ColumnTypeAdapter? { |
Yigit Boyar | 275e708 | 2017-02-02 10:00:11 -0800 | [diff] [blame] | 246 | val adapter = getAllColumnAdapters(out).firstOrNull { |
| 247 | affinity == null || it.typeAffinity == affinity |
| 248 | } |
| 249 | return adapter |
| 250 | } |
| 251 | |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 252 | fun findTypeConverter(input: TypeMirror, output: TypeMirror): TypeConverter? { |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 253 | return findTypeConverter(listOf(input), listOf(output)) |
Yigit Boyar | 2259e4d | 2016-11-25 18:26:10 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Yigit Boyar | 13a2048 | 2017-01-25 17:52:09 -0800 | [diff] [blame] | 256 | fun findQueryResultBinder(typeMirror: TypeMirror, query: ParsedQuery): QueryResultBinder { |
Yigit Boyar | 846dfcf | 2017-01-16 18:05:20 -0800 | [diff] [blame] | 257 | return if (typeMirror.kind == TypeKind.DECLARED) { |
| 258 | val declared = MoreTypes.asDeclared(typeMirror) |
Chris Craik | 9fd8e61 | 2017-06-23 14:07:04 -0700 | [diff] [blame] | 259 | return queryResultBinderProviders.first { |
| 260 | it.matches(declared) |
| 261 | }.provide(declared, query) |
Yigit Boyar | 846dfcf | 2017-01-16 18:05:20 -0800 | [diff] [blame] | 262 | } else { |
Yigit Boyar | 13a2048 | 2017-01-25 17:52:09 -0800 | [diff] [blame] | 263 | InstantQueryResultBinder(findQueryResultAdapter(typeMirror, query)) |
Yigit Boyar | 846dfcf | 2017-01-16 18:05:20 -0800 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Aurimas Liutikas | 6f1f556 | 2017-11-17 10:05:57 -0800 | [diff] [blame] | 267 | fun findQueryResultAdapter(typeMirror: TypeMirror, query: ParsedQuery): QueryResultAdapter? { |
Yigit Boyar | 8a9e8c0 | 2017-02-28 15:55:33 -0800 | [diff] [blame] | 268 | if (typeMirror.kind == TypeKind.ERROR) { |
| 269 | return null |
| 270 | } |
Yigit Boyar | efaf86a | 2016-12-02 15:16:35 -0800 | [diff] [blame] | 271 | if (typeMirror.kind == TypeKind.DECLARED) { |
| 272 | val declared = MoreTypes.asDeclared(typeMirror) |
| 273 | if (declared.typeArguments.isEmpty()) { |
Yigit Boyar | 13a2048 | 2017-01-25 17:52:09 -0800 | [diff] [blame] | 274 | val rowAdapter = findRowAdapter(typeMirror, query) ?: return null |
Yigit Boyar | efaf86a | 2016-12-02 15:16:35 -0800 | [diff] [blame] | 275 | return SingleEntityQueryResultAdapter(rowAdapter) |
| 276 | } |
Yigit Boyar | efaf86a | 2016-12-02 15:16:35 -0800 | [diff] [blame] | 277 | if (MoreTypes.isTypeOf(java.util.List::class.java, typeMirror)) { |
| 278 | val typeArg = declared.typeArguments.first() |
Yigit Boyar | 13a2048 | 2017-01-25 17:52:09 -0800 | [diff] [blame] | 279 | val rowAdapter = findRowAdapter(typeArg, query) ?: return null |
Yigit Boyar | efaf86a | 2016-12-02 15:16:35 -0800 | [diff] [blame] | 280 | return ListQueryResultAdapter(rowAdapter) |
| 281 | } |
| 282 | return null |
Yuichi Araki | bccb62d | 2017-06-26 18:19:52 +0900 | [diff] [blame] | 283 | } else if (typeMirror is ArrayType && typeMirror.componentType.kind != TypeKind.BYTE) { |
Yigit Boyar | 13a2048 | 2017-01-25 17:52:09 -0800 | [diff] [blame] | 284 | val rowAdapter = |
Yuichi Araki | bccb62d | 2017-06-26 18:19:52 +0900 | [diff] [blame] | 285 | findRowAdapter(typeMirror.componentType, query) ?: return null |
Yigit Boyar | efaf86a | 2016-12-02 15:16:35 -0800 | [diff] [blame] | 286 | return ArrayQueryResultAdapter(rowAdapter) |
| 287 | } else { |
Yigit Boyar | 13a2048 | 2017-01-25 17:52:09 -0800 | [diff] [blame] | 288 | val rowAdapter = findRowAdapter(typeMirror, query) ?: return null |
Yigit Boyar | efaf86a | 2016-12-02 15:16:35 -0800 | [diff] [blame] | 289 | return SingleEntityQueryResultAdapter(rowAdapter) |
| 290 | } |
| 291 | } |
| 292 | |
Yigit Boyar | 13a2048 | 2017-01-25 17:52:09 -0800 | [diff] [blame] | 293 | /** |
| 294 | * Find a converter from cursor to the given type mirror. |
| 295 | * If there is information about the query result, we try to use it to accept *any* POJO. |
| 296 | */ |
| 297 | @VisibleForTesting |
| 298 | fun findRowAdapter(typeMirror: TypeMirror, query: ParsedQuery): RowAdapter? { |
Yigit Boyar | 8a9e8c0 | 2017-02-28 15:55:33 -0800 | [diff] [blame] | 299 | if (typeMirror.kind == TypeKind.ERROR) { |
| 300 | return null |
| 301 | } |
Yigit Boyar | efaf86a | 2016-12-02 15:16:35 -0800 | [diff] [blame] | 302 | if (typeMirror.kind == TypeKind.DECLARED) { |
| 303 | val declared = MoreTypes.asDeclared(typeMirror) |
| 304 | if (declared.typeArguments.isNotEmpty()) { |
| 305 | // TODO one day support this |
| 306 | return null |
| 307 | } |
Yigit Boyar | 092164e | 2017-02-26 13:25:17 -0800 | [diff] [blame] | 308 | val resultInfo = query.resultInfo |
Yigit Boyar | 8a9e8c0 | 2017-02-28 15:55:33 -0800 | [diff] [blame] | 309 | |
| 310 | val (rowAdapter, rowAdapterLogs) = if (resultInfo != null && query.errors.isEmpty() |
| 311 | && resultInfo.error == null) { |
| 312 | // if result info is not null, first try a pojo row adapter |
| 313 | context.collectLogs { subContext -> |
| 314 | val pojo = PojoProcessor( |
| 315 | baseContext = subContext, |
| 316 | element = MoreTypes.asTypeElement(typeMirror), |
| 317 | bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR, |
| 318 | parent = null |
| 319 | ).process() |
| 320 | PojoRowAdapter( |
| 321 | context = subContext, |
| 322 | info = resultInfo, |
| 323 | pojo = pojo, |
| 324 | out = typeMirror) |
| 325 | } |
| 326 | } else { |
| 327 | Pair(null, null) |
| 328 | } |
| 329 | |
Yigit Boyar | f8c3624 | 2017-04-10 14:28:17 -0700 | [diff] [blame] | 330 | if (rowAdapter == null && query.resultInfo == null) { |
| 331 | // we don't know what query returns. Check for entity. |
| 332 | val asElement = MoreTypes.asElement(typeMirror) |
| 333 | if (asElement.hasAnnotation(Entity::class)) { |
| 334 | return EntityRowAdapter(EntityProcessor(context, |
| 335 | MoreElements.asType(asElement)).process()) |
| 336 | } |
| 337 | } |
| 338 | |
Yigit Boyar | de23b91 | 2018-01-03 17:36:40 -0800 | [diff] [blame] | 339 | if (rowAdapter != null && rowAdapterLogs?.hasErrors() != true) { |
Yigit Boyar | 8a9e8c0 | 2017-02-28 15:55:33 -0800 | [diff] [blame] | 340 | rowAdapterLogs?.writeTo(context.processingEnv) |
| 341 | return rowAdapter |
| 342 | } |
| 343 | |
Yigit Boyar | 13a2048 | 2017-01-25 17:52:09 -0800 | [diff] [blame] | 344 | if ((resultInfo?.columns?.size ?: 1) == 1) { |
Yigit Boyar | 8a9e8c0 | 2017-02-28 15:55:33 -0800 | [diff] [blame] | 345 | val singleColumn = findCursorValueReader(typeMirror, |
Yigit Boyar | 275e708 | 2017-02-02 10:00:11 -0800 | [diff] [blame] | 346 | resultInfo?.columns?.get(0)?.type) |
Yigit Boyar | 13a2048 | 2017-01-25 17:52:09 -0800 | [diff] [blame] | 347 | if (singleColumn != null) { |
| 348 | return SingleColumnRowAdapter(singleColumn) |
| 349 | } |
Yigit Boyar | efaf86a | 2016-12-02 15:16:35 -0800 | [diff] [blame] | 350 | } |
Yigit Boyar | 8a9e8c0 | 2017-02-28 15:55:33 -0800 | [diff] [blame] | 351 | // if we tried, return its errors |
| 352 | if (rowAdapter != null) { |
| 353 | rowAdapterLogs?.writeTo(context.processingEnv) |
| 354 | return rowAdapter |
Yigit Boyar | 13a2048 | 2017-01-25 17:52:09 -0800 | [diff] [blame] | 355 | } |
Yigit Boyar | de23b91 | 2018-01-03 17:36:40 -0800 | [diff] [blame] | 356 | if (query.runtimeQueryPlaceholder) { |
| 357 | // just go w/ pojo and hope for the best. this happens for @RawQuery where we |
| 358 | // try to guess user's intention and hope that their query fits the result. |
| 359 | val pojo = PojoProcessor( |
| 360 | baseContext = context, |
| 361 | element = MoreTypes.asTypeElement(typeMirror), |
| 362 | bindingScope = FieldProcessor.BindingScope.READ_FROM_CURSOR, |
| 363 | parent = null |
| 364 | ).process() |
| 365 | return PojoRowAdapter( |
| 366 | context = context, |
| 367 | info = null, |
| 368 | pojo = pojo, |
| 369 | out = typeMirror) |
| 370 | } |
Yigit Boyar | efaf86a | 2016-12-02 15:16:35 -0800 | [diff] [blame] | 371 | return null |
| 372 | } else { |
Yigit Boyar | 8a9e8c0 | 2017-02-28 15:55:33 -0800 | [diff] [blame] | 373 | val singleColumn = findCursorValueReader(typeMirror, null) ?: return null |
Yigit Boyar | efaf86a | 2016-12-02 15:16:35 -0800 | [diff] [blame] | 374 | return SingleColumnRowAdapter(singleColumn) |
| 375 | } |
| 376 | } |
| 377 | |
Yigit Boyar | 846dfcf | 2017-01-16 18:05:20 -0800 | [diff] [blame] | 378 | fun findQueryParameterAdapter(typeMirror: TypeMirror): QueryParameterAdapter? { |
Yigit Boyar | 250a3e6 | 2016-11-29 18:06:52 -0800 | [diff] [blame] | 379 | if (MoreTypes.isType(typeMirror) |
| 380 | && (MoreTypes.isTypeOf(java.util.List::class.java, typeMirror) |
Yigit Boyar | 846dfcf | 2017-01-16 18:05:20 -0800 | [diff] [blame] | 381 | || MoreTypes.isTypeOf(java.util.Set::class.java, typeMirror))) { |
Yigit Boyar | 250a3e6 | 2016-11-29 18:06:52 -0800 | [diff] [blame] | 382 | val declared = MoreTypes.asDeclared(typeMirror) |
Yigit Boyar | 275e708 | 2017-02-02 10:00:11 -0800 | [diff] [blame] | 383 | val binder = findStatementValueBinder(declared.typeArguments.first(), |
Yigit Boyar | 2fb00f1 | 2017-12-15 13:18:33 -0800 | [diff] [blame] | 384 | null) |
| 385 | if (binder != null) { |
| 386 | return CollectionQueryParameterAdapter(binder) |
| 387 | } else { |
| 388 | // maybe user wants to convert this collection themselves. look for a match |
| 389 | val collectionBinder = findStatementValueBinder(typeMirror, null) ?: return null |
| 390 | return BasicQueryParameterAdapter(collectionBinder) |
| 391 | } |
Yuichi Araki | bccb62d | 2017-06-26 18:19:52 +0900 | [diff] [blame] | 392 | } else if (typeMirror is ArrayType && typeMirror.componentType.kind != TypeKind.BYTE) { |
Yigit Boyar | 250a3e6 | 2016-11-29 18:06:52 -0800 | [diff] [blame] | 393 | val component = typeMirror.componentType |
Yigit Boyar | 275e708 | 2017-02-02 10:00:11 -0800 | [diff] [blame] | 394 | val binder = findStatementValueBinder(component, null) ?: return null |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 395 | return ArrayQueryParameterAdapter(binder) |
Yigit Boyar | 250a3e6 | 2016-11-29 18:06:52 -0800 | [diff] [blame] | 396 | } else { |
Yigit Boyar | 275e708 | 2017-02-02 10:00:11 -0800 | [diff] [blame] | 397 | val binder = findStatementValueBinder(typeMirror, null) ?: return null |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 398 | return BasicQueryParameterAdapter(binder) |
Yigit Boyar | 250a3e6 | 2016-11-29 18:06:52 -0800 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 402 | private fun findTypeConverter(input: TypeMirror, outputs: List<TypeMirror>): TypeConverter? { |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 403 | return findTypeConverter(listOf(input), outputs) |
| 404 | } |
| 405 | |
Chris Craik | 9fd8e61 | 2017-06-23 14:07:04 -0700 | [diff] [blame] | 406 | private fun findTypeConverter(input: List<TypeMirror>, output: TypeMirror): TypeConverter? { |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 407 | return findTypeConverter(input, listOf(output)) |
| 408 | } |
| 409 | |
Aurimas Liutikas | 6f1f556 | 2017-11-17 10:05:57 -0800 | [diff] [blame] | 410 | private fun findTypeConverter( |
| 411 | inputs: List<TypeMirror>, outputs: List<TypeMirror>): TypeConverter? { |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 412 | if (inputs.isEmpty()) { |
| 413 | return null |
Yigit Boyar | 250a3e6 | 2016-11-29 18:06:52 -0800 | [diff] [blame] | 414 | } |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 415 | val types = context.processingEnv.typeUtils |
| 416 | inputs.forEach { input -> |
| 417 | if (outputs.any { output -> types.isSameType(input, output) }) { |
| 418 | return NoOpConverter(input) |
| 419 | } |
| 420 | } |
| 421 | |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 422 | val excludes = arrayListOf<TypeMirror>() |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 423 | |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 424 | val queue = LinkedList<TypeConverter>() |
Aurimas Liutikas | 6f1f556 | 2017-11-17 10:05:57 -0800 | [diff] [blame] | 425 | fun exactMatch(candidates: List<TypeConverter>): TypeConverter? { |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 426 | return candidates.firstOrNull { |
Yigit Boyar | 846dfcf | 2017-01-16 18:05:20 -0800 | [diff] [blame] | 427 | outputs.any { output -> types.isSameType(output, it.to) } |
| 428 | } |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 429 | } |
| 430 | inputs.forEach { input -> |
| 431 | val candidates = getAllTypeConverters(input, excludes) |
Chris Craik | 9fd8e61 | 2017-06-23 14:07:04 -0700 | [diff] [blame] | 432 | val match = exactMatch(candidates) |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 433 | if (match != null) { |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 434 | return match |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 435 | } |
| 436 | candidates.forEach { |
| 437 | excludes.add(it.to) |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 438 | queue.add(it) |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 439 | } |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 440 | } |
| 441 | excludes.addAll(inputs) |
| 442 | while (queue.isNotEmpty()) { |
| 443 | val prev = queue.pop() |
| 444 | val from = prev.to |
| 445 | val candidates = getAllTypeConverters(from, excludes) |
Chris Craik | 9fd8e61 | 2017-06-23 14:07:04 -0700 | [diff] [blame] | 446 | val match = exactMatch(candidates) |
Yigit Boyar | fb4fcc8 | 2017-02-03 15:22:01 -0800 | [diff] [blame] | 447 | if (match != null) { |
| 448 | return CompositeTypeConverter(prev, match) |
| 449 | } |
| 450 | candidates.forEach { |
| 451 | excludes.add(it.to) |
| 452 | queue.add(CompositeTypeConverter(prev, it)) |
| 453 | } |
| 454 | } |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 455 | return null |
Yigit Boyar | 2259e4d | 2016-11-25 18:26:10 -0800 | [diff] [blame] | 456 | } |
| 457 | |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 458 | private fun getAllColumnAdapters(input: TypeMirror): List<ColumnTypeAdapter> { |
Yigit Boyar | 2259e4d | 2016-11-25 18:26:10 -0800 | [diff] [blame] | 459 | return columnTypeAdapters.filter { |
Yigit Boyar | 250a3e6 | 2016-11-29 18:06:52 -0800 | [diff] [blame] | 460 | context.processingEnv.typeUtils.isSameType(input, it.out) |
Yigit Boyar | 2259e4d | 2016-11-25 18:26:10 -0800 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Yigit Boyar | 846dfcf | 2017-01-16 18:05:20 -0800 | [diff] [blame] | 464 | private fun getAllTypeConverters(input: TypeMirror, excludes: List<TypeMirror>): |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 465 | List<TypeConverter> { |
Yigit Boyar | 250a3e6 | 2016-11-29 18:06:52 -0800 | [diff] [blame] | 466 | val types = context.processingEnv.typeUtils |
Yigit Boyar | 2fb00f1 | 2017-12-15 13:18:33 -0800 | [diff] [blame] | 467 | // for input, check assignability because it defines whether we can use the method or not. |
| 468 | // for excludes, use exact match |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 469 | return typeConverters.filter { converter -> |
Yigit Boyar | 2fb00f1 | 2017-12-15 13:18:33 -0800 | [diff] [blame] | 470 | types.isAssignable(input, converter.from) && |
Yigit Boyar | 8e543c4 | 2016-11-29 16:49:46 -0800 | [diff] [blame] | 471 | !excludes.any { types.isSameType(it, converter.to) } |
Yigit Boyar | 2259e4d | 2016-11-25 18:26:10 -0800 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | } |