1fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar/*
2fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar * Copyright (C) 2017 The Android Open Source Project
3fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar *
4fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
5fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar * you may not use this file except in compliance with the License.
6fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar * You may obtain a copy of the License at
7fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar *
8fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
9fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar *
10fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar * Unless required by applicable law or agreed to in writing, software
11fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
12fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar * See the License for the specific language governing permissions and
14fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar * limitations under the License.
15fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar */
16fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar
1764db0cc15b78b62a1d44a70fc8b4552e660d952cYigit Boyarpackage android.arch.persistence.room.solver.types
18fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar
1964db0cc15b78b62a1d44a70fc8b4552e660d952cYigit Boyarimport android.arch.persistence.room.ext.L
2064db0cc15b78b62a1d44a70fc8b4552e660d952cYigit Boyarimport android.arch.persistence.room.parser.SQLTypeAffinity
2164db0cc15b78b62a1d44a70fc8b4552e660d952cYigit Boyarimport android.arch.persistence.room.solver.CodeGenScope
22fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyarimport javax.annotation.processing.ProcessingEnvironment
23fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyarimport javax.lang.model.type.TypeKind
24fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar
25fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyarclass ByteArrayColumnTypeAdapter(env : ProcessingEnvironment) : ColumnTypeAdapter(
26fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar        out = env.typeUtils.getArrayType(env.typeUtils.getPrimitiveType(TypeKind.BYTE)),
27fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar        typeAffinity = SQLTypeAffinity.BLOB) {
28fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar    override fun readFromCursor(outVarName: String, cursorVarName: String, indexVarName: String,
29fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar                                scope: CodeGenScope) {
30fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar        scope.builder()
31fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar                .addStatement("$L = $L.getBlob($L)", outVarName, cursorVarName, indexVarName)
32fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar    }
33fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar
34fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar    override fun bindToStmt(stmtName: String, indexVarName: String, valueVarName: String,
35fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar                            scope: CodeGenScope) {
36fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar        scope.builder().apply {
37fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar            beginControlFlow("if ($L == null)", valueVarName)
38fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar                    .addStatement("$L.bindNull($L)", stmtName, indexVarName)
39fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar            nextControlFlow("else")
40fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar                    .addStatement("$L.bindBlob($L, $L)", stmtName, indexVarName, valueVarName)
41fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar            endControlFlow()
42fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar        }
43fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar    }
44fb4fcc8caf2a1908843bd18298447ff6fc498896Yigit Boyar}
45