1 /*
2  * Copyright (C) 2018 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 @file:Suppress("NOTHING_TO_INLINE") // Aliases to other public API.
18 
19 package androidx.core.database
20 
21 import android.database.Cursor
22 
23 /**
24  * Returns the value of the requested column as a nullable byte array.
25  *
26  * The result and whether this method throws an exception when the column type is not a blob type is
27  * implementation-defined.
28  *
29  * @see Cursor.isNull
30  * @see Cursor.getBlob
31  */
getBlobOrNullnull32 public inline fun Cursor.getBlobOrNull(index: Int): ByteArray? =
33     if (isNull(index)) null else getBlob(index)
34 
35 /**
36  * Returns the value of the requested column as a nullable double.
37  *
38  * The result and whether this method throws an exception when the column type is not a
39  * floating-point type is implementation-defined.
40  *
41  * @see Cursor.isNull
42  * @see Cursor.getDouble
43  */
44 public inline fun Cursor.getDoubleOrNull(index: Int): Double? =
45     if (isNull(index)) null else getDouble(index)
46 
47 /**
48  * Returns the value of the requested column as a nullable float.
49  *
50  * The result and whether this method throws an exception when the column type is not a
51  * floating-point type is implementation-defined.
52  *
53  * @see Cursor.isNull
54  * @see Cursor.getFloat
55  */
56 public inline fun Cursor.getFloatOrNull(index: Int): Float? =
57     if (isNull(index)) null else getFloat(index)
58 
59 /**
60  * Returns the value of the requested column as a nullable integer.
61  *
62  * The result and whether this method throws an exception when the column type is not an integral
63  * type is implementation-defined.
64  *
65  * @see Cursor.isNull
66  * @see Cursor.getInt
67  */
68 public inline fun Cursor.getIntOrNull(index: Int): Int? = if (isNull(index)) null else getInt(index)
69 
70 /**
71  * Returns the value of the requested column as a nullable long.
72  *
73  * The result and whether this method throws an exception when the column type is not an integral
74  * type is implementation-defined.
75  *
76  * @see Cursor.isNull
77  * @see Cursor.getLong
78  */
79 public inline fun Cursor.getLongOrNull(index: Int): Long? =
80     if (isNull(index)) null else getLong(index)
81 
82 /**
83  * Returns the value of the requested column as a nullable short.
84  *
85  * The result and whether this method throws an exception when the column type is not an integral
86  * type is implementation-defined.
87  *
88  * @see Cursor.isNull
89  * @see Cursor.getShort
90  */
91 public inline fun Cursor.getShortOrNull(index: Int): Short? =
92     if (isNull(index)) null else getShort(index)
93 
94 /**
95  * Returns the value of the requested column as a nullable string.
96  *
97  * The result and whether this method throws an exception when the column type is not a string type
98  * is implementation-defined.
99  *
100  * @see Cursor.isNull
101  * @see Cursor.getString
102  */
103 public inline fun Cursor.getStringOrNull(index: Int): String? =
104     if (isNull(index)) null else getString(index)
105