All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.ditchoom.mqtt3.persistence.BrokerQueries.kt Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package com.ditchoom.mqtt3.persistence

import app.cash.sqldelight.ExecutableQuery
import app.cash.sqldelight.Query
import app.cash.sqldelight.TransacterImpl
import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlCursor
import app.cash.sqldelight.db.SqlDriver
import kotlin.Any
import kotlin.Long
import kotlin.String
import kotlin.Unit

public class BrokerQueries(
  driver: SqlDriver,
) : TransacterImpl(driver) {
  public fun lastRowId(): ExecutableQuery = Query(-572164522, driver, "Broker.sq",
      "lastRowId", "SELECT last_insert_rowid()") { cursor ->
    cursor.getLong(0)!!
  }

  public fun  allBrokers(mapper: (id: Long, next_packet_id: Long) -> T): Query =
      Query(-922191102, arrayOf("Broker"), driver, "Broker.sq", "allBrokers",
      "SELECT * FROM Broker") { cursor ->
    mapper(
      cursor.getLong(0)!!,
      cursor.getLong(1)!!
    )
  }

  public fun allBrokers(): Query = allBrokers { id, next_packet_id ->
    Broker(
      id,
      next_packet_id
    )
  }

  public fun nextPacketId(id: Long): Query = NextPacketIdQuery(id) { cursor ->
    cursor.getLong(0)!!
  }

  public fun insertBroker(): Unit {
    driver.execute(-959983141, """INSERT INTO Broker VALUES(NULL, 1)""", 0)
    notifyQueries(-959983141) { emit ->
      emit("Broker")
    }
  }

  public fun deleteBroker(id: Long): Unit {
    driver.execute(1820858189, """DELETE FROM Broker WHERE id = ?""", 1) {
          bindLong(0, id)
        }
    notifyQueries(1820858189) { emit ->
      emit("Broker")
      emit("ConnectionRequest")
      emit("PublishMessage")
      emit("Qos2Messages")
      emit("SocketConnection")
      emit("SubscribeRequest")
      emit("Subscription")
      emit("UnsubscribeRequest")
    }
  }

  public fun incrementPacketId(id: Long): Unit {
    driver.execute(-1815555063,
        """UPDATE Broker SET next_packet_id = (next_packet_id % 65535) + 1 WHERE id = ?""", 1) {
          bindLong(0, id)
        }
    notifyQueries(-1815555063) { emit ->
      emit("Broker")
    }
  }

  private inner class NextPacketIdQuery(
    public val id: Long,
    mapper: (SqlCursor) -> T,
  ) : Query(mapper) {
    public override fun addListener(listener: Query.Listener): Unit {
      driver.addListener(listener, arrayOf("Broker"))
    }

    public override fun removeListener(listener: Query.Listener): Unit {
      driver.removeListener(listener, arrayOf("Broker"))
    }

    public override fun  execute(mapper: (SqlCursor) -> R): QueryResult =
        driver.executeQuery(-1517160001, """SELECT next_packet_id FROM Broker WHERE id = ?""",
        mapper, 1) {
      bindLong(0, id)
    }

    public override fun toString(): String = "Broker.sq:nextPacketId"
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy