diff --git a/active-record/src/main/java/com/iluwatar/activerecord/App.java b/active-record/src/main/java/com/iluwatar/activerecord/App.java index 7e0ec21b6ca9..016f298b33f7 100644 --- a/active-record/src/main/java/com/iluwatar/activerecord/App.java +++ b/active-record/src/main/java/com/iluwatar/activerecord/App.java @@ -1,12 +1,14 @@ package com.iluwatar.activerecord; +import java.sql.Connection; import java.sql.SQLException; +import java.sql.Statement; import javax.sql.DataSource; import lombok.extern.slf4j.Slf4j; import org.h2.jdbcx.JdbcDataSource; /** - * The amin application for the manual testing purposes. + * The main application for the manual testing purposes. */ @Slf4j public class App { @@ -35,10 +37,10 @@ public class App { * Java main method to execute all the logic out there. * * @param args arguments. - * @throws Exception Any sort of exception that have to be picked up by the JVM. + * @throws Exception Any sort of exception that has to be picked up by the JVM. */ public static void main(final String[] args) throws Exception { - final var dataSource = createDataSource(); + final DataSource dataSource = createDataSource(); createSchema(dataSource); RecordBase.setDataSource(dataSource); executeOperation(); @@ -65,14 +67,14 @@ private static void executeOperation() { } private static void createSchema(DataSource dataSource) throws SQLException { - try (var connection = dataSource.getConnection(); - var statement = connection.createStatement()) { - statement.execute(CREATE_SCHEMA_SQL); + try (Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement()) { + stmt.execute(CREATE_SCHEMA_SQL); } } private static DataSource createDataSource() { - var dataSource = new JdbcDataSource(); + JdbcDataSource dataSource = new JdbcDataSource(); dataSource.setURL(DB_URL); return dataSource; } diff --git a/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java b/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java index 3cf974d207d1..6cc3b4a8662f 100644 --- a/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java +++ b/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java @@ -9,7 +9,6 @@ import java.util.List; import javax.sql.DataSource; import lombok.RequiredArgsConstructor; -import lombok.Setter; /** * An active record base supposed to hold all the necessary active record pattern logic. @@ -24,17 +23,31 @@ public abstract class RecordBase> { private static final String EXCEPTION_MESSAGE = "Couldn't execute database query for the following domain model :"; - @Setter private static DataSource dataSource; @SuppressWarnings({"unchecked"}) private final Class clazz = (Class) getClass(); + /** + * Sets the data source. Preferably to eb done during the application startup or within the + * configuration. + * + * @param dataSource the data source {@link DataSource}. + */ + public static void setDataSource(DataSource dataSource) { + RecordBase.dataSource = dataSource; + } + + /** + * Get an SQL exception for the sake of all other internal persistence methods. + * + * @return the connection {@link Connection}. + */ protected Connection getConnection() { try { return dataSource.getConnection(); } catch (SQLException e) { - throw new RuntimeException("Unable to acquire database connection", e); + throw new RecordDataAccessException("Unable to acquire database connection", e); } } @@ -45,8 +58,20 @@ protected Connection getConnection() { */ protected abstract String getTableName(); + /** + * Set all the fields into the underlying domain model from the result set. + * + * @param rs the result set {@link ResultSet}. + * @throws SQLException an SQL exception. + */ protected abstract void setFieldsFromResultSet(ResultSet rs) throws SQLException; + /** + * Set the prepared statement parameters for the SQL statement to insert/update record. + * + * @param pstmt prepared statement {@link PreparedStatement}. + * @throws SQLException an SQL exception. + */ protected abstract void setPreparedStatementParams(PreparedStatement pstmt) throws SQLException; /** @@ -67,7 +92,7 @@ public List findAll() { return recordList; } } catch (SQLException e) { - throw new RuntimeException(EXCEPTION_MESSAGE + clazz.getName(), e); + throw new RecordDataAccessException(EXCEPTION_MESSAGE + clazz.getName(), e); } } @@ -90,7 +115,8 @@ public T findById(Long id) { return getDeclaredClassInstance(); } } catch (SQLException e) { - throw new RuntimeException(EXCEPTION_MESSAGE + clazz.getName() + " with id=" + id, e); + throw new RecordDataAccessException(EXCEPTION_MESSAGE + clazz.getName() + " with id=" + id, + e); } } @@ -107,20 +133,18 @@ public void save() { pstmt.executeUpdate(); } catch (SQLException e) { - throw new RuntimeException(EXCEPTION_MESSAGE + clazz.getName(), e); + throw new RecordDataAccessException(EXCEPTION_MESSAGE + clazz.getName(), e); } } + private String constructFindByIdQuery() { + return constructFindAllQuery() + " WHERE id = ?"; + } private String constructFindAllQuery() { return "SELECT * FROM " + getDeclaredClassInstance().getTableName(); } - private String constructFindByIdQuery() { - return "SELECT * FROM " + getDeclaredClassInstance().getTableName() - + " WHERE id = ?"; - } - private T getDeclaredClassInstance() { try { return clazz.getDeclaredConstructor().newInstance(); diff --git a/active-record/src/main/java/com/iluwatar/activerecord/RecordDataAccessException.java b/active-record/src/main/java/com/iluwatar/activerecord/RecordDataAccessException.java new file mode 100644 index 000000000000..b46e33494074 --- /dev/null +++ b/active-record/src/main/java/com/iluwatar/activerecord/RecordDataAccessException.java @@ -0,0 +1,19 @@ +package com.iluwatar.activerecord; + +public class RecordDataAccessException extends RuntimeException { + + public RecordDataAccessException() { + } + + public RecordDataAccessException(String message) { + super(message); + } + + public RecordDataAccessException(String message, Throwable cause) { + super(message, cause); + } + + public RecordDataAccessException(Throwable cause) { + super(cause); + } +}