A copy of the generated code used in my blog post about Room database relationships.
package com.adammcneilly.androiduniversity.room; | |
import android.database.Cursor; | |
import androidx.collection.LongSparseArray; | |
import androidx.lifecycle.LiveData; | |
import androidx.room.RoomDatabase; | |
import androidx.room.RoomSQLiteQuery; | |
import androidx.room.util.CursorUtil; | |
import androidx.room.util.DBUtil; | |
import androidx.room.util.StringUtil; | |
import java.lang.Exception; | |
import java.lang.Override; | |
import java.lang.String; | |
import java.lang.StringBuilder; | |
import java.lang.SuppressWarnings; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.Callable; | |
@SuppressWarnings({"unchecked", "deprecation"}) | |
public final class UniversityDAO_Impl implements UniversityDAO { | |
private final RoomDatabase __db; | |
public UniversityDAO_Impl(RoomDatabase __db) { | |
this.__db = __db; | |
} | |
@Override | |
public LiveData<List<StudentWithVehicles>> fetchStudentsWithVehicles() { | |
final String _sql = "SELECT * FROM Student"; | |
final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0); | |
return __db.getInvalidationTracker().createLiveData(new String[]{"Vehicle","Student"}, true, new Callable<List<StudentWithVehicles>>() { | |
@Override | |
public List<StudentWithVehicles> call() throws Exception { | |
__db.beginTransaction(); | |
try { | |
final Cursor _cursor = DBUtil.query(__db, _statement, true, null); | |
try { | |
final int _cursorIndexOfStudentId = CursorUtil.getColumnIndexOrThrow(_cursor, "studentId"); | |
final int _cursorIndexOfFirstName = CursorUtil.getColumnIndexOrThrow(_cursor, "firstName"); | |
final int _cursorIndexOfLastName = CursorUtil.getColumnIndexOrThrow(_cursor, "lastName"); | |
final int _cursorIndexOfStreetName = CursorUtil.getColumnIndexOrThrow(_cursor, "address_streetName"); | |
final int _cursorIndexOfStreetNumber = CursorUtil.getColumnIndexOrThrow(_cursor, "address_streetNumber"); | |
final LongSparseArray<ArrayList<Vehicle>> _collectionVehicles = new LongSparseArray<ArrayList<Vehicle>>(); | |
while (_cursor.moveToNext()) { | |
final long _tmpKey = _cursor.getLong(_cursorIndexOfStudentId); | |
ArrayList<Vehicle> _tmpVehiclesCollection = _collectionVehicles.get(_tmpKey); | |
if (_tmpVehiclesCollection == null) { | |
_tmpVehiclesCollection = new ArrayList<Vehicle>(); | |
_collectionVehicles.put(_tmpKey, _tmpVehiclesCollection); | |
} | |
} | |
_cursor.moveToPosition(-1); | |
__fetchRelationshipVehicleAscomAdammcneillyAndroiduniversityRoomVehicle(_collectionVehicles); | |
final List<StudentWithVehicles> _result = new ArrayList<StudentWithVehicles>(_cursor.getCount()); | |
while(_cursor.moveToNext()) { | |
final StudentWithVehicles _item; | |
final Student _tmpStudent; | |
if (! (_cursor.isNull(_cursorIndexOfStudentId) && _cursor.isNull(_cursorIndexOfFirstName) && _cursor.isNull(_cursorIndexOfLastName) && _cursor.isNull(_cursorIndexOfStreetName) && _cursor.isNull(_cursorIndexOfStreetNumber))) { | |
final long _tmpStudentId; | |
_tmpStudentId = _cursor.getLong(_cursorIndexOfStudentId); | |
final String _tmpFirstName; | |
_tmpFirstName = _cursor.getString(_cursorIndexOfFirstName); | |
final String _tmpLastName; | |
_tmpLastName = _cursor.getString(_cursorIndexOfLastName); | |
final Address _tmpAddress; | |
if (! (_cursor.isNull(_cursorIndexOfStreetName) && _cursor.isNull(_cursorIndexOfStreetNumber))) { | |
final String _tmpStreetName; | |
_tmpStreetName = _cursor.getString(_cursorIndexOfStreetName); | |
final String _tmpStreetNumber; | |
_tmpStreetNumber = _cursor.getString(_cursorIndexOfStreetNumber); | |
_tmpAddress = new Address(_tmpStreetName,_tmpStreetNumber); | |
} else { | |
_tmpAddress = null; | |
} | |
_tmpStudent = new Student(_tmpStudentId,_tmpFirstName,_tmpLastName,_tmpAddress); | |
} else { | |
_tmpStudent = null; | |
} | |
ArrayList<Vehicle> _tmpVehiclesCollection_1 = null; | |
final long _tmpKey_1 = _cursor.getLong(_cursorIndexOfStudentId); | |
_tmpVehiclesCollection_1 = _collectionVehicles.get(_tmpKey_1); | |
if (_tmpVehiclesCollection_1 == null) { | |
_tmpVehiclesCollection_1 = new ArrayList<Vehicle>(); | |
} | |
_item = new StudentWithVehicles(_tmpStudent,_tmpVehiclesCollection_1); | |
_result.add(_item); | |
} | |
__db.setTransactionSuccessful(); | |
return _result; | |
} finally { | |
_cursor.close(); | |
} | |
} finally { | |
__db.endTransaction(); | |
} | |
} | |
@Override | |
protected void finalize() { | |
_statement.release(); | |
} | |
}); | |
} | |
private void __fetchRelationshipVehicleAscomAdammcneillyAndroiduniversityRoomVehicle(final LongSparseArray<ArrayList<Vehicle>> _map) { | |
if (_map.isEmpty()) { | |
return; | |
} | |
// check if the size is too big, if so divide; | |
if(_map.size() > RoomDatabase.MAX_BIND_PARAMETER_CNT) { | |
LongSparseArray<ArrayList<Vehicle>> _tmpInnerMap = new LongSparseArray<ArrayList<Vehicle>>(androidx.room.RoomDatabase.MAX_BIND_PARAMETER_CNT); | |
int _tmpIndex = 0; | |
int _mapIndex = 0; | |
final int _limit = _map.size(); | |
while(_mapIndex < _limit) { | |
_tmpInnerMap.put(_map.keyAt(_mapIndex), _map.valueAt(_mapIndex)); | |
_mapIndex++; | |
_tmpIndex++; | |
if(_tmpIndex == RoomDatabase.MAX_BIND_PARAMETER_CNT) { | |
__fetchRelationshipVehicleAscomAdammcneillyAndroiduniversityRoomVehicle(_tmpInnerMap); | |
_tmpInnerMap = new LongSparseArray<ArrayList<Vehicle>>(RoomDatabase.MAX_BIND_PARAMETER_CNT); | |
_tmpIndex = 0; | |
} | |
} | |
if(_tmpIndex > 0) { | |
__fetchRelationshipVehicleAscomAdammcneillyAndroiduniversityRoomVehicle(_tmpInnerMap); | |
} | |
return; | |
} | |
StringBuilder _stringBuilder = StringUtil.newStringBuilder(); | |
_stringBuilder.append("SELECT `vehicleId`,`ownerId`,`vehicleType` FROM `Vehicle` WHERE `ownerId` IN ("); | |
final int _inputSize = _map.size(); | |
StringUtil.appendPlaceholders(_stringBuilder, _inputSize); | |
_stringBuilder.append(")"); | |
final String _sql = _stringBuilder.toString(); | |
final int _argCount = 0 + _inputSize; | |
final RoomSQLiteQuery _stmt = RoomSQLiteQuery.acquire(_sql, _argCount); | |
int _argIndex = 1; | |
for (int i = 0; i < _map.size(); i++) { | |
long _item = _map.keyAt(i); | |
_stmt.bindLong(_argIndex, _item); | |
_argIndex ++; | |
} | |
final Cursor _cursor = DBUtil.query(__db, _stmt, false, null); | |
try { | |
final int _itemKeyIndex = CursorUtil.getColumnIndex(_cursor, "ownerId"); | |
if (_itemKeyIndex == -1) { | |
return; | |
} | |
final int _cursorIndexOfVehicleId = CursorUtil.getColumnIndex(_cursor, "vehicleId"); | |
final int _cursorIndexOfOwnerId = CursorUtil.getColumnIndex(_cursor, "ownerId"); | |
final int _cursorIndexOfVehicleType = CursorUtil.getColumnIndex(_cursor, "vehicleType"); | |
while(_cursor.moveToNext()) { | |
final long _tmpKey = _cursor.getLong(_itemKeyIndex); | |
ArrayList<Vehicle> _tmpRelation = _map.get(_tmpKey); | |
if (_tmpRelation != null) { | |
final Vehicle _item_1; | |
final long _tmpVehicleId; | |
if (_cursorIndexOfVehicleId == -1) { | |
_tmpVehicleId = 0; | |
} else { | |
_tmpVehicleId = _cursor.getLong(_cursorIndexOfVehicleId); | |
} | |
final long _tmpOwnerId; | |
if (_cursorIndexOfOwnerId == -1) { | |
_tmpOwnerId = 0; | |
} else { | |
_tmpOwnerId = _cursor.getLong(_cursorIndexOfOwnerId); | |
} | |
final String _tmpVehicleType; | |
if (_cursorIndexOfVehicleType == -1) { | |
_tmpVehicleType = null; | |
} else { | |
_tmpVehicleType = _cursor.getString(_cursorIndexOfVehicleType); | |
} | |
_item_1 = new Vehicle(_tmpVehicleId,_tmpOwnerId,_tmpVehicleType); | |
_tmpRelation.add(_item_1); | |
} | |
} | |
} finally { | |
_cursor.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment