From 3932debd927acce6282148b986c65c04bb9fc546 Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 8 Oct 2023 16:18:50 +0100 Subject: [PATCH] Add artifact_sources table Signed-off-by: Graham --- .../openrs2/archive/client/ClientImporter.kt | 27 +++++++++++++++---- .../openrs2/archive/client/ImportCommand.kt | 7 ++++- .../migrations/V23__client_sources.sql | 11 ++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 archive/src/main/resources/org/openrs2/archive/migrations/V23__client_sources.sql diff --git a/archive/src/main/kotlin/org/openrs2/archive/client/ClientImporter.kt b/archive/src/main/kotlin/org/openrs2/archive/client/ClientImporter.kt index 3f5f980e..55f3863a 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/client/ClientImporter.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/client/ClientImporter.kt @@ -60,7 +60,7 @@ public class ClientImporter @Inject constructor( private val packClassLibraryReader: PackClassLibraryReader, private val importer: CacheImporter ) { - public suspend fun import(paths: Iterable) { + public suspend fun import(paths: Iterable, name: String?, description: String?, url: String?) { alloc.buffer().use { buf -> for (path in paths) { buf.clear() @@ -72,19 +72,34 @@ public class ClientImporter @Inject constructor( } logger.info { "Importing $path" } - import(parse(buf)) + import(parse(buf), name, description, url) } } } - public suspend fun import(artifact: Artifact) { + public suspend fun import(artifact: Artifact, name: String?, description: String?, url: String?) { database.execute { connection -> importer.prepare(connection) - import(connection, artifact) + + val id = import(connection, artifact) + + connection.prepareStatement( + """ + INSERT INTO artifact_sources (blob_id, name, description, url) + VALUES (?, ?, ?, ?) + """.trimIndent() + ).use { stmt -> + stmt.setLong(1, id) + stmt.setString(2, name) + stmt.setString(3, description) + stmt.setString(4, url) + + stmt.execute() + } } } - private fun import(connection: Connection, artifact: Artifact) { + private fun import(connection: Connection, artifact: Artifact): Long { val id = importer.addBlob(connection, artifact) val gameId = connection.prepareStatement( @@ -187,6 +202,8 @@ public class ClientImporter @Inject constructor( stmt.executeBatch() } + + return id } public suspend fun refresh() { diff --git a/archive/src/main/kotlin/org/openrs2/archive/client/ImportCommand.kt b/archive/src/main/kotlin/org/openrs2/archive/client/ImportCommand.kt index 64a8b62a..e765e111 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/client/ImportCommand.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/client/ImportCommand.kt @@ -3,6 +3,7 @@ package org.openrs2.archive.client import com.github.ajalt.clikt.core.CliktCommand import com.github.ajalt.clikt.parameters.arguments.argument import com.github.ajalt.clikt.parameters.arguments.multiple +import com.github.ajalt.clikt.parameters.options.option import com.github.ajalt.clikt.parameters.types.path import com.google.inject.Guice import kotlinx.coroutines.runBlocking @@ -10,6 +11,10 @@ import org.openrs2.archive.ArchiveModule import org.openrs2.inject.CloseableInjector public class ImportCommand : CliktCommand(name = "import") { + private val name by option() + private val description by option() + private val url by option() + private val input by argument().path( mustExist = true, canBeDir = false, @@ -19,7 +24,7 @@ public class ImportCommand : CliktCommand(name = "import") { override fun run(): Unit = runBlocking { CloseableInjector(Guice.createInjector(ArchiveModule)).use { injector -> val importer = injector.getInstance(ClientImporter::class.java) - importer.import(input) + importer.import(input, name, description, url) } } } diff --git a/archive/src/main/resources/org/openrs2/archive/migrations/V23__client_sources.sql b/archive/src/main/resources/org/openrs2/archive/migrations/V23__client_sources.sql new file mode 100644 index 00000000..918f2b4e --- /dev/null +++ b/archive/src/main/resources/org/openrs2/archive/migrations/V23__client_sources.sql @@ -0,0 +1,11 @@ +-- @formatter:off + +CREATE TABLE artifact_sources ( + id SERIAL PRIMARY KEY NOT NULL, + blob_id BIGINT NOT NULL REFERENCES artifacts (blob_id), + name TEXT NULL, + description TEXT NULL, + url TEXT NULL +); + +CREATE INDEX ON artifact_sources (blob_id);