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.sqldeligttestapp.app
18 
19 import androidx.sqlite.inspection.sqldeligttestapp.Database
20 import androidx.sqlite.inspection.test.TestEntity
21 import androidx.sqlite.inspection.test.TestEntityQueries
22 import com.squareup.sqldelight.Query
23 import com.squareup.sqldelight.TransacterImpl
24 import com.squareup.sqldelight.db.SqlDriver
25 import com.squareup.sqldelight.internal.copyOnWriteList
26 import kotlin.Any
27 import kotlin.Int
28 import kotlin.Long
29 import kotlin.String
30 import kotlin.collections.MutableList
31 import kotlin.reflect.KClass
32 
33 internal val KClass<Database>.schema: SqlDriver.Schema
34     get() = DatabaseImpl.Schema
35 
36 internal fun KClass<Database>.newInstance(driver: SqlDriver): Database = DatabaseImpl(driver)
37 
38 private class DatabaseImpl(driver: SqlDriver) : TransacterImpl(driver), Database {
39     override val testEntityQueries: TestEntityQueriesImpl = TestEntityQueriesImpl(this, driver)
40 
41     object Schema : SqlDriver.Schema {
42         override val version: Int
43             get() = 1
44 
45         override fun create(driver: SqlDriver) {
46             driver.execute(
47                 null,
48                 """
49           |CREATE TABLE TestEntity(
50           |    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
51           |    value TEXT NOT NULL
52           |)
53           """
54                     .trimMargin(),
55                 0
56             )
57         }
58 
59         override fun migrate(driver: SqlDriver, oldVersion: Int, newVersion: Int) {}
60     }
61 }
62 
63 private class TestEntityQueriesImpl(
64     private val database: DatabaseImpl,
65     private val driver: SqlDriver
66 ) : TransacterImpl(driver), TestEntityQueries {
67     internal val selectAll: MutableList<Query<*>> = copyOnWriteList()
68 
selectAllnull69     override fun <T : Any> selectAll(mapper: (id: Long, value: String) -> T): Query<T> =
70         Query(
71             186750743,
72             selectAll,
73             driver,
74             "TestEntity.sq",
75             "selectAll",
76             "SELECT * FROM TestEntity"
77         ) { cursor ->
78             mapper(cursor.getLong(0)!!, cursor.getString(1)!!)
79         }
80 
selectAllnull81     override fun selectAll(): Query<TestEntity> = selectAll(TestEntity::Impl)
82 
83     override fun insertOrReplace(value: String) {
84         driver.execute(
85             -2020431062,
86             """
87     |INSERT OR REPLACE INTO TestEntity(
88     |  value
89     |)
90     |VALUES (?1)
91     """
92                 .trimMargin(),
93             1
94         ) {
95             bindString(1, value)
96         }
97         notifyQueries(-2020431062, { database.testEntityQueries.selectAll })
98     }
99 }
100