Implementing library
This commit is contained in:
parent
7c16139184
commit
bc7dc206b4
8
pom.xml
8
pom.xml
|
@ -24,7 +24,7 @@
|
|||
<developerConnection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</developerConnection>
|
||||
<url>https://code-repo.d4science.org/gCubeSystem/${project.artifactId}</url>
|
||||
</scm>
|
||||
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
@ -46,6 +46,12 @@
|
|||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.2.19</version>
|
||||
</dependency>
|
||||
<!-- Test Dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
|
|
@ -3,6 +3,12 @@
|
|||
*/
|
||||
package org.gcube.documentstore.persistence;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.Statement;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.gcube.documentstore.records.Record;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -14,44 +20,88 @@ public class PersistencePostgreSQL extends PersistenceBackend {
|
|||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PersistencePostgreSQL.class);
|
||||
|
||||
public static final String URL_PROPERTY_KEY = "URL";
|
||||
public static final String USERNAME_PROPERTY_KEY = "username";
|
||||
public static final String PASSWORD_PROPERTY_KEY = "password";
|
||||
|
||||
private String url;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
private Connection connection;
|
||||
private Statement statement;
|
||||
|
||||
@Override
|
||||
protected void prepareConnection(PersistenceBackendConfiguration configuration) throws Exception {
|
||||
logger.trace("prepareConnection()");
|
||||
url = configuration.getProperty(URL_PROPERTY_KEY);
|
||||
username = configuration.getProperty(USERNAME_PROPERTY_KEY);
|
||||
password = configuration.getProperty(PASSWORD_PROPERTY_KEY);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void openConnection() throws Exception {
|
||||
logger.trace("openConnection()");
|
||||
try {
|
||||
Class.forName("org.postgresql.Driver");
|
||||
connection = DriverManager.getConnection(url, username, password);
|
||||
logger.trace("Opened database successfully");
|
||||
connection.setAutoCommit(false);
|
||||
statement = connection.createStatement();
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reallyAccount(Record record) throws Exception {
|
||||
logger.trace("reallyAccount()");
|
||||
protected String getSQLInsertCommand(Record record) {
|
||||
StringBuffer sql = new StringBuffer();
|
||||
sql.append("INSERT INTO ");
|
||||
sql.append(record.getRecordType()); // tableNme == RecordType
|
||||
boolean first = true;
|
||||
SortedSet<String> keys = new TreeSet<>(record.getRequiredFields());
|
||||
StringBuffer values = new StringBuffer();
|
||||
for(String key : keys) {
|
||||
if(first) {
|
||||
sql.append(" ");
|
||||
values.append(" (");
|
||||
first = false;
|
||||
}else {
|
||||
sql.append(",");
|
||||
values.append(",");
|
||||
}
|
||||
sql.append(key);
|
||||
values.append(record.getResourceProperty(key));
|
||||
|
||||
}
|
||||
sql.append(") VALUES");
|
||||
sql.append(values);
|
||||
sql.append(");");
|
||||
return sql.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accountWithFallback(Record... records) throws Exception {
|
||||
logger.trace("accountWithFallback()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
logger.trace("close()");
|
||||
protected void reallyAccount(Record record) throws Exception {
|
||||
logger.trace("reallyAccount()");
|
||||
String sqlCommand = getSQLInsertCommand(record);
|
||||
statement.executeUpdate(sqlCommand);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void clean() throws Exception {
|
||||
logger.trace("closeAndClean()");
|
||||
logger.trace("clean()");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void closeConnection() throws Exception {
|
||||
logger.trace("closeConnection()");
|
||||
statement.close();
|
||||
connection.commit();
|
||||
connection.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConnectionActive() throws Exception {
|
||||
return true;
|
||||
return connection.isValid(300);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue