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.performInTransactionSuspending
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.IllegalArgumentException
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 kotlin.time.Instant
import kotlinx.coroutines.flow.Flow
import world.respect.datalayer.db.school.entities.ChangeHistoryChangeEntity
import world.respect.datalayer.db.school.entities.ChangeHistoryEntity
import world.respect.datalayer.db.school.entities.ChangeHistoryWithChanges
import world.respect.datalayer.db.shared.SharedConverters
import world.respect.datalayer.school.model.ChangeHistoryFieldEnum
import world.respect.datalayer.school.model.ChangeHistoryTableEnum

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

  private val __insertAdapterOfChangeHistoryEntity: EntityInsertAdapter<ChangeHistoryEntity>

  private val __sharedConverters: SharedConverters = SharedConverters()

  private val __insertAdapterOfChangeHistoryChangeEntity:
      EntityInsertAdapter<ChangeHistoryChangeEntity>
  init {
    this.__db = __db
    this.__insertAdapterOfChangeHistoryEntity = object : EntityInsertAdapter<ChangeHistoryEntity>() {
      protected override fun createQuery(): String = "INSERT OR REPLACE INTO `ChangeHistoryEntity` (`hGuid`,`hGuidHash`,`hTable`,`hTableGuid`,`hWhoGuid`,`hWhoGuidHash`,`hLastModified`,`hStored`) VALUES (?,?,?,?,?,?,?,?)"

      protected override fun bind(statement: SQLiteStatement, entity: ChangeHistoryEntity) {
        statement.bindText(1, entity.hGuid)
        statement.bindLong(2, entity.hGuidHash)
        statement.bindText(3, __ChangeHistoryTableEnum_enumToString(entity.hTable))
        statement.bindText(4, entity.hTableGuid)
        statement.bindText(5, entity.hWhoGuid)
        statement.bindLong(6, entity.hWhoGuidHash)
        val _tmp: Long? = __sharedConverters.fromInstant(entity.hLastModified)
        if (_tmp == null) {
          statement.bindNull(7)
        } else {
          statement.bindLong(7, _tmp)
        }
        val _tmp_1: Long? = __sharedConverters.fromInstant(entity.hStored)
        if (_tmp_1 == null) {
          statement.bindNull(8)
        } else {
          statement.bindLong(8, _tmp_1)
        }
      }
    }
    this.__insertAdapterOfChangeHistoryChangeEntity = object : EntityInsertAdapter<ChangeHistoryChangeEntity>() {
      protected override fun createQuery(): String = "INSERT OR REPLACE INTO `ChangeHistoryChangeEntity` (`hcId`,`hcHistoryGuidHash`,`hcField`,`hcOldVal`,`hcNewVal`,`hcSynced`,`hcLastModified`,`hcStored`) VALUES (nullif(?, 0),?,?,?,?,?,?,?)"

      protected override fun bind(statement: SQLiteStatement, entity: ChangeHistoryChangeEntity) {
        statement.bindLong(1, entity.hcId)
        statement.bindLong(2, entity.hcHistoryGuidHash)
        statement.bindText(3, __ChangeHistoryFieldEnum_enumToString(entity.hcField))
        val _tmpHcOldVal: String? = entity.hcOldVal
        if (_tmpHcOldVal == null) {
          statement.bindNull(4)
        } else {
          statement.bindText(4, _tmpHcOldVal)
        }
        statement.bindText(5, entity.hcNewVal)
        val _tmp: Int = if (entity.hcSynced) 1 else 0
        statement.bindLong(6, _tmp.toLong())
        val _tmp_1: Long? = __sharedConverters.fromInstant(entity.hcLastModified)
        if (_tmp_1 == null) {
          statement.bindNull(7)
        } else {
          statement.bindLong(7, _tmp_1)
        }
        val _tmp_2: Long? = __sharedConverters.fromInstant(entity.hcStored)
        if (_tmp_2 == null) {
          statement.bindNull(8)
        } else {
          statement.bindLong(8, _tmp_2)
        }
      }
    }
  }

  public override suspend fun insertHistory(entity: ChangeHistoryEntity): Unit = performSuspending(__db, false, true) { _connection ->
    __insertAdapterOfChangeHistoryEntity.insert(_connection, entity)
  }

  public override suspend fun insertChanges(entities: List<ChangeHistoryChangeEntity>): Unit = performSuspending(__db, false, true) { _connection ->
    __insertAdapterOfChangeHistoryChangeEntity.insert(_connection, entities)
  }

  public override suspend fun insertHistoryWithChanges(history: ChangeHistoryEntity, changes: List<ChangeHistoryChangeEntity>): Unit = performInTransactionSuspending(__db) {
    super@ChangeHistoryDao_Impl.insertHistoryWithChanges(history, changes)
  }

  public override suspend fun getLastModifiedByUidNum(uidNum: Long): Long? {
    val _sql: String = """
        |
        |        SELECT ChangeHistoryEntity.hLastModified
        |          FROM ChangeHistoryEntity
        |         WHERE ChangeHistoryEntity.hGuidHash = ?
        |    
        """.trimMargin()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, uidNum)
        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 getByTableAndUids(tableEnum: ChangeHistoryTableEnum, uids: List<String>): List<ChangeHistoryWithChanges> {
    val _stringBuilder: StringBuilder = StringBuilder()
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("         SELECT * ")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("           FROM ChangeHistoryEntity")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("          WHERE hTable = ")
    _stringBuilder.append("?")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("           AND hTableGuid IN (")
    val _inputSize: Int = uids.size
    appendPlaceholders(_stringBuilder, _inputSize)
    _stringBuilder.append(")")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("    ")
    val _sql: String = _stringBuilder.toString()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindText(_argIndex, __ChangeHistoryTableEnum_enumToString(tableEnum))
        _argIndex = 2
        for (_item: String in uids) {
          _stmt.bindText(_argIndex, _item)
          _argIndex++
        }
        val _columnIndexOfHGuid: Int = getColumnIndexOrThrow(_stmt, "hGuid")
        val _columnIndexOfHGuidHash: Int = getColumnIndexOrThrow(_stmt, "hGuidHash")
        val _columnIndexOfHTable: Int = getColumnIndexOrThrow(_stmt, "hTable")
        val _columnIndexOfHTableGuid: Int = getColumnIndexOrThrow(_stmt, "hTableGuid")
        val _columnIndexOfHWhoGuid: Int = getColumnIndexOrThrow(_stmt, "hWhoGuid")
        val _columnIndexOfHWhoGuidHash: Int = getColumnIndexOrThrow(_stmt, "hWhoGuidHash")
        val _columnIndexOfHLastModified: Int = getColumnIndexOrThrow(_stmt, "hLastModified")
        val _columnIndexOfHStored: Int = getColumnIndexOrThrow(_stmt, "hStored")
        val _collectionChanges: LongSparseArray<MutableList<ChangeHistoryChangeEntity>> = LongSparseArray<MutableList<ChangeHistoryChangeEntity>>()
        while (_stmt.step()) {
          val _tmpKey: Long
          _tmpKey = _stmt.getLong(_columnIndexOfHGuidHash)
          if (!_collectionChanges.containsKey(_tmpKey)) {
            _collectionChanges.put(_tmpKey, mutableListOf())
          }
        }
        _stmt.reset()
        __fetchRelationshipChangeHistoryChangeEntityAsworldRespectDatalayerDbSchoolEntitiesChangeHistoryChangeEntity(_connection, _collectionChanges)
        val _result: MutableList<ChangeHistoryWithChanges> = mutableListOf()
        while (_stmt.step()) {
          val _item_1: ChangeHistoryWithChanges
          val _tmpHistory: ChangeHistoryEntity
          val _tmpHGuid: String
          _tmpHGuid = _stmt.getText(_columnIndexOfHGuid)
          val _tmpHGuidHash: Long
          _tmpHGuidHash = _stmt.getLong(_columnIndexOfHGuidHash)
          val _tmpHTable: ChangeHistoryTableEnum
          _tmpHTable = __ChangeHistoryTableEnum_stringToEnum(_stmt.getText(_columnIndexOfHTable))
          val _tmpHTableGuid: String
          _tmpHTableGuid = _stmt.getText(_columnIndexOfHTableGuid)
          val _tmpHWhoGuid: String
          _tmpHWhoGuid = _stmt.getText(_columnIndexOfHWhoGuid)
          val _tmpHWhoGuidHash: Long
          _tmpHWhoGuidHash = _stmt.getLong(_columnIndexOfHWhoGuidHash)
          val _tmpHLastModified: Instant
          val _tmp: Long?
          if (_stmt.isNull(_columnIndexOfHLastModified)) {
            _tmp = null
          } else {
            _tmp = _stmt.getLong(_columnIndexOfHLastModified)
          }
          val _tmp_1: Instant? = __sharedConverters.toInstant(_tmp)
          if (_tmp_1 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpHLastModified = _tmp_1
          }
          val _tmpHStored: Instant
          val _tmp_2: Long?
          if (_stmt.isNull(_columnIndexOfHStored)) {
            _tmp_2 = null
          } else {
            _tmp_2 = _stmt.getLong(_columnIndexOfHStored)
          }
          val _tmp_3: Instant? = __sharedConverters.toInstant(_tmp_2)
          if (_tmp_3 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpHStored = _tmp_3
          }
          _tmpHistory = ChangeHistoryEntity(_tmpHGuid,_tmpHGuidHash,_tmpHTable,_tmpHTableGuid,_tmpHWhoGuid,_tmpHWhoGuidHash,_tmpHLastModified,_tmpHStored)
          val _tmpChangesCollection: MutableList<ChangeHistoryChangeEntity>
          val _tmpKey_1: Long
          _tmpKey_1 = _stmt.getLong(_columnIndexOfHGuidHash)
          _tmpChangesCollection = checkNotNull(_collectionChanges.get(_tmpKey_1))
          _item_1 = ChangeHistoryWithChanges(_tmpHistory,_tmpChangesCollection)
          _result.add(_item_1)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun getParentsWithUnsyncedChanges(tableEnum: ChangeHistoryTableEnum, uids: List<String>): List<ChangeHistoryEntity> {
    val _stringBuilder: StringBuilder = StringBuilder()
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("         SELECT *")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("           FROM ChangeHistoryEntity")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("          WHERE hTable = ")
    _stringBuilder.append("?")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("           AND hTableGuid IN (")
    val _inputSize: Int = uids.size
    appendPlaceholders(_stringBuilder, _inputSize)
    _stringBuilder.append(")")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("           AND hGuidHash IN (")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("              SELECT hcHistoryGuidHash")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("                FROM ChangeHistoryChangeEntity")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("               WHERE hcSynced = 0")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("             )")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("      ")
    val _sql: String = _stringBuilder.toString()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindText(_argIndex, __ChangeHistoryTableEnum_enumToString(tableEnum))
        _argIndex = 2
        for (_item: String in uids) {
          _stmt.bindText(_argIndex, _item)
          _argIndex++
        }
        val _columnIndexOfHGuid: Int = getColumnIndexOrThrow(_stmt, "hGuid")
        val _columnIndexOfHGuidHash: Int = getColumnIndexOrThrow(_stmt, "hGuidHash")
        val _columnIndexOfHTable: Int = getColumnIndexOrThrow(_stmt, "hTable")
        val _columnIndexOfHTableGuid: Int = getColumnIndexOrThrow(_stmt, "hTableGuid")
        val _columnIndexOfHWhoGuid: Int = getColumnIndexOrThrow(_stmt, "hWhoGuid")
        val _columnIndexOfHWhoGuidHash: Int = getColumnIndexOrThrow(_stmt, "hWhoGuidHash")
        val _columnIndexOfHLastModified: Int = getColumnIndexOrThrow(_stmt, "hLastModified")
        val _columnIndexOfHStored: Int = getColumnIndexOrThrow(_stmt, "hStored")
        val _result: MutableList<ChangeHistoryEntity> = mutableListOf()
        while (_stmt.step()) {
          val _item_1: ChangeHistoryEntity
          val _tmpHGuid: String
          _tmpHGuid = _stmt.getText(_columnIndexOfHGuid)
          val _tmpHGuidHash: Long
          _tmpHGuidHash = _stmt.getLong(_columnIndexOfHGuidHash)
          val _tmpHTable: ChangeHistoryTableEnum
          _tmpHTable = __ChangeHistoryTableEnum_stringToEnum(_stmt.getText(_columnIndexOfHTable))
          val _tmpHTableGuid: String
          _tmpHTableGuid = _stmt.getText(_columnIndexOfHTableGuid)
          val _tmpHWhoGuid: String
          _tmpHWhoGuid = _stmt.getText(_columnIndexOfHWhoGuid)
          val _tmpHWhoGuidHash: Long
          _tmpHWhoGuidHash = _stmt.getLong(_columnIndexOfHWhoGuidHash)
          val _tmpHLastModified: Instant
          val _tmp: Long?
          if (_stmt.isNull(_columnIndexOfHLastModified)) {
            _tmp = null
          } else {
            _tmp = _stmt.getLong(_columnIndexOfHLastModified)
          }
          val _tmp_1: Instant? = __sharedConverters.toInstant(_tmp)
          if (_tmp_1 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpHLastModified = _tmp_1
          }
          val _tmpHStored: Instant
          val _tmp_2: Long?
          if (_stmt.isNull(_columnIndexOfHStored)) {
            _tmp_2 = null
          } else {
            _tmp_2 = _stmt.getLong(_columnIndexOfHStored)
          }
          val _tmp_3: Instant? = __sharedConverters.toInstant(_tmp_2)
          if (_tmp_3 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpHStored = _tmp_3
          }
          _item_1 = ChangeHistoryEntity(_tmpHGuid,_tmpHGuidHash,_tmpHTable,_tmpHTableGuid,_tmpHWhoGuid,_tmpHWhoGuidHash,_tmpHLastModified,_tmpHStored)
          _result.add(_item_1)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override fun listAsPagingSource(table: ChangeHistoryTableEnum?, whoGuidHash: Long): PagingSource<Int, ChangeHistoryWithChanges> {
    val _sql: String = """
        |
        |         SELECT *
        |           FROM ChangeHistoryEntity
        |          WHERE (? IS NULL OR hTable = ?)
        |            AND (? = 0 OR hWhoGuidHash = ?)
        |           ORDER BY hLastModified DESC
        |     
        """.trimMargin()
    val _rawQuery: RoomRawQuery = RoomRawQuery(_sql) { _stmt ->
      var _argIndex: Int = 1
      if (table == null) {
        _stmt.bindNull(_argIndex)
      } else {
        _stmt.bindText(_argIndex, __ChangeHistoryTableEnum_enumToString(table))
      }
      _argIndex = 2
      if (table == null) {
        _stmt.bindNull(_argIndex)
      } else {
        _stmt.bindText(_argIndex, __ChangeHistoryTableEnum_enumToString(table))
      }
      _argIndex = 3
      _stmt.bindLong(_argIndex, whoGuidHash)
      _argIndex = 4
      _stmt.bindLong(_argIndex, whoGuidHash)
    }
    return object : LimitOffsetPagingSource<ChangeHistoryWithChanges>(_rawQuery, __db, "ChangeHistoryChangeEntity", "ChangeHistoryEntity") {
      protected override suspend fun convertRows(limitOffsetQuery: RoomRawQuery, itemCount: Int): List<ChangeHistoryWithChanges> = performSuspending(__db, true, false) { _connection ->
        val _stmt: SQLiteStatement = _connection.prepare(limitOffsetQuery.sql)
        limitOffsetQuery.getBindingFunction().invoke(_stmt)
        try {
          val _columnIndexOfHGuid: Int = getColumnIndexOrThrow(_stmt, "hGuid")
          val _columnIndexOfHGuidHash: Int = getColumnIndexOrThrow(_stmt, "hGuidHash")
          val _columnIndexOfHTable: Int = getColumnIndexOrThrow(_stmt, "hTable")
          val _columnIndexOfHTableGuid: Int = getColumnIndexOrThrow(_stmt, "hTableGuid")
          val _columnIndexOfHWhoGuid: Int = getColumnIndexOrThrow(_stmt, "hWhoGuid")
          val _columnIndexOfHWhoGuidHash: Int = getColumnIndexOrThrow(_stmt, "hWhoGuidHash")
          val _columnIndexOfHLastModified: Int = getColumnIndexOrThrow(_stmt, "hLastModified")
          val _columnIndexOfHStored: Int = getColumnIndexOrThrow(_stmt, "hStored")
          val _collectionChanges: LongSparseArray<MutableList<ChangeHistoryChangeEntity>> = LongSparseArray<MutableList<ChangeHistoryChangeEntity>>()
          while (_stmt.step()) {
            val _tmpKey: Long
            _tmpKey = _stmt.getLong(_columnIndexOfHGuidHash)
            if (!_collectionChanges.containsKey(_tmpKey)) {
              _collectionChanges.put(_tmpKey, mutableListOf())
            }
          }
          _stmt.reset()
          __fetchRelationshipChangeHistoryChangeEntityAsworldRespectDatalayerDbSchoolEntitiesChangeHistoryChangeEntity(_connection, _collectionChanges)
          val _result: MutableList<ChangeHistoryWithChanges> = mutableListOf()
          while (_stmt.step()) {
            val _item: ChangeHistoryWithChanges
            val _tmpHistory: ChangeHistoryEntity
            val _tmpHGuid: String
            _tmpHGuid = _stmt.getText(_columnIndexOfHGuid)
            val _tmpHGuidHash: Long
            _tmpHGuidHash = _stmt.getLong(_columnIndexOfHGuidHash)
            val _tmpHTable: ChangeHistoryTableEnum
            _tmpHTable = __ChangeHistoryTableEnum_stringToEnum(_stmt.getText(_columnIndexOfHTable))
            val _tmpHTableGuid: String
            _tmpHTableGuid = _stmt.getText(_columnIndexOfHTableGuid)
            val _tmpHWhoGuid: String
            _tmpHWhoGuid = _stmt.getText(_columnIndexOfHWhoGuid)
            val _tmpHWhoGuidHash: Long
            _tmpHWhoGuidHash = _stmt.getLong(_columnIndexOfHWhoGuidHash)
            val _tmpHLastModified: Instant
            val _tmp: Long?
            if (_stmt.isNull(_columnIndexOfHLastModified)) {
              _tmp = null
            } else {
              _tmp = _stmt.getLong(_columnIndexOfHLastModified)
            }
            val _tmp_1: Instant? = __sharedConverters.toInstant(_tmp)
            if (_tmp_1 == null) {
              error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
            } else {
              _tmpHLastModified = _tmp_1
            }
            val _tmpHStored: Instant
            val _tmp_2: Long?
            if (_stmt.isNull(_columnIndexOfHStored)) {
              _tmp_2 = null
            } else {
              _tmp_2 = _stmt.getLong(_columnIndexOfHStored)
            }
            val _tmp_3: Instant? = __sharedConverters.toInstant(_tmp_2)
            if (_tmp_3 == null) {
              error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
            } else {
              _tmpHStored = _tmp_3
            }
            _tmpHistory = ChangeHistoryEntity(_tmpHGuid,_tmpHGuidHash,_tmpHTable,_tmpHTableGuid,_tmpHWhoGuid,_tmpHWhoGuidHash,_tmpHLastModified,_tmpHStored)
            val _tmpChangesCollection: MutableList<ChangeHistoryChangeEntity>
            val _tmpKey_1: Long
            _tmpKey_1 = _stmt.getLong(_columnIndexOfHGuidHash)
            _tmpChangesCollection = checkNotNull(_collectionChanges.get(_tmpKey_1))
            _item = ChangeHistoryWithChanges(_tmpHistory,_tmpChangesCollection)
            _result.add(_item)
          }
          _result
        } finally {
          _stmt.close()
        }
      }
    }
  }

  public override fun listAsPagingSource(): PagingSource<Int, ChangeHistoryWithChanges> {
    val _sql: String = """
        |
        |        SELECT *
        |          FROM ChangeHistoryEntity
        |         ORDER BY hGuidHash DESC
        |    
        """.trimMargin()
    val _rawQuery: RoomRawQuery = RoomRawQuery(_sql)
    return object : LimitOffsetPagingSource<ChangeHistoryWithChanges>(_rawQuery, __db, "ChangeHistoryChangeEntity", "ChangeHistoryEntity") {
      protected override suspend fun convertRows(limitOffsetQuery: RoomRawQuery, itemCount: Int): List<ChangeHistoryWithChanges> = performSuspending(__db, true, false) { _connection ->
        val _stmt: SQLiteStatement = _connection.prepare(limitOffsetQuery.sql)
        limitOffsetQuery.getBindingFunction().invoke(_stmt)
        try {
          val _columnIndexOfHGuid: Int = getColumnIndexOrThrow(_stmt, "hGuid")
          val _columnIndexOfHGuidHash: Int = getColumnIndexOrThrow(_stmt, "hGuidHash")
          val _columnIndexOfHTable: Int = getColumnIndexOrThrow(_stmt, "hTable")
          val _columnIndexOfHTableGuid: Int = getColumnIndexOrThrow(_stmt, "hTableGuid")
          val _columnIndexOfHWhoGuid: Int = getColumnIndexOrThrow(_stmt, "hWhoGuid")
          val _columnIndexOfHWhoGuidHash: Int = getColumnIndexOrThrow(_stmt, "hWhoGuidHash")
          val _columnIndexOfHLastModified: Int = getColumnIndexOrThrow(_stmt, "hLastModified")
          val _columnIndexOfHStored: Int = getColumnIndexOrThrow(_stmt, "hStored")
          val _collectionChanges: LongSparseArray<MutableList<ChangeHistoryChangeEntity>> = LongSparseArray<MutableList<ChangeHistoryChangeEntity>>()
          while (_stmt.step()) {
            val _tmpKey: Long
            _tmpKey = _stmt.getLong(_columnIndexOfHGuidHash)
            if (!_collectionChanges.containsKey(_tmpKey)) {
              _collectionChanges.put(_tmpKey, mutableListOf())
            }
          }
          _stmt.reset()
          __fetchRelationshipChangeHistoryChangeEntityAsworldRespectDatalayerDbSchoolEntitiesChangeHistoryChangeEntity(_connection, _collectionChanges)
          val _result: MutableList<ChangeHistoryWithChanges> = mutableListOf()
          while (_stmt.step()) {
            val _item: ChangeHistoryWithChanges
            val _tmpHistory: ChangeHistoryEntity
            val _tmpHGuid: String
            _tmpHGuid = _stmt.getText(_columnIndexOfHGuid)
            val _tmpHGuidHash: Long
            _tmpHGuidHash = _stmt.getLong(_columnIndexOfHGuidHash)
            val _tmpHTable: ChangeHistoryTableEnum
            _tmpHTable = __ChangeHistoryTableEnum_stringToEnum(_stmt.getText(_columnIndexOfHTable))
            val _tmpHTableGuid: String
            _tmpHTableGuid = _stmt.getText(_columnIndexOfHTableGuid)
            val _tmpHWhoGuid: String
            _tmpHWhoGuid = _stmt.getText(_columnIndexOfHWhoGuid)
            val _tmpHWhoGuidHash: Long
            _tmpHWhoGuidHash = _stmt.getLong(_columnIndexOfHWhoGuidHash)
            val _tmpHLastModified: Instant
            val _tmp: Long?
            if (_stmt.isNull(_columnIndexOfHLastModified)) {
              _tmp = null
            } else {
              _tmp = _stmt.getLong(_columnIndexOfHLastModified)
            }
            val _tmp_1: Instant? = __sharedConverters.toInstant(_tmp)
            if (_tmp_1 == null) {
              error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
            } else {
              _tmpHLastModified = _tmp_1
            }
            val _tmpHStored: Instant
            val _tmp_2: Long?
            if (_stmt.isNull(_columnIndexOfHStored)) {
              _tmp_2 = null
            } else {
              _tmp_2 = _stmt.getLong(_columnIndexOfHStored)
            }
            val _tmp_3: Instant? = __sharedConverters.toInstant(_tmp_2)
            if (_tmp_3 == null) {
              error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
            } else {
              _tmpHStored = _tmp_3
            }
            _tmpHistory = ChangeHistoryEntity(_tmpHGuid,_tmpHGuidHash,_tmpHTable,_tmpHTableGuid,_tmpHWhoGuid,_tmpHWhoGuidHash,_tmpHLastModified,_tmpHStored)
            val _tmpChangesCollection: MutableList<ChangeHistoryChangeEntity>
            val _tmpKey_1: Long
            _tmpKey_1 = _stmt.getLong(_columnIndexOfHGuidHash)
            _tmpChangesCollection = checkNotNull(_collectionChanges.get(_tmpKey_1))
            _item = ChangeHistoryWithChanges(_tmpHistory,_tmpChangesCollection)
            _result.add(_item)
          }
          _result
        } finally {
          _stmt.close()
        }
      }
    }
  }

  public override suspend fun getUnsyncedChanges(historyIds: List<Long>): List<ChangeHistoryChangeEntity> {
    val _stringBuilder: StringBuilder = StringBuilder()
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("        SELECT *")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("          FROM ChangeHistoryChangeEntity")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("         WHERE hcHistoryGuidHash IN (")
    val _inputSize: Int = historyIds.size
    appendPlaceholders(_stringBuilder, _inputSize)
    _stringBuilder.append(")")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("           AND hcSynced = 0")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("    ")
    val _sql: String = _stringBuilder.toString()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        for (_item: Long in historyIds) {
          _stmt.bindLong(_argIndex, _item)
          _argIndex++
        }
        val _columnIndexOfHcId: Int = getColumnIndexOrThrow(_stmt, "hcId")
        val _columnIndexOfHcHistoryGuidHash: Int = getColumnIndexOrThrow(_stmt, "hcHistoryGuidHash")
        val _columnIndexOfHcField: Int = getColumnIndexOrThrow(_stmt, "hcField")
        val _columnIndexOfHcOldVal: Int = getColumnIndexOrThrow(_stmt, "hcOldVal")
        val _columnIndexOfHcNewVal: Int = getColumnIndexOrThrow(_stmt, "hcNewVal")
        val _columnIndexOfHcSynced: Int = getColumnIndexOrThrow(_stmt, "hcSynced")
        val _columnIndexOfHcLastModified: Int = getColumnIndexOrThrow(_stmt, "hcLastModified")
        val _columnIndexOfHcStored: Int = getColumnIndexOrThrow(_stmt, "hcStored")
        val _result: MutableList<ChangeHistoryChangeEntity> = mutableListOf()
        while (_stmt.step()) {
          val _item_1: ChangeHistoryChangeEntity
          val _tmpHcId: Long
          _tmpHcId = _stmt.getLong(_columnIndexOfHcId)
          val _tmpHcHistoryGuidHash: Long
          _tmpHcHistoryGuidHash = _stmt.getLong(_columnIndexOfHcHistoryGuidHash)
          val _tmpHcField: ChangeHistoryFieldEnum
          _tmpHcField = __ChangeHistoryFieldEnum_stringToEnum(_stmt.getText(_columnIndexOfHcField))
          val _tmpHcOldVal: String?
          if (_stmt.isNull(_columnIndexOfHcOldVal)) {
            _tmpHcOldVal = null
          } else {
            _tmpHcOldVal = _stmt.getText(_columnIndexOfHcOldVal)
          }
          val _tmpHcNewVal: String
          _tmpHcNewVal = _stmt.getText(_columnIndexOfHcNewVal)
          val _tmpHcSynced: Boolean
          val _tmp: Int
          _tmp = _stmt.getLong(_columnIndexOfHcSynced).toInt()
          _tmpHcSynced = _tmp != 0
          val _tmpHcLastModified: Instant
          val _tmp_1: Long?
          if (_stmt.isNull(_columnIndexOfHcLastModified)) {
            _tmp_1 = null
          } else {
            _tmp_1 = _stmt.getLong(_columnIndexOfHcLastModified)
          }
          val _tmp_2: Instant? = __sharedConverters.toInstant(_tmp_1)
          if (_tmp_2 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpHcLastModified = _tmp_2
          }
          val _tmpHcStored: Instant
          val _tmp_3: Long?
          if (_stmt.isNull(_columnIndexOfHcStored)) {
            _tmp_3 = null
          } else {
            _tmp_3 = _stmt.getLong(_columnIndexOfHcStored)
          }
          val _tmp_4: Instant? = __sharedConverters.toInstant(_tmp_3)
          if (_tmp_4 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpHcStored = _tmp_4
          }
          _item_1 = ChangeHistoryChangeEntity(_tmpHcId,_tmpHcHistoryGuidHash,_tmpHcField,_tmpHcOldVal,_tmpHcNewVal,_tmpHcSynced,_tmpHcLastModified,_tmpHcStored)
          _result.add(_item_1)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun findByGuid(tableGuid: String): List<ChangeHistoryWithChanges>? {
    val _sql: String = """
        |
        |        SELECT *
        |          FROM ChangeHistoryEntity
        |         WHERE hTableGuid = ?
        |     
        """.trimMargin()
    return performSuspending(__db, true, true) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindText(_argIndex, tableGuid)
        val _columnIndexOfHGuid: Int = getColumnIndexOrThrow(_stmt, "hGuid")
        val _columnIndexOfHGuidHash: Int = getColumnIndexOrThrow(_stmt, "hGuidHash")
        val _columnIndexOfHTable: Int = getColumnIndexOrThrow(_stmt, "hTable")
        val _columnIndexOfHTableGuid: Int = getColumnIndexOrThrow(_stmt, "hTableGuid")
        val _columnIndexOfHWhoGuid: Int = getColumnIndexOrThrow(_stmt, "hWhoGuid")
        val _columnIndexOfHWhoGuidHash: Int = getColumnIndexOrThrow(_stmt, "hWhoGuidHash")
        val _columnIndexOfHLastModified: Int = getColumnIndexOrThrow(_stmt, "hLastModified")
        val _columnIndexOfHStored: Int = getColumnIndexOrThrow(_stmt, "hStored")
        val _collectionChanges: LongSparseArray<MutableList<ChangeHistoryChangeEntity>> = LongSparseArray<MutableList<ChangeHistoryChangeEntity>>()
        while (_stmt.step()) {
          val _tmpKey: Long
          _tmpKey = _stmt.getLong(_columnIndexOfHGuidHash)
          if (!_collectionChanges.containsKey(_tmpKey)) {
            _collectionChanges.put(_tmpKey, mutableListOf())
          }
        }
        _stmt.reset()
        __fetchRelationshipChangeHistoryChangeEntityAsworldRespectDatalayerDbSchoolEntitiesChangeHistoryChangeEntity(_connection, _collectionChanges)
        val _result: MutableList<ChangeHistoryWithChanges> = mutableListOf()
        while (_stmt.step()) {
          val _item: ChangeHistoryWithChanges
          val _tmpHistory: ChangeHistoryEntity
          val _tmpHGuid: String
          _tmpHGuid = _stmt.getText(_columnIndexOfHGuid)
          val _tmpHGuidHash: Long
          _tmpHGuidHash = _stmt.getLong(_columnIndexOfHGuidHash)
          val _tmpHTable: ChangeHistoryTableEnum
          _tmpHTable = __ChangeHistoryTableEnum_stringToEnum(_stmt.getText(_columnIndexOfHTable))
          val _tmpHTableGuid: String
          _tmpHTableGuid = _stmt.getText(_columnIndexOfHTableGuid)
          val _tmpHWhoGuid: String
          _tmpHWhoGuid = _stmt.getText(_columnIndexOfHWhoGuid)
          val _tmpHWhoGuidHash: Long
          _tmpHWhoGuidHash = _stmt.getLong(_columnIndexOfHWhoGuidHash)
          val _tmpHLastModified: Instant
          val _tmp: Long?
          if (_stmt.isNull(_columnIndexOfHLastModified)) {
            _tmp = null
          } else {
            _tmp = _stmt.getLong(_columnIndexOfHLastModified)
          }
          val _tmp_1: Instant? = __sharedConverters.toInstant(_tmp)
          if (_tmp_1 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpHLastModified = _tmp_1
          }
          val _tmpHStored: Instant
          val _tmp_2: Long?
          if (_stmt.isNull(_columnIndexOfHStored)) {
            _tmp_2 = null
          } else {
            _tmp_2 = _stmt.getLong(_columnIndexOfHStored)
          }
          val _tmp_3: Instant? = __sharedConverters.toInstant(_tmp_2)
          if (_tmp_3 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpHStored = _tmp_3
          }
          _tmpHistory = ChangeHistoryEntity(_tmpHGuid,_tmpHGuidHash,_tmpHTable,_tmpHTableGuid,_tmpHWhoGuid,_tmpHWhoGuidHash,_tmpHLastModified,_tmpHStored)
          val _tmpChangesCollection: MutableList<ChangeHistoryChangeEntity>
          val _tmpKey_1: Long
          _tmpKey_1 = _stmt.getLong(_columnIndexOfHGuidHash)
          _tmpChangesCollection = checkNotNull(_collectionChanges.get(_tmpKey_1))
          _item = ChangeHistoryWithChanges(_tmpHistory,_tmpChangesCollection)
          _result.add(_item)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override fun findByGuidAsFlow(guid: String): Flow<List<ChangeHistoryWithChanges>> {
    val _sql: String = """
        |
        |        SELECT *
        |          FROM ChangeHistoryEntity
        |         WHERE hTableGuid = ?
        |         ORDER BY hLastModified DESC
        |     
        """.trimMargin()
    return createFlow(__db, false, arrayOf("ChangeHistoryChangeEntity", "ChangeHistoryEntity")) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindText(_argIndex, guid)
        val _columnIndexOfHGuid: Int = getColumnIndexOrThrow(_stmt, "hGuid")
        val _columnIndexOfHGuidHash: Int = getColumnIndexOrThrow(_stmt, "hGuidHash")
        val _columnIndexOfHTable: Int = getColumnIndexOrThrow(_stmt, "hTable")
        val _columnIndexOfHTableGuid: Int = getColumnIndexOrThrow(_stmt, "hTableGuid")
        val _columnIndexOfHWhoGuid: Int = getColumnIndexOrThrow(_stmt, "hWhoGuid")
        val _columnIndexOfHWhoGuidHash: Int = getColumnIndexOrThrow(_stmt, "hWhoGuidHash")
        val _columnIndexOfHLastModified: Int = getColumnIndexOrThrow(_stmt, "hLastModified")
        val _columnIndexOfHStored: Int = getColumnIndexOrThrow(_stmt, "hStored")
        val _collectionChanges: LongSparseArray<MutableList<ChangeHistoryChangeEntity>> = LongSparseArray<MutableList<ChangeHistoryChangeEntity>>()
        while (_stmt.step()) {
          val _tmpKey: Long
          _tmpKey = _stmt.getLong(_columnIndexOfHGuidHash)
          if (!_collectionChanges.containsKey(_tmpKey)) {
            _collectionChanges.put(_tmpKey, mutableListOf())
          }
        }
        _stmt.reset()
        __fetchRelationshipChangeHistoryChangeEntityAsworldRespectDatalayerDbSchoolEntitiesChangeHistoryChangeEntity(_connection, _collectionChanges)
        val _result: MutableList<ChangeHistoryWithChanges> = mutableListOf()
        while (_stmt.step()) {
          val _item: ChangeHistoryWithChanges
          val _tmpHistory: ChangeHistoryEntity
          val _tmpHGuid: String
          _tmpHGuid = _stmt.getText(_columnIndexOfHGuid)
          val _tmpHGuidHash: Long
          _tmpHGuidHash = _stmt.getLong(_columnIndexOfHGuidHash)
          val _tmpHTable: ChangeHistoryTableEnum
          _tmpHTable = __ChangeHistoryTableEnum_stringToEnum(_stmt.getText(_columnIndexOfHTable))
          val _tmpHTableGuid: String
          _tmpHTableGuid = _stmt.getText(_columnIndexOfHTableGuid)
          val _tmpHWhoGuid: String
          _tmpHWhoGuid = _stmt.getText(_columnIndexOfHWhoGuid)
          val _tmpHWhoGuidHash: Long
          _tmpHWhoGuidHash = _stmt.getLong(_columnIndexOfHWhoGuidHash)
          val _tmpHLastModified: Instant
          val _tmp: Long?
          if (_stmt.isNull(_columnIndexOfHLastModified)) {
            _tmp = null
          } else {
            _tmp = _stmt.getLong(_columnIndexOfHLastModified)
          }
          val _tmp_1: Instant? = __sharedConverters.toInstant(_tmp)
          if (_tmp_1 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpHLastModified = _tmp_1
          }
          val _tmpHStored: Instant
          val _tmp_2: Long?
          if (_stmt.isNull(_columnIndexOfHStored)) {
            _tmp_2 = null
          } else {
            _tmp_2 = _stmt.getLong(_columnIndexOfHStored)
          }
          val _tmp_3: Instant? = __sharedConverters.toInstant(_tmp_2)
          if (_tmp_3 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpHStored = _tmp_3
          }
          _tmpHistory = ChangeHistoryEntity(_tmpHGuid,_tmpHGuidHash,_tmpHTable,_tmpHTableGuid,_tmpHWhoGuid,_tmpHWhoGuidHash,_tmpHLastModified,_tmpHStored)
          val _tmpChangesCollection: MutableList<ChangeHistoryChangeEntity>
          val _tmpKey_1: Long
          _tmpKey_1 = _stmt.getLong(_columnIndexOfHGuidHash)
          _tmpChangesCollection = checkNotNull(_collectionChanges.get(_tmpKey_1))
          _item = ChangeHistoryWithChanges(_tmpHistory,_tmpChangesCollection)
          _result.add(_item)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun markByHistoryGuids(historyGuids: List<Long>) {
    val _stringBuilder: StringBuilder = StringBuilder()
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("         UPDATE ChangeHistoryChangeEntity")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("           SET hcSynced = 1")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("             WHERE hcHistoryGuidHash IN (")
    val _inputSize: Int = historyGuids.size
    appendPlaceholders(_stringBuilder, _inputSize)
    _stringBuilder.append(")")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("      ")
    val _sql: String = _stringBuilder.toString()
    return performSuspending(__db, false, true) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        for (_item: Long in historyGuids) {
          _stmt.bindLong(_argIndex, _item)
          _argIndex++
        }
        _stmt.step()
      } finally {
        _stmt.close()
      }
    }
  }

  private fun __ChangeHistoryTableEnum_enumToString(_value: ChangeHistoryTableEnum): String = when (_value) {
    ChangeHistoryTableEnum.PERSON -> "PERSON"
    ChangeHistoryTableEnum.CLASS -> "CLASS"
    ChangeHistoryTableEnum.ENROLLMENT -> "ENROLLMENT"
  }

  private fun __ChangeHistoryFieldEnum_enumToString(_value: ChangeHistoryFieldEnum): String = when (_value) {
    ChangeHistoryFieldEnum.PERSON_GIVEN_NAME -> "PERSON_GIVEN_NAME"
    ChangeHistoryFieldEnum.PERSON_FAMILY_NAME -> "PERSON_FAMILY_NAME"
    ChangeHistoryFieldEnum.PERSON_MIDDLE_NAME -> "PERSON_MIDDLE_NAME"
    ChangeHistoryFieldEnum.PERSON_USERNAME -> "PERSON_USERNAME"
    ChangeHistoryFieldEnum.PERSON_GENDER -> "PERSON_GENDER"
    ChangeHistoryFieldEnum.PERSON_EMAIL -> "PERSON_EMAIL"
    ChangeHistoryFieldEnum.PERSON_PHONE_NUMBER -> "PERSON_PHONE_NUMBER"
    ChangeHistoryFieldEnum.PERSON_DATE_OF_BIRTH -> "PERSON_DATE_OF_BIRTH"
    ChangeHistoryFieldEnum.CLASS_STATUS -> "CLASS_STATUS"
    ChangeHistoryFieldEnum.CLASS_TITLE -> "CLASS_TITLE"
    ChangeHistoryFieldEnum.CLASS_DESCRIPTION -> "CLASS_DESCRIPTION"
    ChangeHistoryFieldEnum.CLASS_TEACHER_ADDED -> "CLASS_TEACHER_ADDED"
    ChangeHistoryFieldEnum.CLASS_TEACHER_REMOVED -> "CLASS_TEACHER_REMOVED"
    ChangeHistoryFieldEnum.CLASS_STUDENT_ADDED -> "CLASS_STUDENT_ADDED"
    ChangeHistoryFieldEnum.CLASS_STUDENT_REMOVED -> "CLASS_STUDENT_REMOVED"
    ChangeHistoryFieldEnum.JOIN_REQUEST_APPROVED -> "JOIN_REQUEST_APPROVED"
    ChangeHistoryFieldEnum.JOIN_REQUEST_REJECTED -> "JOIN_REQUEST_REJECTED"
    ChangeHistoryFieldEnum.ENROLLMENT_ROLE -> "ENROLLMENT_ROLE"
    ChangeHistoryFieldEnum.ENROLLMENT_BEGIN_DATE -> "ENROLLMENT_BEGIN_DATE"
    ChangeHistoryFieldEnum.ENROLLMENT_END_DATE -> "ENROLLMENT_END_DATE"
    ChangeHistoryFieldEnum.ENROLLMENT_STATUS -> "ENROLLMENT_STATUS"
  }

  private fun __ChangeHistoryFieldEnum_stringToEnum(_value: String): ChangeHistoryFieldEnum = when (_value) {
    "PERSON_GIVEN_NAME" -> ChangeHistoryFieldEnum.PERSON_GIVEN_NAME
    "PERSON_FAMILY_NAME" -> ChangeHistoryFieldEnum.PERSON_FAMILY_NAME
    "PERSON_MIDDLE_NAME" -> ChangeHistoryFieldEnum.PERSON_MIDDLE_NAME
    "PERSON_USERNAME" -> ChangeHistoryFieldEnum.PERSON_USERNAME
    "PERSON_GENDER" -> ChangeHistoryFieldEnum.PERSON_GENDER
    "PERSON_EMAIL" -> ChangeHistoryFieldEnum.PERSON_EMAIL
    "PERSON_PHONE_NUMBER" -> ChangeHistoryFieldEnum.PERSON_PHONE_NUMBER
    "PERSON_DATE_OF_BIRTH" -> ChangeHistoryFieldEnum.PERSON_DATE_OF_BIRTH
    "CLASS_STATUS" -> ChangeHistoryFieldEnum.CLASS_STATUS
    "CLASS_TITLE" -> ChangeHistoryFieldEnum.CLASS_TITLE
    "CLASS_DESCRIPTION" -> ChangeHistoryFieldEnum.CLASS_DESCRIPTION
    "CLASS_TEACHER_ADDED" -> ChangeHistoryFieldEnum.CLASS_TEACHER_ADDED
    "CLASS_TEACHER_REMOVED" -> ChangeHistoryFieldEnum.CLASS_TEACHER_REMOVED
    "CLASS_STUDENT_ADDED" -> ChangeHistoryFieldEnum.CLASS_STUDENT_ADDED
    "CLASS_STUDENT_REMOVED" -> ChangeHistoryFieldEnum.CLASS_STUDENT_REMOVED
    "JOIN_REQUEST_APPROVED" -> ChangeHistoryFieldEnum.JOIN_REQUEST_APPROVED
    "JOIN_REQUEST_REJECTED" -> ChangeHistoryFieldEnum.JOIN_REQUEST_REJECTED
    "ENROLLMENT_ROLE" -> ChangeHistoryFieldEnum.ENROLLMENT_ROLE
    "ENROLLMENT_BEGIN_DATE" -> ChangeHistoryFieldEnum.ENROLLMENT_BEGIN_DATE
    "ENROLLMENT_END_DATE" -> ChangeHistoryFieldEnum.ENROLLMENT_END_DATE
    "ENROLLMENT_STATUS" -> ChangeHistoryFieldEnum.ENROLLMENT_STATUS
    else -> throw IllegalArgumentException("Can't convert value to enum, unknown value: " + _value)
  }

  private fun __fetchRelationshipChangeHistoryChangeEntityAsworldRespectDatalayerDbSchoolEntitiesChangeHistoryChangeEntity(_connection: SQLiteConnection, _map: LongSparseArray<MutableList<ChangeHistoryChangeEntity>>) {
    if (_map.isEmpty()) {
      return
    }
    if (_map.size() > 999) {
      recursiveFetchLongSparseArray(_map, true) { _tmpMap ->
        __fetchRelationshipChangeHistoryChangeEntityAsworldRespectDatalayerDbSchoolEntitiesChangeHistoryChangeEntity(_connection, _tmpMap)
      }
      return
    }
    val _stringBuilder: StringBuilder = StringBuilder()
    _stringBuilder.append("SELECT `hcId`,`hcHistoryGuidHash`,`hcField`,`hcOldVal`,`hcNewVal`,`hcSynced`,`hcLastModified`,`hcStored` FROM `ChangeHistoryChangeEntity` WHERE `hcHistoryGuidHash` 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, "hcHistoryGuidHash")
      if (_itemKeyIndex == -1) {
        return
      }
      val _columnIndexOfHcId: Int = 0
      val _columnIndexOfHcHistoryGuidHash: Int = 1
      val _columnIndexOfHcField: Int = 2
      val _columnIndexOfHcOldVal: Int = 3
      val _columnIndexOfHcNewVal: Int = 4
      val _columnIndexOfHcSynced: Int = 5
      val _columnIndexOfHcLastModified: Int = 6
      val _columnIndexOfHcStored: Int = 7
      while (_stmt.step()) {
        val _tmpKey: Long
        _tmpKey = _stmt.getLong(_itemKeyIndex)
        val _tmpRelation: MutableList<ChangeHistoryChangeEntity>? = _map.get(_tmpKey)
        if (_tmpRelation != null) {
          val _item_1: ChangeHistoryChangeEntity
          val _tmpHcId: Long
          _tmpHcId = _stmt.getLong(_columnIndexOfHcId)
          val _tmpHcHistoryGuidHash: Long
          _tmpHcHistoryGuidHash = _stmt.getLong(_columnIndexOfHcHistoryGuidHash)
          val _tmpHcField: ChangeHistoryFieldEnum
          _tmpHcField = __ChangeHistoryFieldEnum_stringToEnum(_stmt.getText(_columnIndexOfHcField))
          val _tmpHcOldVal: String?
          if (_stmt.isNull(_columnIndexOfHcOldVal)) {
            _tmpHcOldVal = null
          } else {
            _tmpHcOldVal = _stmt.getText(_columnIndexOfHcOldVal)
          }
          val _tmpHcNewVal: String
          _tmpHcNewVal = _stmt.getText(_columnIndexOfHcNewVal)
          val _tmpHcSynced: Boolean
          val _tmp: Int
          _tmp = _stmt.getLong(_columnIndexOfHcSynced).toInt()
          _tmpHcSynced = _tmp != 0
          val _tmpHcLastModified: Instant
          val _tmp_1: Long?
          if (_stmt.isNull(_columnIndexOfHcLastModified)) {
            _tmp_1 = null
          } else {
            _tmp_1 = _stmt.getLong(_columnIndexOfHcLastModified)
          }
          val _tmp_2: Instant? = __sharedConverters.toInstant(_tmp_1)
          if (_tmp_2 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpHcLastModified = _tmp_2
          }
          val _tmpHcStored: Instant
          val _tmp_3: Long?
          if (_stmt.isNull(_columnIndexOfHcStored)) {
            _tmp_3 = null
          } else {
            _tmp_3 = _stmt.getLong(_columnIndexOfHcStored)
          }
          val _tmp_4: Instant? = __sharedConverters.toInstant(_tmp_3)
          if (_tmp_4 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpHcStored = _tmp_4
          }
          _item_1 = ChangeHistoryChangeEntity(_tmpHcId,_tmpHcHistoryGuidHash,_tmpHcField,_tmpHcOldVal,_tmpHcNewVal,_tmpHcSynced,_tmpHcLastModified,_tmpHcStored)
          _tmpRelation.add(_item_1)
        }
      }
    } finally {
      _stmt.close()
    }
  }

  private fun __ChangeHistoryTableEnum_stringToEnum(_value: String): ChangeHistoryTableEnum = when (_value) {
    "PERSON" -> ChangeHistoryTableEnum.PERSON
    "CLASS" -> ChangeHistoryTableEnum.CLASS
    "ENROLLMENT" -> ChangeHistoryTableEnum.ENROLLMENT
    else -> throw IllegalArgumentException("Can't convert value to enum, unknown value: " + _value)
  }

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