@return null if we cannot read the index due to older sqlite implementations.
(SupportSQLiteDatabase database, String name, boolean unique)
| 307 | * @return null if we cannot read the index due to older sqlite implementations. |
| 308 | */ |
| 309 | @Nullable |
| 310 | private static Index readIndex(SupportSQLiteDatabase database, String name, boolean unique) { |
| 311 | Cursor cursor = database.query("PRAGMA index_xinfo(`" + name + "`)"); |
| 312 | try { |
| 313 | final int seqnoColumnIndex = cursor.getColumnIndex("seqno"); |
| 314 | final int cidColumnIndex = cursor.getColumnIndex("cid"); |
| 315 | final int nameColumnIndex = cursor.getColumnIndex("name"); |
| 316 | final int descColumnIndex = cursor.getColumnIndex("desc"); |
| 317 | if (seqnoColumnIndex == -1 || cidColumnIndex == -1 |
| 318 | || nameColumnIndex == -1 || descColumnIndex == -1) { |
| 319 | // we cannot read them so better not validate any index. |
| 320 | return null; |
| 321 | } |
| 322 | final TreeMap<Integer, String> columnsMap = new TreeMap<>(); |
| 323 | final TreeMap<Integer, String> ordersMap = new TreeMap<>(); |
| 324 | |
| 325 | while (cursor.moveToNext()) { |
| 326 | int cid = cursor.getInt(cidColumnIndex); |
| 327 | if (cid < 0) { |
| 328 | // Ignore SQLite row ID |
| 329 | continue; |
| 330 | } |
| 331 | int seq = cursor.getInt(seqnoColumnIndex); |
| 332 | String columnName = cursor.getString(nameColumnIndex); |
| 333 | String order = cursor.getInt(descColumnIndex) > 0 ? "DESC" : "ASC"; |
| 334 | |
| 335 | columnsMap.put(seq, columnName); |
| 336 | ordersMap.put(seq, order); |
| 337 | } |
| 338 | final List<String> columns = new ArrayList<>(columnsMap.size()); |
| 339 | columns.addAll(columnsMap.values()); |
| 340 | final List<String> orders = new ArrayList<>(ordersMap.size()); |
| 341 | orders.addAll(ordersMap.values()); |
| 342 | return new Index(name, unique, columns, orders); |
| 343 | } finally { |
| 344 | cursor.close(); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Holds the information about a database column. |
no test coverage detected