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 .append(getJDBCBaseURI())
131 url.append(
"postgres");
136 return url.toString();
145 Connection getEphemeralConnection(
boolean usePostgresDb) {
148 String url = getConnectionURL(usePostgresDb);
149 Properties props =
new Properties();
153 Class.forName(getDriver());
154 conn = DriverManager.getConnection(url, props);
155 }
catch (ClassNotFoundException | SQLException ex) {
158 LOGGER.log(Level.SEVERE,
"Failed to acquire ephemeral connection to postgresql.", ex);
172 Connection conn = getEphemeralConnection(
true);
189 Connection conn = getEphemeralConnection(
true);
194 String sql =
"SELECT datname FROM pg_catalog.pg_database WHERE lower(datname) = lower(?) LIMIT 1";
195 PreparedStatement ps = null;
198 ps = conn.prepareStatement(sql);
200 rs = ps.executeQuery();
204 }
catch (SQLException ex) {
205 LOGGER.log(Level.SEVERE,
"Failed to execute database existance query.", ex);
223 Connection conn = getEphemeralConnection(
false);
236 Connection conn = getEphemeralConnection(
true);
241 String sql =
"CREATE DATABASE %s OWNER %s";
244 stmt = conn.createStatement();
246 }
catch (SQLException ex) {
247 LOGGER.log(Level.SEVERE,
"Failed to execute create database statement.", ex);
258 Connection conn = getEphemeralConnection(
true);
263 String sql =
"DROP DATABASE %s";
266 stmt = conn.createStatement();
267 stmt.execute(String.format(sql,
getDbName()));
268 }
catch (SQLException ex) {
269 LOGGER.log(Level.SEVERE,
"Failed to execute drop database statement.", ex);
314 return connSettings.
getDbName() == null ? null : connSettings.
getDbName().toLowerCase();
327 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)