1 /*
2  * Copyright 2024 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.room
18 
19 import androidx.sqlite.SQLiteStatement
20 import kotlin.jvm.JvmOverloads
21 
22 /**
23  * A query with an argument binding function.
24  *
25  * @see [RawQuery]
26  */
27 class RoomRawQuery
28 @JvmOverloads
29 constructor(
30     /**
31      * The SQL query.
32      *
33      * The query can have placeholders (?) to bind arguments.
34      */
35     val sql: String,
36     /**
37      * The function that receives a [SQLiteStatement] and binds arguments.
38      *
39      * Only `bind*()` calls should be invoked on the received statement.
40      */
<lambda>null41     onBindStatement: (SQLiteStatement) -> Unit = {}
42 ) {
<lambda>null43     private val bindingFunction: (SQLiteStatement) -> Unit = {
44         onBindStatement.invoke(BindOnlySQLiteStatement(it))
45     }
46 
getBindingFunctionnull47     fun getBindingFunction(): (SQLiteStatement) -> Unit = bindingFunction
48 }
49 
50 private class BindOnlySQLiteStatement(delegate: SQLiteStatement) : SQLiteStatement by delegate {
51 
52     override fun getBlob(index: Int): ByteArray {
53         error(ONLY_BIND_CALLS_ALLOWED_ERROR)
54     }
55 
56     override fun getDouble(index: Int): Double {
57         error(ONLY_BIND_CALLS_ALLOWED_ERROR)
58     }
59 
60     override fun getLong(index: Int): Long {
61         error(ONLY_BIND_CALLS_ALLOWED_ERROR)
62     }
63 
64     override fun getText(index: Int): String {
65         error(ONLY_BIND_CALLS_ALLOWED_ERROR)
66     }
67 
68     override fun isNull(index: Int): Boolean {
69         error(ONLY_BIND_CALLS_ALLOWED_ERROR)
70     }
71 
72     override fun getColumnCount(): Int {
73         error(ONLY_BIND_CALLS_ALLOWED_ERROR)
74     }
75 
76     override fun getColumnName(index: Int): String {
77         error(ONLY_BIND_CALLS_ALLOWED_ERROR)
78     }
79 
80     override fun getColumnType(index: Int): Int {
81         error(ONLY_BIND_CALLS_ALLOWED_ERROR)
82     }
83 
84     override fun step(): Boolean {
85         error(ONLY_BIND_CALLS_ALLOWED_ERROR)
86     }
87 
88     override fun reset() {
89         error(ONLY_BIND_CALLS_ALLOWED_ERROR)
90     }
91 
92     override fun close() {
93         error(ONLY_BIND_CALLS_ALLOWED_ERROR)
94     }
95 
96     companion object {
97         private const val ONLY_BIND_CALLS_ALLOWED_ERROR =
98             "Only bind*() calls are allowed on the RoomRawQuery received statement."
99     }
100 }
101