commonMain.com.ditchoom.modelsv4.Mqtt4Impl.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mqtt-4-models Show documentation
Show all versions of mqtt-4-models Show documentation
Defines the MQTT 3 and 4 control packets
package com.ditchoom.modelsv4
import app.cash.sqldelight.TransacterImpl
import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlSchema
import com.ditchoom.Mqtt4
import com.ditchoom.mqtt3.persistence.BrokerQueries
import com.ditchoom.mqtt3.persistence.ConnectionRequestQueries
import com.ditchoom.mqtt3.persistence.PublishMessageQueries
import com.ditchoom.mqtt3.persistence.QoS2MessagesQueries
import com.ditchoom.mqtt3.persistence.SocketConnectionQueries
import com.ditchoom.mqtt3.persistence.SubscriptionQueries
import com.ditchoom.mqtt3.persistence.SubscriptionRequestQueries
import com.ditchoom.mqtt3.persistence.UnsubscribeRequestQueries
import kotlin.Int
import kotlin.Unit
import kotlin.reflect.KClass
internal val KClass.schema: SqlSchema
get() = Mqtt4Impl.Schema
internal fun KClass.newInstance(driver: SqlDriver): Mqtt4 = Mqtt4Impl(driver)
private class Mqtt4Impl(
driver: SqlDriver,
) : TransacterImpl(driver), Mqtt4 {
public override val brokerQueries: BrokerQueries = BrokerQueries(driver)
public override val connectionRequestQueries: ConnectionRequestQueries =
ConnectionRequestQueries(driver)
public override val publishMessageQueries: PublishMessageQueries = PublishMessageQueries(driver)
public override val qoS2MessagesQueries: QoS2MessagesQueries = QoS2MessagesQueries(driver)
public override val socketConnectionQueries: SocketConnectionQueries =
SocketConnectionQueries(driver)
public override val subscriptionQueries: SubscriptionQueries = SubscriptionQueries(driver)
public override val subscriptionRequestQueries: SubscriptionRequestQueries =
SubscriptionRequestQueries(driver)
public override val unsubscribeRequestQueries: UnsubscribeRequestQueries =
UnsubscribeRequestQueries(driver)
public object Schema : SqlSchema {
public override val version: Int
get() = 1
public override fun create(driver: SqlDriver): QueryResult {
driver.execute(null, """
|CREATE TABLE IF NOT EXISTS Broker (
| id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
| next_packet_id INTEGER NOT NULL DEFAULT 1 CHECK(next_packet_id BETWEEN 0 AND 65535)
|)
""".trimMargin(), 0)
driver.execute(null, """
|CREATE TABLE IF NOT EXISTS ConnectionRequest (
| broker_id INTEGER NOT NULL UNIQUE,
| protocol_name TEXT NOT NULL DEFAULT 'MQTT',
| protocol_level INTEGER NOT NULL DEFAULT 4 CHECK(protocol_level BETWEEN 0 AND 255),
| will_retain INTEGER NOT NULL DEFAULT 0 CHECK(will_retain BETWEEN 0 AND 1),
| will_qos INTEGER NOT NULL DEFAULT 0 CHECK(will_qos BETWEEN 0 AND 2),
| will_flag INTEGER NOT NULL DEFAULT 0 CHECK(will_flag BETWEEN 0 AND 1),
| clean_session INTEGER NOT NULL DEFAULT 0 CHECK(clean_session BETWEEN 0 AND 1),
| keep_alive_seconds INTEGER NOT NULL DEFAULT 3600 CHECK(keep_alive_seconds BETWEEN 0 AND 65535),
| client_id TEXT NOT NULL,
| will_topic TEXT,
| will_payload BLOB,
| username TEXT,
| password TEXT,
| FOREIGN KEY(broker_id) REFERENCES Broker(id) ON DELETE CASCADE
|)
""".trimMargin(), 0)
driver.execute(null, """
|CREATE TABLE IF NOT EXISTS PublishMessage (
| broker_id INTEGER NOT NULL,
| incoming INTEGER NOT NULL DEFAULT 0 CHECK(incoming BETWEEN 0 AND 1),
|-- fixed header
| dup INTEGER NOT NULL DEFAULT 0 CHECK(dup BETWEEN 0 AND 1),
| qos INTEGER NOT NULL DEFAULT 1 CHECK(qos BETWEEN 0 AND 2), -- cannot be 0 if we are persisting
| retain INTEGER NOT NULL DEFAULT 0 CHECK(retain BETWEEN 0 AND 1),
|-- variable header
| topic_name TEXT NOT NULL,
| packet_id INTEGER NOT NULL CHECK(packet_id BETWEEN 0 AND 65535),
|-- payload
| payload BLOB,
| FOREIGN KEY(broker_id) REFERENCES Broker(id) ON DELETE CASCADE,
| PRIMARY KEY (broker_id, incoming, packet_id)
|)
""".trimMargin(), 0)
driver.execute(null, """
|CREATE TABLE IF NOT EXISTS Qos2Messages (
| broker_id INTEGER NOT NULL,
| incoming INTEGER NOT NULL DEFAULT 0 CHECK(incoming BETWEEN 0 AND 1),
| packet_id INTEGER NOT NULL CHECK(packet_id BETWEEN 0 AND 65535),
| type INTEGER NOT NULL CHECK(type BETWEEN 5 AND 7),
| FOREIGN KEY(broker_id) REFERENCES Broker(id) ON DELETE CASCADE,
| PRIMARY KEY (broker_id, incoming, packet_id, type)
|)
""".trimMargin(), 0)
driver.execute(null, """
|CREATE TABLE IF NOT EXISTS SocketConnection (
| id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
| broker_id INTEGER NOT NULL,
| type TEXT NOT NULL DEFAULT 'tcp' CHECK(type LIKE 'tcp' OR type LIKE 'websocket'),
| host TEXT NOT NULL,
| port INTEGER NOT NULL CHECK(port BETWEEN 1 AND 65535),
| tls INTEGER NOT NULL CHECK(tls BETWEEN 0 AND 1),
| connection_timeout_ms INTEGER NOT NULL CHECK(connection_timeout_ms > 0),
| read_timeout_ms INTEGER NOT NULL CHECK(read_timeout_ms > 0),
| write_timeout_ms INTEGER NOT NULL CHECK(write_timeout_ms > 0),
| websocket_endpoint TEXT,
| websocket_protocols TEXT,
| FOREIGN KEY(broker_id) REFERENCES Broker(id) ON DELETE CASCADE
|)
""".trimMargin(), 0)
driver.execute(null, """
|CREATE TABLE IF NOT EXISTS Subscription (
| broker_id INTEGER NOT NULL,
| subscribe_packet_id INTEGER NOT NULL CHECK(subscribe_packet_id BETWEEN 0 AND 65535),
| unsubscribe_packet_id INTEGER NOT NULL CHECK(unsubscribe_packet_id BETWEEN 0 AND 65535),
| topic_filter TEXT NOT NULL,
| qos INTEGER NOT NULL DEFAULT 0 CHECK(qos BETWEEN 0 AND 2),
| FOREIGN KEY(broker_id) REFERENCES Broker(id) ON DELETE CASCADE,
| PRIMARY KEY (broker_id,subscribe_packet_id, topic_filter)
|)
""".trimMargin(), 0)
driver.execute(null, """
|CREATE TABLE IF NOT EXISTS SubscribeRequest(
| broker_id INTEGER NOT NULL,
| packet_id INTEGER NOT NULL CHECK(packet_id BETWEEN 0 AND 65535),
| FOREIGN KEY(broker_id) REFERENCES Broker(id) ON DELETE CASCADE,
| PRIMARY KEY (broker_id,packet_id)
|)
""".trimMargin(), 0)
driver.execute(null, """
|CREATE TABLE IF NOT EXISTS UnsubscribeRequest(
| broker_id INTEGER NOT NULL,
| packet_id INTEGER NOT NULL CHECK(packet_id BETWEEN 0 AND 65535),
| FOREIGN KEY(broker_id) REFERENCES Broker(id) ON DELETE CASCADE,
| PRIMARY KEY (broker_id,packet_id)
|)
""".trimMargin(), 0)
return QueryResult.Unit
}
public override fun migrate(
driver: SqlDriver,
oldVersion: Int,
newVersion: Int,
): QueryResult = QueryResult.Unit
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy