package world.respect.datalayer.db.school.daos

import androidx.collection.LongSparseArray
import androidx.paging.PagingSource
import androidx.room.EntityInsertAdapter
import androidx.room.RoomDatabase
import androidx.room.RoomRawQuery
import androidx.room.coroutines.createFlow
import androidx.room.paging.LimitOffsetPagingSource
import androidx.room.util.appendPlaceholders
import androidx.room.util.getColumnIndex
import androidx.room.util.getColumnIndexOrThrow
import androidx.room.util.performSuspending
import androidx.room.util.recursiveFetchLongSparseArray
import androidx.sqlite.SQLiteConnection
import androidx.sqlite.SQLiteStatement
import javax.`annotation`.processing.Generated
import kotlin.Boolean
import kotlin.Int
import kotlin.Long
import kotlin.String
import kotlin.Suppress
import kotlin.Unit
import kotlin.collections.List
import kotlin.collections.MutableList
import kotlin.collections.mutableListOf
import kotlin.reflect.KClass
import kotlin.text.StringBuilder
import kotlinx.coroutines.flow.Flow
import kotlinx.datetime.LocalDate
import kotlinx.serialization.json.JsonObject
import world.respect.datalayer.db.school.SchoolTypeConverters
import world.respect.datalayer.db.school.entities.LastModifiedAndPermission
import world.respect.datalayer.db.school.entities.PersonEntity
import world.respect.datalayer.db.school.entities.PersonEntityWithRoles
import world.respect.datalayer.db.school.entities.PersonRelatedPersonEntity
import world.respect.datalayer.db.school.entities.PersonRoleEntity
import world.respect.datalayer.school.model.PersonGenderEnum
import world.respect.datalayer.school.model.PersonRoleEnum
import world.respect.datalayer.school.model.PersonStatusEnum
import world.respect.datalayer.school.model.composites.PersonListDetails

@Generated(value = ["androidx.room.RoomProcessor"])
@Suppress(names = ["UNCHECKED_CAST", "DEPRECATION", "REDUNDANT_PROJECTION", "REMOVAL"])
public class PersonEntityDao_Impl(
  __db: RoomDatabase,
) : PersonEntityDao {
  private val __db: RoomDatabase

  private val __insertAdapterOfPersonEntity: EntityInsertAdapter<PersonEntity>

  private val __schoolTypeConverters: SchoolTypeConverters = SchoolTypeConverters()
  init {
    this.__db = __db
    this.__insertAdapterOfPersonEntity = object : EntityInsertAdapter<PersonEntity>() {
      protected override fun createQuery(): String = "INSERT OR REPLACE INTO `PersonEntity` (`pGuid`,`pGuidHash`,`pActive`,`pStatus`,`pLastModified`,`pStored`,`pMetadata`,`pUsername`,`pGivenName`,`pFamilyName`,`pMiddleName`,`pGender`,`pDateOfBirth`,`pEmail`,`pPhoneNumber`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"

      protected override fun bind(statement: SQLiteStatement, entity: PersonEntity) {
        statement.bindText(1, entity.pGuid)
        statement.bindLong(2, entity.pGuidHash)
        val _tmp: Int = if (entity.pActive) 1 else 0
        statement.bindLong(3, _tmp.toLong())
        val _tmp_1: Int = __schoolTypeConverters.fromPersonStatusEnum(entity.pStatus)
        statement.bindLong(4, _tmp_1.toLong())
        statement.bindLong(5, entity.pLastModified)
        statement.bindLong(6, entity.pStored)
        val _tmpPMetadata: JsonObject? = entity.pMetadata
        val _tmp_2: String? = __schoolTypeConverters.fromJsonObject(_tmpPMetadata)
        if (_tmp_2 == null) {
          statement.bindNull(7)
        } else {
          statement.bindText(7, _tmp_2)
        }
        val _tmpPUsername: String? = entity.pUsername
        if (_tmpPUsername == null) {
          statement.bindNull(8)
        } else {
          statement.bindText(8, _tmpPUsername)
        }
        statement.bindText(9, entity.pGivenName)
        statement.bindText(10, entity.pFamilyName)
        val _tmpPMiddleName: String? = entity.pMiddleName
        if (_tmpPMiddleName == null) {
          statement.bindNull(11)
        } else {
          statement.bindText(11, _tmpPMiddleName)
        }
        val _tmp_3: Int = __schoolTypeConverters.fromPersonGenderEnum(entity.pGender)
        statement.bindLong(12, _tmp_3.toLong())
        val _tmpPDateOfBirth: LocalDate? = entity.pDateOfBirth
        val _tmp_4: Long? = __schoolTypeConverters.fromLocalDate(_tmpPDateOfBirth)
        if (_tmp_4 == null) {
          statement.bindNull(13)
        } else {
          statement.bindLong(13, _tmp_4)
        }
        val _tmpPEmail: String? = entity.pEmail
        if (_tmpPEmail == null) {
          statement.bindNull(14)
        } else {
          statement.bindText(14, _tmpPEmail)
        }
        val _tmpPPhoneNumber: String? = entity.pPhoneNumber
        if (_tmpPPhoneNumber == null) {
          statement.bindNull(15)
        } else {
          statement.bindText(15, _tmpPPhoneNumber)
        }
      }
    }
  }

  public override suspend fun insert(personEntity: PersonEntity): Unit = performSuspending(__db, false, true) { _connection ->
    __insertAdapterOfPersonEntity.insert(_connection, personEntity)
  }

  public override suspend fun getLastModifiedByGuid(guidHash: Long): Long? {
    val _sql: String = """
        |
        |        SELECT PersonEntity.pLastModified
        |          FROM PersonEntity
        |         WHERE PersonEntity.pGuidHash = ?
        |         LIMIT 1
        |    
        """.trimMargin()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, guidHash)
        val _result: Long?
        if (_stmt.step()) {
          if (_stmt.isNull(0)) {
            _result = null
          } else {
            _result = _stmt.getLong(0)
          }
        } else {
          _result = null
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun getLastModifiedAndHasPermission(
    authenticatedPersonUidNum: Long,
    personUidNum: Long,
    knownRoleFlag: Int,
    roleAdminPermissionRequired: Long,
    roleTeacherPermissionRequired: Long,
    roleStudentPermissionRequired: Long,
    roleParentPermissionRequired: Long,
  ): LastModifiedAndPermission {
    val _sql: String = """
        |
        |            WITH 
        |            AuthenticatedPermissionPersonUids(uidNum) AS (
        |                SELECT ? AS uidNum
        |                UNION
        |                SELECT PersonRelatedPersonEntity.prpOtherPersonUidNum AS uidNum
        |                  FROM PersonRelatedPersonEntity
        |                 WHERE 16 IN 
        |                       (SELECT PersonRoleEntity.prRoleEnum
        |                          FROM PersonRoleEntity
        |                         WHERE PersonRoleEntity.prPersonGuidHash = ?)
        |                   AND PersonRelatedPersonEntity.prpPersonUidNum = ?)     
        |        ,
        |                 
        |            AuthenticatedPersonClassPermissions AS (
        |                SELECT ClassPermissionEntity.*
        |                  FROM ClassPermissionEntity
        |                 WHERE (ClassPermissionEntity.cpeToEnrollmentRole, ClassPermissionEntity.cpeClassUidNum) IN 
        |                       (SELECT EnrollmentEntity.eRole, EnrollmentEntity.eClassUidNum
        |                          FROM EnrollmentEntity
        |                         WHERE EnrollmentEntity.ePersonUidNum IN 
        |                               (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        )
        |                           AND EnrollmentEntity.eStatus = 1)
        |            )
        |        ,
        |                 RequiredPermission(flag) AS (
        |                 SELECT CASE(
        |                          SELECT COALESCE(
        |                                 (SELECT PersonRoleEntity.prRoleEnum
        |                                    FROM PersonRoleEntity
        |                                   WHERE PersonRoleEntity.prPersonGuidHash = ?
        |                                   LIMIT 1), ?)
        |                          )
        |                          WHEN 1 THEN ?
        |                          WHEN 4 THEN ?
        |                          WHEN 8 THEN ?
        |                          WHEN 2 THEN ?
        |                          WHEN 16 THEN ?
        |                          ELSE 9223372036854775807
        |                        END
        |                 )
        |            
        |          SELECT ? AS uidNum,
        |                   (SELECT PersonEntity.pLastModified
        |                      FROM PersonEntity
        |                     WHERE PersonEntity.pGuidHash = ?) AS lastModified,
        |                   (    ? IN 
        |                        (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        )
        |                     OR EXISTS(
        |                            SELECT 1
        |                              FROM SchoolPermissionGrantEntity
        |                             WHERE SchoolPermissionGrantEntity.spgToRole IN 
        |                                   (SELECT PersonRoleEntity.prRoleEnum
        |                                      FROM PersonRoleEntity
        |                                     WHERE PersonRoleEntity.prPersonGuidHash IN 
        |                                           (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        ))
        |                                       AND (SchoolPermissionGrantEntity.spgPermissions & (SELECT flag FROM RequiredPermission)) > 0)
        |                     OR EXISTS(
        |                            SELECT 1
        |                              FROM AuthenticatedPersonClassPermissions
        |                             WHERE AuthenticatedPersonClassPermissions.cpeClassUidNum IN 
        |                                   (SELECT EnrollmentEntity.eClassUidNum
        |                                      FROM EnrollmentEntity
        |                                     WHERE EnrollmentEntity.ePersonUidNum = ?)
        |                               AND (AuthenticatedPersonClassPermissions.cpePermissions & (SELECT flag FROM RequiredPermission)) > 0)
        |                   ) AS hasPermission
        |        
        """.trimMargin()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 2
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 3
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 4
        _stmt.bindLong(_argIndex, personUidNum)
        _argIndex = 5
        _stmt.bindLong(_argIndex, knownRoleFlag.toLong())
        _argIndex = 6
        _stmt.bindLong(_argIndex, roleAdminPermissionRequired)
        _argIndex = 7
        _stmt.bindLong(_argIndex, roleAdminPermissionRequired)
        _argIndex = 8
        _stmt.bindLong(_argIndex, roleTeacherPermissionRequired)
        _argIndex = 9
        _stmt.bindLong(_argIndex, roleStudentPermissionRequired)
        _argIndex = 10
        _stmt.bindLong(_argIndex, roleParentPermissionRequired)
        _argIndex = 11
        _stmt.bindLong(_argIndex, personUidNum)
        _argIndex = 12
        _stmt.bindLong(_argIndex, personUidNum)
        _argIndex = 13
        _stmt.bindLong(_argIndex, personUidNum)
        _argIndex = 14
        _stmt.bindLong(_argIndex, personUidNum)
        val _columnIndexOfUidNum: Int = getColumnIndexOrThrow(_stmt, "uidNum")
        val _columnIndexOfLastModified: Int = getColumnIndexOrThrow(_stmt, "lastModified")
        val _columnIndexOfHasPermission: Int = getColumnIndexOrThrow(_stmt, "hasPermission")
        val _result: LastModifiedAndPermission
        if (_stmt.step()) {
          val _tmpUidNum: Long
          _tmpUidNum = _stmt.getLong(_columnIndexOfUidNum)
          val _tmpLastModified: Long?
          if (_stmt.isNull(_columnIndexOfLastModified)) {
            _tmpLastModified = null
          } else {
            _tmpLastModified = _stmt.getLong(_columnIndexOfLastModified)
          }
          val _tmpHasPermission: Boolean
          val _tmp: Int
          _tmp = _stmt.getLong(_columnIndexOfHasPermission).toInt()
          _tmpHasPermission = _tmp != 0
          _result = LastModifiedAndPermission(_tmpUidNum,_tmpLastModified,_tmpHasPermission)
        } else {
          error("The query result was empty, but expected a single row to return a NON-NULL object of type 'world.respect.datalayer.db.school.entities.LastModifiedAndPermission'.")
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun findByUsername(username: String): PersonEntityWithRoles? {
    val _sql: String = """
        |
        |        SELECT * 
        |         FROM PersonEntity
        |        WHERE pUsername = ?
        |    
        """.trimMargin()
    return performSuspending(__db, true, true) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindText(_argIndex, username)
        val _columnIndexOfPGuid: Int = getColumnIndexOrThrow(_stmt, "pGuid")
        val _columnIndexOfPGuidHash: Int = getColumnIndexOrThrow(_stmt, "pGuidHash")
        val _columnIndexOfPActive: Int = getColumnIndexOrThrow(_stmt, "pActive")
        val _columnIndexOfPStatus: Int = getColumnIndexOrThrow(_stmt, "pStatus")
        val _columnIndexOfPLastModified: Int = getColumnIndexOrThrow(_stmt, "pLastModified")
        val _columnIndexOfPStored: Int = getColumnIndexOrThrow(_stmt, "pStored")
        val _columnIndexOfPMetadata: Int = getColumnIndexOrThrow(_stmt, "pMetadata")
        val _columnIndexOfPUsername: Int = getColumnIndexOrThrow(_stmt, "pUsername")
        val _columnIndexOfPGivenName: Int = getColumnIndexOrThrow(_stmt, "pGivenName")
        val _columnIndexOfPFamilyName: Int = getColumnIndexOrThrow(_stmt, "pFamilyName")
        val _columnIndexOfPMiddleName: Int = getColumnIndexOrThrow(_stmt, "pMiddleName")
        val _columnIndexOfPGender: Int = getColumnIndexOrThrow(_stmt, "pGender")
        val _columnIndexOfPDateOfBirth: Int = getColumnIndexOrThrow(_stmt, "pDateOfBirth")
        val _columnIndexOfPEmail: Int = getColumnIndexOrThrow(_stmt, "pEmail")
        val _columnIndexOfPPhoneNumber: Int = getColumnIndexOrThrow(_stmt, "pPhoneNumber")
        val _collectionRoles: LongSparseArray<MutableList<PersonRoleEntity>> = LongSparseArray<MutableList<PersonRoleEntity>>()
        val _collectionRelatedPersons: LongSparseArray<MutableList<PersonRelatedPersonEntity>> = LongSparseArray<MutableList<PersonRelatedPersonEntity>>()
        while (_stmt.step()) {
          val _tmpKey: Long
          _tmpKey = _stmt.getLong(_columnIndexOfPGuidHash)
          if (!_collectionRoles.containsKey(_tmpKey)) {
            _collectionRoles.put(_tmpKey, mutableListOf())
          }
          val _tmpKey_1: Long
          _tmpKey_1 = _stmt.getLong(_columnIndexOfPGuidHash)
          if (!_collectionRelatedPersons.containsKey(_tmpKey_1)) {
            _collectionRelatedPersons.put(_tmpKey_1, mutableListOf())
          }
        }
        _stmt.reset()
        __fetchRelationshipPersonRoleEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRoleEntity(_connection, _collectionRoles)
        __fetchRelationshipPersonRelatedPersonEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRelatedPersonEntity(_connection, _collectionRelatedPersons)
        val _result: PersonEntityWithRoles?
        if (_stmt.step()) {
          val _tmpPerson: PersonEntity
          val _tmpPGuid: String
          _tmpPGuid = _stmt.getText(_columnIndexOfPGuid)
          val _tmpPGuidHash: Long
          _tmpPGuidHash = _stmt.getLong(_columnIndexOfPGuidHash)
          val _tmpPActive: Boolean
          val _tmp: Int
          _tmp = _stmt.getLong(_columnIndexOfPActive).toInt()
          _tmpPActive = _tmp != 0
          val _tmpPStatus: PersonStatusEnum
          val _tmp_1: Int
          _tmp_1 = _stmt.getLong(_columnIndexOfPStatus).toInt()
          _tmpPStatus = __schoolTypeConverters.toPersonStatusEnum(_tmp_1)
          val _tmpPLastModified: Long
          _tmpPLastModified = _stmt.getLong(_columnIndexOfPLastModified)
          val _tmpPStored: Long
          _tmpPStored = _stmt.getLong(_columnIndexOfPStored)
          val _tmpPMetadata: JsonObject?
          val _tmp_2: String?
          if (_stmt.isNull(_columnIndexOfPMetadata)) {
            _tmp_2 = null
          } else {
            _tmp_2 = _stmt.getText(_columnIndexOfPMetadata)
          }
          _tmpPMetadata = __schoolTypeConverters.toJsonObject(_tmp_2)
          val _tmpPUsername: String?
          if (_stmt.isNull(_columnIndexOfPUsername)) {
            _tmpPUsername = null
          } else {
            _tmpPUsername = _stmt.getText(_columnIndexOfPUsername)
          }
          val _tmpPGivenName: String
          _tmpPGivenName = _stmt.getText(_columnIndexOfPGivenName)
          val _tmpPFamilyName: String
          _tmpPFamilyName = _stmt.getText(_columnIndexOfPFamilyName)
          val _tmpPMiddleName: String?
          if (_stmt.isNull(_columnIndexOfPMiddleName)) {
            _tmpPMiddleName = null
          } else {
            _tmpPMiddleName = _stmt.getText(_columnIndexOfPMiddleName)
          }
          val _tmpPGender: PersonGenderEnum
          val _tmp_3: Int
          _tmp_3 = _stmt.getLong(_columnIndexOfPGender).toInt()
          _tmpPGender = __schoolTypeConverters.toPersonGenderEnum(_tmp_3)
          val _tmpPDateOfBirth: LocalDate?
          val _tmp_4: Long?
          if (_stmt.isNull(_columnIndexOfPDateOfBirth)) {
            _tmp_4 = null
          } else {
            _tmp_4 = _stmt.getLong(_columnIndexOfPDateOfBirth)
          }
          _tmpPDateOfBirth = __schoolTypeConverters.toLocalDate(_tmp_4)
          val _tmpPEmail: String?
          if (_stmt.isNull(_columnIndexOfPEmail)) {
            _tmpPEmail = null
          } else {
            _tmpPEmail = _stmt.getText(_columnIndexOfPEmail)
          }
          val _tmpPPhoneNumber: String?
          if (_stmt.isNull(_columnIndexOfPPhoneNumber)) {
            _tmpPPhoneNumber = null
          } else {
            _tmpPPhoneNumber = _stmt.getText(_columnIndexOfPPhoneNumber)
          }
          _tmpPerson = PersonEntity(_tmpPGuid,_tmpPGuidHash,_tmpPActive,_tmpPStatus,_tmpPLastModified,_tmpPStored,_tmpPMetadata,_tmpPUsername,_tmpPGivenName,_tmpPFamilyName,_tmpPMiddleName,_tmpPGender,_tmpPDateOfBirth,_tmpPEmail,_tmpPPhoneNumber)
          val _tmpRolesCollection: MutableList<PersonRoleEntity>
          val _tmpKey_2: Long
          _tmpKey_2 = _stmt.getLong(_columnIndexOfPGuidHash)
          _tmpRolesCollection = checkNotNull(_collectionRoles.get(_tmpKey_2))
          val _tmpRelatedPersonsCollection: MutableList<PersonRelatedPersonEntity>
          val _tmpKey_3: Long
          _tmpKey_3 = _stmt.getLong(_columnIndexOfPGuidHash)
          _tmpRelatedPersonsCollection = checkNotNull(_collectionRelatedPersons.get(_tmpKey_3))
          _result = PersonEntityWithRoles(_tmpPerson,_tmpRolesCollection,_tmpRelatedPersonsCollection)
        } else {
          _result = null
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun findByGuidNum(guidHash: Long): PersonEntityWithRoles? {
    val _sql: String = """
        |
        |       SELECT * 
        |         FROM PersonEntity
        |        WHERE pGuidHash = ?
        |    
        """.trimMargin()
    return performSuspending(__db, true, true) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, guidHash)
        val _columnIndexOfPGuid: Int = getColumnIndexOrThrow(_stmt, "pGuid")
        val _columnIndexOfPGuidHash: Int = getColumnIndexOrThrow(_stmt, "pGuidHash")
        val _columnIndexOfPActive: Int = getColumnIndexOrThrow(_stmt, "pActive")
        val _columnIndexOfPStatus: Int = getColumnIndexOrThrow(_stmt, "pStatus")
        val _columnIndexOfPLastModified: Int = getColumnIndexOrThrow(_stmt, "pLastModified")
        val _columnIndexOfPStored: Int = getColumnIndexOrThrow(_stmt, "pStored")
        val _columnIndexOfPMetadata: Int = getColumnIndexOrThrow(_stmt, "pMetadata")
        val _columnIndexOfPUsername: Int = getColumnIndexOrThrow(_stmt, "pUsername")
        val _columnIndexOfPGivenName: Int = getColumnIndexOrThrow(_stmt, "pGivenName")
        val _columnIndexOfPFamilyName: Int = getColumnIndexOrThrow(_stmt, "pFamilyName")
        val _columnIndexOfPMiddleName: Int = getColumnIndexOrThrow(_stmt, "pMiddleName")
        val _columnIndexOfPGender: Int = getColumnIndexOrThrow(_stmt, "pGender")
        val _columnIndexOfPDateOfBirth: Int = getColumnIndexOrThrow(_stmt, "pDateOfBirth")
        val _columnIndexOfPEmail: Int = getColumnIndexOrThrow(_stmt, "pEmail")
        val _columnIndexOfPPhoneNumber: Int = getColumnIndexOrThrow(_stmt, "pPhoneNumber")
        val _collectionRoles: LongSparseArray<MutableList<PersonRoleEntity>> = LongSparseArray<MutableList<PersonRoleEntity>>()
        val _collectionRelatedPersons: LongSparseArray<MutableList<PersonRelatedPersonEntity>> = LongSparseArray<MutableList<PersonRelatedPersonEntity>>()
        while (_stmt.step()) {
          val _tmpKey: Long
          _tmpKey = _stmt.getLong(_columnIndexOfPGuidHash)
          if (!_collectionRoles.containsKey(_tmpKey)) {
            _collectionRoles.put(_tmpKey, mutableListOf())
          }
          val _tmpKey_1: Long
          _tmpKey_1 = _stmt.getLong(_columnIndexOfPGuidHash)
          if (!_collectionRelatedPersons.containsKey(_tmpKey_1)) {
            _collectionRelatedPersons.put(_tmpKey_1, mutableListOf())
          }
        }
        _stmt.reset()
        __fetchRelationshipPersonRoleEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRoleEntity(_connection, _collectionRoles)
        __fetchRelationshipPersonRelatedPersonEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRelatedPersonEntity(_connection, _collectionRelatedPersons)
        val _result: PersonEntityWithRoles?
        if (_stmt.step()) {
          val _tmpPerson: PersonEntity
          val _tmpPGuid: String
          _tmpPGuid = _stmt.getText(_columnIndexOfPGuid)
          val _tmpPGuidHash: Long
          _tmpPGuidHash = _stmt.getLong(_columnIndexOfPGuidHash)
          val _tmpPActive: Boolean
          val _tmp: Int
          _tmp = _stmt.getLong(_columnIndexOfPActive).toInt()
          _tmpPActive = _tmp != 0
          val _tmpPStatus: PersonStatusEnum
          val _tmp_1: Int
          _tmp_1 = _stmt.getLong(_columnIndexOfPStatus).toInt()
          _tmpPStatus = __schoolTypeConverters.toPersonStatusEnum(_tmp_1)
          val _tmpPLastModified: Long
          _tmpPLastModified = _stmt.getLong(_columnIndexOfPLastModified)
          val _tmpPStored: Long
          _tmpPStored = _stmt.getLong(_columnIndexOfPStored)
          val _tmpPMetadata: JsonObject?
          val _tmp_2: String?
          if (_stmt.isNull(_columnIndexOfPMetadata)) {
            _tmp_2 = null
          } else {
            _tmp_2 = _stmt.getText(_columnIndexOfPMetadata)
          }
          _tmpPMetadata = __schoolTypeConverters.toJsonObject(_tmp_2)
          val _tmpPUsername: String?
          if (_stmt.isNull(_columnIndexOfPUsername)) {
            _tmpPUsername = null
          } else {
            _tmpPUsername = _stmt.getText(_columnIndexOfPUsername)
          }
          val _tmpPGivenName: String
          _tmpPGivenName = _stmt.getText(_columnIndexOfPGivenName)
          val _tmpPFamilyName: String
          _tmpPFamilyName = _stmt.getText(_columnIndexOfPFamilyName)
          val _tmpPMiddleName: String?
          if (_stmt.isNull(_columnIndexOfPMiddleName)) {
            _tmpPMiddleName = null
          } else {
            _tmpPMiddleName = _stmt.getText(_columnIndexOfPMiddleName)
          }
          val _tmpPGender: PersonGenderEnum
          val _tmp_3: Int
          _tmp_3 = _stmt.getLong(_columnIndexOfPGender).toInt()
          _tmpPGender = __schoolTypeConverters.toPersonGenderEnum(_tmp_3)
          val _tmpPDateOfBirth: LocalDate?
          val _tmp_4: Long?
          if (_stmt.isNull(_columnIndexOfPDateOfBirth)) {
            _tmp_4 = null
          } else {
            _tmp_4 = _stmt.getLong(_columnIndexOfPDateOfBirth)
          }
          _tmpPDateOfBirth = __schoolTypeConverters.toLocalDate(_tmp_4)
          val _tmpPEmail: String?
          if (_stmt.isNull(_columnIndexOfPEmail)) {
            _tmpPEmail = null
          } else {
            _tmpPEmail = _stmt.getText(_columnIndexOfPEmail)
          }
          val _tmpPPhoneNumber: String?
          if (_stmt.isNull(_columnIndexOfPPhoneNumber)) {
            _tmpPPhoneNumber = null
          } else {
            _tmpPPhoneNumber = _stmt.getText(_columnIndexOfPPhoneNumber)
          }
          _tmpPerson = PersonEntity(_tmpPGuid,_tmpPGuidHash,_tmpPActive,_tmpPStatus,_tmpPLastModified,_tmpPStored,_tmpPMetadata,_tmpPUsername,_tmpPGivenName,_tmpPFamilyName,_tmpPMiddleName,_tmpPGender,_tmpPDateOfBirth,_tmpPEmail,_tmpPPhoneNumber)
          val _tmpRolesCollection: MutableList<PersonRoleEntity>
          val _tmpKey_2: Long
          _tmpKey_2 = _stmt.getLong(_columnIndexOfPGuidHash)
          _tmpRolesCollection = checkNotNull(_collectionRoles.get(_tmpKey_2))
          val _tmpRelatedPersonsCollection: MutableList<PersonRelatedPersonEntity>
          val _tmpKey_3: Long
          _tmpKey_3 = _stmt.getLong(_columnIndexOfPGuidHash)
          _tmpRelatedPersonsCollection = checkNotNull(_collectionRelatedPersons.get(_tmpKey_3))
          _result = PersonEntityWithRoles(_tmpPerson,_tmpRolesCollection,_tmpRelatedPersonsCollection)
        } else {
          _result = null
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override fun findByGuidHashAsFlow(guidHash: Long): Flow<PersonEntityWithRoles?> {
    val _sql: String = """
        |
        |        SELECT * 
        |         FROM PersonEntity
        |        WHERE pGuidHash = ?
        |    
        """.trimMargin()
    return createFlow(__db, true, arrayOf("PersonRoleEntity", "PersonRelatedPersonEntity", "PersonEntity")) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, guidHash)
        val _columnIndexOfPGuid: Int = getColumnIndexOrThrow(_stmt, "pGuid")
        val _columnIndexOfPGuidHash: Int = getColumnIndexOrThrow(_stmt, "pGuidHash")
        val _columnIndexOfPActive: Int = getColumnIndexOrThrow(_stmt, "pActive")
        val _columnIndexOfPStatus: Int = getColumnIndexOrThrow(_stmt, "pStatus")
        val _columnIndexOfPLastModified: Int = getColumnIndexOrThrow(_stmt, "pLastModified")
        val _columnIndexOfPStored: Int = getColumnIndexOrThrow(_stmt, "pStored")
        val _columnIndexOfPMetadata: Int = getColumnIndexOrThrow(_stmt, "pMetadata")
        val _columnIndexOfPUsername: Int = getColumnIndexOrThrow(_stmt, "pUsername")
        val _columnIndexOfPGivenName: Int = getColumnIndexOrThrow(_stmt, "pGivenName")
        val _columnIndexOfPFamilyName: Int = getColumnIndexOrThrow(_stmt, "pFamilyName")
        val _columnIndexOfPMiddleName: Int = getColumnIndexOrThrow(_stmt, "pMiddleName")
        val _columnIndexOfPGender: Int = getColumnIndexOrThrow(_stmt, "pGender")
        val _columnIndexOfPDateOfBirth: Int = getColumnIndexOrThrow(_stmt, "pDateOfBirth")
        val _columnIndexOfPEmail: Int = getColumnIndexOrThrow(_stmt, "pEmail")
        val _columnIndexOfPPhoneNumber: Int = getColumnIndexOrThrow(_stmt, "pPhoneNumber")
        val _collectionRoles: LongSparseArray<MutableList<PersonRoleEntity>> = LongSparseArray<MutableList<PersonRoleEntity>>()
        val _collectionRelatedPersons: LongSparseArray<MutableList<PersonRelatedPersonEntity>> = LongSparseArray<MutableList<PersonRelatedPersonEntity>>()
        while (_stmt.step()) {
          val _tmpKey: Long
          _tmpKey = _stmt.getLong(_columnIndexOfPGuidHash)
          if (!_collectionRoles.containsKey(_tmpKey)) {
            _collectionRoles.put(_tmpKey, mutableListOf())
          }
          val _tmpKey_1: Long
          _tmpKey_1 = _stmt.getLong(_columnIndexOfPGuidHash)
          if (!_collectionRelatedPersons.containsKey(_tmpKey_1)) {
            _collectionRelatedPersons.put(_tmpKey_1, mutableListOf())
          }
        }
        _stmt.reset()
        __fetchRelationshipPersonRoleEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRoleEntity(_connection, _collectionRoles)
        __fetchRelationshipPersonRelatedPersonEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRelatedPersonEntity(_connection, _collectionRelatedPersons)
        val _result: PersonEntityWithRoles?
        if (_stmt.step()) {
          val _tmpPerson: PersonEntity
          val _tmpPGuid: String
          _tmpPGuid = _stmt.getText(_columnIndexOfPGuid)
          val _tmpPGuidHash: Long
          _tmpPGuidHash = _stmt.getLong(_columnIndexOfPGuidHash)
          val _tmpPActive: Boolean
          val _tmp: Int
          _tmp = _stmt.getLong(_columnIndexOfPActive).toInt()
          _tmpPActive = _tmp != 0
          val _tmpPStatus: PersonStatusEnum
          val _tmp_1: Int
          _tmp_1 = _stmt.getLong(_columnIndexOfPStatus).toInt()
          _tmpPStatus = __schoolTypeConverters.toPersonStatusEnum(_tmp_1)
          val _tmpPLastModified: Long
          _tmpPLastModified = _stmt.getLong(_columnIndexOfPLastModified)
          val _tmpPStored: Long
          _tmpPStored = _stmt.getLong(_columnIndexOfPStored)
          val _tmpPMetadata: JsonObject?
          val _tmp_2: String?
          if (_stmt.isNull(_columnIndexOfPMetadata)) {
            _tmp_2 = null
          } else {
            _tmp_2 = _stmt.getText(_columnIndexOfPMetadata)
          }
          _tmpPMetadata = __schoolTypeConverters.toJsonObject(_tmp_2)
          val _tmpPUsername: String?
          if (_stmt.isNull(_columnIndexOfPUsername)) {
            _tmpPUsername = null
          } else {
            _tmpPUsername = _stmt.getText(_columnIndexOfPUsername)
          }
          val _tmpPGivenName: String
          _tmpPGivenName = _stmt.getText(_columnIndexOfPGivenName)
          val _tmpPFamilyName: String
          _tmpPFamilyName = _stmt.getText(_columnIndexOfPFamilyName)
          val _tmpPMiddleName: String?
          if (_stmt.isNull(_columnIndexOfPMiddleName)) {
            _tmpPMiddleName = null
          } else {
            _tmpPMiddleName = _stmt.getText(_columnIndexOfPMiddleName)
          }
          val _tmpPGender: PersonGenderEnum
          val _tmp_3: Int
          _tmp_3 = _stmt.getLong(_columnIndexOfPGender).toInt()
          _tmpPGender = __schoolTypeConverters.toPersonGenderEnum(_tmp_3)
          val _tmpPDateOfBirth: LocalDate?
          val _tmp_4: Long?
          if (_stmt.isNull(_columnIndexOfPDateOfBirth)) {
            _tmp_4 = null
          } else {
            _tmp_4 = _stmt.getLong(_columnIndexOfPDateOfBirth)
          }
          _tmpPDateOfBirth = __schoolTypeConverters.toLocalDate(_tmp_4)
          val _tmpPEmail: String?
          if (_stmt.isNull(_columnIndexOfPEmail)) {
            _tmpPEmail = null
          } else {
            _tmpPEmail = _stmt.getText(_columnIndexOfPEmail)
          }
          val _tmpPPhoneNumber: String?
          if (_stmt.isNull(_columnIndexOfPPhoneNumber)) {
            _tmpPPhoneNumber = null
          } else {
            _tmpPPhoneNumber = _stmt.getText(_columnIndexOfPPhoneNumber)
          }
          _tmpPerson = PersonEntity(_tmpPGuid,_tmpPGuidHash,_tmpPActive,_tmpPStatus,_tmpPLastModified,_tmpPStored,_tmpPMetadata,_tmpPUsername,_tmpPGivenName,_tmpPFamilyName,_tmpPMiddleName,_tmpPGender,_tmpPDateOfBirth,_tmpPEmail,_tmpPPhoneNumber)
          val _tmpRolesCollection: MutableList<PersonRoleEntity>
          val _tmpKey_2: Long
          _tmpKey_2 = _stmt.getLong(_columnIndexOfPGuidHash)
          _tmpRolesCollection = checkNotNull(_collectionRoles.get(_tmpKey_2))
          val _tmpRelatedPersonsCollection: MutableList<PersonRelatedPersonEntity>
          val _tmpKey_3: Long
          _tmpKey_3 = _stmt.getLong(_columnIndexOfPGuidHash)
          _tmpRelatedPersonsCollection = checkNotNull(_collectionRelatedPersons.get(_tmpKey_3))
          _result = PersonEntityWithRoles(_tmpPerson,_tmpRolesCollection,_tmpRelatedPersonsCollection)
        } else {
          _result = null
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override fun findAllListDetailsAsFlow(): Flow<List<PersonListDetails>> {
    val _sql: String = """
        |
        |        SELECT PersonEntity.pGuid AS guid, 
        |               PersonEntity.pGivenName AS givenName, 
        |               PersonEntity.pFamilyName AS familyName, 
        |               PersonEntity.pUsername AS username
        |          FROM PersonEntity
        |    
        """.trimMargin()
    return createFlow(__db, false, arrayOf("PersonEntity")) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        val _columnIndexOfGuid: Int = 0
        val _columnIndexOfGivenName: Int = 1
        val _columnIndexOfFamilyName: Int = 2
        val _columnIndexOfUsername: Int = 3
        val _result: MutableList<PersonListDetails> = mutableListOf()
        while (_stmt.step()) {
          val _item: PersonListDetails
          val _tmpGuid: String
          _tmpGuid = _stmt.getText(_columnIndexOfGuid)
          val _tmpGivenName: String
          _tmpGivenName = _stmt.getText(_columnIndexOfGivenName)
          val _tmpFamilyName: String
          _tmpFamilyName = _stmt.getText(_columnIndexOfFamilyName)
          val _tmpUsername: String?
          if (_stmt.isNull(_columnIndexOfUsername)) {
            _tmpUsername = null
          } else {
            _tmpUsername = _stmt.getText(_columnIndexOfUsername)
          }
          _item = PersonListDetails(_tmpGuid,_tmpGivenName,_tmpFamilyName,_tmpUsername,null,null)
          _result.add(_item)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override fun listAsFlow(
    authenticatedPersonUidNum: Long,
    since: Long,
    guidHash: Long,
    inClazzGuidHash: Long,
    inClazzRoleFlag: Int,
    inClassOnDayInUtcMs: Long,
    filterByName: String?,
    timeNow: Long,
    filterByPersonRole: Int,
    filterByPersonStatus: Int,
    includeRelated: Boolean,
    includeDeleted: Boolean,
  ): Flow<List<PersonEntityWithRoles>> {
    val _sql: String = """
        |
        |           WITH 
        |            AuthenticatedPermissionPersonUids(uidNum) AS (
        |                SELECT ? AS uidNum
        |                UNION
        |                SELECT PersonRelatedPersonEntity.prpOtherPersonUidNum AS uidNum
        |                  FROM PersonRelatedPersonEntity
        |                 WHERE 16 IN 
        |                       (SELECT PersonRoleEntity.prRoleEnum
        |                          FROM PersonRoleEntity
        |                         WHERE PersonRoleEntity.prPersonGuidHash = ?)
        |                   AND PersonRelatedPersonEntity.prpPersonUidNum = ?)     
        |        ,  
        |                
        |            AuthenticatedPersonClassPermissions AS (
        |                SELECT ClassPermissionEntity.*
        |                  FROM ClassPermissionEntity
        |                 WHERE (ClassPermissionEntity.cpeToEnrollmentRole, ClassPermissionEntity.cpeClassUidNum) IN 
        |                       (SELECT EnrollmentEntity.eRole, EnrollmentEntity.eClassUidNum
        |                          FROM EnrollmentEntity
        |                         WHERE EnrollmentEntity.ePersonUidNum IN 
        |                               (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        )
        |                           AND EnrollmentEntity.eStatus = 1)
        |            )
        |        ,
        |                
        |            Persons(uidNum) AS (
        |                   SELECT PersonEntity.pGuidHash 
        |                     FROM PersonEntity
        |                    WHERE PersonEntity.pStored > ? 
        |                      AND (? = 0 OR PersonEntity.pGuidHash = ?)
        |                      AND (? = 0 OR
        |                           EXISTS(
        |                                SELECT EnrollmentEntity.eUid
        |                                  FROM EnrollmentEntity
        |                                 WHERE EnrollmentEntity.ePersonUidNum = PersonEntity.pGuidHash
        |                                   AND EnrollmentEntity.eClassUidNum = ?
        |                                   AND (? = 0 OR EnrollmentEntity.eRole = ?)
        |                                   AND ((? OR ? = 0) 
        |                                        OR (     (? >= COALESCE(EnrollmentEntity.eBeginDate, 0))
        |                                            AND ((? - 86399999) < COALESCE(EnrollmentEntity.eEndDate, 9223372036854775807))
        |                                            AND (? <= COALESCE(EnrollmentEntity.eRemovedAt, 9223372036854775807))
        |                                            AND EnrollmentEntity.eStatus = 1 ))         
        |                           ) 
        |                          ) 
        |                          -- Begin permission NOTE check should add permissions granted to children for parent
        |                      AND (
        |                PersonEntity.pGuidHash = ?
        |             OR PersonEntity.pGuidHash IN 
        |                (SELECT PersonRelatedPersonEntity.prpOtherPersonUidNum
        |                   FROM PersonRelatedPersonEntity
        |                  WHERE PersonRelatedPersonEntity.prpPersonUidNum = ?)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM SchoolPermissionGrantEntity
        |                     WHERE SchoolPermissionGrantEntity.spgToRole IN 
        |                           (SELECT PersonRoleEntity.prRoleEnum
        |                              FROM PersonRoleEntity
        |                             WHERE PersonRoleEntity.prPersonGuidHash IN 
        |                                   (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        ))
        |                               AND (SchoolPermissionGrantEntity.spgPermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM AuthenticatedPersonClassPermissions
        |                     WHERE AuthenticatedPersonClassPermissions.cpeClassUidNum IN 
        |                           (SELECT EnrollmentEntity.eClassUidNum
        |                              FROM EnrollmentEntity
        |                             WHERE EnrollmentEntity.ePersonUidNum = PersonEntity.pGuidHash)
        |                       AND (AuthenticatedPersonClassPermissions.cpePermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |        )
        |                      AND (? IS NULL 
        |                           OR (PersonEntity.pGivenName || ' ' || PersonEntity.pFamilyName) LIKE ('%' || ? || '%'))
        |                      AND (? = 0 OR ? IN 
        |                           (SELECT PersonRoleEntity.prRoleEnum
        |                              FROM PersonRoleEntity
        |                             WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash))
        |                      AND (? = 0 OR PersonEntity.pStatus = ?)
        |                      AND (? OR PersonEntity.pStatus != 0)       
        |            ),
        |                
        |            RelatedPersons(uidNum) AS (
        |                SELECT PersonEntity.pGuidHash
        |                  FROM PersonEntity
        |                 WHERE ? 
        |                   AND PersonEntity.pGuidHash IN(
        |                        SELECT DISTINCT PersonRelatedPersonEntity.prpOtherPersonUidNum
        |                          FROM PersonRelatedPersonEntity
        |                         WHERE PersonRelatedPersonEntity.prpPersonUidNum IN(
        |                               SELECT Persons.uidNum
        |                                 FROM Persons)
        |                       )
        |                   AND (
        |                PersonEntity.pGuidHash = ?
        |             OR PersonEntity.pGuidHash IN 
        |                (SELECT PersonRelatedPersonEntity.prpOtherPersonUidNum
        |                   FROM PersonRelatedPersonEntity
        |                  WHERE PersonRelatedPersonEntity.prpPersonUidNum = ?)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM SchoolPermissionGrantEntity
        |                     WHERE SchoolPermissionGrantEntity.spgToRole IN 
        |                           (SELECT PersonRoleEntity.prRoleEnum
        |                              FROM PersonRoleEntity
        |                             WHERE PersonRoleEntity.prPersonGuidHash IN 
        |                                   (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        ))
        |                               AND (SchoolPermissionGrantEntity.spgPermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM AuthenticatedPersonClassPermissions
        |                     WHERE AuthenticatedPersonClassPermissions.cpeClassUidNum IN 
        |                           (SELECT EnrollmentEntity.eClassUidNum
        |                              FROM EnrollmentEntity
        |                             WHERE EnrollmentEntity.ePersonUidNum = PersonEntity.pGuidHash)
        |                       AND (AuthenticatedPersonClassPermissions.cpePermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |        )    
        |            ),
        |                
        |            AllPersons(uidNum) AS (
        |                SELECT uidNum 
        |                  FROM Persons
        |                 UNION
        |                 SELECT uidNum 
        |                   FROM RelatedPersons
        |            )
        |        
        |                
        |                         
        |         SELECT PersonEntity.*
        |           FROM PersonEntity
        |          WHERE PersonEntity.pGuidHash IN (
        |                SELECT DISTINCT uidNum
        |                  FROM AllPersons)
        |          ORDER BY PersonEntity.pGivenName
        |    
        """.trimMargin()
    return createFlow(__db, true, arrayOf("PersonRoleEntity", "PersonRelatedPersonEntity", "ClassPermissionEntity", "EnrollmentEntity", "PersonEntity", "SchoolPermissionGrantEntity")) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 2
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 3
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 4
        _stmt.bindLong(_argIndex, since)
        _argIndex = 5
        _stmt.bindLong(_argIndex, guidHash)
        _argIndex = 6
        _stmt.bindLong(_argIndex, guidHash)
        _argIndex = 7
        _stmt.bindLong(_argIndex, inClazzGuidHash)
        _argIndex = 8
        _stmt.bindLong(_argIndex, inClazzGuidHash)
        _argIndex = 9
        _stmt.bindLong(_argIndex, inClazzRoleFlag.toLong())
        _argIndex = 10
        _stmt.bindLong(_argIndex, inClazzRoleFlag.toLong())
        _argIndex = 11
        val _tmp: Int = if (includeDeleted) 1 else 0
        _stmt.bindLong(_argIndex, _tmp.toLong())
        _argIndex = 12
        _stmt.bindLong(_argIndex, inClassOnDayInUtcMs)
        _argIndex = 13
        _stmt.bindLong(_argIndex, inClassOnDayInUtcMs)
        _argIndex = 14
        _stmt.bindLong(_argIndex, inClassOnDayInUtcMs)
        _argIndex = 15
        _stmt.bindLong(_argIndex, timeNow)
        _argIndex = 16
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 17
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 18
        if (filterByName == null) {
          _stmt.bindNull(_argIndex)
        } else {
          _stmt.bindText(_argIndex, filterByName)
        }
        _argIndex = 19
        if (filterByName == null) {
          _stmt.bindNull(_argIndex)
        } else {
          _stmt.bindText(_argIndex, filterByName)
        }
        _argIndex = 20
        _stmt.bindLong(_argIndex, filterByPersonRole.toLong())
        _argIndex = 21
        _stmt.bindLong(_argIndex, filterByPersonRole.toLong())
        _argIndex = 22
        _stmt.bindLong(_argIndex, filterByPersonStatus.toLong())
        _argIndex = 23
        _stmt.bindLong(_argIndex, filterByPersonStatus.toLong())
        _argIndex = 24
        val _tmp_1: Int = if (includeDeleted) 1 else 0
        _stmt.bindLong(_argIndex, _tmp_1.toLong())
        _argIndex = 25
        val _tmp_2: Int = if (includeRelated) 1 else 0
        _stmt.bindLong(_argIndex, _tmp_2.toLong())
        _argIndex = 26
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 27
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        val _columnIndexOfPGuid: Int = getColumnIndexOrThrow(_stmt, "pGuid")
        val _columnIndexOfPGuidHash: Int = getColumnIndexOrThrow(_stmt, "pGuidHash")
        val _columnIndexOfPActive: Int = getColumnIndexOrThrow(_stmt, "pActive")
        val _columnIndexOfPStatus: Int = getColumnIndexOrThrow(_stmt, "pStatus")
        val _columnIndexOfPLastModified: Int = getColumnIndexOrThrow(_stmt, "pLastModified")
        val _columnIndexOfPStored: Int = getColumnIndexOrThrow(_stmt, "pStored")
        val _columnIndexOfPMetadata: Int = getColumnIndexOrThrow(_stmt, "pMetadata")
        val _columnIndexOfPUsername: Int = getColumnIndexOrThrow(_stmt, "pUsername")
        val _columnIndexOfPGivenName: Int = getColumnIndexOrThrow(_stmt, "pGivenName")
        val _columnIndexOfPFamilyName: Int = getColumnIndexOrThrow(_stmt, "pFamilyName")
        val _columnIndexOfPMiddleName: Int = getColumnIndexOrThrow(_stmt, "pMiddleName")
        val _columnIndexOfPGender: Int = getColumnIndexOrThrow(_stmt, "pGender")
        val _columnIndexOfPDateOfBirth: Int = getColumnIndexOrThrow(_stmt, "pDateOfBirth")
        val _columnIndexOfPEmail: Int = getColumnIndexOrThrow(_stmt, "pEmail")
        val _columnIndexOfPPhoneNumber: Int = getColumnIndexOrThrow(_stmt, "pPhoneNumber")
        val _collectionRoles: LongSparseArray<MutableList<PersonRoleEntity>> = LongSparseArray<MutableList<PersonRoleEntity>>()
        val _collectionRelatedPersons: LongSparseArray<MutableList<PersonRelatedPersonEntity>> = LongSparseArray<MutableList<PersonRelatedPersonEntity>>()
        while (_stmt.step()) {
          val _tmpKey: Long
          _tmpKey = _stmt.getLong(_columnIndexOfPGuidHash)
          if (!_collectionRoles.containsKey(_tmpKey)) {
            _collectionRoles.put(_tmpKey, mutableListOf())
          }
          val _tmpKey_1: Long
          _tmpKey_1 = _stmt.getLong(_columnIndexOfPGuidHash)
          if (!_collectionRelatedPersons.containsKey(_tmpKey_1)) {
            _collectionRelatedPersons.put(_tmpKey_1, mutableListOf())
          }
        }
        _stmt.reset()
        __fetchRelationshipPersonRoleEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRoleEntity(_connection, _collectionRoles)
        __fetchRelationshipPersonRelatedPersonEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRelatedPersonEntity(_connection, _collectionRelatedPersons)
        val _result: MutableList<PersonEntityWithRoles> = mutableListOf()
        while (_stmt.step()) {
          val _item: PersonEntityWithRoles
          val _tmpPerson: PersonEntity
          val _tmpPGuid: String
          _tmpPGuid = _stmt.getText(_columnIndexOfPGuid)
          val _tmpPGuidHash: Long
          _tmpPGuidHash = _stmt.getLong(_columnIndexOfPGuidHash)
          val _tmpPActive: Boolean
          val _tmp_3: Int
          _tmp_3 = _stmt.getLong(_columnIndexOfPActive).toInt()
          _tmpPActive = _tmp_3 != 0
          val _tmpPStatus: PersonStatusEnum
          val _tmp_4: Int
          _tmp_4 = _stmt.getLong(_columnIndexOfPStatus).toInt()
          _tmpPStatus = __schoolTypeConverters.toPersonStatusEnum(_tmp_4)
          val _tmpPLastModified: Long
          _tmpPLastModified = _stmt.getLong(_columnIndexOfPLastModified)
          val _tmpPStored: Long
          _tmpPStored = _stmt.getLong(_columnIndexOfPStored)
          val _tmpPMetadata: JsonObject?
          val _tmp_5: String?
          if (_stmt.isNull(_columnIndexOfPMetadata)) {
            _tmp_5 = null
          } else {
            _tmp_5 = _stmt.getText(_columnIndexOfPMetadata)
          }
          _tmpPMetadata = __schoolTypeConverters.toJsonObject(_tmp_5)
          val _tmpPUsername: String?
          if (_stmt.isNull(_columnIndexOfPUsername)) {
            _tmpPUsername = null
          } else {
            _tmpPUsername = _stmt.getText(_columnIndexOfPUsername)
          }
          val _tmpPGivenName: String
          _tmpPGivenName = _stmt.getText(_columnIndexOfPGivenName)
          val _tmpPFamilyName: String
          _tmpPFamilyName = _stmt.getText(_columnIndexOfPFamilyName)
          val _tmpPMiddleName: String?
          if (_stmt.isNull(_columnIndexOfPMiddleName)) {
            _tmpPMiddleName = null
          } else {
            _tmpPMiddleName = _stmt.getText(_columnIndexOfPMiddleName)
          }
          val _tmpPGender: PersonGenderEnum
          val _tmp_6: Int
          _tmp_6 = _stmt.getLong(_columnIndexOfPGender).toInt()
          _tmpPGender = __schoolTypeConverters.toPersonGenderEnum(_tmp_6)
          val _tmpPDateOfBirth: LocalDate?
          val _tmp_7: Long?
          if (_stmt.isNull(_columnIndexOfPDateOfBirth)) {
            _tmp_7 = null
          } else {
            _tmp_7 = _stmt.getLong(_columnIndexOfPDateOfBirth)
          }
          _tmpPDateOfBirth = __schoolTypeConverters.toLocalDate(_tmp_7)
          val _tmpPEmail: String?
          if (_stmt.isNull(_columnIndexOfPEmail)) {
            _tmpPEmail = null
          } else {
            _tmpPEmail = _stmt.getText(_columnIndexOfPEmail)
          }
          val _tmpPPhoneNumber: String?
          if (_stmt.isNull(_columnIndexOfPPhoneNumber)) {
            _tmpPPhoneNumber = null
          } else {
            _tmpPPhoneNumber = _stmt.getText(_columnIndexOfPPhoneNumber)
          }
          _tmpPerson = PersonEntity(_tmpPGuid,_tmpPGuidHash,_tmpPActive,_tmpPStatus,_tmpPLastModified,_tmpPStored,_tmpPMetadata,_tmpPUsername,_tmpPGivenName,_tmpPFamilyName,_tmpPMiddleName,_tmpPGender,_tmpPDateOfBirth,_tmpPEmail,_tmpPPhoneNumber)
          val _tmpRolesCollection: MutableList<PersonRoleEntity>
          val _tmpKey_2: Long
          _tmpKey_2 = _stmt.getLong(_columnIndexOfPGuidHash)
          _tmpRolesCollection = checkNotNull(_collectionRoles.get(_tmpKey_2))
          val _tmpRelatedPersonsCollection: MutableList<PersonRelatedPersonEntity>
          val _tmpKey_3: Long
          _tmpKey_3 = _stmt.getLong(_columnIndexOfPGuidHash)
          _tmpRelatedPersonsCollection = checkNotNull(_collectionRelatedPersons.get(_tmpKey_3))
          _item = PersonEntityWithRoles(_tmpPerson,_tmpRolesCollection,_tmpRelatedPersonsCollection)
          _result.add(_item)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun list(
    authenticatedPersonUidNum: Long,
    since: Long,
    guidHash: Long,
    inClazzGuidHash: Long,
    inClazzRoleFlag: Int,
    inClassOnDayInUtcMs: Long,
    filterByName: String?,
    timeNow: Long,
    filterByPersonRole: Int,
    filterByPersonStatus: Int,
    includeRelated: Boolean,
    includeDeleted: Boolean,
  ): List<PersonEntityWithRoles> {
    val _sql: String = """
        |
        |           WITH 
        |            AuthenticatedPermissionPersonUids(uidNum) AS (
        |                SELECT ? AS uidNum
        |                UNION
        |                SELECT PersonRelatedPersonEntity.prpOtherPersonUidNum AS uidNum
        |                  FROM PersonRelatedPersonEntity
        |                 WHERE 16 IN 
        |                       (SELECT PersonRoleEntity.prRoleEnum
        |                          FROM PersonRoleEntity
        |                         WHERE PersonRoleEntity.prPersonGuidHash = ?)
        |                   AND PersonRelatedPersonEntity.prpPersonUidNum = ?)     
        |        ,  
        |                
        |            AuthenticatedPersonClassPermissions AS (
        |                SELECT ClassPermissionEntity.*
        |                  FROM ClassPermissionEntity
        |                 WHERE (ClassPermissionEntity.cpeToEnrollmentRole, ClassPermissionEntity.cpeClassUidNum) IN 
        |                       (SELECT EnrollmentEntity.eRole, EnrollmentEntity.eClassUidNum
        |                          FROM EnrollmentEntity
        |                         WHERE EnrollmentEntity.ePersonUidNum IN 
        |                               (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        )
        |                           AND EnrollmentEntity.eStatus = 1)
        |            )
        |        ,
        |                
        |            Persons(uidNum) AS (
        |                   SELECT PersonEntity.pGuidHash 
        |                     FROM PersonEntity
        |                    WHERE PersonEntity.pStored > ? 
        |                      AND (? = 0 OR PersonEntity.pGuidHash = ?)
        |                      AND (? = 0 OR
        |                           EXISTS(
        |                                SELECT EnrollmentEntity.eUid
        |                                  FROM EnrollmentEntity
        |                                 WHERE EnrollmentEntity.ePersonUidNum = PersonEntity.pGuidHash
        |                                   AND EnrollmentEntity.eClassUidNum = ?
        |                                   AND (? = 0 OR EnrollmentEntity.eRole = ?)
        |                                   AND ((? OR ? = 0) 
        |                                        OR (     (? >= COALESCE(EnrollmentEntity.eBeginDate, 0))
        |                                            AND ((? - 86399999) < COALESCE(EnrollmentEntity.eEndDate, 9223372036854775807))
        |                                            AND (? <= COALESCE(EnrollmentEntity.eRemovedAt, 9223372036854775807))
        |                                            AND EnrollmentEntity.eStatus = 1 ))         
        |                           ) 
        |                          ) 
        |                          -- Begin permission NOTE check should add permissions granted to children for parent
        |                      AND (
        |                PersonEntity.pGuidHash = ?
        |             OR PersonEntity.pGuidHash IN 
        |                (SELECT PersonRelatedPersonEntity.prpOtherPersonUidNum
        |                   FROM PersonRelatedPersonEntity
        |                  WHERE PersonRelatedPersonEntity.prpPersonUidNum = ?)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM SchoolPermissionGrantEntity
        |                     WHERE SchoolPermissionGrantEntity.spgToRole IN 
        |                           (SELECT PersonRoleEntity.prRoleEnum
        |                              FROM PersonRoleEntity
        |                             WHERE PersonRoleEntity.prPersonGuidHash IN 
        |                                   (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        ))
        |                               AND (SchoolPermissionGrantEntity.spgPermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM AuthenticatedPersonClassPermissions
        |                     WHERE AuthenticatedPersonClassPermissions.cpeClassUidNum IN 
        |                           (SELECT EnrollmentEntity.eClassUidNum
        |                              FROM EnrollmentEntity
        |                             WHERE EnrollmentEntity.ePersonUidNum = PersonEntity.pGuidHash)
        |                       AND (AuthenticatedPersonClassPermissions.cpePermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |        )
        |                      AND (? IS NULL 
        |                           OR (PersonEntity.pGivenName || ' ' || PersonEntity.pFamilyName) LIKE ('%' || ? || '%'))
        |                      AND (? = 0 OR ? IN 
        |                           (SELECT PersonRoleEntity.prRoleEnum
        |                              FROM PersonRoleEntity
        |                             WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash))
        |                      AND (? = 0 OR PersonEntity.pStatus = ?)
        |                      AND (? OR PersonEntity.pStatus != 0)       
        |            ),
        |                
        |            RelatedPersons(uidNum) AS (
        |                SELECT PersonEntity.pGuidHash
        |                  FROM PersonEntity
        |                 WHERE ? 
        |                   AND PersonEntity.pGuidHash IN(
        |                        SELECT DISTINCT PersonRelatedPersonEntity.prpOtherPersonUidNum
        |                          FROM PersonRelatedPersonEntity
        |                         WHERE PersonRelatedPersonEntity.prpPersonUidNum IN(
        |                               SELECT Persons.uidNum
        |                                 FROM Persons)
        |                       )
        |                   AND (
        |                PersonEntity.pGuidHash = ?
        |             OR PersonEntity.pGuidHash IN 
        |                (SELECT PersonRelatedPersonEntity.prpOtherPersonUidNum
        |                   FROM PersonRelatedPersonEntity
        |                  WHERE PersonRelatedPersonEntity.prpPersonUidNum = ?)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM SchoolPermissionGrantEntity
        |                     WHERE SchoolPermissionGrantEntity.spgToRole IN 
        |                           (SELECT PersonRoleEntity.prRoleEnum
        |                              FROM PersonRoleEntity
        |                             WHERE PersonRoleEntity.prPersonGuidHash IN 
        |                                   (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        ))
        |                               AND (SchoolPermissionGrantEntity.spgPermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM AuthenticatedPersonClassPermissions
        |                     WHERE AuthenticatedPersonClassPermissions.cpeClassUidNum IN 
        |                           (SELECT EnrollmentEntity.eClassUidNum
        |                              FROM EnrollmentEntity
        |                             WHERE EnrollmentEntity.ePersonUidNum = PersonEntity.pGuidHash)
        |                       AND (AuthenticatedPersonClassPermissions.cpePermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |        )    
        |            ),
        |                
        |            AllPersons(uidNum) AS (
        |                SELECT uidNum 
        |                  FROM Persons
        |                 UNION
        |                 SELECT uidNum 
        |                   FROM RelatedPersons
        |            )
        |        
        |                         
        |         SELECT PersonEntity.*
        |           FROM PersonEntity
        |          WHERE PersonEntity.pGuidHash IN (
        |                SELECT DISTINCT uidNum
        |                  FROM AllPersons)
        |          ORDER BY PersonEntity.pGivenName
        |    
        """.trimMargin()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 2
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 3
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 4
        _stmt.bindLong(_argIndex, since)
        _argIndex = 5
        _stmt.bindLong(_argIndex, guidHash)
        _argIndex = 6
        _stmt.bindLong(_argIndex, guidHash)
        _argIndex = 7
        _stmt.bindLong(_argIndex, inClazzGuidHash)
        _argIndex = 8
        _stmt.bindLong(_argIndex, inClazzGuidHash)
        _argIndex = 9
        _stmt.bindLong(_argIndex, inClazzRoleFlag.toLong())
        _argIndex = 10
        _stmt.bindLong(_argIndex, inClazzRoleFlag.toLong())
        _argIndex = 11
        val _tmp: Int = if (includeDeleted) 1 else 0
        _stmt.bindLong(_argIndex, _tmp.toLong())
        _argIndex = 12
        _stmt.bindLong(_argIndex, inClassOnDayInUtcMs)
        _argIndex = 13
        _stmt.bindLong(_argIndex, inClassOnDayInUtcMs)
        _argIndex = 14
        _stmt.bindLong(_argIndex, inClassOnDayInUtcMs)
        _argIndex = 15
        _stmt.bindLong(_argIndex, timeNow)
        _argIndex = 16
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 17
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 18
        if (filterByName == null) {
          _stmt.bindNull(_argIndex)
        } else {
          _stmt.bindText(_argIndex, filterByName)
        }
        _argIndex = 19
        if (filterByName == null) {
          _stmt.bindNull(_argIndex)
        } else {
          _stmt.bindText(_argIndex, filterByName)
        }
        _argIndex = 20
        _stmt.bindLong(_argIndex, filterByPersonRole.toLong())
        _argIndex = 21
        _stmt.bindLong(_argIndex, filterByPersonRole.toLong())
        _argIndex = 22
        _stmt.bindLong(_argIndex, filterByPersonStatus.toLong())
        _argIndex = 23
        _stmt.bindLong(_argIndex, filterByPersonStatus.toLong())
        _argIndex = 24
        val _tmp_1: Int = if (includeDeleted) 1 else 0
        _stmt.bindLong(_argIndex, _tmp_1.toLong())
        _argIndex = 25
        val _tmp_2: Int = if (includeRelated) 1 else 0
        _stmt.bindLong(_argIndex, _tmp_2.toLong())
        _argIndex = 26
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 27
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        val _columnIndexOfPGuid: Int = getColumnIndexOrThrow(_stmt, "pGuid")
        val _columnIndexOfPGuidHash: Int = getColumnIndexOrThrow(_stmt, "pGuidHash")
        val _columnIndexOfPActive: Int = getColumnIndexOrThrow(_stmt, "pActive")
        val _columnIndexOfPStatus: Int = getColumnIndexOrThrow(_stmt, "pStatus")
        val _columnIndexOfPLastModified: Int = getColumnIndexOrThrow(_stmt, "pLastModified")
        val _columnIndexOfPStored: Int = getColumnIndexOrThrow(_stmt, "pStored")
        val _columnIndexOfPMetadata: Int = getColumnIndexOrThrow(_stmt, "pMetadata")
        val _columnIndexOfPUsername: Int = getColumnIndexOrThrow(_stmt, "pUsername")
        val _columnIndexOfPGivenName: Int = getColumnIndexOrThrow(_stmt, "pGivenName")
        val _columnIndexOfPFamilyName: Int = getColumnIndexOrThrow(_stmt, "pFamilyName")
        val _columnIndexOfPMiddleName: Int = getColumnIndexOrThrow(_stmt, "pMiddleName")
        val _columnIndexOfPGender: Int = getColumnIndexOrThrow(_stmt, "pGender")
        val _columnIndexOfPDateOfBirth: Int = getColumnIndexOrThrow(_stmt, "pDateOfBirth")
        val _columnIndexOfPEmail: Int = getColumnIndexOrThrow(_stmt, "pEmail")
        val _columnIndexOfPPhoneNumber: Int = getColumnIndexOrThrow(_stmt, "pPhoneNumber")
        val _collectionRoles: LongSparseArray<MutableList<PersonRoleEntity>> = LongSparseArray<MutableList<PersonRoleEntity>>()
        val _collectionRelatedPersons: LongSparseArray<MutableList<PersonRelatedPersonEntity>> = LongSparseArray<MutableList<PersonRelatedPersonEntity>>()
        while (_stmt.step()) {
          val _tmpKey: Long
          _tmpKey = _stmt.getLong(_columnIndexOfPGuidHash)
          if (!_collectionRoles.containsKey(_tmpKey)) {
            _collectionRoles.put(_tmpKey, mutableListOf())
          }
          val _tmpKey_1: Long
          _tmpKey_1 = _stmt.getLong(_columnIndexOfPGuidHash)
          if (!_collectionRelatedPersons.containsKey(_tmpKey_1)) {
            _collectionRelatedPersons.put(_tmpKey_1, mutableListOf())
          }
        }
        _stmt.reset()
        __fetchRelationshipPersonRoleEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRoleEntity(_connection, _collectionRoles)
        __fetchRelationshipPersonRelatedPersonEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRelatedPersonEntity(_connection, _collectionRelatedPersons)
        val _result: MutableList<PersonEntityWithRoles> = mutableListOf()
        while (_stmt.step()) {
          val _item: PersonEntityWithRoles
          val _tmpPerson: PersonEntity
          val _tmpPGuid: String
          _tmpPGuid = _stmt.getText(_columnIndexOfPGuid)
          val _tmpPGuidHash: Long
          _tmpPGuidHash = _stmt.getLong(_columnIndexOfPGuidHash)
          val _tmpPActive: Boolean
          val _tmp_3: Int
          _tmp_3 = _stmt.getLong(_columnIndexOfPActive).toInt()
          _tmpPActive = _tmp_3 != 0
          val _tmpPStatus: PersonStatusEnum
          val _tmp_4: Int
          _tmp_4 = _stmt.getLong(_columnIndexOfPStatus).toInt()
          _tmpPStatus = __schoolTypeConverters.toPersonStatusEnum(_tmp_4)
          val _tmpPLastModified: Long
          _tmpPLastModified = _stmt.getLong(_columnIndexOfPLastModified)
          val _tmpPStored: Long
          _tmpPStored = _stmt.getLong(_columnIndexOfPStored)
          val _tmpPMetadata: JsonObject?
          val _tmp_5: String?
          if (_stmt.isNull(_columnIndexOfPMetadata)) {
            _tmp_5 = null
          } else {
            _tmp_5 = _stmt.getText(_columnIndexOfPMetadata)
          }
          _tmpPMetadata = __schoolTypeConverters.toJsonObject(_tmp_5)
          val _tmpPUsername: String?
          if (_stmt.isNull(_columnIndexOfPUsername)) {
            _tmpPUsername = null
          } else {
            _tmpPUsername = _stmt.getText(_columnIndexOfPUsername)
          }
          val _tmpPGivenName: String
          _tmpPGivenName = _stmt.getText(_columnIndexOfPGivenName)
          val _tmpPFamilyName: String
          _tmpPFamilyName = _stmt.getText(_columnIndexOfPFamilyName)
          val _tmpPMiddleName: String?
          if (_stmt.isNull(_columnIndexOfPMiddleName)) {
            _tmpPMiddleName = null
          } else {
            _tmpPMiddleName = _stmt.getText(_columnIndexOfPMiddleName)
          }
          val _tmpPGender: PersonGenderEnum
          val _tmp_6: Int
          _tmp_6 = _stmt.getLong(_columnIndexOfPGender).toInt()
          _tmpPGender = __schoolTypeConverters.toPersonGenderEnum(_tmp_6)
          val _tmpPDateOfBirth: LocalDate?
          val _tmp_7: Long?
          if (_stmt.isNull(_columnIndexOfPDateOfBirth)) {
            _tmp_7 = null
          } else {
            _tmp_7 = _stmt.getLong(_columnIndexOfPDateOfBirth)
          }
          _tmpPDateOfBirth = __schoolTypeConverters.toLocalDate(_tmp_7)
          val _tmpPEmail: String?
          if (_stmt.isNull(_columnIndexOfPEmail)) {
            _tmpPEmail = null
          } else {
            _tmpPEmail = _stmt.getText(_columnIndexOfPEmail)
          }
          val _tmpPPhoneNumber: String?
          if (_stmt.isNull(_columnIndexOfPPhoneNumber)) {
            _tmpPPhoneNumber = null
          } else {
            _tmpPPhoneNumber = _stmt.getText(_columnIndexOfPPhoneNumber)
          }
          _tmpPerson = PersonEntity(_tmpPGuid,_tmpPGuidHash,_tmpPActive,_tmpPStatus,_tmpPLastModified,_tmpPStored,_tmpPMetadata,_tmpPUsername,_tmpPGivenName,_tmpPFamilyName,_tmpPMiddleName,_tmpPGender,_tmpPDateOfBirth,_tmpPEmail,_tmpPPhoneNumber)
          val _tmpRolesCollection: MutableList<PersonRoleEntity>
          val _tmpKey_2: Long
          _tmpKey_2 = _stmt.getLong(_columnIndexOfPGuidHash)
          _tmpRolesCollection = checkNotNull(_collectionRoles.get(_tmpKey_2))
          val _tmpRelatedPersonsCollection: MutableList<PersonRelatedPersonEntity>
          val _tmpKey_3: Long
          _tmpKey_3 = _stmt.getLong(_columnIndexOfPGuidHash)
          _tmpRelatedPersonsCollection = checkNotNull(_collectionRelatedPersons.get(_tmpKey_3))
          _item = PersonEntityWithRoles(_tmpPerson,_tmpRolesCollection,_tmpRelatedPersonsCollection)
          _result.add(_item)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun findByUidList(uidNums: List<Long>): List<PersonEntityWithRoles> {
    val _stringBuilder: StringBuilder = StringBuilder()
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("        SELECT PersonEntity.*")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("          FROM PersonEntity")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("         WHERE PersonEntity.pGuidHash IN (")
    val _inputSize: Int = uidNums.size
    appendPlaceholders(_stringBuilder, _inputSize)
    _stringBuilder.append(") ")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("    ")
    val _sql: String = _stringBuilder.toString()
    return performSuspending(__db, true, true) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        for (_item: Long in uidNums) {
          _stmt.bindLong(_argIndex, _item)
          _argIndex++
        }
        val _columnIndexOfPGuid: Int = getColumnIndexOrThrow(_stmt, "pGuid")
        val _columnIndexOfPGuidHash: Int = getColumnIndexOrThrow(_stmt, "pGuidHash")
        val _columnIndexOfPActive: Int = getColumnIndexOrThrow(_stmt, "pActive")
        val _columnIndexOfPStatus: Int = getColumnIndexOrThrow(_stmt, "pStatus")
        val _columnIndexOfPLastModified: Int = getColumnIndexOrThrow(_stmt, "pLastModified")
        val _columnIndexOfPStored: Int = getColumnIndexOrThrow(_stmt, "pStored")
        val _columnIndexOfPMetadata: Int = getColumnIndexOrThrow(_stmt, "pMetadata")
        val _columnIndexOfPUsername: Int = getColumnIndexOrThrow(_stmt, "pUsername")
        val _columnIndexOfPGivenName: Int = getColumnIndexOrThrow(_stmt, "pGivenName")
        val _columnIndexOfPFamilyName: Int = getColumnIndexOrThrow(_stmt, "pFamilyName")
        val _columnIndexOfPMiddleName: Int = getColumnIndexOrThrow(_stmt, "pMiddleName")
        val _columnIndexOfPGender: Int = getColumnIndexOrThrow(_stmt, "pGender")
        val _columnIndexOfPDateOfBirth: Int = getColumnIndexOrThrow(_stmt, "pDateOfBirth")
        val _columnIndexOfPEmail: Int = getColumnIndexOrThrow(_stmt, "pEmail")
        val _columnIndexOfPPhoneNumber: Int = getColumnIndexOrThrow(_stmt, "pPhoneNumber")
        val _collectionRoles: LongSparseArray<MutableList<PersonRoleEntity>> = LongSparseArray<MutableList<PersonRoleEntity>>()
        val _collectionRelatedPersons: LongSparseArray<MutableList<PersonRelatedPersonEntity>> = LongSparseArray<MutableList<PersonRelatedPersonEntity>>()
        while (_stmt.step()) {
          val _tmpKey: Long
          _tmpKey = _stmt.getLong(_columnIndexOfPGuidHash)
          if (!_collectionRoles.containsKey(_tmpKey)) {
            _collectionRoles.put(_tmpKey, mutableListOf())
          }
          val _tmpKey_1: Long
          _tmpKey_1 = _stmt.getLong(_columnIndexOfPGuidHash)
          if (!_collectionRelatedPersons.containsKey(_tmpKey_1)) {
            _collectionRelatedPersons.put(_tmpKey_1, mutableListOf())
          }
        }
        _stmt.reset()
        __fetchRelationshipPersonRoleEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRoleEntity(_connection, _collectionRoles)
        __fetchRelationshipPersonRelatedPersonEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRelatedPersonEntity(_connection, _collectionRelatedPersons)
        val _result: MutableList<PersonEntityWithRoles> = mutableListOf()
        while (_stmt.step()) {
          val _item_1: PersonEntityWithRoles
          val _tmpPerson: PersonEntity
          val _tmpPGuid: String
          _tmpPGuid = _stmt.getText(_columnIndexOfPGuid)
          val _tmpPGuidHash: Long
          _tmpPGuidHash = _stmt.getLong(_columnIndexOfPGuidHash)
          val _tmpPActive: Boolean
          val _tmp: Int
          _tmp = _stmt.getLong(_columnIndexOfPActive).toInt()
          _tmpPActive = _tmp != 0
          val _tmpPStatus: PersonStatusEnum
          val _tmp_1: Int
          _tmp_1 = _stmt.getLong(_columnIndexOfPStatus).toInt()
          _tmpPStatus = __schoolTypeConverters.toPersonStatusEnum(_tmp_1)
          val _tmpPLastModified: Long
          _tmpPLastModified = _stmt.getLong(_columnIndexOfPLastModified)
          val _tmpPStored: Long
          _tmpPStored = _stmt.getLong(_columnIndexOfPStored)
          val _tmpPMetadata: JsonObject?
          val _tmp_2: String?
          if (_stmt.isNull(_columnIndexOfPMetadata)) {
            _tmp_2 = null
          } else {
            _tmp_2 = _stmt.getText(_columnIndexOfPMetadata)
          }
          _tmpPMetadata = __schoolTypeConverters.toJsonObject(_tmp_2)
          val _tmpPUsername: String?
          if (_stmt.isNull(_columnIndexOfPUsername)) {
            _tmpPUsername = null
          } else {
            _tmpPUsername = _stmt.getText(_columnIndexOfPUsername)
          }
          val _tmpPGivenName: String
          _tmpPGivenName = _stmt.getText(_columnIndexOfPGivenName)
          val _tmpPFamilyName: String
          _tmpPFamilyName = _stmt.getText(_columnIndexOfPFamilyName)
          val _tmpPMiddleName: String?
          if (_stmt.isNull(_columnIndexOfPMiddleName)) {
            _tmpPMiddleName = null
          } else {
            _tmpPMiddleName = _stmt.getText(_columnIndexOfPMiddleName)
          }
          val _tmpPGender: PersonGenderEnum
          val _tmp_3: Int
          _tmp_3 = _stmt.getLong(_columnIndexOfPGender).toInt()
          _tmpPGender = __schoolTypeConverters.toPersonGenderEnum(_tmp_3)
          val _tmpPDateOfBirth: LocalDate?
          val _tmp_4: Long?
          if (_stmt.isNull(_columnIndexOfPDateOfBirth)) {
            _tmp_4 = null
          } else {
            _tmp_4 = _stmt.getLong(_columnIndexOfPDateOfBirth)
          }
          _tmpPDateOfBirth = __schoolTypeConverters.toLocalDate(_tmp_4)
          val _tmpPEmail: String?
          if (_stmt.isNull(_columnIndexOfPEmail)) {
            _tmpPEmail = null
          } else {
            _tmpPEmail = _stmt.getText(_columnIndexOfPEmail)
          }
          val _tmpPPhoneNumber: String?
          if (_stmt.isNull(_columnIndexOfPPhoneNumber)) {
            _tmpPPhoneNumber = null
          } else {
            _tmpPPhoneNumber = _stmt.getText(_columnIndexOfPPhoneNumber)
          }
          _tmpPerson = PersonEntity(_tmpPGuid,_tmpPGuidHash,_tmpPActive,_tmpPStatus,_tmpPLastModified,_tmpPStored,_tmpPMetadata,_tmpPUsername,_tmpPGivenName,_tmpPFamilyName,_tmpPMiddleName,_tmpPGender,_tmpPDateOfBirth,_tmpPEmail,_tmpPPhoneNumber)
          val _tmpRolesCollection: MutableList<PersonRoleEntity>
          val _tmpKey_2: Long
          _tmpKey_2 = _stmt.getLong(_columnIndexOfPGuidHash)
          _tmpRolesCollection = checkNotNull(_collectionRoles.get(_tmpKey_2))
          val _tmpRelatedPersonsCollection: MutableList<PersonRelatedPersonEntity>
          val _tmpKey_3: Long
          _tmpKey_3 = _stmt.getLong(_columnIndexOfPGuidHash)
          _tmpRelatedPersonsCollection = checkNotNull(_collectionRelatedPersons.get(_tmpKey_3))
          _item_1 = PersonEntityWithRoles(_tmpPerson,_tmpRolesCollection,_tmpRelatedPersonsCollection)
          _result.add(_item_1)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override fun listAsPagingSource(
    authenticatedPersonUidNum: Long,
    since: Long,
    guidHash: Long,
    inClazzGuidHash: Long,
    inClassOnDayInUtcMs: Long,
    inClazzRoleFlag: Int,
    filterByName: String?,
    timeNow: Long,
    filterByPersonRole: Int,
    filterByPersonStatus: Int,
    includeRelated: Boolean,
    includeDeleted: Boolean,
  ): PagingSource<Int, PersonEntityWithRoles> {
    val _sql: String = """
        |
        |           WITH 
        |            AuthenticatedPermissionPersonUids(uidNum) AS (
        |                SELECT ? AS uidNum
        |                UNION
        |                SELECT PersonRelatedPersonEntity.prpOtherPersonUidNum AS uidNum
        |                  FROM PersonRelatedPersonEntity
        |                 WHERE 16 IN 
        |                       (SELECT PersonRoleEntity.prRoleEnum
        |                          FROM PersonRoleEntity
        |                         WHERE PersonRoleEntity.prPersonGuidHash = ?)
        |                   AND PersonRelatedPersonEntity.prpPersonUidNum = ?)     
        |        ,  
        |                
        |            AuthenticatedPersonClassPermissions AS (
        |                SELECT ClassPermissionEntity.*
        |                  FROM ClassPermissionEntity
        |                 WHERE (ClassPermissionEntity.cpeToEnrollmentRole, ClassPermissionEntity.cpeClassUidNum) IN 
        |                       (SELECT EnrollmentEntity.eRole, EnrollmentEntity.eClassUidNum
        |                          FROM EnrollmentEntity
        |                         WHERE EnrollmentEntity.ePersonUidNum IN 
        |                               (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        )
        |                           AND EnrollmentEntity.eStatus = 1)
        |            )
        |        ,
        |                
        |            Persons(uidNum) AS (
        |                   SELECT PersonEntity.pGuidHash 
        |                     FROM PersonEntity
        |                    WHERE PersonEntity.pStored > ? 
        |                      AND (? = 0 OR PersonEntity.pGuidHash = ?)
        |                      AND (? = 0 OR
        |                           EXISTS(
        |                                SELECT EnrollmentEntity.eUid
        |                                  FROM EnrollmentEntity
        |                                 WHERE EnrollmentEntity.ePersonUidNum = PersonEntity.pGuidHash
        |                                   AND EnrollmentEntity.eClassUidNum = ?
        |                                   AND (? = 0 OR EnrollmentEntity.eRole = ?)
        |                                   AND ((? OR ? = 0) 
        |                                        OR (     (? >= COALESCE(EnrollmentEntity.eBeginDate, 0))
        |                                            AND ((? - 86399999) < COALESCE(EnrollmentEntity.eEndDate, 9223372036854775807))
        |                                            AND (? <= COALESCE(EnrollmentEntity.eRemovedAt, 9223372036854775807))
        |                                            AND EnrollmentEntity.eStatus = 1 ))         
        |                           ) 
        |                          ) 
        |                          -- Begin permission NOTE check should add permissions granted to children for parent
        |                      AND (
        |                PersonEntity.pGuidHash = ?
        |             OR PersonEntity.pGuidHash IN 
        |                (SELECT PersonRelatedPersonEntity.prpOtherPersonUidNum
        |                   FROM PersonRelatedPersonEntity
        |                  WHERE PersonRelatedPersonEntity.prpPersonUidNum = ?)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM SchoolPermissionGrantEntity
        |                     WHERE SchoolPermissionGrantEntity.spgToRole IN 
        |                           (SELECT PersonRoleEntity.prRoleEnum
        |                              FROM PersonRoleEntity
        |                             WHERE PersonRoleEntity.prPersonGuidHash IN 
        |                                   (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        ))
        |                               AND (SchoolPermissionGrantEntity.spgPermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM AuthenticatedPersonClassPermissions
        |                     WHERE AuthenticatedPersonClassPermissions.cpeClassUidNum IN 
        |                           (SELECT EnrollmentEntity.eClassUidNum
        |                              FROM EnrollmentEntity
        |                             WHERE EnrollmentEntity.ePersonUidNum = PersonEntity.pGuidHash)
        |                       AND (AuthenticatedPersonClassPermissions.cpePermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |        )
        |                      AND (? IS NULL 
        |                           OR (PersonEntity.pGivenName || ' ' || PersonEntity.pFamilyName) LIKE ('%' || ? || '%'))
        |                      AND (? = 0 OR ? IN 
        |                           (SELECT PersonRoleEntity.prRoleEnum
        |                              FROM PersonRoleEntity
        |                             WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash))
        |                      AND (? = 0 OR PersonEntity.pStatus = ?)
        |                      AND (? OR PersonEntity.pStatus != 0)       
        |            ),
        |                
        |            RelatedPersons(uidNum) AS (
        |                SELECT PersonEntity.pGuidHash
        |                  FROM PersonEntity
        |                 WHERE ? 
        |                   AND PersonEntity.pGuidHash IN(
        |                        SELECT DISTINCT PersonRelatedPersonEntity.prpOtherPersonUidNum
        |                          FROM PersonRelatedPersonEntity
        |                         WHERE PersonRelatedPersonEntity.prpPersonUidNum IN(
        |                               SELECT Persons.uidNum
        |                                 FROM Persons)
        |                       )
        |                   AND (
        |                PersonEntity.pGuidHash = ?
        |             OR PersonEntity.pGuidHash IN 
        |                (SELECT PersonRelatedPersonEntity.prpOtherPersonUidNum
        |                   FROM PersonRelatedPersonEntity
        |                  WHERE PersonRelatedPersonEntity.prpPersonUidNum = ?)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM SchoolPermissionGrantEntity
        |                     WHERE SchoolPermissionGrantEntity.spgToRole IN 
        |                           (SELECT PersonRoleEntity.prRoleEnum
        |                              FROM PersonRoleEntity
        |                             WHERE PersonRoleEntity.prPersonGuidHash IN 
        |                                   (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        ))
        |                               AND (SchoolPermissionGrantEntity.spgPermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM AuthenticatedPersonClassPermissions
        |                     WHERE AuthenticatedPersonClassPermissions.cpeClassUidNum IN 
        |                           (SELECT EnrollmentEntity.eClassUidNum
        |                              FROM EnrollmentEntity
        |                             WHERE EnrollmentEntity.ePersonUidNum = PersonEntity.pGuidHash)
        |                       AND (AuthenticatedPersonClassPermissions.cpePermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |        )    
        |            ),
        |                
        |            AllPersons(uidNum) AS (
        |                SELECT uidNum 
        |                  FROM Persons
        |                 UNION
        |                 SELECT uidNum 
        |                   FROM RelatedPersons
        |            )
        |        
        |                         
        |         SELECT PersonEntity.*
        |           FROM PersonEntity
        |          WHERE PersonEntity.pGuidHash IN (
        |                SELECT DISTINCT uidNum
        |                  FROM AllPersons)
        |          ORDER BY PersonEntity.pGivenName
        |    
        """.trimMargin()
    val _rawQuery: RoomRawQuery = RoomRawQuery(_sql) { _stmt ->
      var _argIndex: Int = 1
      _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
      _argIndex = 2
      _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
      _argIndex = 3
      _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
      _argIndex = 4
      _stmt.bindLong(_argIndex, since)
      _argIndex = 5
      _stmt.bindLong(_argIndex, guidHash)
      _argIndex = 6
      _stmt.bindLong(_argIndex, guidHash)
      _argIndex = 7
      _stmt.bindLong(_argIndex, inClazzGuidHash)
      _argIndex = 8
      _stmt.bindLong(_argIndex, inClazzGuidHash)
      _argIndex = 9
      _stmt.bindLong(_argIndex, inClazzRoleFlag.toLong())
      _argIndex = 10
      _stmt.bindLong(_argIndex, inClazzRoleFlag.toLong())
      _argIndex = 11
      val _tmp: Int = if (includeDeleted) 1 else 0
      _stmt.bindLong(_argIndex, _tmp.toLong())
      _argIndex = 12
      _stmt.bindLong(_argIndex, inClassOnDayInUtcMs)
      _argIndex = 13
      _stmt.bindLong(_argIndex, inClassOnDayInUtcMs)
      _argIndex = 14
      _stmt.bindLong(_argIndex, inClassOnDayInUtcMs)
      _argIndex = 15
      _stmt.bindLong(_argIndex, timeNow)
      _argIndex = 16
      _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
      _argIndex = 17
      _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
      _argIndex = 18
      if (filterByName == null) {
        _stmt.bindNull(_argIndex)
      } else {
        _stmt.bindText(_argIndex, filterByName)
      }
      _argIndex = 19
      if (filterByName == null) {
        _stmt.bindNull(_argIndex)
      } else {
        _stmt.bindText(_argIndex, filterByName)
      }
      _argIndex = 20
      _stmt.bindLong(_argIndex, filterByPersonRole.toLong())
      _argIndex = 21
      _stmt.bindLong(_argIndex, filterByPersonRole.toLong())
      _argIndex = 22
      _stmt.bindLong(_argIndex, filterByPersonStatus.toLong())
      _argIndex = 23
      _stmt.bindLong(_argIndex, filterByPersonStatus.toLong())
      _argIndex = 24
      val _tmp_1: Int = if (includeDeleted) 1 else 0
      _stmt.bindLong(_argIndex, _tmp_1.toLong())
      _argIndex = 25
      val _tmp_2: Int = if (includeRelated) 1 else 0
      _stmt.bindLong(_argIndex, _tmp_2.toLong())
      _argIndex = 26
      _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
      _argIndex = 27
      _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
    }
    return object : LimitOffsetPagingSource<PersonEntityWithRoles>(_rawQuery, __db, "PersonRoleEntity", "PersonRelatedPersonEntity", "ClassPermissionEntity", "EnrollmentEntity", "PersonEntity", "SchoolPermissionGrantEntity") {
      protected override suspend fun convertRows(limitOffsetQuery: RoomRawQuery, itemCount: Int): List<PersonEntityWithRoles> = performSuspending(__db, true, false) { _connection ->
        val _stmt: SQLiteStatement = _connection.prepare(limitOffsetQuery.sql)
        limitOffsetQuery.getBindingFunction().invoke(_stmt)
        try {
          val _columnIndexOfPGuid: Int = getColumnIndexOrThrow(_stmt, "pGuid")
          val _columnIndexOfPGuidHash: Int = getColumnIndexOrThrow(_stmt, "pGuidHash")
          val _columnIndexOfPActive: Int = getColumnIndexOrThrow(_stmt, "pActive")
          val _columnIndexOfPStatus: Int = getColumnIndexOrThrow(_stmt, "pStatus")
          val _columnIndexOfPLastModified: Int = getColumnIndexOrThrow(_stmt, "pLastModified")
          val _columnIndexOfPStored: Int = getColumnIndexOrThrow(_stmt, "pStored")
          val _columnIndexOfPMetadata: Int = getColumnIndexOrThrow(_stmt, "pMetadata")
          val _columnIndexOfPUsername: Int = getColumnIndexOrThrow(_stmt, "pUsername")
          val _columnIndexOfPGivenName: Int = getColumnIndexOrThrow(_stmt, "pGivenName")
          val _columnIndexOfPFamilyName: Int = getColumnIndexOrThrow(_stmt, "pFamilyName")
          val _columnIndexOfPMiddleName: Int = getColumnIndexOrThrow(_stmt, "pMiddleName")
          val _columnIndexOfPGender: Int = getColumnIndexOrThrow(_stmt, "pGender")
          val _columnIndexOfPDateOfBirth: Int = getColumnIndexOrThrow(_stmt, "pDateOfBirth")
          val _columnIndexOfPEmail: Int = getColumnIndexOrThrow(_stmt, "pEmail")
          val _columnIndexOfPPhoneNumber: Int = getColumnIndexOrThrow(_stmt, "pPhoneNumber")
          val _collectionRoles: LongSparseArray<MutableList<PersonRoleEntity>> = LongSparseArray<MutableList<PersonRoleEntity>>()
          val _collectionRelatedPersons: LongSparseArray<MutableList<PersonRelatedPersonEntity>> = LongSparseArray<MutableList<PersonRelatedPersonEntity>>()
          while (_stmt.step()) {
            val _tmpKey: Long
            _tmpKey = _stmt.getLong(_columnIndexOfPGuidHash)
            if (!_collectionRoles.containsKey(_tmpKey)) {
              _collectionRoles.put(_tmpKey, mutableListOf())
            }
            val _tmpKey_1: Long
            _tmpKey_1 = _stmt.getLong(_columnIndexOfPGuidHash)
            if (!_collectionRelatedPersons.containsKey(_tmpKey_1)) {
              _collectionRelatedPersons.put(_tmpKey_1, mutableListOf())
            }
          }
          _stmt.reset()
          __fetchRelationshipPersonRoleEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRoleEntity(_connection, _collectionRoles)
          __fetchRelationshipPersonRelatedPersonEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRelatedPersonEntity(_connection, _collectionRelatedPersons)
          val _result: MutableList<PersonEntityWithRoles> = mutableListOf()
          while (_stmt.step()) {
            val _item: PersonEntityWithRoles
            val _tmpPerson: PersonEntity
            val _tmpPGuid: String
            _tmpPGuid = _stmt.getText(_columnIndexOfPGuid)
            val _tmpPGuidHash: Long
            _tmpPGuidHash = _stmt.getLong(_columnIndexOfPGuidHash)
            val _tmpPActive: Boolean
            val _tmp_3: Int
            _tmp_3 = _stmt.getLong(_columnIndexOfPActive).toInt()
            _tmpPActive = _tmp_3 != 0
            val _tmpPStatus: PersonStatusEnum
            val _tmp_4: Int
            _tmp_4 = _stmt.getLong(_columnIndexOfPStatus).toInt()
            _tmpPStatus = __schoolTypeConverters.toPersonStatusEnum(_tmp_4)
            val _tmpPLastModified: Long
            _tmpPLastModified = _stmt.getLong(_columnIndexOfPLastModified)
            val _tmpPStored: Long
            _tmpPStored = _stmt.getLong(_columnIndexOfPStored)
            val _tmpPMetadata: JsonObject?
            val _tmp_5: String?
            if (_stmt.isNull(_columnIndexOfPMetadata)) {
              _tmp_5 = null
            } else {
              _tmp_5 = _stmt.getText(_columnIndexOfPMetadata)
            }
            _tmpPMetadata = __schoolTypeConverters.toJsonObject(_tmp_5)
            val _tmpPUsername: String?
            if (_stmt.isNull(_columnIndexOfPUsername)) {
              _tmpPUsername = null
            } else {
              _tmpPUsername = _stmt.getText(_columnIndexOfPUsername)
            }
            val _tmpPGivenName: String
            _tmpPGivenName = _stmt.getText(_columnIndexOfPGivenName)
            val _tmpPFamilyName: String
            _tmpPFamilyName = _stmt.getText(_columnIndexOfPFamilyName)
            val _tmpPMiddleName: String?
            if (_stmt.isNull(_columnIndexOfPMiddleName)) {
              _tmpPMiddleName = null
            } else {
              _tmpPMiddleName = _stmt.getText(_columnIndexOfPMiddleName)
            }
            val _tmpPGender: PersonGenderEnum
            val _tmp_6: Int
            _tmp_6 = _stmt.getLong(_columnIndexOfPGender).toInt()
            _tmpPGender = __schoolTypeConverters.toPersonGenderEnum(_tmp_6)
            val _tmpPDateOfBirth: LocalDate?
            val _tmp_7: Long?
            if (_stmt.isNull(_columnIndexOfPDateOfBirth)) {
              _tmp_7 = null
            } else {
              _tmp_7 = _stmt.getLong(_columnIndexOfPDateOfBirth)
            }
            _tmpPDateOfBirth = __schoolTypeConverters.toLocalDate(_tmp_7)
            val _tmpPEmail: String?
            if (_stmt.isNull(_columnIndexOfPEmail)) {
              _tmpPEmail = null
            } else {
              _tmpPEmail = _stmt.getText(_columnIndexOfPEmail)
            }
            val _tmpPPhoneNumber: String?
            if (_stmt.isNull(_columnIndexOfPPhoneNumber)) {
              _tmpPPhoneNumber = null
            } else {
              _tmpPPhoneNumber = _stmt.getText(_columnIndexOfPPhoneNumber)
            }
            _tmpPerson = PersonEntity(_tmpPGuid,_tmpPGuidHash,_tmpPActive,_tmpPStatus,_tmpPLastModified,_tmpPStored,_tmpPMetadata,_tmpPUsername,_tmpPGivenName,_tmpPFamilyName,_tmpPMiddleName,_tmpPGender,_tmpPDateOfBirth,_tmpPEmail,_tmpPPhoneNumber)
            val _tmpRolesCollection: MutableList<PersonRoleEntity>
            val _tmpKey_2: Long
            _tmpKey_2 = _stmt.getLong(_columnIndexOfPGuidHash)
            _tmpRolesCollection = checkNotNull(_collectionRoles.get(_tmpKey_2))
            val _tmpRelatedPersonsCollection: MutableList<PersonRelatedPersonEntity>
            val _tmpKey_3: Long
            _tmpKey_3 = _stmt.getLong(_columnIndexOfPGuidHash)
            _tmpRelatedPersonsCollection = checkNotNull(_collectionRelatedPersons.get(_tmpKey_3))
            _item = PersonEntityWithRoles(_tmpPerson,_tmpRolesCollection,_tmpRelatedPersonsCollection)
            _result.add(_item)
          }
          _result
        } finally {
          _stmt.close()
        }
      }
    }
  }

  public override fun findAllListDetailsAsPagingSource(
    authenticatedPersonUidNum: Long,
    since: Long,
    guidHash: Long,
    inClazzGuidHash: Long,
    inClazzRoleFlag: Int,
    inClassOnDayInUtcMs: Long,
    filterByName: String?,
    timeNow: Long,
    filterByPersonRole: Int,
    filterByPersonStatus: Int,
    includeRelated: Boolean,
    includeDeleted: Boolean,
  ): PagingSource<Int, PersonListDetails> {
    val _sql: String = """
        |
        |          WITH 
        |            AuthenticatedPermissionPersonUids(uidNum) AS (
        |                SELECT ? AS uidNum
        |                UNION
        |                SELECT PersonRelatedPersonEntity.prpOtherPersonUidNum AS uidNum
        |                  FROM PersonRelatedPersonEntity
        |                 WHERE 16 IN 
        |                       (SELECT PersonRoleEntity.prRoleEnum
        |                          FROM PersonRoleEntity
        |                         WHERE PersonRoleEntity.prPersonGuidHash = ?)
        |                   AND PersonRelatedPersonEntity.prpPersonUidNum = ?)     
        |        ,  
        |               
        |            AuthenticatedPersonClassPermissions AS (
        |                SELECT ClassPermissionEntity.*
        |                  FROM ClassPermissionEntity
        |                 WHERE (ClassPermissionEntity.cpeToEnrollmentRole, ClassPermissionEntity.cpeClassUidNum) IN 
        |                       (SELECT EnrollmentEntity.eRole, EnrollmentEntity.eClassUidNum
        |                          FROM EnrollmentEntity
        |                         WHERE EnrollmentEntity.ePersonUidNum IN 
        |                               (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        )
        |                           AND EnrollmentEntity.eStatus = 1)
        |            )
        |        ,
        |               
        |            Persons(uidNum) AS (
        |                   SELECT PersonEntity.pGuidHash 
        |                     FROM PersonEntity
        |                    WHERE PersonEntity.pStored > ? 
        |                      AND (? = 0 OR PersonEntity.pGuidHash = ?)
        |                      AND (? = 0 OR
        |                           EXISTS(
        |                                SELECT EnrollmentEntity.eUid
        |                                  FROM EnrollmentEntity
        |                                 WHERE EnrollmentEntity.ePersonUidNum = PersonEntity.pGuidHash
        |                                   AND EnrollmentEntity.eClassUidNum = ?
        |                                   AND (? = 0 OR EnrollmentEntity.eRole = ?)
        |                                   AND ((? OR ? = 0) 
        |                                        OR (     (? >= COALESCE(EnrollmentEntity.eBeginDate, 0))
        |                                            AND ((? - 86399999) < COALESCE(EnrollmentEntity.eEndDate, 9223372036854775807))
        |                                            AND (? <= COALESCE(EnrollmentEntity.eRemovedAt, 9223372036854775807))
        |                                            AND EnrollmentEntity.eStatus = 1 ))         
        |                           ) 
        |                          ) 
        |                          -- Begin permission NOTE check should add permissions granted to children for parent
        |                      AND (
        |                PersonEntity.pGuidHash = ?
        |             OR PersonEntity.pGuidHash IN 
        |                (SELECT PersonRelatedPersonEntity.prpOtherPersonUidNum
        |                   FROM PersonRelatedPersonEntity
        |                  WHERE PersonRelatedPersonEntity.prpPersonUidNum = ?)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM SchoolPermissionGrantEntity
        |                     WHERE SchoolPermissionGrantEntity.spgToRole IN 
        |                           (SELECT PersonRoleEntity.prRoleEnum
        |                              FROM PersonRoleEntity
        |                             WHERE PersonRoleEntity.prPersonGuidHash IN 
        |                                   (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        ))
        |                               AND (SchoolPermissionGrantEntity.spgPermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM AuthenticatedPersonClassPermissions
        |                     WHERE AuthenticatedPersonClassPermissions.cpeClassUidNum IN 
        |                           (SELECT EnrollmentEntity.eClassUidNum
        |                              FROM EnrollmentEntity
        |                             WHERE EnrollmentEntity.ePersonUidNum = PersonEntity.pGuidHash)
        |                       AND (AuthenticatedPersonClassPermissions.cpePermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |        )
        |                      AND (? IS NULL 
        |                           OR (PersonEntity.pGivenName || ' ' || PersonEntity.pFamilyName) LIKE ('%' || ? || '%'))
        |                      AND (? = 0 OR ? IN 
        |                           (SELECT PersonRoleEntity.prRoleEnum
        |                              FROM PersonRoleEntity
        |                             WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash))
        |                      AND (? = 0 OR PersonEntity.pStatus = ?)
        |                      AND (? OR PersonEntity.pStatus != 0)       
        |            ),
        |                
        |            RelatedPersons(uidNum) AS (
        |                SELECT PersonEntity.pGuidHash
        |                  FROM PersonEntity
        |                 WHERE ? 
        |                   AND PersonEntity.pGuidHash IN(
        |                        SELECT DISTINCT PersonRelatedPersonEntity.prpOtherPersonUidNum
        |                          FROM PersonRelatedPersonEntity
        |                         WHERE PersonRelatedPersonEntity.prpPersonUidNum IN(
        |                               SELECT Persons.uidNum
        |                                 FROM Persons)
        |                       )
        |                   AND (
        |                PersonEntity.pGuidHash = ?
        |             OR PersonEntity.pGuidHash IN 
        |                (SELECT PersonRelatedPersonEntity.prpOtherPersonUidNum
        |                   FROM PersonRelatedPersonEntity
        |                  WHERE PersonRelatedPersonEntity.prpPersonUidNum = ?)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM SchoolPermissionGrantEntity
        |                     WHERE SchoolPermissionGrantEntity.spgToRole IN 
        |                           (SELECT PersonRoleEntity.prRoleEnum
        |                              FROM PersonRoleEntity
        |                             WHERE PersonRoleEntity.prPersonGuidHash IN 
        |                                   (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        ))
        |                               AND (SchoolPermissionGrantEntity.spgPermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |             OR EXISTS(
        |                    SELECT 1
        |                      FROM AuthenticatedPersonClassPermissions
        |                     WHERE AuthenticatedPersonClassPermissions.cpeClassUidNum IN 
        |                           (SELECT EnrollmentEntity.eClassUidNum
        |                              FROM EnrollmentEntity
        |                             WHERE EnrollmentEntity.ePersonUidNum = PersonEntity.pGuidHash)
        |                       AND (AuthenticatedPersonClassPermissions.cpePermissions & (
        |            CASE(SELECT PersonRoleEntity.prRoleEnum
        |                   FROM PersonRoleEntity
        |                  WHERE PersonRoleEntity.prPersonGuidHash = PersonEntity.pGuidHash
        |                  LIMIT 1)                    
        |                 
        |             WHEN 1 THEN 256
        |             WHEN 4 THEN 256
        |             WHEN 8 THEN 64
        |             WHEN 2 THEN 4
        |             WHEN 16 THEN 16
        |             ELSE 9223372036854775807
        |        
        |            END     
        |        )) > 0)
        |        )    
        |            ),
        |                
        |            AllPersons(uidNum) AS (
        |                SELECT uidNum 
        |                  FROM Persons
        |                 UNION
        |                 SELECT uidNum 
        |                   FROM RelatedPersons
        |            )
        |        
        |        
        |        SELECT PersonEntity.pGuid AS guid, 
        |               PersonEntity.pGivenName AS givenName, 
        |               PersonEntity.pFamilyName AS familyName, 
        |               PersonEntity.pUsername AS username
        |          FROM PersonEntity
        |         WHERE PersonEntity.pGuidHash IN (
        |                SELECT DISTINCT uidNum
        |                  FROM AllPersons)
        |      ORDER BY PersonEntity.pGivenName
        |    
        """.trimMargin()
    val _rawQuery: RoomRawQuery = RoomRawQuery(_sql) { _stmt ->
      var _argIndex: Int = 1
      _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
      _argIndex = 2
      _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
      _argIndex = 3
      _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
      _argIndex = 4
      _stmt.bindLong(_argIndex, since)
      _argIndex = 5
      _stmt.bindLong(_argIndex, guidHash)
      _argIndex = 6
      _stmt.bindLong(_argIndex, guidHash)
      _argIndex = 7
      _stmt.bindLong(_argIndex, inClazzGuidHash)
      _argIndex = 8
      _stmt.bindLong(_argIndex, inClazzGuidHash)
      _argIndex = 9
      _stmt.bindLong(_argIndex, inClazzRoleFlag.toLong())
      _argIndex = 10
      _stmt.bindLong(_argIndex, inClazzRoleFlag.toLong())
      _argIndex = 11
      val _tmp: Int = if (includeDeleted) 1 else 0
      _stmt.bindLong(_argIndex, _tmp.toLong())
      _argIndex = 12
      _stmt.bindLong(_argIndex, inClassOnDayInUtcMs)
      _argIndex = 13
      _stmt.bindLong(_argIndex, inClassOnDayInUtcMs)
      _argIndex = 14
      _stmt.bindLong(_argIndex, inClassOnDayInUtcMs)
      _argIndex = 15
      _stmt.bindLong(_argIndex, timeNow)
      _argIndex = 16
      _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
      _argIndex = 17
      _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
      _argIndex = 18
      if (filterByName == null) {
        _stmt.bindNull(_argIndex)
      } else {
        _stmt.bindText(_argIndex, filterByName)
      }
      _argIndex = 19
      if (filterByName == null) {
        _stmt.bindNull(_argIndex)
      } else {
        _stmt.bindText(_argIndex, filterByName)
      }
      _argIndex = 20
      _stmt.bindLong(_argIndex, filterByPersonRole.toLong())
      _argIndex = 21
      _stmt.bindLong(_argIndex, filterByPersonRole.toLong())
      _argIndex = 22
      _stmt.bindLong(_argIndex, filterByPersonStatus.toLong())
      _argIndex = 23
      _stmt.bindLong(_argIndex, filterByPersonStatus.toLong())
      _argIndex = 24
      val _tmp_1: Int = if (includeDeleted) 1 else 0
      _stmt.bindLong(_argIndex, _tmp_1.toLong())
      _argIndex = 25
      val _tmp_2: Int = if (includeRelated) 1 else 0
      _stmt.bindLong(_argIndex, _tmp_2.toLong())
      _argIndex = 26
      _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
      _argIndex = 27
      _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
    }
    return object : LimitOffsetPagingSource<PersonListDetails>(_rawQuery, __db, "PersonRelatedPersonEntity", "PersonRoleEntity", "ClassPermissionEntity", "EnrollmentEntity", "PersonEntity", "SchoolPermissionGrantEntity") {
      protected override suspend fun convertRows(limitOffsetQuery: RoomRawQuery, itemCount: Int): List<PersonListDetails> = performSuspending(__db, true, false) { _connection ->
        val _stmt: SQLiteStatement = _connection.prepare(limitOffsetQuery.sql)
        limitOffsetQuery.getBindingFunction().invoke(_stmt)
        try {
          val _columnIndexOfGuid: Int = getColumnIndexOrThrow(_stmt, "guid")
          val _columnIndexOfGivenName: Int = getColumnIndexOrThrow(_stmt, "givenName")
          val _columnIndexOfFamilyName: Int = getColumnIndexOrThrow(_stmt, "familyName")
          val _columnIndexOfUsername: Int = getColumnIndexOrThrow(_stmt, "username")
          val _result: MutableList<PersonListDetails> = mutableListOf()
          while (_stmt.step()) {
            val _item: PersonListDetails
            val _tmpGuid: String
            _tmpGuid = _stmt.getText(_columnIndexOfGuid)
            val _tmpGivenName: String
            _tmpGivenName = _stmt.getText(_columnIndexOfGivenName)
            val _tmpFamilyName: String
            _tmpFamilyName = _stmt.getText(_columnIndexOfFamilyName)
            val _tmpUsername: String?
            if (_stmt.isNull(_columnIndexOfUsername)) {
              _tmpUsername = null
            } else {
              _tmpUsername = _stmt.getText(_columnIndexOfUsername)
            }
            _item = PersonListDetails(_tmpGuid,_tmpGivenName,_tmpFamilyName,_tmpUsername,null,null)
            _result.add(_item)
          }
          _result
        } finally {
          _stmt.close()
        }
      }
    }
  }

  public override suspend fun getAllUsers(sourcedId: String): List<PersonEntity> {
    val _sql: String = """
        |
        |            SELECT * 
        |            FROM PersonEntity
        |            WHERE pGuid = ?
        |            
        """.trimMargin()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindText(_argIndex, sourcedId)
        val _columnIndexOfPGuid: Int = getColumnIndexOrThrow(_stmt, "pGuid")
        val _columnIndexOfPGuidHash: Int = getColumnIndexOrThrow(_stmt, "pGuidHash")
        val _columnIndexOfPActive: Int = getColumnIndexOrThrow(_stmt, "pActive")
        val _columnIndexOfPStatus: Int = getColumnIndexOrThrow(_stmt, "pStatus")
        val _columnIndexOfPLastModified: Int = getColumnIndexOrThrow(_stmt, "pLastModified")
        val _columnIndexOfPStored: Int = getColumnIndexOrThrow(_stmt, "pStored")
        val _columnIndexOfPMetadata: Int = getColumnIndexOrThrow(_stmt, "pMetadata")
        val _columnIndexOfPUsername: Int = getColumnIndexOrThrow(_stmt, "pUsername")
        val _columnIndexOfPGivenName: Int = getColumnIndexOrThrow(_stmt, "pGivenName")
        val _columnIndexOfPFamilyName: Int = getColumnIndexOrThrow(_stmt, "pFamilyName")
        val _columnIndexOfPMiddleName: Int = getColumnIndexOrThrow(_stmt, "pMiddleName")
        val _columnIndexOfPGender: Int = getColumnIndexOrThrow(_stmt, "pGender")
        val _columnIndexOfPDateOfBirth: Int = getColumnIndexOrThrow(_stmt, "pDateOfBirth")
        val _columnIndexOfPEmail: Int = getColumnIndexOrThrow(_stmt, "pEmail")
        val _columnIndexOfPPhoneNumber: Int = getColumnIndexOrThrow(_stmt, "pPhoneNumber")
        val _result: MutableList<PersonEntity> = mutableListOf()
        while (_stmt.step()) {
          val _item: PersonEntity
          val _tmpPGuid: String
          _tmpPGuid = _stmt.getText(_columnIndexOfPGuid)
          val _tmpPGuidHash: Long
          _tmpPGuidHash = _stmt.getLong(_columnIndexOfPGuidHash)
          val _tmpPActive: Boolean
          val _tmp: Int
          _tmp = _stmt.getLong(_columnIndexOfPActive).toInt()
          _tmpPActive = _tmp != 0
          val _tmpPStatus: PersonStatusEnum
          val _tmp_1: Int
          _tmp_1 = _stmt.getLong(_columnIndexOfPStatus).toInt()
          _tmpPStatus = __schoolTypeConverters.toPersonStatusEnum(_tmp_1)
          val _tmpPLastModified: Long
          _tmpPLastModified = _stmt.getLong(_columnIndexOfPLastModified)
          val _tmpPStored: Long
          _tmpPStored = _stmt.getLong(_columnIndexOfPStored)
          val _tmpPMetadata: JsonObject?
          val _tmp_2: String?
          if (_stmt.isNull(_columnIndexOfPMetadata)) {
            _tmp_2 = null
          } else {
            _tmp_2 = _stmt.getText(_columnIndexOfPMetadata)
          }
          _tmpPMetadata = __schoolTypeConverters.toJsonObject(_tmp_2)
          val _tmpPUsername: String?
          if (_stmt.isNull(_columnIndexOfPUsername)) {
            _tmpPUsername = null
          } else {
            _tmpPUsername = _stmt.getText(_columnIndexOfPUsername)
          }
          val _tmpPGivenName: String
          _tmpPGivenName = _stmt.getText(_columnIndexOfPGivenName)
          val _tmpPFamilyName: String
          _tmpPFamilyName = _stmt.getText(_columnIndexOfPFamilyName)
          val _tmpPMiddleName: String?
          if (_stmt.isNull(_columnIndexOfPMiddleName)) {
            _tmpPMiddleName = null
          } else {
            _tmpPMiddleName = _stmt.getText(_columnIndexOfPMiddleName)
          }
          val _tmpPGender: PersonGenderEnum
          val _tmp_3: Int
          _tmp_3 = _stmt.getLong(_columnIndexOfPGender).toInt()
          _tmpPGender = __schoolTypeConverters.toPersonGenderEnum(_tmp_3)
          val _tmpPDateOfBirth: LocalDate?
          val _tmp_4: Long?
          if (_stmt.isNull(_columnIndexOfPDateOfBirth)) {
            _tmp_4 = null
          } else {
            _tmp_4 = _stmt.getLong(_columnIndexOfPDateOfBirth)
          }
          _tmpPDateOfBirth = __schoolTypeConverters.toLocalDate(_tmp_4)
          val _tmpPEmail: String?
          if (_stmt.isNull(_columnIndexOfPEmail)) {
            _tmpPEmail = null
          } else {
            _tmpPEmail = _stmt.getText(_columnIndexOfPEmail)
          }
          val _tmpPPhoneNumber: String?
          if (_stmt.isNull(_columnIndexOfPPhoneNumber)) {
            _tmpPPhoneNumber = null
          } else {
            _tmpPPhoneNumber = _stmt.getText(_columnIndexOfPPhoneNumber)
          }
          _item = PersonEntity(_tmpPGuid,_tmpPGuidHash,_tmpPActive,_tmpPStatus,_tmpPLastModified,_tmpPStored,_tmpPMetadata,_tmpPUsername,_tmpPGivenName,_tmpPFamilyName,_tmpPMiddleName,_tmpPGender,_tmpPDateOfBirth,_tmpPEmail,_tmpPPhoneNumber)
          _result.add(_item)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun getMostRecentPermissionChangeTime(authenticatedPersonUidNum: Long): Long {
    val _sql: String = """
        |
        |        WITH 
        |            AuthenticatedPermissionPersonUids(uidNum) AS (
        |                SELECT ? AS uidNum
        |                UNION
        |                SELECT PersonRelatedPersonEntity.prpOtherPersonUidNum AS uidNum
        |                  FROM PersonRelatedPersonEntity
        |                 WHERE 16 IN 
        |                       (SELECT PersonRoleEntity.prRoleEnum
        |                          FROM PersonRoleEntity
        |                         WHERE PersonRoleEntity.prPersonGuidHash = ?)
        |                   AND PersonRelatedPersonEntity.prpPersonUidNum = ?)     
        |         
        |      SELECT MAX(
        |               -- Most recent change to schoolpermissiongrant for the given person
        |               COALESCE(
        |                   (SELECT MAX(SchoolPermissionGrantEntity.spgStored)
        |                      FROM SchoolPermissionGrantEntity
        |                     WHERE SchoolPermissionGrantEntity.spgToRole IN
        |                           (SELECT PersonRoleEntity.prRoleEnum
        |                              FROM PersonRoleEntity
        |                             WHERE PersonRoleEntity.prPersonGuidHash IN 
        |                                   (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        ))), 
        |                   0
        |               ),
        |               -- Most recent change to enrollment that will also affect permissions for person
        |               COALESCE(
        |                   (SELECT MAX(EnrollmentEntity.eStored)
        |                      FROM EnrollmentEntity
        |                     WHERE EnrollmentEntity.ePersonUidNum IN 
        |                           (
        |            SELECT AuthenticatedPermissionPersonUids.uidNum
        |              FROM AuthenticatedPermissionPersonUids
        |        )),
        |                   0
        |               )
        |             )
        |    
        """.trimMargin()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 2
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        _argIndex = 3
        _stmt.bindLong(_argIndex, authenticatedPersonUidNum)
        val _result: Long
        if (_stmt.step()) {
          val _tmp: Long
          _tmp = _stmt.getLong(0)
          _result = _tmp
        } else {
          _result = 0L
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  private fun __fetchRelationshipPersonRoleEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRoleEntity(_connection: SQLiteConnection, _map: LongSparseArray<MutableList<PersonRoleEntity>>) {
    if (_map.isEmpty()) {
      return
    }
    if (_map.size() > 999) {
      recursiveFetchLongSparseArray(_map, true) { _tmpMap ->
        __fetchRelationshipPersonRoleEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRoleEntity(_connection, _tmpMap)
      }
      return
    }
    val _stringBuilder: StringBuilder = StringBuilder()
    _stringBuilder.append("SELECT `prUid`,`prPersonGuidHash`,`prIsPrimaryRole`,`prRoleEnum`,`prBeginDate`,`prEndDate` FROM `PersonRoleEntity` WHERE `prPersonGuidHash` IN (")
    val _inputSize: Int = _map.size()
    appendPlaceholders(_stringBuilder, _inputSize)
    _stringBuilder.append(")")
    val _sql: String = _stringBuilder.toString()
    val _stmt: SQLiteStatement = _connection.prepare(_sql)
    var _argIndex: Int = 1
    for (i in 0 until _map.size()) {
      val _item: Long = _map.keyAt(i)
      _stmt.bindLong(_argIndex, _item)
      _argIndex++
    }
    try {
      val _itemKeyIndex: Int = getColumnIndex(_stmt, "prPersonGuidHash")
      if (_itemKeyIndex == -1) {
        return
      }
      val _columnIndexOfPrUid: Int = 0
      val _columnIndexOfPrPersonGuidHash: Int = 1
      val _columnIndexOfPrIsPrimaryRole: Int = 2
      val _columnIndexOfPrRoleEnum: Int = 3
      val _columnIndexOfPrBeginDate: Int = 4
      val _columnIndexOfPrEndDate: Int = 5
      while (_stmt.step()) {
        val _tmpKey: Long
        _tmpKey = _stmt.getLong(_itemKeyIndex)
        val _tmpRelation: MutableList<PersonRoleEntity>? = _map.get(_tmpKey)
        if (_tmpRelation != null) {
          val _item_1: PersonRoleEntity
          val _tmpPrUid: Int
          _tmpPrUid = _stmt.getLong(_columnIndexOfPrUid).toInt()
          val _tmpPrPersonGuidHash: Long
          _tmpPrPersonGuidHash = _stmt.getLong(_columnIndexOfPrPersonGuidHash)
          val _tmpPrIsPrimaryRole: Boolean
          val _tmp: Int
          _tmp = _stmt.getLong(_columnIndexOfPrIsPrimaryRole).toInt()
          _tmpPrIsPrimaryRole = _tmp != 0
          val _tmpPrRoleEnum: PersonRoleEnum
          val _tmp_1: Int
          _tmp_1 = _stmt.getLong(_columnIndexOfPrRoleEnum).toInt()
          _tmpPrRoleEnum = __schoolTypeConverters.toPersonRoleType(_tmp_1)
          val _tmpPrBeginDate: LocalDate?
          val _tmp_2: Long?
          if (_stmt.isNull(_columnIndexOfPrBeginDate)) {
            _tmp_2 = null
          } else {
            _tmp_2 = _stmt.getLong(_columnIndexOfPrBeginDate)
          }
          _tmpPrBeginDate = __schoolTypeConverters.toLocalDate(_tmp_2)
          val _tmpPrEndDate: LocalDate?
          val _tmp_3: Long?
          if (_stmt.isNull(_columnIndexOfPrEndDate)) {
            _tmp_3 = null
          } else {
            _tmp_3 = _stmt.getLong(_columnIndexOfPrEndDate)
          }
          _tmpPrEndDate = __schoolTypeConverters.toLocalDate(_tmp_3)
          _item_1 = PersonRoleEntity(_tmpPrUid,_tmpPrPersonGuidHash,_tmpPrIsPrimaryRole,_tmpPrRoleEnum,_tmpPrBeginDate,_tmpPrEndDate)
          _tmpRelation.add(_item_1)
        }
      }
    } finally {
      _stmt.close()
    }
  }

  private fun __fetchRelationshipPersonRelatedPersonEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRelatedPersonEntity(_connection: SQLiteConnection, _map: LongSparseArray<MutableList<PersonRelatedPersonEntity>>) {
    if (_map.isEmpty()) {
      return
    }
    if (_map.size() > 999) {
      recursiveFetchLongSparseArray(_map, true) { _tmpMap ->
        __fetchRelationshipPersonRelatedPersonEntityAsworldRespectDatalayerDbSchoolEntitiesPersonRelatedPersonEntity(_connection, _tmpMap)
      }
      return
    }
    val _stringBuilder: StringBuilder = StringBuilder()
    _stringBuilder.append("SELECT `prpUid`,`prpPersonUidNum`,`prpOtherPersonUid`,`prpOtherPersonUidNum` FROM `PersonRelatedPersonEntity` WHERE `prpPersonUidNum` IN (")
    val _inputSize: Int = _map.size()
    appendPlaceholders(_stringBuilder, _inputSize)
    _stringBuilder.append(")")
    val _sql: String = _stringBuilder.toString()
    val _stmt: SQLiteStatement = _connection.prepare(_sql)
    var _argIndex: Int = 1
    for (i in 0 until _map.size()) {
      val _item: Long = _map.keyAt(i)
      _stmt.bindLong(_argIndex, _item)
      _argIndex++
    }
    try {
      val _itemKeyIndex: Int = getColumnIndex(_stmt, "prpPersonUidNum")
      if (_itemKeyIndex == -1) {
        return
      }
      val _columnIndexOfPrpUid: Int = 0
      val _columnIndexOfPrpPersonUidNum: Int = 1
      val _columnIndexOfPrpOtherPersonUid: Int = 2
      val _columnIndexOfPrpOtherPersonUidNum: Int = 3
      while (_stmt.step()) {
        val _tmpKey: Long
        _tmpKey = _stmt.getLong(_itemKeyIndex)
        val _tmpRelation: MutableList<PersonRelatedPersonEntity>? = _map.get(_tmpKey)
        if (_tmpRelation != null) {
          val _item_1: PersonRelatedPersonEntity
          val _tmpPrpUid: Int
          _tmpPrpUid = _stmt.getLong(_columnIndexOfPrpUid).toInt()
          val _tmpPrpPersonUidNum: Long
          _tmpPrpPersonUidNum = _stmt.getLong(_columnIndexOfPrpPersonUidNum)
          val _tmpPrpOtherPersonUid: String
          _tmpPrpOtherPersonUid = _stmt.getText(_columnIndexOfPrpOtherPersonUid)
          val _tmpPrpOtherPersonUidNum: Long
          _tmpPrpOtherPersonUidNum = _stmt.getLong(_columnIndexOfPrpOtherPersonUidNum)
          _item_1 = PersonRelatedPersonEntity(_tmpPrpUid,_tmpPrpPersonUidNum,_tmpPrpOtherPersonUid,_tmpPrpOtherPersonUidNum)
          _tmpRelation.add(_item_1)
        }
      }
    } finally {
      _stmt.close()
    }
  }

  public companion object {
    public fun getRequiredConverters(): List<KClass<*>> = emptyList()
  }
}
