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 com.eygraber.uri.Uri
import io.ktor.http.Url
import javax.`annotation`.processing.Generated
import kotlin.Boolean
import kotlin.Double
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.SchoolTypeConverters
import world.respect.datalayer.db.school.adapters.BookmarkEntities
import world.respect.datalayer.db.school.adapters.OpdsPublicationEntities
import world.respect.datalayer.db.school.entities.BookmarkEntity
import world.respect.datalayer.db.school.opds.OpdsParentType
import world.respect.datalayer.db.school.opds.OpdsTypeConverters
import world.respect.datalayer.db.school.opds.entities.OpdsPublicationEntity
import world.respect.datalayer.db.school.opds.entities.ReadiumLinkEntity
import world.respect.datalayer.db.shared.SharedConverters
import world.respect.datalayer.db.shared.entities.LangMapEntity
import world.respect.datalayer.school.model.StatusEnum

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

  private val __insertAdapterOfBookmarkEntity: EntityInsertAdapter<BookmarkEntity>

  private val __sharedConverters: SharedConverters = SharedConverters()

  private val __schoolTypeConverters: SchoolTypeConverters = SchoolTypeConverters()

  private val __opdsTypeConverters: OpdsTypeConverters = OpdsTypeConverters()
  init {
    this.__db = __db
    this.__insertAdapterOfBookmarkEntity = object : EntityInsertAdapter<BookmarkEntity>() {
      protected override fun createQuery(): String = "INSERT OR REPLACE INTO `BookmarkEntity` (`bPersonUid`,`bPersonUidHash`,`bUrl`,`bUrlHash`,`bStatus`,`bLastModified`,`bStored`,`bAppManifestUrl`) VALUES (?,?,?,?,?,?,?,?)"

      protected override fun bind(statement: SQLiteStatement, entity: BookmarkEntity) {
        statement.bindText(1, entity.bPersonUid)
        statement.bindLong(2, entity.bPersonUidHash)
        val _tmp: String? = __sharedConverters.fromUrl(entity.bUrl)
        if (_tmp == null) {
          statement.bindNull(3)
        } else {
          statement.bindText(3, _tmp)
        }
        statement.bindLong(4, entity.bUrlHash)
        val _tmp_1: Int = __schoolTypeConverters.fromStatusEnum(entity.bStatus)
        statement.bindLong(5, _tmp_1.toLong())
        val _tmp_2: Long? = __sharedConverters.fromInstant(entity.bLastModified)
        if (_tmp_2 == null) {
          statement.bindNull(6)
        } else {
          statement.bindLong(6, _tmp_2)
        }
        val _tmp_3: Long? = __sharedConverters.fromInstant(entity.bStored)
        if (_tmp_3 == null) {
          statement.bindNull(7)
        } else {
          statement.bindLong(7, _tmp_3)
        }
        val _tmp_4: String? = __sharedConverters.fromUrl(entity.bAppManifestUrl)
        if (_tmp_4 == null) {
          statement.bindNull(8)
        } else {
          statement.bindText(8, _tmp_4)
        }
      }
    }
  }

  public override suspend fun upsert(bookmarks: List<BookmarkEntity>): Unit = performSuspending(__db, false, true) { _connection ->
    __insertAdapterOfBookmarkEntity.insert(_connection, bookmarks)
  }

  public override fun getBookmarkStatus(
    personUid: String,
    url: String,
    status: StatusEnum,
  ): Flow<Boolean> {
    val _sql: String = """
        |
        |        SELECT EXISTS(
        |        SELECT 1 FROM BookmarkEntity
        |         WHERE bPersonUid = ?
        |           AND bUrl = ?
        |           AND bStatus = ?
        |         )
        |    
        """.trimMargin()
    return createFlow(__db, false, arrayOf("BookmarkEntity")) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindText(_argIndex, personUid)
        _argIndex = 2
        _stmt.bindText(_argIndex, url)
        _argIndex = 3
        val _tmp: Int = __schoolTypeConverters.fromStatusEnum(status)
        _stmt.bindLong(_argIndex, _tmp.toLong())
        val _result: Boolean
        if (_stmt.step()) {
          val _tmp_1: Int
          _tmp_1 = _stmt.getLong(0).toInt()
          _result = _tmp_1 != 0
        } else {
          _result = false
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun list(
    personUid: String,
    includeDeleted: Boolean,
    activeStatus: StatusEnum,
  ): List<BookmarkEntities> {
    val _sql: String = """
        |
        |        SELECT *
        |          FROM BookmarkEntity
        |         WHERE bPersonUid = ?
        |           AND (? OR bStatus = ?)
        |      ORDER BY bLastModified ASC
        |    
        """.trimMargin()
    return performSuspending(__db, true, true) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindText(_argIndex, personUid)
        _argIndex = 2
        val _tmp: Int = if (includeDeleted) 1 else 0
        _stmt.bindLong(_argIndex, _tmp.toLong())
        _argIndex = 3
        val _tmp_1: Int = __schoolTypeConverters.fromStatusEnum(activeStatus)
        _stmt.bindLong(_argIndex, _tmp_1.toLong())
        val _columnIndexOfBPersonUid: Int = getColumnIndexOrThrow(_stmt, "bPersonUid")
        val _columnIndexOfBPersonUidHash: Int = getColumnIndexOrThrow(_stmt, "bPersonUidHash")
        val _columnIndexOfBUrl: Int = getColumnIndexOrThrow(_stmt, "bUrl")
        val _columnIndexOfBUrlHash: Int = getColumnIndexOrThrow(_stmt, "bUrlHash")
        val _columnIndexOfBStatus: Int = getColumnIndexOrThrow(_stmt, "bStatus")
        val _columnIndexOfBLastModified: Int = getColumnIndexOrThrow(_stmt, "bLastModified")
        val _columnIndexOfBStored: Int = getColumnIndexOrThrow(_stmt, "bStored")
        val _columnIndexOfBAppManifestUrl: Int = getColumnIndexOrThrow(_stmt, "bAppManifestUrl")
        val _collectionOpdsPublicationEntities: LongSparseArray<OpdsPublicationEntities?> = LongSparseArray<OpdsPublicationEntities?>()
        while (_stmt.step()) {
          val _tmpKey: Long
          _tmpKey = _stmt.getLong(_columnIndexOfBUrlHash)
          _collectionOpdsPublicationEntities.put(_tmpKey, null)
        }
        _stmt.reset()
        __fetchRelationshipOpdsPublicationEntityAsworldRespectDatalayerDbSchoolAdaptersOpdsPublicationEntities(_connection, _collectionOpdsPublicationEntities)
        val _result: MutableList<BookmarkEntities> = mutableListOf()
        while (_stmt.step()) {
          val _item: BookmarkEntities
          val _tmpBookmark: BookmarkEntity
          val _tmpBPersonUid: String
          _tmpBPersonUid = _stmt.getText(_columnIndexOfBPersonUid)
          val _tmpBPersonUidHash: Long
          _tmpBPersonUidHash = _stmt.getLong(_columnIndexOfBPersonUidHash)
          val _tmpBUrl: Url
          val _tmp_2: String?
          if (_stmt.isNull(_columnIndexOfBUrl)) {
            _tmp_2 = null
          } else {
            _tmp_2 = _stmt.getText(_columnIndexOfBUrl)
          }
          val _tmp_3: Url? = __sharedConverters.toUrl(_tmp_2)
          if (_tmp_3 == null) {
            error("Expected NON-NULL 'io.ktor.http.Url', but it was NULL.")
          } else {
            _tmpBUrl = _tmp_3
          }
          val _tmpBUrlHash: Long
          _tmpBUrlHash = _stmt.getLong(_columnIndexOfBUrlHash)
          val _tmpBStatus: StatusEnum
          val _tmp_4: Int
          _tmp_4 = _stmt.getLong(_columnIndexOfBStatus).toInt()
          _tmpBStatus = __schoolTypeConverters.toStatusEnum(_tmp_4)
          val _tmpBLastModified: Instant
          val _tmp_5: Long?
          if (_stmt.isNull(_columnIndexOfBLastModified)) {
            _tmp_5 = null
          } else {
            _tmp_5 = _stmt.getLong(_columnIndexOfBLastModified)
          }
          val _tmp_6: Instant? = __sharedConverters.toInstant(_tmp_5)
          if (_tmp_6 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpBLastModified = _tmp_6
          }
          val _tmpBStored: Instant
          val _tmp_7: Long?
          if (_stmt.isNull(_columnIndexOfBStored)) {
            _tmp_7 = null
          } else {
            _tmp_7 = _stmt.getLong(_columnIndexOfBStored)
          }
          val _tmp_8: Instant? = __sharedConverters.toInstant(_tmp_7)
          if (_tmp_8 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpBStored = _tmp_8
          }
          val _tmpBAppManifestUrl: Url
          val _tmp_9: String?
          if (_stmt.isNull(_columnIndexOfBAppManifestUrl)) {
            _tmp_9 = null
          } else {
            _tmp_9 = _stmt.getText(_columnIndexOfBAppManifestUrl)
          }
          val _tmp_10: Url? = __sharedConverters.toUrl(_tmp_9)
          if (_tmp_10 == null) {
            error("Expected NON-NULL 'io.ktor.http.Url', but it was NULL.")
          } else {
            _tmpBAppManifestUrl = _tmp_10
          }
          _tmpBookmark = BookmarkEntity(_tmpBPersonUid,_tmpBPersonUidHash,_tmpBUrl,_tmpBUrlHash,_tmpBStatus,_tmpBLastModified,_tmpBStored,_tmpBAppManifestUrl)
          val _tmpOpdsPublicationEntities: OpdsPublicationEntities?
          val _tmpKey_1: Long
          _tmpKey_1 = _stmt.getLong(_columnIndexOfBUrlHash)
          _tmpOpdsPublicationEntities = _collectionOpdsPublicationEntities.get(_tmpKey_1)
          _item = BookmarkEntities(_tmpBookmark,_tmpOpdsPublicationEntities)
          _result.add(_item)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override fun listAsFlow(
    personUid: String,
    includeDeleted: Boolean,
    activeStatus: StatusEnum,
  ): Flow<List<BookmarkEntities>> {
    val _sql: String = """
        |
        |        SELECT *
        |          FROM BookmarkEntity
        |         WHERE bPersonUid = ?
        |           AND (? OR bStatus = ?)
        |      ORDER BY bLastModified ASC
        |    
        """.trimMargin()
    return createFlow(__db, true, arrayOf("LangMapEntity", "ReadiumLinkEntity", "OpdsPublicationEntity", "BookmarkEntity")) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindText(_argIndex, personUid)
        _argIndex = 2
        val _tmp: Int = if (includeDeleted) 1 else 0
        _stmt.bindLong(_argIndex, _tmp.toLong())
        _argIndex = 3
        val _tmp_1: Int = __schoolTypeConverters.fromStatusEnum(activeStatus)
        _stmt.bindLong(_argIndex, _tmp_1.toLong())
        val _columnIndexOfBPersonUid: Int = getColumnIndexOrThrow(_stmt, "bPersonUid")
        val _columnIndexOfBPersonUidHash: Int = getColumnIndexOrThrow(_stmt, "bPersonUidHash")
        val _columnIndexOfBUrl: Int = getColumnIndexOrThrow(_stmt, "bUrl")
        val _columnIndexOfBUrlHash: Int = getColumnIndexOrThrow(_stmt, "bUrlHash")
        val _columnIndexOfBStatus: Int = getColumnIndexOrThrow(_stmt, "bStatus")
        val _columnIndexOfBLastModified: Int = getColumnIndexOrThrow(_stmt, "bLastModified")
        val _columnIndexOfBStored: Int = getColumnIndexOrThrow(_stmt, "bStored")
        val _columnIndexOfBAppManifestUrl: Int = getColumnIndexOrThrow(_stmt, "bAppManifestUrl")
        val _collectionOpdsPublicationEntities: LongSparseArray<OpdsPublicationEntities?> = LongSparseArray<OpdsPublicationEntities?>()
        while (_stmt.step()) {
          val _tmpKey: Long
          _tmpKey = _stmt.getLong(_columnIndexOfBUrlHash)
          _collectionOpdsPublicationEntities.put(_tmpKey, null)
        }
        _stmt.reset()
        __fetchRelationshipOpdsPublicationEntityAsworldRespectDatalayerDbSchoolAdaptersOpdsPublicationEntities(_connection, _collectionOpdsPublicationEntities)
        val _result: MutableList<BookmarkEntities> = mutableListOf()
        while (_stmt.step()) {
          val _item: BookmarkEntities
          val _tmpBookmark: BookmarkEntity
          val _tmpBPersonUid: String
          _tmpBPersonUid = _stmt.getText(_columnIndexOfBPersonUid)
          val _tmpBPersonUidHash: Long
          _tmpBPersonUidHash = _stmt.getLong(_columnIndexOfBPersonUidHash)
          val _tmpBUrl: Url
          val _tmp_2: String?
          if (_stmt.isNull(_columnIndexOfBUrl)) {
            _tmp_2 = null
          } else {
            _tmp_2 = _stmt.getText(_columnIndexOfBUrl)
          }
          val _tmp_3: Url? = __sharedConverters.toUrl(_tmp_2)
          if (_tmp_3 == null) {
            error("Expected NON-NULL 'io.ktor.http.Url', but it was NULL.")
          } else {
            _tmpBUrl = _tmp_3
          }
          val _tmpBUrlHash: Long
          _tmpBUrlHash = _stmt.getLong(_columnIndexOfBUrlHash)
          val _tmpBStatus: StatusEnum
          val _tmp_4: Int
          _tmp_4 = _stmt.getLong(_columnIndexOfBStatus).toInt()
          _tmpBStatus = __schoolTypeConverters.toStatusEnum(_tmp_4)
          val _tmpBLastModified: Instant
          val _tmp_5: Long?
          if (_stmt.isNull(_columnIndexOfBLastModified)) {
            _tmp_5 = null
          } else {
            _tmp_5 = _stmt.getLong(_columnIndexOfBLastModified)
          }
          val _tmp_6: Instant? = __sharedConverters.toInstant(_tmp_5)
          if (_tmp_6 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpBLastModified = _tmp_6
          }
          val _tmpBStored: Instant
          val _tmp_7: Long?
          if (_stmt.isNull(_columnIndexOfBStored)) {
            _tmp_7 = null
          } else {
            _tmp_7 = _stmt.getLong(_columnIndexOfBStored)
          }
          val _tmp_8: Instant? = __sharedConverters.toInstant(_tmp_7)
          if (_tmp_8 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpBStored = _tmp_8
          }
          val _tmpBAppManifestUrl: Url
          val _tmp_9: String?
          if (_stmt.isNull(_columnIndexOfBAppManifestUrl)) {
            _tmp_9 = null
          } else {
            _tmp_9 = _stmt.getText(_columnIndexOfBAppManifestUrl)
          }
          val _tmp_10: Url? = __sharedConverters.toUrl(_tmp_9)
          if (_tmp_10 == null) {
            error("Expected NON-NULL 'io.ktor.http.Url', but it was NULL.")
          } else {
            _tmpBAppManifestUrl = _tmp_10
          }
          _tmpBookmark = BookmarkEntity(_tmpBPersonUid,_tmpBPersonUidHash,_tmpBUrl,_tmpBUrlHash,_tmpBStatus,_tmpBLastModified,_tmpBStored,_tmpBAppManifestUrl)
          val _tmpOpdsPublicationEntities: OpdsPublicationEntities?
          val _tmpKey_1: Long
          _tmpKey_1 = _stmt.getLong(_columnIndexOfBUrlHash)
          _tmpOpdsPublicationEntities = _collectionOpdsPublicationEntities.get(_tmpKey_1)
          _item = BookmarkEntities(_tmpBookmark,_tmpOpdsPublicationEntities)
          _result.add(_item)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override fun listAsPagingSource(
    personUid: String,
    includeDeleted: Boolean,
    activeStatus: StatusEnum,
  ): PagingSource<Int, BookmarkEntities> {
    val _sql: String = """
        |
        |        SELECT *
        |          FROM BookmarkEntity
        |         WHERE bPersonUid = ?
        |           AND (? OR bStatus = ?)
        |      ORDER BY bLastModified ASC
        |    
        """.trimMargin()
    val _rawQuery: RoomRawQuery = RoomRawQuery(_sql) { _stmt ->
      var _argIndex: Int = 1
      _stmt.bindText(_argIndex, personUid)
      _argIndex = 2
      val _tmp: Int = if (includeDeleted) 1 else 0
      _stmt.bindLong(_argIndex, _tmp.toLong())
      _argIndex = 3
      val _tmp_1: Int = __schoolTypeConverters.fromStatusEnum(activeStatus)
      _stmt.bindLong(_argIndex, _tmp_1.toLong())
    }
    return object : LimitOffsetPagingSource<BookmarkEntities>(_rawQuery, __db, "LangMapEntity", "ReadiumLinkEntity", "OpdsPublicationEntity", "BookmarkEntity") {
      protected override suspend fun convertRows(limitOffsetQuery: RoomRawQuery, itemCount: Int): List<BookmarkEntities> = performSuspending(__db, true, false) { _connection ->
        val _stmt: SQLiteStatement = _connection.prepare(limitOffsetQuery.sql)
        limitOffsetQuery.getBindingFunction().invoke(_stmt)
        try {
          val _columnIndexOfBPersonUid: Int = getColumnIndexOrThrow(_stmt, "bPersonUid")
          val _columnIndexOfBPersonUidHash: Int = getColumnIndexOrThrow(_stmt, "bPersonUidHash")
          val _columnIndexOfBUrl: Int = getColumnIndexOrThrow(_stmt, "bUrl")
          val _columnIndexOfBUrlHash: Int = getColumnIndexOrThrow(_stmt, "bUrlHash")
          val _columnIndexOfBStatus: Int = getColumnIndexOrThrow(_stmt, "bStatus")
          val _columnIndexOfBLastModified: Int = getColumnIndexOrThrow(_stmt, "bLastModified")
          val _columnIndexOfBStored: Int = getColumnIndexOrThrow(_stmt, "bStored")
          val _columnIndexOfBAppManifestUrl: Int = getColumnIndexOrThrow(_stmt, "bAppManifestUrl")
          val _collectionOpdsPublicationEntities: LongSparseArray<OpdsPublicationEntities?> = LongSparseArray<OpdsPublicationEntities?>()
          while (_stmt.step()) {
            val _tmpKey: Long
            _tmpKey = _stmt.getLong(_columnIndexOfBUrlHash)
            _collectionOpdsPublicationEntities.put(_tmpKey, null)
          }
          _stmt.reset()
          __fetchRelationshipOpdsPublicationEntityAsworldRespectDatalayerDbSchoolAdaptersOpdsPublicationEntities(_connection, _collectionOpdsPublicationEntities)
          val _result: MutableList<BookmarkEntities> = mutableListOf()
          while (_stmt.step()) {
            val _item: BookmarkEntities
            val _tmpBookmark: BookmarkEntity
            val _tmpBPersonUid: String
            _tmpBPersonUid = _stmt.getText(_columnIndexOfBPersonUid)
            val _tmpBPersonUidHash: Long
            _tmpBPersonUidHash = _stmt.getLong(_columnIndexOfBPersonUidHash)
            val _tmpBUrl: Url
            val _tmp_2: String?
            if (_stmt.isNull(_columnIndexOfBUrl)) {
              _tmp_2 = null
            } else {
              _tmp_2 = _stmt.getText(_columnIndexOfBUrl)
            }
            val _tmp_3: Url? = __sharedConverters.toUrl(_tmp_2)
            if (_tmp_3 == null) {
              error("Expected NON-NULL 'io.ktor.http.Url', but it was NULL.")
            } else {
              _tmpBUrl = _tmp_3
            }
            val _tmpBUrlHash: Long
            _tmpBUrlHash = _stmt.getLong(_columnIndexOfBUrlHash)
            val _tmpBStatus: StatusEnum
            val _tmp_4: Int
            _tmp_4 = _stmt.getLong(_columnIndexOfBStatus).toInt()
            _tmpBStatus = __schoolTypeConverters.toStatusEnum(_tmp_4)
            val _tmpBLastModified: Instant
            val _tmp_5: Long?
            if (_stmt.isNull(_columnIndexOfBLastModified)) {
              _tmp_5 = null
            } else {
              _tmp_5 = _stmt.getLong(_columnIndexOfBLastModified)
            }
            val _tmp_6: Instant? = __sharedConverters.toInstant(_tmp_5)
            if (_tmp_6 == null) {
              error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
            } else {
              _tmpBLastModified = _tmp_6
            }
            val _tmpBStored: Instant
            val _tmp_7: Long?
            if (_stmt.isNull(_columnIndexOfBStored)) {
              _tmp_7 = null
            } else {
              _tmp_7 = _stmt.getLong(_columnIndexOfBStored)
            }
            val _tmp_8: Instant? = __sharedConverters.toInstant(_tmp_7)
            if (_tmp_8 == null) {
              error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
            } else {
              _tmpBStored = _tmp_8
            }
            val _tmpBAppManifestUrl: Url
            val _tmp_9: String?
            if (_stmt.isNull(_columnIndexOfBAppManifestUrl)) {
              _tmp_9 = null
            } else {
              _tmp_9 = _stmt.getText(_columnIndexOfBAppManifestUrl)
            }
            val _tmp_10: Url? = __sharedConverters.toUrl(_tmp_9)
            if (_tmp_10 == null) {
              error("Expected NON-NULL 'io.ktor.http.Url', but it was NULL.")
            } else {
              _tmpBAppManifestUrl = _tmp_10
            }
            _tmpBookmark = BookmarkEntity(_tmpBPersonUid,_tmpBPersonUidHash,_tmpBUrl,_tmpBUrlHash,_tmpBStatus,_tmpBLastModified,_tmpBStored,_tmpBAppManifestUrl)
            val _tmpOpdsPublicationEntities: OpdsPublicationEntities?
            val _tmpKey_1: Long
            _tmpKey_1 = _stmt.getLong(_columnIndexOfBUrlHash)
            _tmpOpdsPublicationEntities = _collectionOpdsPublicationEntities.get(_tmpKey_1)
            _item = BookmarkEntities(_tmpBookmark,_tmpOpdsPublicationEntities)
            _result.add(_item)
          }
          _result
        } finally {
          _stmt.close()
        }
      }
    }
  }

  public override suspend fun findBookmarksWithMissingPublication(personUid: String): List<BookmarkEntities> {
    val _sql: String = """
        |
        |        SELECT * 
        |          FROM BookmarkEntity 
        |         WHERE bPersonUid = ?
        |         AND NOT EXISTS ( 
        |                 SELECT 1
        |                   FROM OpdsPublicationEntity 
        |                  WHERE opeUrlHash = bUrlHash
        |             )
        |    
        """.trimMargin()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindText(_argIndex, personUid)
        val _columnIndexOfBPersonUid: Int = getColumnIndexOrThrow(_stmt, "bPersonUid")
        val _columnIndexOfBPersonUidHash: Int = getColumnIndexOrThrow(_stmt, "bPersonUidHash")
        val _columnIndexOfBUrl: Int = getColumnIndexOrThrow(_stmt, "bUrl")
        val _columnIndexOfBUrlHash: Int = getColumnIndexOrThrow(_stmt, "bUrlHash")
        val _columnIndexOfBStatus: Int = getColumnIndexOrThrow(_stmt, "bStatus")
        val _columnIndexOfBLastModified: Int = getColumnIndexOrThrow(_stmt, "bLastModified")
        val _columnIndexOfBStored: Int = getColumnIndexOrThrow(_stmt, "bStored")
        val _columnIndexOfBAppManifestUrl: Int = getColumnIndexOrThrow(_stmt, "bAppManifestUrl")
        val _collectionOpdsPublicationEntities: LongSparseArray<OpdsPublicationEntities?> = LongSparseArray<OpdsPublicationEntities?>()
        while (_stmt.step()) {
          val _tmpKey: Long
          _tmpKey = _stmt.getLong(_columnIndexOfBUrlHash)
          _collectionOpdsPublicationEntities.put(_tmpKey, null)
        }
        _stmt.reset()
        __fetchRelationshipOpdsPublicationEntityAsworldRespectDatalayerDbSchoolAdaptersOpdsPublicationEntities(_connection, _collectionOpdsPublicationEntities)
        val _result: MutableList<BookmarkEntities> = mutableListOf()
        while (_stmt.step()) {
          val _item: BookmarkEntities
          val _tmpBookmark: BookmarkEntity
          val _tmpBPersonUid: String
          _tmpBPersonUid = _stmt.getText(_columnIndexOfBPersonUid)
          val _tmpBPersonUidHash: Long
          _tmpBPersonUidHash = _stmt.getLong(_columnIndexOfBPersonUidHash)
          val _tmpBUrl: Url
          val _tmp: String?
          if (_stmt.isNull(_columnIndexOfBUrl)) {
            _tmp = null
          } else {
            _tmp = _stmt.getText(_columnIndexOfBUrl)
          }
          val _tmp_1: Url? = __sharedConverters.toUrl(_tmp)
          if (_tmp_1 == null) {
            error("Expected NON-NULL 'io.ktor.http.Url', but it was NULL.")
          } else {
            _tmpBUrl = _tmp_1
          }
          val _tmpBUrlHash: Long
          _tmpBUrlHash = _stmt.getLong(_columnIndexOfBUrlHash)
          val _tmpBStatus: StatusEnum
          val _tmp_2: Int
          _tmp_2 = _stmt.getLong(_columnIndexOfBStatus).toInt()
          _tmpBStatus = __schoolTypeConverters.toStatusEnum(_tmp_2)
          val _tmpBLastModified: Instant
          val _tmp_3: Long?
          if (_stmt.isNull(_columnIndexOfBLastModified)) {
            _tmp_3 = null
          } else {
            _tmp_3 = _stmt.getLong(_columnIndexOfBLastModified)
          }
          val _tmp_4: Instant? = __sharedConverters.toInstant(_tmp_3)
          if (_tmp_4 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpBLastModified = _tmp_4
          }
          val _tmpBStored: Instant
          val _tmp_5: Long?
          if (_stmt.isNull(_columnIndexOfBStored)) {
            _tmp_5 = null
          } else {
            _tmp_5 = _stmt.getLong(_columnIndexOfBStored)
          }
          val _tmp_6: Instant? = __sharedConverters.toInstant(_tmp_5)
          if (_tmp_6 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpBStored = _tmp_6
          }
          val _tmpBAppManifestUrl: Url
          val _tmp_7: String?
          if (_stmt.isNull(_columnIndexOfBAppManifestUrl)) {
            _tmp_7 = null
          } else {
            _tmp_7 = _stmt.getText(_columnIndexOfBAppManifestUrl)
          }
          val _tmp_8: Url? = __sharedConverters.toUrl(_tmp_7)
          if (_tmp_8 == null) {
            error("Expected NON-NULL 'io.ktor.http.Url', but it was NULL.")
          } else {
            _tmpBAppManifestUrl = _tmp_8
          }
          _tmpBookmark = BookmarkEntity(_tmpBPersonUid,_tmpBPersonUidHash,_tmpBUrl,_tmpBUrlHash,_tmpBStatus,_tmpBLastModified,_tmpBStored,_tmpBAppManifestUrl)
          val _tmpOpdsPublicationEntities: OpdsPublicationEntities?
          val _tmpKey_1: Long
          _tmpKey_1 = _stmt.getLong(_columnIndexOfBUrlHash)
          _tmpOpdsPublicationEntities = _collectionOpdsPublicationEntities.get(_tmpKey_1)
          _item = BookmarkEntities(_tmpBookmark,_tmpOpdsPublicationEntities)
          _result.add(_item)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun getBookmarkLastModified(personUid: String, urlHash: Long): Long? {
    val _sql: String = """
        |
        |        SELECT bLastModified
        |          FROM BookmarkEntity
        |         WHERE bPersonUid = ?
        |           AND bUrlHash = ?
        |    
        """.trimMargin()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindText(_argIndex, personUid)
        _argIndex = 2
        _stmt.bindLong(_argIndex, urlHash)
        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 findByUidList(uids: List<Long>): List<BookmarkEntities> {
    val _stringBuilder: StringBuilder = StringBuilder()
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("        SELECT *")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("          FROM BookmarkEntity")
    _stringBuilder.append("""
        |
        |""".trimMargin())
    _stringBuilder.append("         WHERE bPersonUid 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
        for (_item: Long in uids) {
          _stmt.bindLong(_argIndex, _item)
          _argIndex++
        }
        val _columnIndexOfBPersonUid: Int = getColumnIndexOrThrow(_stmt, "bPersonUid")
        val _columnIndexOfBPersonUidHash: Int = getColumnIndexOrThrow(_stmt, "bPersonUidHash")
        val _columnIndexOfBUrl: Int = getColumnIndexOrThrow(_stmt, "bUrl")
        val _columnIndexOfBUrlHash: Int = getColumnIndexOrThrow(_stmt, "bUrlHash")
        val _columnIndexOfBStatus: Int = getColumnIndexOrThrow(_stmt, "bStatus")
        val _columnIndexOfBLastModified: Int = getColumnIndexOrThrow(_stmt, "bLastModified")
        val _columnIndexOfBStored: Int = getColumnIndexOrThrow(_stmt, "bStored")
        val _columnIndexOfBAppManifestUrl: Int = getColumnIndexOrThrow(_stmt, "bAppManifestUrl")
        val _collectionOpdsPublicationEntities: LongSparseArray<OpdsPublicationEntities?> = LongSparseArray<OpdsPublicationEntities?>()
        while (_stmt.step()) {
          val _tmpKey: Long
          _tmpKey = _stmt.getLong(_columnIndexOfBUrlHash)
          _collectionOpdsPublicationEntities.put(_tmpKey, null)
        }
        _stmt.reset()
        __fetchRelationshipOpdsPublicationEntityAsworldRespectDatalayerDbSchoolAdaptersOpdsPublicationEntities(_connection, _collectionOpdsPublicationEntities)
        val _result: MutableList<BookmarkEntities> = mutableListOf()
        while (_stmt.step()) {
          val _item_1: BookmarkEntities
          val _tmpBookmark: BookmarkEntity
          val _tmpBPersonUid: String
          _tmpBPersonUid = _stmt.getText(_columnIndexOfBPersonUid)
          val _tmpBPersonUidHash: Long
          _tmpBPersonUidHash = _stmt.getLong(_columnIndexOfBPersonUidHash)
          val _tmpBUrl: Url
          val _tmp: String?
          if (_stmt.isNull(_columnIndexOfBUrl)) {
            _tmp = null
          } else {
            _tmp = _stmt.getText(_columnIndexOfBUrl)
          }
          val _tmp_1: Url? = __sharedConverters.toUrl(_tmp)
          if (_tmp_1 == null) {
            error("Expected NON-NULL 'io.ktor.http.Url', but it was NULL.")
          } else {
            _tmpBUrl = _tmp_1
          }
          val _tmpBUrlHash: Long
          _tmpBUrlHash = _stmt.getLong(_columnIndexOfBUrlHash)
          val _tmpBStatus: StatusEnum
          val _tmp_2: Int
          _tmp_2 = _stmt.getLong(_columnIndexOfBStatus).toInt()
          _tmpBStatus = __schoolTypeConverters.toStatusEnum(_tmp_2)
          val _tmpBLastModified: Instant
          val _tmp_3: Long?
          if (_stmt.isNull(_columnIndexOfBLastModified)) {
            _tmp_3 = null
          } else {
            _tmp_3 = _stmt.getLong(_columnIndexOfBLastModified)
          }
          val _tmp_4: Instant? = __sharedConverters.toInstant(_tmp_3)
          if (_tmp_4 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpBLastModified = _tmp_4
          }
          val _tmpBStored: Instant
          val _tmp_5: Long?
          if (_stmt.isNull(_columnIndexOfBStored)) {
            _tmp_5 = null
          } else {
            _tmp_5 = _stmt.getLong(_columnIndexOfBStored)
          }
          val _tmp_6: Instant? = __sharedConverters.toInstant(_tmp_5)
          if (_tmp_6 == null) {
            error("Expected NON-NULL 'kotlin.time.Instant', but it was NULL.")
          } else {
            _tmpBStored = _tmp_6
          }
          val _tmpBAppManifestUrl: Url
          val _tmp_7: String?
          if (_stmt.isNull(_columnIndexOfBAppManifestUrl)) {
            _tmp_7 = null
          } else {
            _tmp_7 = _stmt.getText(_columnIndexOfBAppManifestUrl)
          }
          val _tmp_8: Url? = __sharedConverters.toUrl(_tmp_7)
          if (_tmp_8 == null) {
            error("Expected NON-NULL 'io.ktor.http.Url', but it was NULL.")
          } else {
            _tmpBAppManifestUrl = _tmp_8
          }
          _tmpBookmark = BookmarkEntity(_tmpBPersonUid,_tmpBPersonUidHash,_tmpBUrl,_tmpBUrlHash,_tmpBStatus,_tmpBLastModified,_tmpBStored,_tmpBAppManifestUrl)
          val _tmpOpdsPublicationEntities: OpdsPublicationEntities?
          val _tmpKey_1: Long
          _tmpKey_1 = _stmt.getLong(_columnIndexOfBUrlHash)
          _tmpOpdsPublicationEntities = _collectionOpdsPublicationEntities.get(_tmpKey_1)
          _item_1 = BookmarkEntities(_tmpBookmark,_tmpOpdsPublicationEntities)
          _result.add(_item_1)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  private fun __fetchRelationshipLangMapEntityAsworldRespectDatalayerDbSharedEntitiesLangMapEntity(_connection: SQLiteConnection, _map: LongSparseArray<MutableList<LangMapEntity>>) {
    if (_map.isEmpty()) {
      return
    }
    if (_map.size() > 999) {
      recursiveFetchLongSparseArray(_map, true) { _tmpMap ->
        __fetchRelationshipLangMapEntityAsworldRespectDatalayerDbSharedEntitiesLangMapEntity(_connection, _tmpMap)
      }
      return
    }
    val _stringBuilder: StringBuilder = StringBuilder()
    _stringBuilder.append("SELECT `lmeId`,`lmeTopParentType`,`lmeTopParentUid1`,`lmeTopParentUid2`,`lmePropType`,`lmePropFk`,`lmeLang`,`lmeRegion`,`lmeValue` FROM `LangMapEntity` WHERE `lmeTopParentUid1` 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, "lmeTopParentUid1")
      if (_itemKeyIndex == -1) {
        return
      }
      val _columnIndexOfLmeId: Int = 0
      val _columnIndexOfLmeTopParentType: Int = 1
      val _columnIndexOfLmeTopParentUid1: Int = 2
      val _columnIndexOfLmeTopParentUid2: Int = 3
      val _columnIndexOfLmePropType: Int = 4
      val _columnIndexOfLmePropFk: Int = 5
      val _columnIndexOfLmeLang: Int = 6
      val _columnIndexOfLmeRegion: Int = 7
      val _columnIndexOfLmeValue: Int = 8
      while (_stmt.step()) {
        val _tmpKey: Long
        _tmpKey = _stmt.getLong(_itemKeyIndex)
        val _tmpRelation: MutableList<LangMapEntity>? = _map.get(_tmpKey)
        if (_tmpRelation != null) {
          val _item_1: LangMapEntity
          val _tmpLmeId: Long
          _tmpLmeId = _stmt.getLong(_columnIndexOfLmeId)
          val _tmpLmeTopParentType: LangMapEntity.TopParentType
          val _tmp: Int
          _tmp = _stmt.getLong(_columnIndexOfLmeTopParentType).toInt()
          _tmpLmeTopParentType = __sharedConverters.toLangMapTopParentType(_tmp)
          val _tmpLmeTopParentUid1: Long
          _tmpLmeTopParentUid1 = _stmt.getLong(_columnIndexOfLmeTopParentUid1)
          val _tmpLmeTopParentUid2: Long
          _tmpLmeTopParentUid2 = _stmt.getLong(_columnIndexOfLmeTopParentUid2)
          val _tmpLmePropType: LangMapEntity.PropType
          val _tmp_1: Int
          _tmp_1 = _stmt.getLong(_columnIndexOfLmePropType).toInt()
          _tmpLmePropType = __sharedConverters.toLangMapPropType(_tmp_1)
          val _tmpLmePropFk: Long
          _tmpLmePropFk = _stmt.getLong(_columnIndexOfLmePropFk)
          val _tmpLmeLang: String
          _tmpLmeLang = _stmt.getText(_columnIndexOfLmeLang)
          val _tmpLmeRegion: String?
          if (_stmt.isNull(_columnIndexOfLmeRegion)) {
            _tmpLmeRegion = null
          } else {
            _tmpLmeRegion = _stmt.getText(_columnIndexOfLmeRegion)
          }
          val _tmpLmeValue: String
          _tmpLmeValue = _stmt.getText(_columnIndexOfLmeValue)
          _item_1 = LangMapEntity(_tmpLmeId,_tmpLmeTopParentType,_tmpLmeTopParentUid1,_tmpLmeTopParentUid2,_tmpLmePropType,_tmpLmePropFk,_tmpLmeLang,_tmpLmeRegion,_tmpLmeValue)
          _tmpRelation.add(_item_1)
        }
      }
    } finally {
      _stmt.close()
    }
  }

  private fun __PropertyType_stringToEnum(_value: String): ReadiumLinkEntity.PropertyType = when (_value) {
    "LINK_ALTERNATE" -> ReadiumLinkEntity.PropertyType.LINK_ALTERNATE
    "LINK_CHILDREN" -> ReadiumLinkEntity.PropertyType.LINK_CHILDREN
    "LINK_SUB_COLLECTION" -> ReadiumLinkEntity.PropertyType.LINK_SUB_COLLECTION
    "OPDS_PUB_LINKS" -> ReadiumLinkEntity.PropertyType.OPDS_PUB_LINKS
    "OPDS_PUB_IMAGES" -> ReadiumLinkEntity.PropertyType.OPDS_PUB_IMAGES
    "OPDS_PUB_READING_ORDER" -> ReadiumLinkEntity.PropertyType.OPDS_PUB_READING_ORDER
    "OPDS_PUB_RESOURCES" -> ReadiumLinkEntity.PropertyType.OPDS_PUB_RESOURCES
    "OPDS_PUB_TOC" -> ReadiumLinkEntity.PropertyType.OPDS_PUB_TOC
    "OPDS_PUB_SUBJECT_LINKS" -> ReadiumLinkEntity.PropertyType.OPDS_PUB_SUBJECT_LINKS
    "OPDS_FACET_LINKS" -> ReadiumLinkEntity.PropertyType.OPDS_FACET_LINKS
    "OPDS_GROUP_LINKS" -> ReadiumLinkEntity.PropertyType.OPDS_GROUP_LINKS
    "OPDS_GROUP_NAVIGATION" -> ReadiumLinkEntity.PropertyType.OPDS_GROUP_NAVIGATION
    "OPDS_FEED_LINKS" -> ReadiumLinkEntity.PropertyType.OPDS_FEED_LINKS
    "OPDS_FEED_NAVIGATION" -> ReadiumLinkEntity.PropertyType.OPDS_FEED_NAVIGATION
    "READIUM_CONTRIBUTOR" -> ReadiumLinkEntity.PropertyType.READIUM_CONTRIBUTOR
    "READIUM_SUBJECT_LINKS" -> ReadiumLinkEntity.PropertyType.READIUM_SUBJECT_LINKS
    else -> throw IllegalArgumentException("Can't convert value to enum, unknown value: " + _value)
  }

  private fun __fetchRelationshipReadiumLinkEntityAsworldRespectDatalayerDbSchoolOpdsEntitiesReadiumLinkEntity(_connection: SQLiteConnection, _map: LongSparseArray<MutableList<ReadiumLinkEntity>>) {
    if (_map.isEmpty()) {
      return
    }
    if (_map.size() > 999) {
      recursiveFetchLongSparseArray(_map, true) { _tmpMap ->
        __fetchRelationshipReadiumLinkEntityAsworldRespectDatalayerDbSchoolOpdsEntitiesReadiumLinkEntity(_connection, _tmpMap)
      }
      return
    }
    val _stringBuilder: StringBuilder = StringBuilder()
    _stringBuilder.append("SELECT `rleId`,`rleOpdsParentType`,`rleOpdsParentUid`,`rlePropType`,`rlePropFk`,`rleIndex`,`rleHref`,`rleRel`,`rleType`,`rleTitle`,`rleTemplated`,`rleProperties`,`rleHeight`,`rleWidth`,`rleSize`,`rleBitrate`,`rleDuration`,`rleLanguage` FROM `ReadiumLinkEntity` WHERE `rleOpdsParentUid` 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, "rleOpdsParentUid")
      if (_itemKeyIndex == -1) {
        return
      }
      val _columnIndexOfRleId: Int = 0
      val _columnIndexOfRleOpdsParentType: Int = 1
      val _columnIndexOfRleOpdsParentUid: Int = 2
      val _columnIndexOfRlePropType: Int = 3
      val _columnIndexOfRlePropFk: Int = 4
      val _columnIndexOfRleIndex: Int = 5
      val _columnIndexOfRleHref: Int = 6
      val _columnIndexOfRleRel: Int = 7
      val _columnIndexOfRleType: Int = 8
      val _columnIndexOfRleTitle: Int = 9
      val _columnIndexOfRleTemplated: Int = 10
      val _columnIndexOfRleProperties: Int = 11
      val _columnIndexOfRleHeight: Int = 12
      val _columnIndexOfRleWidth: Int = 13
      val _columnIndexOfRleSize: Int = 14
      val _columnIndexOfRleBitrate: Int = 15
      val _columnIndexOfRleDuration: Int = 16
      val _columnIndexOfRleLanguage: Int = 17
      while (_stmt.step()) {
        val _tmpKey: Long
        _tmpKey = _stmt.getLong(_itemKeyIndex)
        val _tmpRelation: MutableList<ReadiumLinkEntity>? = _map.get(_tmpKey)
        if (_tmpRelation != null) {
          val _item_1: ReadiumLinkEntity
          val _tmpRleId: Long
          _tmpRleId = _stmt.getLong(_columnIndexOfRleId)
          val _tmpRleOpdsParentType: OpdsParentType
          val _tmp: Int
          _tmp = _stmt.getLong(_columnIndexOfRleOpdsParentType).toInt()
          _tmpRleOpdsParentType = __opdsTypeConverters.toOpdsTopParentType(_tmp)
          val _tmpRleOpdsParentUid: Long
          _tmpRleOpdsParentUid = _stmt.getLong(_columnIndexOfRleOpdsParentUid)
          val _tmpRlePropType: ReadiumLinkEntity.PropertyType
          _tmpRlePropType = __PropertyType_stringToEnum(_stmt.getText(_columnIndexOfRlePropType))
          val _tmpRlePropFk: Long
          _tmpRlePropFk = _stmt.getLong(_columnIndexOfRlePropFk)
          val _tmpRleIndex: Int
          _tmpRleIndex = _stmt.getLong(_columnIndexOfRleIndex).toInt()
          val _tmpRleHref: String
          _tmpRleHref = _stmt.getText(_columnIndexOfRleHref)
          val _tmpRleRel: List<String>?
          val _tmp_1: String?
          if (_stmt.isNull(_columnIndexOfRleRel)) {
            _tmp_1 = null
          } else {
            _tmp_1 = _stmt.getText(_columnIndexOfRleRel)
          }
          _tmpRleRel = __sharedConverters.toStringList(_tmp_1)
          val _tmpRleType: String?
          if (_stmt.isNull(_columnIndexOfRleType)) {
            _tmpRleType = null
          } else {
            _tmpRleType = _stmt.getText(_columnIndexOfRleType)
          }
          val _tmpRleTitle: String?
          if (_stmt.isNull(_columnIndexOfRleTitle)) {
            _tmpRleTitle = null
          } else {
            _tmpRleTitle = _stmt.getText(_columnIndexOfRleTitle)
          }
          val _tmpRleTemplated: Boolean?
          val _tmp_2: Int?
          if (_stmt.isNull(_columnIndexOfRleTemplated)) {
            _tmp_2 = null
          } else {
            _tmp_2 = _stmt.getLong(_columnIndexOfRleTemplated).toInt()
          }
          _tmpRleTemplated = _tmp_2?.let { it != 0 }
          val _tmpRleProperties: String?
          if (_stmt.isNull(_columnIndexOfRleProperties)) {
            _tmpRleProperties = null
          } else {
            _tmpRleProperties = _stmt.getText(_columnIndexOfRleProperties)
          }
          val _tmpRleHeight: Int?
          if (_stmt.isNull(_columnIndexOfRleHeight)) {
            _tmpRleHeight = null
          } else {
            _tmpRleHeight = _stmt.getLong(_columnIndexOfRleHeight).toInt()
          }
          val _tmpRleWidth: Int?
          if (_stmt.isNull(_columnIndexOfRleWidth)) {
            _tmpRleWidth = null
          } else {
            _tmpRleWidth = _stmt.getLong(_columnIndexOfRleWidth).toInt()
          }
          val _tmpRleSize: Int?
          if (_stmt.isNull(_columnIndexOfRleSize)) {
            _tmpRleSize = null
          } else {
            _tmpRleSize = _stmt.getLong(_columnIndexOfRleSize).toInt()
          }
          val _tmpRleBitrate: Double?
          if (_stmt.isNull(_columnIndexOfRleBitrate)) {
            _tmpRleBitrate = null
          } else {
            _tmpRleBitrate = _stmt.getDouble(_columnIndexOfRleBitrate)
          }
          val _tmpRleDuration: Double?
          if (_stmt.isNull(_columnIndexOfRleDuration)) {
            _tmpRleDuration = null
          } else {
            _tmpRleDuration = _stmt.getDouble(_columnIndexOfRleDuration)
          }
          val _tmpRleLanguage: List<String>?
          val _tmp_3: String?
          if (_stmt.isNull(_columnIndexOfRleLanguage)) {
            _tmp_3 = null
          } else {
            _tmp_3 = _stmt.getText(_columnIndexOfRleLanguage)
          }
          _tmpRleLanguage = __sharedConverters.toStringList(_tmp_3)
          _item_1 = ReadiumLinkEntity(_tmpRleId,_tmpRleOpdsParentType,_tmpRleOpdsParentUid,_tmpRlePropType,_tmpRlePropFk,_tmpRleIndex,_tmpRleHref,_tmpRleRel,_tmpRleType,_tmpRleTitle,_tmpRleTemplated,_tmpRleProperties,_tmpRleHeight,_tmpRleWidth,_tmpRleSize,_tmpRleBitrate,_tmpRleDuration,_tmpRleLanguage)
          _tmpRelation.add(_item_1)
        }
      }
    } finally {
      _stmt.close()
    }
  }

  private fun __fetchRelationshipOpdsPublicationEntityAsworldRespectDatalayerDbSchoolAdaptersOpdsPublicationEntities(_connection: SQLiteConnection, _map: LongSparseArray<OpdsPublicationEntities?>) {
    if (_map.isEmpty()) {
      return
    }
    if (_map.size() > 999) {
      recursiveFetchLongSparseArray(_map, false) { _tmpMap ->
        __fetchRelationshipOpdsPublicationEntityAsworldRespectDatalayerDbSchoolAdaptersOpdsPublicationEntities(_connection, _tmpMap)
      }
      return
    }
    val _stringBuilder: StringBuilder = StringBuilder()
    _stringBuilder.append("SELECT `opeUid`,`opeOfeUid`,`opeOgeUid`,`opeIndex`,`opeUrl`,`opeUrlHash`,`opeLastModified`,`opeEtag`,`opeMdIdentifier`,`opeMdLanguage`,`opeMdType`,`opeMdDescription`,`opeMdNumberOfPages`,`opeMdDuration` FROM `OpdsPublicationEntity` WHERE `opeUrlHash` 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, "opeUrlHash")
      if (_itemKeyIndex == -1) {
        return
      }
      val _columnIndexOfOpeUid: Int = 0
      val _columnIndexOfOpeOfeUid: Int = 1
      val _columnIndexOfOpeOgeUid: Int = 2
      val _columnIndexOfOpeIndex: Int = 3
      val _columnIndexOfOpeUrl: Int = 4
      val _columnIndexOfOpeUrlHash: Int = 5
      val _columnIndexOfOpeLastModified: Int = 6
      val _columnIndexOfOpeEtag: Int = 7
      val _columnIndexOfOpeMdIdentifier: Int = 8
      val _columnIndexOfOpeMdLanguage: Int = 9
      val _columnIndexOfOpeMdType: Int = 10
      val _columnIndexOfOpeMdDescription: Int = 11
      val _columnIndexOfOpeMdNumberOfPages: Int = 12
      val _columnIndexOfOpeMdDuration: Int = 13
      val _collectionLangMaps: LongSparseArray<MutableList<LangMapEntity>> = LongSparseArray<MutableList<LangMapEntity>>()
      val _collectionLinks: LongSparseArray<MutableList<ReadiumLinkEntity>> = LongSparseArray<MutableList<ReadiumLinkEntity>>()
      while (_stmt.step()) {
        val _tmpKey: Long
        _tmpKey = _stmt.getLong(_columnIndexOfOpeUid)
        if (!_collectionLangMaps.containsKey(_tmpKey)) {
          _collectionLangMaps.put(_tmpKey, mutableListOf())
        }
        val _tmpKey_1: Long
        _tmpKey_1 = _stmt.getLong(_columnIndexOfOpeUid)
        if (!_collectionLinks.containsKey(_tmpKey_1)) {
          _collectionLinks.put(_tmpKey_1, mutableListOf())
        }
      }
      _stmt.reset()
      __fetchRelationshipLangMapEntityAsworldRespectDatalayerDbSharedEntitiesLangMapEntity(_connection, _collectionLangMaps)
      __fetchRelationshipReadiumLinkEntityAsworldRespectDatalayerDbSchoolOpdsEntitiesReadiumLinkEntity(_connection, _collectionLinks)
      while (_stmt.step()) {
        val _tmpKey_2: Long
        _tmpKey_2 = _stmt.getLong(_itemKeyIndex)
        if (_map.containsKey(_tmpKey_2)) {
          val _item_1: OpdsPublicationEntities?
          val _tmpPublication: OpdsPublicationEntity
          val _tmpOpeUid: Long
          _tmpOpeUid = _stmt.getLong(_columnIndexOfOpeUid)
          val _tmpOpeOfeUid: Long
          _tmpOpeOfeUid = _stmt.getLong(_columnIndexOfOpeOfeUid)
          val _tmpOpeOgeUid: Long
          _tmpOpeOgeUid = _stmt.getLong(_columnIndexOfOpeOgeUid)
          val _tmpOpeIndex: Int
          _tmpOpeIndex = _stmt.getLong(_columnIndexOfOpeIndex).toInt()
          val _tmpOpeUrl: Url?
          val _tmp: String?
          if (_stmt.isNull(_columnIndexOfOpeUrl)) {
            _tmp = null
          } else {
            _tmp = _stmt.getText(_columnIndexOfOpeUrl)
          }
          _tmpOpeUrl = __sharedConverters.toUrl(_tmp)
          val _tmpOpeUrlHash: Long
          _tmpOpeUrlHash = _stmt.getLong(_columnIndexOfOpeUrlHash)
          val _tmpOpeLastModified: Long
          _tmpOpeLastModified = _stmt.getLong(_columnIndexOfOpeLastModified)
          val _tmpOpeEtag: String?
          if (_stmt.isNull(_columnIndexOfOpeEtag)) {
            _tmpOpeEtag = null
          } else {
            _tmpOpeEtag = _stmt.getText(_columnIndexOfOpeEtag)
          }
          val _tmpOpeMdIdentifier: Uri?
          val _tmp_1: String?
          if (_stmt.isNull(_columnIndexOfOpeMdIdentifier)) {
            _tmp_1 = null
          } else {
            _tmp_1 = _stmt.getText(_columnIndexOfOpeMdIdentifier)
          }
          _tmpOpeMdIdentifier = __sharedConverters.toUri(_tmp_1)
          val _tmpOpeMdLanguage: List<String>?
          val _tmp_2: String?
          if (_stmt.isNull(_columnIndexOfOpeMdLanguage)) {
            _tmp_2 = null
          } else {
            _tmp_2 = _stmt.getText(_columnIndexOfOpeMdLanguage)
          }
          _tmpOpeMdLanguage = __sharedConverters.toStringList(_tmp_2)
          val _tmpOpeMdType: Uri?
          val _tmp_3: String?
          if (_stmt.isNull(_columnIndexOfOpeMdType)) {
            _tmp_3 = null
          } else {
            _tmp_3 = _stmt.getText(_columnIndexOfOpeMdType)
          }
          _tmpOpeMdType = __sharedConverters.toUri(_tmp_3)
          val _tmpOpeMdDescription: String?
          if (_stmt.isNull(_columnIndexOfOpeMdDescription)) {
            _tmpOpeMdDescription = null
          } else {
            _tmpOpeMdDescription = _stmt.getText(_columnIndexOfOpeMdDescription)
          }
          val _tmpOpeMdNumberOfPages: Int?
          if (_stmt.isNull(_columnIndexOfOpeMdNumberOfPages)) {
            _tmpOpeMdNumberOfPages = null
          } else {
            _tmpOpeMdNumberOfPages = _stmt.getLong(_columnIndexOfOpeMdNumberOfPages).toInt()
          }
          val _tmpOpeMdDuration: Double?
          if (_stmt.isNull(_columnIndexOfOpeMdDuration)) {
            _tmpOpeMdDuration = null
          } else {
            _tmpOpeMdDuration = _stmt.getDouble(_columnIndexOfOpeMdDuration)
          }
          _tmpPublication = OpdsPublicationEntity(_tmpOpeUid,_tmpOpeOfeUid,_tmpOpeOgeUid,_tmpOpeIndex,_tmpOpeUrl,_tmpOpeUrlHash,_tmpOpeLastModified,_tmpOpeEtag,_tmpOpeMdIdentifier,_tmpOpeMdLanguage,_tmpOpeMdType,_tmpOpeMdDescription,_tmpOpeMdNumberOfPages,_tmpOpeMdDuration)
          val _tmpLangMapsCollection: MutableList<LangMapEntity>
          val _tmpKey_3: Long
          _tmpKey_3 = _stmt.getLong(_columnIndexOfOpeUid)
          _tmpLangMapsCollection = checkNotNull(_collectionLangMaps.get(_tmpKey_3))
          val _tmpLinksCollection: MutableList<ReadiumLinkEntity>
          val _tmpKey_4: Long
          _tmpKey_4 = _stmt.getLong(_columnIndexOfOpeUid)
          _tmpLinksCollection = checkNotNull(_collectionLinks.get(_tmpKey_4))
          _item_1 = OpdsPublicationEntities(_tmpPublication,_tmpLangMapsCollection,_tmpLinksCollection)
          _map.put(_tmpKey_2, _item_1)
        }
      }
    } finally {
      _stmt.close()
    }
  }

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