package com.ustadmobile.libcache.db.dao

import androidx.room.EntityInsertAdapter
import androidx.room.RoomDatabase
import androidx.room.util.getColumnIndexOrThrow
import androidx.room.util.performSuspending
import androidx.sqlite.SQLiteStatement
import com.ustadmobile.libcache.db.entities.CacheEntryExtraHeaders
import javax.`annotation`.processing.Generated
import kotlin.Int
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

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

  private val __insertAdapterOfCacheEntryExtraHeaders: EntityInsertAdapter<CacheEntryExtraHeaders>
  init {
    this.__db = __db
    this.__insertAdapterOfCacheEntryExtraHeaders = object : EntityInsertAdapter<CacheEntryExtraHeaders>() {
      protected override fun createQuery(): String = "INSERT OR REPLACE INTO `CacheEntryExtraHeaders` (`ceehKey`,`ceehUrl`,`extraHeaders`) VALUES (?,?,?)"

      protected override fun bind(statement: SQLiteStatement, entity: CacheEntryExtraHeaders) {
        statement.bindText(1, entity.ceehKey)
        statement.bindText(2, entity.ceehUrl)
        statement.bindText(3, entity.extraHeaders)
      }
    }
  }

  public override suspend fun upsertList(cacheEntryExtraHeaders: List<CacheEntryExtraHeaders>): Unit = performSuspending(__db, false, true) { _connection ->
    __insertAdapterOfCacheEntryExtraHeaders.insert(_connection, cacheEntryExtraHeaders)
  }

  public override suspend fun findByRequestBatchId(batchId: Int): List<CacheEntryExtraHeaders> {
    val _sql: String = """
        |
        |        SELECT CacheEntryExtraHeaders.*
        |          FROM CacheEntryExtraHeaders
        |         WHERE CacheEntryExtraHeaders.ceehKey IN
        |               (SELECT RequestedEntry.requestedKey
        |                  FROM RequestedEntry
        |                 WHERE RequestedEntry.batchId = ?)
        |    
        """.trimMargin()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindLong(_argIndex, batchId.toLong())
        val _columnIndexOfCeehKey: Int = getColumnIndexOrThrow(_stmt, "ceehKey")
        val _columnIndexOfCeehUrl: Int = getColumnIndexOrThrow(_stmt, "ceehUrl")
        val _columnIndexOfExtraHeaders: Int = getColumnIndexOrThrow(_stmt, "extraHeaders")
        val _result: MutableList<CacheEntryExtraHeaders> = mutableListOf()
        while (_stmt.step()) {
          val _item: CacheEntryExtraHeaders
          val _tmpCeehKey: String
          _tmpCeehKey = _stmt.getText(_columnIndexOfCeehKey)
          val _tmpCeehUrl: String
          _tmpCeehUrl = _stmt.getText(_columnIndexOfCeehUrl)
          val _tmpExtraHeaders: String
          _tmpExtraHeaders = _stmt.getText(_columnIndexOfExtraHeaders)
          _item = CacheEntryExtraHeaders(_tmpCeehKey,_tmpCeehUrl,_tmpExtraHeaders)
          _result.add(_item)
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

  public override suspend fun findByKey(urlKey: String): CacheEntryExtraHeaders? {
    val _sql: String = """
        |
        |        SELECT CacheEntryExtraHeaders.*
        |          FROM CacheEntryExtraHeaders
        |         WHERE CacheEntryExtraHeaders.ceehKey = ?
        |    
        """.trimMargin()
    return performSuspending(__db, true, false) { _connection ->
      val _stmt: SQLiteStatement = _connection.prepare(_sql)
      try {
        var _argIndex: Int = 1
        _stmt.bindText(_argIndex, urlKey)
        val _columnIndexOfCeehKey: Int = getColumnIndexOrThrow(_stmt, "ceehKey")
        val _columnIndexOfCeehUrl: Int = getColumnIndexOrThrow(_stmt, "ceehUrl")
        val _columnIndexOfExtraHeaders: Int = getColumnIndexOrThrow(_stmt, "extraHeaders")
        val _result: CacheEntryExtraHeaders?
        if (_stmt.step()) {
          val _tmpCeehKey: String
          _tmpCeehKey = _stmt.getText(_columnIndexOfCeehKey)
          val _tmpCeehUrl: String
          _tmpCeehUrl = _stmt.getText(_columnIndexOfCeehUrl)
          val _tmpExtraHeaders: String
          _tmpExtraHeaders = _stmt.getText(_columnIndexOfExtraHeaders)
          _result = CacheEntryExtraHeaders(_tmpCeehKey,_tmpCeehUrl,_tmpExtraHeaders)
        } else {
          _result = null
        }
        _result
      } finally {
        _stmt.close()
      }
    }
  }

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