prepared database for bookmark service
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.bookmarks;
|
||||
|
||||
import static de.srsoftware.umbrella.bookmarks.Constants.TABLE_URLS;
|
||||
import static de.srsoftware.umbrella.bookmarks.Constants.TABLE_URL_COMMENTS;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Constants.ERROR_FAILED_CREATE_TABLE;
|
||||
import static java.lang.System.Logger.Level.ERROR;
|
||||
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class SqliteDb extends BaseDb implements BookmarkDb {
|
||||
public SqliteDb(Connection conn) {
|
||||
super(conn);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int createTables() {
|
||||
var version = createSettingsTable();
|
||||
switch (version){
|
||||
case 0:
|
||||
createUrlsTable();
|
||||
createUrlCommentsTable();
|
||||
}
|
||||
return setCurrentVersion(1);
|
||||
}
|
||||
|
||||
private void createUrlCommentsTable() {
|
||||
var sql = """
|
||||
CREATE TABLE IF NOT EXISTS "url_comments" (
|
||||
`url_hash` VARCHAR ( 255 ) NOT NULL,
|
||||
`user_id` LONG NOT NULL,
|
||||
`comment` TEXT NOT NULL,
|
||||
PRIMARY KEY (`url_hash`,`user_id`)
|
||||
)""";
|
||||
try {
|
||||
var stmt = db.prepareStatement(sql);
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
LOG.log(ERROR, ERROR_FAILED_CREATE_TABLE, TABLE_URL_COMMENTS, e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void createUrlsTable() {
|
||||
var sql = "CREATE TABLE IF NOT EXISTS urls (hash VARCHAR(255) PRIMARY KEY, url TEXT NOT NULL)";
|
||||
try {
|
||||
var stmt = db.prepareStatement(sql);
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
LOG.log(ERROR, ERROR_FAILED_CREATE_TABLE, TABLE_URLS, e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user