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 android.database.sqlite.SQLiteDatabase
20 import android.database.sqlite.SQLiteOpenHelper
21 import androidx.test.core.app.ApplicationProvider
22 import com.google.common.truth.Truth.assertThat
23 import java.io.File
24 import org.junit.rules.TemporaryFolder
25
26 fun SQLiteDatabase.addTable(table: Table) = execSQL(table.toCreateString())
27
28 val SQLiteDatabase.displayName: String
29 get() =
30 if (path != ":memory:") path
31 else ":memory: {hashcode=0x${String.format("%x", this.hashCode())}}"
32
33 val SQLiteDatabase.absolutePath: String
34 get() = File(path).absolutePath
35
36 fun SQLiteDatabase.insertValues(table: Table, vararg values: String) {
37 assertThat(values).isNotEmpty()
38 assertThat(values).hasLength(table.columns.size)
39 execSQL(
40 values.joinToString(prefix = "INSERT INTO ${table.name} VALUES(", postfix = ");") { it }
41 )
42 }
43
createInstancenull44 fun Database.createInstance(
45 temporaryFolder: TemporaryFolder,
46 writeAheadLoggingEnabled: Boolean? = null
47 ): SQLiteDatabase {
48 val path =
49 if (name == null) null
50 else
51 File(temporaryFolder.root, name)
52 .also { it.createNewFile() } // can handle an existing file
53 .absolutePath
54
55 val context = ApplicationProvider.getApplicationContext() as android.content.Context
56 val openHelper =
57 object : SQLiteOpenHelper(context, path, null, 1) {
58 override fun onCreate(db: SQLiteDatabase?) = Unit
59
60 override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) = Unit
61 }
62
63 writeAheadLoggingEnabled?.let { openHelper.setWriteAheadLoggingEnabled(it) }
64 val db = openHelper.readableDatabase
65 tables.forEach { t -> db.addTable(t) }
66 return db
67 }
68
Tablenull69 fun Table.toCreateString(): String {
70 val primaryKeyColumns = columns.filter { it.isPrimaryKey }
71 val primaryKeyPart =
72 if (primaryKeyColumns.isEmpty()) ""
73 else
74 primaryKeyColumns
75 .sortedBy { it.primaryKey }
76 .joinToString(prefix = ",PRIMARY KEY(", postfix = ")") { it.name }
77
78 return columns.joinToString(
79 prefix = "CREATE ${if (isView) "VIEW" else "TABLE"} $name (",
80 postfix = "$primaryKeyPart )${if (isView) " AS $viewQuery" else ""};"
81 ) {
82 it.name +
83 "${if (isView) "" else " ${it.type}"} " +
84 (if (it.isNotNull) "NOT NULL " else "") +
85 (if (it.isUnique) "UNIQUE " else "")
86 }
87 }
88