|
|
|
|
@ -3,25 +3,125 @@ package de.srsoftware.umbrella.project;
@@ -3,25 +3,125 @@ package de.srsoftware.umbrella.project;
|
|
|
|
|
|
|
|
|
|
import static de.srsoftware.tools.jdbc.Condition.equal; |
|
|
|
|
import static de.srsoftware.tools.jdbc.Condition.lessThan; |
|
|
|
|
import static de.srsoftware.tools.jdbc.Query.insertInto; |
|
|
|
|
import static de.srsoftware.tools.jdbc.Query.select; |
|
|
|
|
import static de.srsoftware.umbrella.core.Constants.COMPANY_ID; |
|
|
|
|
import static de.srsoftware.umbrella.core.Constants.STATUS; |
|
|
|
|
import static de.srsoftware.umbrella.core.Constants.*; |
|
|
|
|
import static de.srsoftware.umbrella.core.Constants.TABLE_SETTINGS; |
|
|
|
|
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_SERVER_ERROR; |
|
|
|
|
import static de.srsoftware.umbrella.project.Constants.TABLE_PROJECTS; |
|
|
|
|
import static de.srsoftware.umbrella.core.Util.LOG; |
|
|
|
|
import static de.srsoftware.umbrella.core.model.Project.Status.Open; |
|
|
|
|
import static de.srsoftware.umbrella.project.Constants.*; |
|
|
|
|
import static java.lang.System.Logger.Level.ERROR; |
|
|
|
|
import static java.lang.System.Logger.Level.INFO; |
|
|
|
|
import static java.text.MessageFormat.format; |
|
|
|
|
|
|
|
|
|
import de.srsoftware.tools.PasswordHasher; |
|
|
|
|
import de.srsoftware.tools.jdbc.Query; |
|
|
|
|
import de.srsoftware.umbrella.core.exceptions.UmbrellaException; |
|
|
|
|
import de.srsoftware.umbrella.core.model.Project; |
|
|
|
|
|
|
|
|
|
import java.security.NoSuchAlgorithmException; |
|
|
|
|
import java.sql.Connection; |
|
|
|
|
import java.sql.ResultSet; |
|
|
|
|
import java.sql.SQLException; |
|
|
|
|
import java.util.Collection; |
|
|
|
|
import java.util.HashSet; |
|
|
|
|
|
|
|
|
|
public class SqliteDb implements ProjectDb { |
|
|
|
|
private static final System.Logger LOG = System.getLogger("Sqlite4Project"); |
|
|
|
|
private static final int INITIAL_DB_VERSION = 1; |
|
|
|
|
|
|
|
|
|
private final Connection db; |
|
|
|
|
|
|
|
|
|
public SqliteDb(Connection connection) { |
|
|
|
|
db = connection; |
|
|
|
|
init(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int createTables() { |
|
|
|
|
createProjectTables(); |
|
|
|
|
return createSettingsTable(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void createProjectTables() { |
|
|
|
|
var createTable = """ |
|
|
|
|
CREATE TABLE IF NOT EXISTS {0} ( |
|
|
|
|
`{1}` INTEGER PRIMARY KEY, |
|
|
|
|
`{2}` INTEGER, |
|
|
|
|
`{3}` VARCHAR(255) NOT NULL, |
|
|
|
|
`{4}` TEXT, |
|
|
|
|
`{5}` INT DEFAULT {6}, |
|
|
|
|
`{7}` BOOLEAN DEFAULT 0 |
|
|
|
|
)"""; |
|
|
|
|
try { |
|
|
|
|
var stmt = db.prepareStatement(format(createTable,TABLE_PROJECTS, ID, COMPANY_ID, NAME, DESCRIPTION, STATUS, Open.code(), SHOW_CLOSED)); |
|
|
|
|
stmt.execute(); |
|
|
|
|
stmt.close(); |
|
|
|
|
} catch (SQLException e) { |
|
|
|
|
LOG.log(ERROR,ERROR_FAILED_CREATE_TABLE,TABLE_PROJECTS ,e); |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
long count = 0L; |
|
|
|
|
try { |
|
|
|
|
ResultSet rs = select("COUNT(*)").from(TABLE_PROJECTS).exec(db); |
|
|
|
|
if (rs.next()) count = rs.getLong(1); |
|
|
|
|
rs.close(); |
|
|
|
|
} catch (SQLException ignored) { |
|
|
|
|
// go on with table creation
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
createTable = """ |
|
|
|
|
CREATE TABLE IF NOT EXISTS {0} ( |
|
|
|
|
{1} INT NOT NULL, |
|
|
|
|
{2} INT NOT NULL, |
|
|
|
|
{3} INT DEFAULT {3}, |
|
|
|
|
PRIMARY KEY ({1}, {2}) |
|
|
|
|
)"""; |
|
|
|
|
try { |
|
|
|
|
var stmt = db.prepareStatement(format(createTable,TABLE_PROJECT_USERS, PROJECT_ID, USER_ID, PERMISSIONS)); |
|
|
|
|
stmt.execute(); |
|
|
|
|
stmt.close(); |
|
|
|
|
} catch (SQLException e) { |
|
|
|
|
LOG.log(ERROR,ERROR_FAILED_CREATE_TABLE,TABLE_PROJECT_USERS,e); |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private int createSettingsTable() { |
|
|
|
|
var createTable = """ |
|
|
|
|
CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255) NOT NULL); |
|
|
|
|
"""; |
|
|
|
|
try { |
|
|
|
|
var stmt = db.prepareStatement(format(createTable,TABLE_SETTINGS, KEY, VALUE)); |
|
|
|
|
stmt.execute(); |
|
|
|
|
stmt.close(); |
|
|
|
|
} catch (SQLException e) { |
|
|
|
|
LOG.log(ERROR,ERROR_FAILED_CREATE_TABLE,TABLE_SETTINGS,e); |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Integer version = null; |
|
|
|
|
try { |
|
|
|
|
var rs = Query.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(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return version; |
|
|
|
|
} catch (SQLException e) { |
|
|
|
|
LOG.log(ERROR,ERROR_READ_TABLE,DB_VERSION,TABLE_SETTINGS,e); |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void init(){ |
|
|
|
|
var version = createTables(); |
|
|
|
|
LOG.log(INFO,"Updated project db to version {0}",version); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|