Browse Source

preparing for import of old database

featue/module_registry
Stephan Richter 3 months ago
parent
commit
41cab2e594
  1. 76
      tags/src/main/java/de/srsoftware/umbrella/tags/SqliteDb.java

76
tags/src/main/java/de/srsoftware/umbrella/tags/SqliteDb.java

@ -14,56 +14,86 @@ import static java.lang.System.Logger.Level.INFO; @@ -14,56 +14,86 @@ import static java.lang.System.Logger.Level.INFO;
import static java.text.MessageFormat.format;
import de.srsoftware.tools.jdbc.Query;
import de.srsoftware.umbrella.core.BaseDb;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
public class SqliteDb implements TagDB{
public class SqliteDb extends BaseDb implements TagDB{
private static final int INITIAL_DB_VERSION = 1;
private static final System.Logger LOG = System.getLogger("TagDB");
private final Connection db;
public SqliteDb(Connection conn) {
this.db = conn;
init();
super(conn);
}
private int createTables() {
createTagTables();
return createSettingsTable();
@Override
protected int createTables() {
var version = createSettingsTable();
switch (version){
case 0:
/* createLegacyUrlsTable();
createLegacyCommentsTable();
createLegacyUrlCommentsTable();
createLegacyTagsTable();
case 1:
splitContentToBookmarks();*/
createTagTables();
}
return setCurrentVersion(1);
}
private int createSettingsTable() {
var createTable = """
CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255) NOT NULL);
""";
private void createLegacyCommentsTable() {
var sql = "CREATE TABLE IF NOT EXISTS comments (hash VARCHAR(255) PRIMARY KEY, comment TEXT NOT NULL)";
try {
var stmt = db.prepareStatement(format(createTable,TABLE_SETTINGS, KEY, VALUE));
var stmt = db.prepareStatement(sql);
stmt.execute();
stmt.close();
} catch (SQLException e) {
LOG.log(ERROR,ERROR_FAILED_CREATE_TABLE,TABLE_SETTINGS,e);
LOG.log(ERROR, ERROR_FAILED_CREATE_TABLE, "Comments (legacy)", e);
throw new RuntimeException(e);
}
}
Integer version = null;
private void createLegacyTagsTable() {
var sql = "CREATE TABLE IF NOT EXISTS tags (tag VARCHAR(255) NOT NULL, url_hash VARCHAR(255) NOT NULL, user_id int NOT NULL, UNIQUE(tag, url_hash, user_id))";
try {
var rs = select(VALUE).from(TABLE_SETTINGS).where(KEY, equal(DB_VERSION)).exec(db);
if (rs.next()) version = rs.getInt(VALUE);
rs.close();
if (version == null) {
version = INITIAL_DB_VERSION;
insertInto(TABLE_SETTINGS, KEY, VALUE).values(DB_VERSION,version).execute(db).close();
}
var stmt = db.prepareStatement(sql);
stmt.execute();
stmt.close();
} catch (SQLException e) {
LOG.log(ERROR, ERROR_FAILED_CREATE_TABLE, "Tags (legacy)", e);
throw new RuntimeException(e);
}
}
return version;
private void createLegacyUrlsTable() {
var sql = "CREATE TABLE IF NOT EXISTS urls (hash VARCHAR(255) PRIMARY KEY, url TEXT NOT NULL, timestamp INT NOT NULL DEFAULT 0)";
try {
var stmt = db.prepareStatement(sql);
stmt.execute();
stmt.close();
} catch (SQLException e) {
LOG.log(ERROR, ERROR_FAILED_CREATE_TABLE, "URLs (legacy)", e);
throw new RuntimeException(e);
}
}
private void createLegacyUrlCommentsTable() {
var sql = "CREATE TABLE IF NOT EXISTS url_comments (`url_hash` VARCHAR ( 255 ) NOT NULL, `comment_hash` VARCHAR ( 255 ) NOT NULL, `user_id` INT NOT NULL, UNIQUE(`url_hash`,`user_id`))";
try {
var stmt = db.prepareStatement(sql);
stmt.execute();
stmt.close();
} catch (SQLException e) {
LOG.log(ERROR,ERROR_READ_TABLE,DB_VERSION,TABLE_SETTINGS,e);
LOG.log(ERROR, ERROR_FAILED_CREATE_TABLE, "URL_comments (legacy)", e);
throw new RuntimeException(e);
}
}
private void createTagTables() {
var createTable = """
CREATE TABLE IF NOT EXISTS "{0}" (

Loading…
Cancel
Save