Skip to content

Commit

Permalink
polish + exception (iluwatar#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergejsvisockis committed Apr 15, 2024
1 parent 53304a5 commit 49c3f63
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
16 changes: 9 additions & 7 deletions active-record/src/main/java/com/iluwatar/activerecord/App.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -24,17 +23,31 @@ public abstract class RecordBase<T extends 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<T> clazz = (Class<T>) 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);
}
}

Expand All @@ -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;

/**
Expand All @@ -67,7 +92,7 @@ public List<T> findAll() {
return recordList;
}
} catch (SQLException e) {
throw new RuntimeException(EXCEPTION_MESSAGE + clazz.getName(), e);
throw new RecordDataAccessException(EXCEPTION_MESSAGE + clazz.getName(), e);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 49c3f63

Please sign in to comment.