Replace function in DAO to 'OR' in SQL query

This commit is contained in:
Faierbel 2024-03-26 20:29:35 +01:00
parent d799ec7ac9
commit 7463cf6253
6 changed files with 30 additions and 33 deletions

View File

@ -112,7 +112,7 @@ class WulkanowySdkFactory @Inject constructor(
return false
}
Timber.d("Migration eduOne: success. New isEduOne flag: ${newCurrentStudent.isEduOne}")
Timber.i("Migration eduOne: success. New isEduOne flag: ${newCurrentStudent.isEduOne}")
val studentIsEduOne = StudentIsEduOne(
id = student.id,

View File

@ -5,7 +5,6 @@ import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student
import javax.inject.Singleton
@Singleton
@ -15,17 +14,6 @@ interface SemesterDao : BaseDao<Semester> {
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insertSemesters(items: List<Semester>): List<Long>
@Query("SELECT * FROM Semesters WHERE student_id = :studentId AND class_id = :classId")
@Query("SELECT * FROM Semesters WHERE (student_id = :studentId AND class_id = :classId) OR (student_id = :studentId AND class_id = 0)")
suspend fun loadAll(studentId: Int, classId: Int): List<Semester>
@Query("SELECT * FROM Semesters WHERE student_id = :studentId")
suspend fun loadAll(studentId: Int): List<Semester>
suspend fun loadAll(student: Student): List<Semester> {
return if (student.isEduOne == true) {
loadAll(student.studentId)
} else {
loadAll(student.studentId, student.classId)
}
}
}

View File

@ -47,11 +47,11 @@ abstract class StudentDao {
abstract suspend fun loadAll(): List<Student>
@Transaction
@Query("SELECT * FROM Students JOIN Semesters ON Students.student_id = Semesters.student_id AND Students.class_id = Semesters.class_id")
@Query("SELECT * FROM Students JOIN Semesters ON (Students.student_id = Semesters.student_id AND Students.class_id = Semesters.class_id) OR (Students.student_id = Semesters.student_id AND Semesters.class_id = 0)")
abstract suspend fun loadStudentsWithSemesters(): Map<Student, List<Semester>>
@Transaction
@Query("SELECT * FROM Students JOIN Semesters ON Students.student_id = Semesters.student_id AND Students.class_id = Semesters.class_id WHERE Students.id = :id")
@Query("SELECT * FROM Students JOIN Semesters ON (Students.student_id = Semesters.student_id AND Students.class_id = Semesters.class_id) OR (Students.student_id = Semesters.student_id AND Semesters.class_id = 0) WHERE Students.id = :id")
abstract suspend fun loadStudentWithSemestersById(id: Long): Map<Student, List<Semester>>
@Query("UPDATE Students SET is_current = 1 WHERE id = :id")

View File

@ -27,11 +27,11 @@ class SemesterRepository @Inject constructor(
forceRefresh: Boolean = false,
refreshOnNoCurrent: Boolean = false
) = withContext(dispatchers.io) {
val semesters = semesterDb.loadAll(student)
val semesters = semesterDb.loadAll(student.studentId, student.classId)
if (isShouldFetch(student, semesters, forceRefresh, refreshOnNoCurrent)) {
refreshSemesters(student)
semesterDb.loadAll(student)
semesterDb.loadAll(student.studentId, student.classId)
} else semesters
}
@ -69,7 +69,7 @@ class SemesterRepository @Inject constructor(
return
}
val old = semesterDb.loadAll(student)
val old = semesterDb.loadAll(student.studentId, student.classId)
semesterDb.removeOldAndSaveNew(
oldItems = old uniqueSubtract new,
newItems = new uniqueSubtract old,

View File

@ -123,7 +123,7 @@ class StudentRepository @Inject constructor(
return
}
val currentStudentSemesters = semesterDb.loadAll(student)
val currentStudentSemesters = semesterDb.loadAll(student.studentId, student.classId)
if (currentStudentSemesters.isEmpty()) {
Timber.d("Check isAuthorized: apply empty semesters workaround")
semesterDb.insertSemesters(
@ -209,7 +209,7 @@ class StudentRepository @Inject constructor(
studentDb.update(studentName)
semesterDb.removeOldAndSaveNew(
oldItems = semesterDb.loadAll(student),
oldItems = semesterDb.loadAll(student.studentId, semester.classId),
newItems = newCurrentApiStudent.semesters.mapToEntities(newCurrentApiStudent.studentId)
)
}

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.repositories
import io.github.wulkanowy.TestDispatchersProvider
import io.github.wulkanowy.createWulkanowySdkFactoryMock
import io.github.wulkanowy.data.db.dao.SemesterDao
import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.mappers.mapToEntities
import io.github.wulkanowy.getSemesterEntity
import io.github.wulkanowy.getSemesterPojo
@ -51,7 +50,7 @@ class SemesterRepositoryTest {
getSemesterPojo(1, 2, now().minusMonths(3), now())
)
coEvery { semesterDb.loadAll(student) } returns emptyList()
coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns emptyList()
coEvery { sdk.getSemesters() } returns semesters
coEvery { semesterDb.removeOldAndSaveNew(any(), any()) } just Runs
@ -77,7 +76,12 @@ class SemesterRepositoryTest {
getSemesterPojo(123, 2, now().minusMonths(3), now())
)
coEvery { semesterDb.loadAll(student) } returns badSemesters.mapToEntities(student.studentId)
coEvery {
semesterDb.loadAll(
student.studentId,
student.classId
)
} returns badSemesters.mapToEntities(student.studentId)
coEvery { sdk.getSemesters() } returns goodSemesters
coEvery { semesterDb.removeOldAndSaveNew(any(), any()) } just Runs
@ -99,7 +103,7 @@ class SemesterRepositoryTest {
getSemesterPojo(2, 3, now(), now().plusMonths(6)),
)
coEvery { semesterDb.loadAll(any<Student>()) } returnsMany listOf(
coEvery { semesterDb.loadAll(student.studentId, student.classId) } returnsMany listOf(
badSemesters.mapToEntities(student.studentId),
badSemesters.mapToEntities(student.studentId),
goodSemesters.mapToEntities(student.studentId)
@ -121,7 +125,7 @@ class SemesterRepositoryTest {
getSemesterEntity(1, 2, now().minusMonths(6), now().minusMonths(1))
)
coEvery { semesterDb.loadAll(student) } returns semesters
coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns semesters
val items = runBlocking { semesterRepository.getSemesters(student) }
assertEquals(2, items.size)
@ -134,7 +138,7 @@ class SemesterRepositoryTest {
getSemesterEntity(1, 2, now().minusMonths(3), now())
)
coEvery { semesterDb.loadAll(student) } returns semesters
coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns semesters
val items = runBlocking { semesterRepository.getSemesters(student) }
assertEquals(2, items.size)
@ -147,7 +151,7 @@ class SemesterRepositoryTest {
getSemesterEntity(1, 2, now(), now())
)
coEvery { semesterDb.loadAll(student) } returns semesters
coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns semesters
val items = runBlocking { semesterRepository.getSemesters(student) }
assertEquals(2, items.size)
@ -160,7 +164,7 @@ class SemesterRepositoryTest {
getSemesterPojo(1, 2, now().minusMonths(3), now())
)
coEvery { semesterDb.loadAll(student) } returns emptyList()
coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns emptyList()
coEvery { sdk.getSemesters() } returns semesters
coEvery { semesterDb.removeOldAndSaveNew(any(), any()) } just Runs
@ -189,7 +193,12 @@ class SemesterRepositoryTest {
getSemesterPojo(2, 2, now().plusMonths(5), now().plusMonths(11)),
)
coEvery { semesterDb.loadAll(student) } returns semestersWithNoCurrent
coEvery {
semesterDb.loadAll(
student.studentId,
student.classId
)
} returns semestersWithNoCurrent
coEvery { sdk.getSemesters() } returns newSemesters
coEvery { semesterDb.removeOldAndSaveNew(any(), any()) } just Runs
@ -205,7 +214,7 @@ class SemesterRepositoryTest {
getSemesterEntity(1, 2, now().minusMonths(1), now().plusMonths(1))
)
coEvery { semesterDb.loadAll(student) } returns semesters
coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns semesters
val items = semesterRepository.getSemesters(student, refreshOnNoCurrent = true)
assertEquals(2, items.size)
@ -218,14 +227,14 @@ class SemesterRepositoryTest {
getSemesterEntity(1, 1, now(), now())
)
coEvery { semesterDb.loadAll(student) } returns semesters
coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns semesters
runBlocking { semesterRepository.getCurrentSemester(student) }
}
@Test(expected = RuntimeException::class)
fun getCurrentSemester_emptyList() {
coEvery { semesterDb.loadAll(student) } returns emptyList()
coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns emptyList()
coEvery { sdk.getSemesters() } returns emptyList()
runBlocking { semesterRepository.getCurrentSemester(student) }