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 19package androidx.core.database 20 21import android.database.Cursor 22 23/** 24 * Returns the value of the requested column as a byte array. 25 * 26 * The result and whether this method throws an exception when the column value is null or the 27 * column type is not a blob type is implementation-defined. 28 * 29 * @see Cursor.getColumnIndexOrThrow 30 * @see Cursor.getBlob 31 */ 32inline fun Cursor.getBlob(columnName: String): ByteArray = 33 getBlob(getColumnIndexOrThrow(columnName)) 34 35/** 36 * Returns the value of the requested column as a double. 37 * 38 * The result and whether this method throws an exception when the column value is null or the 39 * column type is not a floating-point type is implementation-defined. 40 * 41 * @see Cursor.getColumnIndexOrThrow 42 * @see Cursor.getDouble 43 */ 44inline fun Cursor.getDouble(columnName: String): Double = 45 getDouble(getColumnIndexOrThrow(columnName)) 46 47/** 48 * Returns the value of the requested column as a float. 49 * 50 * The result and whether this method throws an exception when the column value is null or the 51 * column type is not a floating-point type is implementation-defined. 52 * 53 * @see Cursor.getColumnIndexOrThrow 54 * @see Cursor.getFloat 55 */ 56inline fun Cursor.getFloat(columnName: String): Float = getFloat(getColumnIndexOrThrow(columnName)) 57 58/** 59 * Returns the value of the requested column as an integer. 60 * 61 * The result and whether this method throws an exception when the column value is null or the 62 * column type is not an integral type is implementation-defined. 63 * 64 * @see Cursor.getColumnIndexOrThrow 65 * @see Cursor.getInt 66 */ 67inline fun Cursor.getInt(columnName: String): Int = getInt(getColumnIndexOrThrow(columnName)) 68 69/** 70 * Returns the value of the requested column as a long. 71 * 72 * The result and whether this method throws an exception when the column value is null or the 73 * column type is not an integral type is implementation-defined. 74 * 75 * @see Cursor.getColumnIndexOrThrow 76 * @see Cursor.getLong 77 */ 78inline fun Cursor.getLong(columnName: String): Long = getLong(getColumnIndexOrThrow(columnName)) 79 80/** 81 * Returns the value of the requested column as a short. 82 * 83 * The result and whether this method throws an exception when the column value is null or the 84 * column type is not an integral type is implementation-defined. 85 * 86 * @see Cursor.getColumnIndexOrThrow 87 * @see Cursor.getShort 88 */ 89inline fun Cursor.getShort(columnName: String): Short = getShort(getColumnIndexOrThrow(columnName)) 90 91/** 92 * Returns the value of the requested column as a string. 93 * 94 * The result and whether this method throws an exception when the column value is null or the 95 * column type is not a string type is implementation-defined. 96 * 97 * @see Cursor.getColumnIndexOrThrow 98 * @see Cursor.getString 99 */ 100inline fun Cursor.getString(columnName: String): String = 101 getString(getColumnIndexOrThrow(columnName)) 102 103/** 104 * Returns the value of the requested column as a nullable byte array. 105 * 106 * The result and whether this method throws an exception when the column type is not a blob type is 107 * implementation-defined. 108 * 109 * @see Cursor.isNull 110 * @see Cursor.getBlob 111 */ 112inline fun Cursor.getBlobOrNull(index: Int) = if (isNull(index)) null else getBlob(index) 113 114/** 115 * Returns the value of the requested column as a nullable double. 116 * 117 * The result and whether this method throws an exception when the column type is not a 118 * floating-point type is implementation-defined. 119 * 120 * @see Cursor.isNull 121 * @see Cursor.getDouble 122 */ 123inline fun Cursor.getDoubleOrNull(index: Int) = if (isNull(index)) null else getDouble(index) 124 125/** 126 * Returns the value of the requested column as a nullable float. 127 * 128 * The result and whether this method throws an exception when the column type is not a 129 * floating-point type is implementation-defined. 130 * 131 * @see Cursor.isNull 132 * @see Cursor.getFloat 133 */ 134inline fun Cursor.getFloatOrNull(index: Int) = if (isNull(index)) null else getFloat(index) 135 136/** 137 * Returns the value of the requested column as a nullable integer. 138 * 139 * The result and whether this method throws an exception when the column type is not an integral 140 * type is implementation-defined. 141 * 142 * @see Cursor.isNull 143 * @see Cursor.getInt 144 */ 145inline fun Cursor.getIntOrNull(index: Int) = if (isNull(index)) null else getInt(index) 146 147/** 148 * Returns the value of the requested column as a nullable long. 149 * 150 * The result and whether this method throws an exception when the column type is not an integral 151 * type is implementation-defined. 152 * 153 * @see Cursor.isNull 154 * @see Cursor.getLong 155 */ 156inline fun Cursor.getLongOrNull(index: Int) = if (isNull(index)) null else getLong(index) 157 158/** 159 * Returns the value of the requested column as a nullable short. 160 * 161 * The result and whether this method throws an exception when the column type is not an integral 162 * type is implementation-defined. 163 * 164 * @see Cursor.isNull 165 * @see Cursor.getShort 166 */ 167inline fun Cursor.getShortOrNull(index: Int) = if (isNull(index)) null else getShort(index) 168 169/** 170 * Returns the value of the requested column as a nullable string. 171 * 172 * The result and whether this method throws an exception when the column type is not a string type 173 * is implementation-defined. 174 * 175 * @see Cursor.isNull 176 * @see Cursor.getString 177 */ 178inline fun Cursor.getStringOrNull(index: Int) = if (isNull(index)) null else getString(index) 179 180/** 181 * Returns the value of the requested column as a nullable byte array. 182 * 183 * The result and whether this method throws an exception when the column type is not a blob type is 184 * implementation-defined. 185 * 186 * @see Cursor.getColumnIndexOrThrow 187 * @see Cursor.isNull 188 * @see Cursor.getBlob 189 */ 190inline fun Cursor.getBlobOrNull(columnName: String) = 191 getColumnIndexOrThrow(columnName).let { if (isNull(it)) null else getBlob(it) } 192 193/** 194 * Returns the value of the requested column as a nullable double. 195 * 196 * The result and whether this method throws an exception when the column type is not a 197 * floating-point type is implementation-defined. 198 * 199 * @see Cursor.getColumnIndexOrThrow 200 * @see Cursor.isNull 201 * @see Cursor.getDouble 202 */ 203inline fun Cursor.getDoubleOrNull(columnName: String) = 204 getColumnIndexOrThrow(columnName).let { if (isNull(it)) null else getDouble(it) } 205 206/** 207 * Returns the value of the requested column as a nullable float. 208 * 209 * The result and whether this method throws an exception when the column type is not a 210 * floating-point type is implementation-defined. 211 * 212 * @see Cursor.getColumnIndexOrThrow 213 * @see Cursor.isNull 214 * @see Cursor.getFloat 215 */ 216inline fun Cursor.getFloatOrNull(columnName: String) = 217 getColumnIndexOrThrow(columnName).let { if (isNull(it)) null else getFloat(it) } 218 219/** 220 * Returns the value of the requested column as a nullable integer. 221 * 222 * The result and whether this method throws an exception when the column type is not an integral 223 * type is implementation-defined. 224 * 225 * @see Cursor.getColumnIndexOrThrow 226 * @see Cursor.isNull 227 * @see Cursor.getInt 228 */ 229inline fun Cursor.getIntOrNull(columnName: String) = 230 getColumnIndexOrThrow(columnName).let { if (isNull(it)) null else getInt(it) } 231 232/** 233 * Returns the value of the requested column as a nullable long. 234 * 235 * The result and whether this method throws an exception when the column type is not an integral 236 * type is implementation-defined. 237 * 238 * @see Cursor.getColumnIndexOrThrow 239 * @see Cursor.isNull 240 * @see Cursor.getLong 241 */ 242inline fun Cursor.getLongOrNull(columnName: String) = 243 getColumnIndexOrThrow(columnName).let { if (isNull(it)) null else getLong(it) } 244 245/** 246 * Returns the value of the requested column as a nullable short. 247 * 248 * The result and whether this method throws an exception when the column type is not an integral 249 * type is implementation-defined. 250 * 251 * @see Cursor.getColumnIndexOrThrow 252 * @see Cursor.isNull 253 * @see Cursor.getShort 254 */ 255inline fun Cursor.getShortOrNull(columnName: String) = 256 getColumnIndexOrThrow(columnName).let { if (isNull(it)) null else getShort(it) } 257 258/** 259 * Returns the value of the requested column as a nullable string. 260 * 261 * The result and whether this method throws an exception when the column type is not a string type 262 * is implementation-defined. 263 * 264 * @see Cursor.getColumnIndexOrThrow 265 * @see Cursor.isNull 266 * @see Cursor.getString 267 */ 268inline fun Cursor.getStringOrNull(columnName: String) = 269 getColumnIndexOrThrow(columnName).let { if (isNull(it)) null else getString(it) } 270