Removed limbo in favor of files with document_id null, adds document creation.
This commit is contained in:
parent
fae33db529
commit
3caa52d82c
2 changed files with 52 additions and 32 deletions
|
@ -15,7 +15,7 @@ class SqliteRepository(url: String) {
|
||||||
val connection: Connection = DriverManager.getConnection(url)
|
val connection: Connection = DriverManager.getConnection(url)
|
||||||
|
|
||||||
fun addFileToLimbo(accountId: Long, filename: String, contentType: String?, size: Long, content: InputStream) {
|
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 ->
|
.use { stmt ->
|
||||||
stmt.setLong(1, accountId)
|
stmt.setLong(1, accountId)
|
||||||
stmt.setString(2, generateExtId())
|
stmt.setString(2, generateExtId())
|
||||||
|
@ -31,16 +31,17 @@ class SqliteRepository(url: String) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getLimboFileCount(accountId: Long): Long {
|
fun getLimboFileCount(accountId: Long): Long {
|
||||||
connection.prepareStatement("SELECT count(*) as count FROM limbo WHERE account_id=?").use { stmt ->
|
connection.prepareStatement("SELECT count(*) AS count FROM file WHERE account_id=? AND document_id IS NULL")
|
||||||
stmt.setLong(1, accountId)
|
.use { stmt ->
|
||||||
val rs = stmt.executeQuery()
|
stmt.setLong(1, accountId)
|
||||||
rs.next()
|
val rs = stmt.executeQuery()
|
||||||
return rs.getLong(1)
|
rs.next()
|
||||||
}
|
return rs.getLong(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getFilesInLimbo(accountId: Long): List<LimboFile> {
|
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 ->
|
.use { stmt ->
|
||||||
stmt.setLong(1, accountId)
|
stmt.setLong(1, accountId)
|
||||||
val res = stmt.executeQuery()
|
val res = stmt.executeQuery()
|
||||||
|
@ -70,7 +71,41 @@ class SqliteRepository(url: String) {
|
||||||
description: String,
|
description: String,
|
||||||
fileIds: List<Long>
|
fileIds: List<Long>
|
||||||
): Long {
|
): Long {
|
||||||
TODO("Not implemented")
|
val savePoint = connection.setSavepoint()
|
||||||
return 1L;
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -4,7 +4,7 @@ CREATE TABLE account (
|
||||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||||
email TEXT NOT NULL,
|
email TEXT NOT NULL,
|
||||||
password TEXT NOT NULL,
|
password TEXT NOT NULL,
|
||||||
created TEXT NOT NULL,
|
created TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
unique(email)
|
unique(email)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -17,40 +17,25 @@ CREATE TABLE document (
|
||||||
title TEXT NOT NULL,
|
title TEXT NOT NULL,
|
||||||
description TEXT NOT NULL,
|
description TEXT NOT NULL,
|
||||||
tags TEXT NOT NULL,
|
tags TEXT NOT NULL,
|
||||||
created TEXT NOT NULL,
|
created TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
reference_date TEXT,
|
reference_date TEXT,
|
||||||
CONSTRAINT document_account_FK FOREIGN KEY (account_id) REFERENCES account(id) ON DELETE CASCADE,
|
CONSTRAINT document_account_FK FOREIGN KEY (account_id) REFERENCES account(id) ON DELETE CASCADE,
|
||||||
unique(ext_id)
|
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
|
-- file definition
|
||||||
|
|
||||||
CREATE TABLE file (
|
CREATE TABLE file (
|
||||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
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,
|
ext_id TEXT NOT NULL,
|
||||||
filename TEXT NOT NULL,
|
filename TEXT NOT NULL,
|
||||||
|
file_size INTEGER NOT NULL,
|
||||||
content BLOB NOT NULL,
|
content BLOB NOT NULL,
|
||||||
content_type TEXT,
|
content_type TEXT,
|
||||||
content_extracted 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,
|
CONSTRAINT file_document_FK FOREIGN KEY (document_id) REFERENCES document(id) ON DELETE CASCADE,
|
||||||
unique(ext_id)
|
unique(ext_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue