From 3caa52d82c9faf7675ff4cd90fec3fe02fcb7d15 Mon Sep 17 00:00:00 2001 From: Stefan Schallerl Date: Sun, 2 Feb 2025 23:53:58 +0100 Subject: [PATCH] Removed limbo in favor of files with document_id null, adds document creation. --- .../filemure/repository/SqliteRepository.kt | 55 +++++++++++++++---- app/src/main/resources/create_db.sql | 29 +++------- 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/app/src/main/kotlin/net/h34t/filemure/repository/SqliteRepository.kt b/app/src/main/kotlin/net/h34t/filemure/repository/SqliteRepository.kt index 53f810b..1277e11 100644 --- a/app/src/main/kotlin/net/h34t/filemure/repository/SqliteRepository.kt +++ b/app/src/main/kotlin/net/h34t/filemure/repository/SqliteRepository.kt @@ -15,7 +15,7 @@ class SqliteRepository(url: String) { val connection: Connection = DriverManager.getConnection(url) fun addFileToLimbo(accountId: Long, filename: String, contentType: String?, size: Long, content: InputStream) { - connection.prepareStatement("INSERT INTO limbo (account_id, ext_id, filename, content_type, file_size, created, content) VALUES (?,?,?,?,?,datetime(),?)") + connection.prepareStatement("INSERT INTO file (account_id, ext_id, filename, content_type, file_size, content) VALUES (?,?,?,?,?,?)") .use { stmt -> stmt.setLong(1, accountId) stmt.setString(2, generateExtId()) @@ -31,16 +31,17 @@ class SqliteRepository(url: String) { } fun getLimboFileCount(accountId: Long): Long { - connection.prepareStatement("SELECT count(*) as count FROM limbo WHERE account_id=?").use { stmt -> - stmt.setLong(1, accountId) - val rs = stmt.executeQuery() - rs.next() - return rs.getLong(1) - } + connection.prepareStatement("SELECT count(*) AS count FROM file WHERE account_id=? AND document_id IS NULL") + .use { stmt -> + stmt.setLong(1, accountId) + val rs = stmt.executeQuery() + rs.next() + return rs.getLong(1) + } } fun getFilesInLimbo(accountId: Long): List { - connection.prepareStatement("SELECT id, account_id, filename, content_type, file_size, created FROM limbo WHERE account_id=? ORDER BY created DESC") + connection.prepareStatement("SELECT id, account_id, filename, content_type, file_size, created FROM file WHERE account_id=? AND document_id IS NULL ORDER BY created DESC") .use { stmt -> stmt.setLong(1, accountId) val res = stmt.executeQuery() @@ -70,7 +71,41 @@ class SqliteRepository(url: String) { description: String, fileIds: List ): Long { - TODO("Not implemented") - return 1L; + val savePoint = connection.setSavepoint() + try { + val documentId = connection.prepareStatement( + """INSERT INTO document + |(account_id, ext_id, title, description, tags, created, reference_date) + |VALUES + |(?, ?, ?, ?, ?, datetime(), ?)""".trimMargin() + ).use { stmt -> + stmt.setLong(1, accountId) + stmt.setString(2, generateExtId()) + stmt.setString(3, title) + stmt.setString(4, description) + stmt.setString(5, tags.joinToString(",")) + stmt.setString(6, referenceDate.format(dtf)) + stmt.executeUpdate() + val gks = stmt.generatedKeys + gks.next() + gks.getLong(1) + } + + val ids = fileIds.joinToString(",") + + connection.prepareStatement("""UPDATE file SET document_id=? WHERE account_id=? AND id IN ($ids)""") + .use { stmt -> + stmt.setLong(1, documentId) + stmt.setLong(2, accountId) + val affected = stmt.executeUpdate() + require(affected == fileIds.size) + } + + connection.commit() + return documentId + } catch (exception: Exception) { + connection.rollback(savePoint) + throw exception + } } } \ No newline at end of file diff --git a/app/src/main/resources/create_db.sql b/app/src/main/resources/create_db.sql index 264f129..8c9022c 100644 --- a/app/src/main/resources/create_db.sql +++ b/app/src/main/resources/create_db.sql @@ -4,7 +4,7 @@ CREATE TABLE account ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, email TEXT NOT NULL, password TEXT NOT NULL, - created TEXT NOT NULL, + created TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, unique(email) ); @@ -17,40 +17,25 @@ CREATE TABLE document ( title TEXT NOT NULL, description TEXT NOT NULL, tags TEXT NOT NULL, - created TEXT NOT NULL, + created TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, reference_date TEXT, CONSTRAINT document_account_FK FOREIGN KEY (account_id) REFERENCES account(id) ON DELETE CASCADE, unique(ext_id) ); --- limbo definition - -CREATE TABLE limbo ( - id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - account_id INTEGER NOT NULL, - ext_id TEXT NOT NULL, - filename TEXT NOT NULL, - content_type TEXT, - file_size INTEGER NOT NULL, - content BLOB NOT NULL, - created TEXT NOT NULL, - unique(ext_id), - CONSTRAINT limbo_account_FK FOREIGN KEY (account_id) REFERENCES account(id) ON DELETE CASCADE, -); - - -- file definition - CREATE TABLE file ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - document_id INTEGER NOT NULL, + account_id INTEGER NOT NULL, + document_id INTEGER DEFAULT null, ext_id TEXT NOT NULL, filename TEXT NOT NULL, + file_size INTEGER NOT NULL, content BLOB NOT NULL, content_type TEXT, content_extracted TEXT, - created TEXT NOT NULL, + created TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT file_account_FK FOREIGN KEY (account_id) REFERENCES account(id) ON DELETE CASCADE, CONSTRAINT file_document_FK FOREIGN KEY (document_id) REFERENCES document(id) ON DELETE CASCADE, unique(ext_id) ); -