1 /*
<lambda>null2  * Copyright 2020 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 
17 package androidx.sqlite.inspection.test
18 
19 import androidx.sqlite.inspection.SqliteInspectorProtocol.CellValue
20 import androidx.sqlite.inspection.SqliteInspectorProtocol.CellValue.OneOfCase
21 import androidx.sqlite.inspection.SqliteInspectorProtocol.Command
22 import androidx.sqlite.inspection.SqliteInspectorProtocol.GetSchemaCommand
23 import androidx.sqlite.inspection.SqliteInspectorProtocol.GetSchemaResponse
24 import androidx.sqlite.inspection.SqliteInspectorProtocol.KeepDatabasesOpenCommand
25 import androidx.sqlite.inspection.SqliteInspectorProtocol.KeepDatabasesOpenResponse
26 import androidx.sqlite.inspection.SqliteInspectorProtocol.QueryCommand
27 import androidx.sqlite.inspection.SqliteInspectorProtocol.QueryParameterValue
28 import androidx.sqlite.inspection.SqliteInspectorProtocol.Response
29 import androidx.sqlite.inspection.SqliteInspectorProtocol.TrackDatabasesCommand
30 import androidx.sqlite.inspection.SqliteInspectorProtocol.TrackDatabasesResponse
31 
32 val CellValue.value: Any?
33     get() = valueType.first
34 val CellValue.type: String
35     get() = valueType.second
36 val CellValue.valueType: Pair<Any?, String>
37     get() =
38         when (oneOfCase) {
39             OneOfCase.STRING_VALUE -> stringValue to "text"
40             OneOfCase.LONG_VALUE -> longValue to "integer"
41             OneOfCase.DOUBLE_VALUE -> doubleValue to "float"
42             OneOfCase.BLOB_VALUE -> blobValue.toByteArray().toTypedArray() to "blob"
43             OneOfCase.ONEOF_NOT_SET -> null to "null"
44             else -> throw IllegalArgumentException()
45         }
46 
toTableListnull47 fun GetSchemaResponse.toTableList(): List<Table> =
48     tablesList.map { t -> Table(t.name, t.columnsList.map { c -> Column(c.name, c.type) }) }
49 
50 object MessageFactory {
createTrackDatabasesCommandnull51     fun createTrackDatabasesCommand(): Command =
52         Command.newBuilder().setTrackDatabases(TrackDatabasesCommand.getDefaultInstance()).build()
53 
54     fun createTrackDatabasesResponse(): Response =
55         Response.newBuilder().setTrackDatabases(TrackDatabasesResponse.getDefaultInstance()).build()
56 
57     fun createKeepDatabasesOpenCommand(setEnabled: Boolean): Command =
58         Command.newBuilder()
59             .setKeepDatabasesOpen(KeepDatabasesOpenCommand.newBuilder().setSetEnabled(setEnabled))
60             .build()
61 
62     fun createKeepDatabasesOpenResponse(): Response =
63         Response.newBuilder()
64             .setKeepDatabasesOpen(KeepDatabasesOpenResponse.getDefaultInstance())
65             .build()
66 
67     fun createGetSchemaCommand(databaseId: Int): Command =
68         Command.newBuilder()
69             .setGetSchema(GetSchemaCommand.newBuilder().setDatabaseId(databaseId).build())
70             .build()
71 
72     fun createQueryCommand(
73         databaseId: Int,
74         query: String,
75         queryParams: List<String?>? = null,
76         responseSizeLimitHint: Long? = null
77     ): Command =
78         Command.newBuilder()
79             .setQuery(
80                 QueryCommand.newBuilder()
81                     .setDatabaseId(databaseId)
82                     .setQuery(query)
83                     .also { queryCommandBuilder ->
84                         if (queryParams != null)
85                             queryCommandBuilder.addAllQueryParameterValues(
86                                 queryParams.map { param ->
87                                     QueryParameterValue.newBuilder()
88                                         .also { builder ->
89                                             if (param != null) builder.stringValue = param
90                                         }
91                                         .build()
92                                 }
93                             )
94                         if (responseSizeLimitHint != null) {
95                             queryCommandBuilder.responseSizeLimitHint = responseSizeLimitHint
96                         }
97                     }
98                     .build()
99             )
100             .build()
101 }
102