1 /*
2  * Copyright 2023 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 @file:JvmName("SQLite")
17 
18 package androidx.sqlite
19 
20 import androidx.annotation.IntDef
21 import androidx.annotation.RestrictTo
22 import kotlin.jvm.JvmName
23 
24 /** The data type for a 64-bit signed integer. */
25 public const val SQLITE_DATA_INTEGER: Int = 1
26 
27 /** The data type for a 64-bit IEEE floating point number. */
28 public const val SQLITE_DATA_FLOAT: Int = 2
29 
30 /** The data type for a [String]. */
31 public const val SQLITE_DATA_TEXT: Int = 3
32 
33 /** The data type for a `BLOB` value, i.e. binary data. */
34 public const val SQLITE_DATA_BLOB: Int = 4
35 
36 /** The data type for a `NULL` value. */
37 public const val SQLITE_DATA_NULL: Int = 5
38 
39 /** The data type constants. */
40 @IntDef(
41     value =
42         [
43             SQLITE_DATA_INTEGER,
44             SQLITE_DATA_FLOAT,
45             SQLITE_DATA_TEXT,
46             SQLITE_DATA_BLOB,
47             SQLITE_DATA_NULL,
48         ]
49 )
50 @Retention(AnnotationRetention.SOURCE)
51 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
52 public annotation class DataType
53 
54 /** Executes a single SQL statement that returns no values. */
execSQLnull55 public fun SQLiteConnection.execSQL(sql: String) {
56     prepare(sql).use { it.step() }
57 }
58 
59 /** Throws a [SQLiteException] with its message formed by the given [errorCode] amd [errorMsg]. */
throwSQLiteExceptionnull60 public fun throwSQLiteException(errorCode: Int, errorMsg: String?): Nothing {
61     val message = buildString {
62         append("Error code: $errorCode")
63         if (errorMsg != null) {
64             append(", message: $errorMsg")
65         }
66     }
67     throw SQLiteException(message)
68 }
69