@return null if we cannot read the indices due to older sqlite implementations.
(SupportSQLiteDatabase database, String tableName)
| 271 | * @return null if we cannot read the indices due to older sqlite implementations. |
| 272 | */ |
| 273 | @Nullable |
| 274 | private static Set<Index> readIndices(SupportSQLiteDatabase database, String tableName) { |
| 275 | Cursor cursor = database.query("PRAGMA index_list(`" + tableName + "`)"); |
| 276 | try { |
| 277 | final int nameColumnIndex = cursor.getColumnIndex("name"); |
| 278 | final int originColumnIndex = cursor.getColumnIndex("origin"); |
| 279 | final int uniqueIndex = cursor.getColumnIndex("unique"); |
| 280 | if (nameColumnIndex == -1 || originColumnIndex == -1 || uniqueIndex == -1) { |
| 281 | // we cannot read them so better not validate any index. |
| 282 | return null; |
| 283 | } |
| 284 | HashSet<Index> indices = new HashSet<>(); |
| 285 | while (cursor.moveToNext()) { |
| 286 | String origin = cursor.getString(originColumnIndex); |
| 287 | if (!"c".equals(origin)) { |
| 288 | // Ignore auto-created indices |
| 289 | continue; |
| 290 | } |
| 291 | String name = cursor.getString(nameColumnIndex); |
| 292 | boolean unique = cursor.getInt(uniqueIndex) == 1; |
| 293 | Index index = readIndex(database, name, unique); |
| 294 | if (index == null) { |
| 295 | // we cannot read it properly so better not read it |
| 296 | return null; |
| 297 | } |
| 298 | indices.add(index); |
| 299 | } |
| 300 | return indices; |
| 301 | } finally { |
| 302 | cursor.close(); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * @return null if we cannot read the index due to older sqlite implementations. |
no test coverage detected