Removed limbo in favor of files with document_id null, adds document creation.

This commit is contained in:
Stefan Schallerl 2025-02-02 23:53:58 +01:00
parent fae33db529
commit 3caa52d82c
2 changed files with 52 additions and 32 deletions

View file

@ -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<LimboFile> {
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>
): 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
}
}
}

View file

@ -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)
);