19 package org.sleuthkit.autopsy.centralrepository.datamodel;
21 import java.sql.Connection;
22 import java.sql.DriverManager;
23 import java.sql.PreparedStatement;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.sql.Statement;
27 import java.util.Properties;
28 import java.util.logging.Level;
42 private final static String
JDBC_DRIVER =
"org.postgresql.Driver";
55 throw new CentralRepoException(
"cannot load or save postgres settings for selection: " + choice);
88 return String.format(
"PostgresCentralRepoSettings: [db type: postgres, host: %s:%d, db name: %s, username: %s]",
96 String getValidationQuery() {
110 String getJDBCBaseURI() {
123 String getConnectionURL(
boolean usePostgresDb) {
124 StringBuilder url =
new StringBuilder();
125 url.append(getJDBCBaseURI());
129 url.append(
"postgres");
134 return url.toString();
143 Connection getEphemeralConnection(
boolean usePostgresDb) {
146 String url = getConnectionURL(usePostgresDb);
147 Properties props =
new Properties();
151 Class.forName(getDriver());
152 conn = DriverManager.getConnection(url, props);
153 }
catch (ClassNotFoundException | SQLException ex) {
156 LOGGER.log(Level.SEVERE,
"Failed to acquire ephemeral connection to postgresql.");
170 Connection conn = getEphemeralConnection(
true);
187 Connection conn = getEphemeralConnection(
true);
192 String sql =
"SELECT datname FROM pg_catalog.pg_database WHERE lower(datname) = lower(?) LIMIT 1";
193 PreparedStatement ps = null;
196 ps = conn.prepareStatement(sql);
198 rs = ps.executeQuery();
202 }
catch (SQLException ex) {
203 LOGGER.log(Level.SEVERE,
"Failed to execute database existance query.", ex);
221 Connection conn = getEphemeralConnection(
false);
234 Connection conn = getEphemeralConnection(
true);
239 String sql =
"CREATE DATABASE %s OWNER %s";
242 stmt = conn.createStatement();
244 }
catch (SQLException ex) {
245 LOGGER.log(Level.SEVERE,
"Failed to execute create database statement.", ex);
256 Connection conn = getEphemeralConnection(
true);
261 String sql =
"DROP DATABASE %s";
264 stmt = conn.createStatement();
265 stmt.execute(String.format(sql,
getDbName()));
266 }
catch (SQLException ex) {
267 LOGGER.log(Level.SEVERE,
"Failed to execute drop database statement.", ex);
312 return connSettings.
getDbName() == null ? null : connSettings.
getDbName().toLowerCase();
325 int getBulkThreshold() {
void setPassword(String password)
static PostgresSettingsLoader getLoaderFromSaved()
void setBulkThreshold(int bulkThreshold)
void setUserName(String userName)
static void closeResultSet(ResultSet resultSet)
void setHost(String host)
void setDbName(String dbName)
static void closeConnection(Connection conn)
static CentralRepoDbChoice getSavedDbChoice()
final PostgresSettingsLoader loader
static final String VALIDATION_QUERY
PostgresConnectionSettings loadSettings()
PostgresSettingsLoader MULTIUSER_SETTINGS_LOADER
PostgresConnectionSettings connSettings
PostgresSettingsLoader CUSTOM_SETTINGS_LOADER
void setBulkThreshold(int bulkThreshold)
void setUserName(String userName)
void setPassword(String password)
static boolean schemaVersionIsSet(Connection conn)
void setHost(String host)
boolean verifyConnection()
static final String JDBC_DRIVER
PostgresCentralRepoSettings(PostgresSettingsLoader loader)
boolean verifyDatabaseExists()
static boolean executeValidationQuery(Connection conn, String validationQuery)
static final Logger LOGGER
DatabaseTestResult testStatus()
synchronized static Logger getLogger(String name)
void setDbName(String dbName)
boolean verifyDatabaseSchema()
PostgresCentralRepoSettings()
static final String JDBC_BASE_URI
static void closeStatement(Statement statement)
void saveSettings(PostgresConnectionSettings settings)