(SupportSQLiteDatabase database,
String tableName)
| 173 | } |
| 174 | |
| 175 | private static Set<ForeignKey> readForeignKeys(SupportSQLiteDatabase database, |
| 176 | String tableName) { |
| 177 | Set<ForeignKey> foreignKeys = new HashSet<>(); |
| 178 | // this seems to return everything in order but it is not documented so better be safe |
| 179 | Cursor cursor = database.query("PRAGMA foreign_key_list(`" + tableName + "`)"); |
| 180 | try { |
| 181 | final int idColumnIndex = cursor.getColumnIndex("id"); |
| 182 | final int seqColumnIndex = cursor.getColumnIndex("seq"); |
| 183 | final int tableColumnIndex = cursor.getColumnIndex("table"); |
| 184 | final int onDeleteColumnIndex = cursor.getColumnIndex("on_delete"); |
| 185 | final int onUpdateColumnIndex = cursor.getColumnIndex("on_update"); |
| 186 | |
| 187 | final List<ForeignKeyWithSequence> ordered = readForeignKeyFieldMappings(cursor); |
| 188 | final int count = cursor.getCount(); |
| 189 | for (int position = 0; position < count; position++) { |
| 190 | cursor.moveToPosition(position); |
| 191 | final int seq = cursor.getInt(seqColumnIndex); |
| 192 | if (seq != 0) { |
| 193 | continue; |
| 194 | } |
| 195 | final int id = cursor.getInt(idColumnIndex); |
| 196 | List<String> myColumns = new ArrayList<>(); |
| 197 | List<String> refColumns = new ArrayList<>(); |
| 198 | for (ForeignKeyWithSequence key : ordered) { |
| 199 | if (key.mId == id) { |
| 200 | myColumns.add(key.mFrom); |
| 201 | refColumns.add(key.mTo); |
| 202 | } |
| 203 | } |
| 204 | foreignKeys.add(new ForeignKey( |
| 205 | cursor.getString(tableColumnIndex), |
| 206 | cursor.getString(onDeleteColumnIndex), |
| 207 | cursor.getString(onUpdateColumnIndex), |
| 208 | myColumns, |
| 209 | refColumns |
| 210 | )); |
| 211 | } |
| 212 | } finally { |
| 213 | cursor.close(); |
| 214 | } |
| 215 | return foreignKeys; |
| 216 | } |
| 217 | |
| 218 | private static List<ForeignKeyWithSequence> readForeignKeyFieldMappings(Cursor cursor) { |
| 219 | final int idColumnIndex = cursor.getColumnIndex("id"); |
no test coverage detected