Skip to content

Commit

Permalink
Add executeStatement utility
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmihalcea committed Jun 27, 2024
1 parent f475a73 commit 5e5a07c
Show file tree
Hide file tree
Showing 67 changed files with 312 additions and 1,106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,10 @@ protected Class<?>[] entities() {
}

@Override
public void init() {
DataSource dataSource = newDataSource();
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
try {
statement.executeUpdate(
"DROP TYPE sensor_state CASCADE"
);
} catch (SQLException ignore) {
}
statement.executeUpdate(
"CREATE TYPE sensor_state AS ENUM ('ONLINE', 'OFFLINE', 'UNKNOWN')"
);
statement.executeUpdate(
"CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\""
);
} catch (SQLException e) {
fail(e.getMessage());
}
super.init();
protected void beforeInit() {
executeStatement("DROP TYPE sensor_state CASCADE");
executeStatement("CREATE TYPE sensor_state AS ENUM ('ONLINE', 'OFFLINE', 'UNKNOWN')");
executeStatement("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,20 @@ protected Class<?>[] entities() {
}

@Override
public void init() {
DataSource dataSource = newDataSource();
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
statement.executeUpdate(
"CREATE OR REPLACE FUNCTION " +
" fn_array_contains(" +
" left_array integer[], " +
" right_array integer[]" +
") RETURNS " +
" boolean AS " +
"$$ " +
"BEGIN " +
" return left_array @> right_array; " +
"END; " +
"$$ LANGUAGE 'plpgsql';"
);
} catch (SQLException e) {
fail(e.getMessage());
}
super.init();
protected void beforeInit() {
executeStatement(
"CREATE OR REPLACE FUNCTION " +
" fn_array_contains(" +
" left_array integer[], " +
" right_array integer[]" +
") RETURNS " +
" boolean AS " +
"$$ " +
"BEGIN " +
" return left_array @> right_array; " +
"END; " +
"$$ LANGUAGE 'plpgsql';"
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,10 @@ protected Class<?>[] entities() {
}

@Override
public void init() {
DataSource dataSource = newDataSource();
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
try {
statement.executeUpdate(
"DROP TYPE sensor_state CASCADE"
);
} catch (SQLException ignore) {
}
statement.executeUpdate(
"CREATE TYPE sensor_state AS ENUM ('ONLINE', 'OFFLINE', 'UNKNOWN')"
);
statement.executeUpdate(
"CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\""
);
} catch (SQLException e) {
fail(e.getMessage());
}
super.init();
protected void beforeInit() {
executeStatement("DROP TYPE sensor_state CASCADE");
executeStatement("CREATE TYPE sensor_state AS ENUM ('ONLINE', 'OFFLINE', 'UNKNOWN')");
executeStatement("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,10 @@ protected void additionalProperties(Properties properties) {
}

@Override
public void init() {
DataSource dataSource = newDataSource();
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
try {
statement.executeUpdate(
"DROP TYPE sensor_state CASCADE"
);
} catch (SQLException ignore) {
}
statement.executeUpdate(
"CREATE TYPE sensor_state AS ENUM ('ONLINE', 'OFFLINE', 'UNKNOWN')"
);
statement.executeUpdate(
"CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\""
);
} catch (SQLException e) {
fail(e.getMessage());
}
super.init();
protected void beforeInit() {
executeStatement("DROP TYPE sensor_state CASCADE");
executeStatement("CREATE TYPE sensor_state AS ENUM ('ONLINE', 'OFFLINE', 'UNKNOWN')");
executeStatement("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,10 @@ protected Class<?>[] entities() {
}

@Override
public void init() {
DataSource dataSource = newDataSource();
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
try {
statement.executeUpdate(
"DROP TYPE sensor_state CASCADE"
);
} catch (SQLException ignore) {
}
statement.executeUpdate(
"CREATE TYPE sensor_state AS ENUM ('ONLINE', 'OFFLINE', 'UNKNOWN')"
);
statement.executeUpdate(
"CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\""
);
} catch (SQLException e) {
fail(e.getMessage());
}
super.init();
protected void beforeInit() {
executeStatement("DROP TYPE sensor_state CASCADE");
executeStatement("CREATE TYPE sensor_state AS ENUM ('ONLINE', 'OFFLINE', 'UNKNOWN')");
executeStatement("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,11 @@ public String hibernateDialect() {
};
}

@Before
public void init() {
DataSource dataSource = newDataSource();
try (Connection connection = dataSource.getConnection()) {
try (Statement statement = connection.createStatement()){
statement.executeUpdate("DROP TABLE IF EXISTS plane;");
statement.executeUpdate("DROP TYPE IF EXISTS seat_status CASCADE;");
statement.executeUpdate("CREATE TYPE seat_status AS ENUM ('UNRESERVED', 'RESERVED', 'BLOCKED');");
}
} catch (SQLException e) {
fail(e.getMessage());
}
super.init();
@Override
protected void beforeInit() {
executeStatement("DROP TABLE IF EXISTS plane;");
executeStatement("DROP TYPE IF EXISTS seat_status CASCADE;");
executeStatement("CREATE TYPE seat_status AS ENUM ('UNRESERVED', 'RESERVED', 'BLOCKED');");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,12 @@ protected Class<?>[] entities() {
};
}

@Before
public void init() {
DataSource dataSource = newDataSource();

try (Connection connection = dataSource.getConnection()) {
try (Statement statement = connection.createStatement()){
statement.executeUpdate("DROP TYPE IF EXISTS user_role;");
statement.executeUpdate("CREATE TYPE user_role AS ENUM ('ROLE_ADMIN', 'ROLE_USER');");
}
} catch (SQLException e) {
fail(e.getMessage());
}

try (Connection connection = dataSource.getConnection()) {
try (Statement statement = connection.createStatement()){
statement.executeUpdate("DROP TYPE IF EXISTS user_role;");
statement.executeUpdate("CREATE TYPE user_role AS ENUM ('ROLE_ADMIN', 'ROLE_USER');");
}

try (Statement statement = connection.createStatement()){
statement.executeUpdate("DROP TYPE IF EXISTS user_type;");
statement.executeUpdate("CREATE TYPE user_type AS ENUM ('SUPER_USER', 'REGULAR');");
}
} catch (SQLException e) {
fail(e.getMessage());
}
super.init();
@Override
protected void beforeInit() {
executeStatement("DROP TYPE IF EXISTS user_role;");
executeStatement("CREATE TYPE user_role AS ENUM ('ROLE_ADMIN', 'ROLE_USER');");
executeStatement("DROP TYPE IF EXISTS user_type;");
executeStatement("CREATE TYPE user_type AS ENUM ('SUPER_USER', 'REGULAR');");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,9 @@ protected Class<?>[] entities() {
}

@Override
public void init() {
DataSource dataSource = newDataSource();
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
try {
statement.executeUpdate(
"DROP TYPE sensor_state CASCADE"
);
} catch (SQLException ignore) {
}
statement.executeUpdate(
"CREATE TYPE sensor_state AS ENUM ('ONLINE', 'OFFLINE', 'UNKNOWN')"
);
} catch (SQLException e) {
fail(e.getMessage());
}
super.init();
protected void beforeInit() {
executeStatement("DROP TYPE sensor_state CASCADE");
executeStatement("CREATE TYPE sensor_state AS ENUM ('ONLINE', 'OFFLINE', 'UNKNOWN')");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,8 @@ protected Class<?>[] entities() {
}

@Override
public void init() {
DataSource dataSource = newDataSource();
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
statement.executeUpdate(
"CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\""
);
} catch (SQLException e) {
fail(e.getMessage());
}
super.init();
protected void beforeInit() {
executeStatement("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,10 @@ protected Class<?>[] entities() {
}

@Override
public void init() {
super.init();
doInJDBC(connection -> {
try (
Statement statement = connection.createStatement();
) {
statement.executeUpdate("INSERT INTO EVENT (ID, EVENT_TYPE) VALUES (1, 'abc')");
statement.executeUpdate("INSERT INTO EVENT (ID, EVENT_TYPE) VALUES (2, '')");
statement.executeUpdate("INSERT INTO EVENT (ID, EVENT_TYPE) VALUES (3, 'b')");
} catch (SQLException e) {
fail(e.getMessage());
}
});
protected void afterInit() {
executeStatement("INSERT INTO EVENT (ID, EVENT_TYPE) VALUES (1, 'abc')");
executeStatement("INSERT INTO EVENT (ID, EVENT_TYPE) VALUES (2, '')");
executeStatement("INSERT INTO EVENT (ID, EVENT_TYPE) VALUES (3, 'b')");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,8 @@ protected Class<?>[] entities() {
}

@Override
public void init() {
DataSource dataSource = newDataSource();
Connection connection = null;
Statement statement = null;
try {
connection = dataSource.getConnection();
statement = connection.createStatement();

statement.executeUpdate("CREATE EXTENSION IF NOT EXISTS citext");
} catch (SQLException e) {
fail(e.getMessage());
} finally {
if(statement != null) {
try {
statement.close();
} catch (SQLException e) {
fail(e.getMessage());
}
}
if(connection != null) {
try {
connection.close();
} catch (SQLException e) {
fail(e.getMessage());
}
}
}
super.init();
protected void beforeInit() {
executeStatement("CREATE EXTENSION IF NOT EXISTS citext");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,8 @@ protected Class<?>[] entities() {
}

@Override
public void init() {
DataSource dataSource = newDataSource();
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
statement.executeUpdate("CREATE EXTENSION IF NOT EXISTS hstore");
} catch (SQLException e) {
fail(e.getMessage());
}
super.init();
protected void beforeInit() {
executeStatement("CREATE EXTENSION IF NOT EXISTS hstore");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ protected Class<?>[] entities() {

@Override
public void afterInit() {
doInJDBC(connection -> {
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("CREATE INDEX ON event USING gist (ip inet_ops)");
} catch (SQLException e) {
fail(e.getMessage());
}
});
executeStatement("CREATE INDEX ON event USING gist (ip inet_ops)");

_event = doInJPA(entityManager -> {
entityManager.persist(new Event());
Expand Down
Loading

0 comments on commit 5e5a07c

Please sign in to comment.