Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions morf-core/src/main/java/org/alfasoftware/morf/jdbc/SqlDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,17 @@
*/
public abstract class SqlDialect {

private static final String AND = " AND ";

private static final String WHERE = " WHERE ";

/**
*
* On a table allowing for table-id column handling, the name of the column holding the next id value
*/
protected static final String ID_INCREMENTOR_TABLE_COLUMN_VALUE = "nextvalue";

/**
*
* On a table allowing for table-id column handling, the name of the column holding the name of the table that the id value is for.
*/
protected static final String ID_INCREMENTOR_TABLE_COLUMN_NAME = "name";

Expand Down Expand Up @@ -1195,7 +1199,7 @@ protected void appendGroupBy(StringBuilder result, SelectStatement stmt) {
*/
protected <T extends AbstractSelectStatement<T>> void appendWhere(StringBuilder result, AbstractSelectStatement<T> stmt) {
if (stmt.getWhereCriterion() != null) {
result.append(" WHERE ");
result.append(WHERE);
result.append(getSqlFrom(stmt.getWhereCriterion()));
}
}
Expand Down Expand Up @@ -3233,7 +3237,7 @@ protected String getSqlFrom(DeleteStatement statement) {

// Prepare to append the where clause or, for appropriate dialects, the delete limit
if (statement.getWhereCriterion() != null || statement.getLimit().isPresent() && getDeleteLimitWhereClause(statement.getLimit().get()).isPresent()) {
sqlBuilder.append(" WHERE ");
sqlBuilder.append(WHERE);
}

// Now put the where clause in
Expand All @@ -3244,7 +3248,7 @@ protected String getSqlFrom(DeleteStatement statement) {
// Append the delete limit, for appropriate dialects
if (statement.getLimit().isPresent() && getDeleteLimitWhereClause(statement.getLimit().get()).isPresent()) {
if (statement.getWhereCriterion() != null) {
sqlBuilder.append(" AND ");
sqlBuilder.append(AND);
}

sqlBuilder.append(getDeleteLimitWhereClause(statement.getLimit().get()).get());
Expand Down Expand Up @@ -3326,7 +3330,7 @@ protected String getSqlFrom(UpdateStatement statement) {

// Now put the where clause in
if (statement.getWhereCriterion() != null) {
sqlBuilder.append(" WHERE ");
sqlBuilder.append(WHERE);
sqlBuilder.append(getSqlFrom(statement.getWhereCriterion()));
}

Expand Down Expand Up @@ -3396,7 +3400,7 @@ protected String mergeStatementWhenMatchedUpdateClause(MergeStatement statement)
MergeMatchClause mergeMatchClause = whenMatchedAction.get();
Optional<Criterion> whereClause = mergeMatchClause.getWhereClause();
if (mergeMatchClause.getAction() == MatchAction.UPDATE && whereClause.isPresent()) {
sqlBuilder.append(" AND ")
sqlBuilder.append(AND)
.append(getSqlFrom(whereClause.get()));
}
}
Expand Down Expand Up @@ -4305,7 +4309,7 @@ protected String matchConditionSqlForMergeFields(MergeStatement statement, Strin
Iterable<String> expressions = Iterables.transform(statement.getTableUniqueKey(),
field -> String.format("%s.%s = %s.%s", targetTableName, field.getImpliedName(), selectAlias, field.getImpliedName()));

return Joiner.on(" AND ").join(expressions);
return Joiner.on(AND).join(expressions);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,8 @@ public boolean equals(Object obj) {
return false;
} else if (!ifUpdating.equals(other.ifUpdating))
return false;
if (!whenMatchedAction.equals(other.whenMatchedAction))
return false;
return true;

return whenMatchedAction.equals(other.whenMatchedAction);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,13 @@ public PortableSqlStatement add(String databaseTypeIdentifier, String sql) {
* @return This {@link PortableSqlStatement}.
*/
public PortableSqlStatement add(String databaseTypeIdentifier, InputStream stream) {
InputStreamReader streamReader = null;
try {
streamReader = new InputStreamReader(stream, StandardCharsets.UTF_8);
try (InputStreamReader streamReader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
statements.put(databaseTypeIdentifier, CharStreams.toString(streamReader));
} catch (IOException e) {
throw new RuntimeException(
"Error loading SQL upgrade script from server.", e);
} finally {
Closeables.closeQuietly(stream);
Closeables.closeQuietly(streamReader);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@
* @author Copyright (c) Alfa Financial Software 2010 - 2013
*/
public class Upgrade {

private static final Log log = LogFactory.getLog(Upgrade.class);

private static final String COLUMN_NAME_UPGRADE_UUID = "upgradeUUID";

private final ConnectionResources connectionResources;
private final UpgradePathFactory upgradePathFactory;
private final UpgradeStatusTableService upgradeStatusTableService;
Expand Down Expand Up @@ -390,7 +393,7 @@ private Map<String, String> getUpgradeAuditRecords() {
TableReference upgradeAuditTable = tableRef(DatabaseUpgradeTableContribution.UPGRADE_AUDIT_NAME);

SelectStatement selectStatement = select(
upgradeAuditTable.field("upgradeUUID"),
upgradeAuditTable.field(COLUMN_NAME_UPGRADE_UUID),
upgradeAuditTable.field("description")
)
.from(upgradeAuditTable)
Expand Down Expand Up @@ -418,7 +421,7 @@ private ResultSetProcessor<Map<String, String>> resultSetProcessor() {
try {
while(resultSet.next()) {
String description = resultSet.getString("description");
String upgradeUUID = resultSet.getString("upgradeUUID");
String upgradeUUID = resultSet.getString(COLUMN_NAME_UPGRADE_UUID);
upgradeAudit.put(description, upgradeUUID);
}
} catch (SQLException e) {
Expand All @@ -434,7 +437,7 @@ private ResultSetProcessor<Map<String, String>> resultSetProcessor() {
*/
private SelectStatement selectUpgradeAuditTableCount() {
TableReference upgradeAuditTable = tableRef(DatabaseUpgradeTableContribution.UPGRADE_AUDIT_NAME);
return select(count(upgradeAuditTable.field("upgradeUUID")))
return select(count(upgradeAuditTable.field(COLUMN_NAME_UPGRADE_UUID)))
.from(upgradeAuditTable)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class TestSqlScriptExecutor {
private final DataSource dataSource = mock(DataSource.class);
private final SqlDialect sqlDialect = mock(SqlDialect.class);
private final DatabaseType databaseType = mock(DatabaseType.class);
private final SqlScriptVisitor SqlScriptVisitor = mock(SqlScriptVisitor.class);
private final SqlScriptVisitor sqlScriptVisitor = mock(SqlScriptVisitor.class);
private final ConnectionResources connectionResources = mock(ConnectionResources.class);

private final SqlScriptExecutor sqlScriptExecutor = new SqlScriptExecutorProvider(dataSource, sqlDialect).get();
Expand Down Expand Up @@ -239,13 +239,13 @@ public void testSqlExecutionTiming1() {
Long expectedDurationInMs = endTimeInMs - startTimeInMs;
when(clock.millis()).thenReturn(startTimeInMs, endTimeInMs);

var sqlScriptExecutor = new SqlScriptExecutor(SqlScriptVisitor, dataSource, sqlDialect, connectionResources, clock);
var localSqlScriptExecutor = new SqlScriptExecutor(sqlScriptVisitor, dataSource, sqlDialect, connectionResources, clock);

// WHEN:
sqlScriptExecutor.execute(ImmutableList.of(sqlScriptOne));
localSqlScriptExecutor.execute(ImmutableList.of(sqlScriptOne));

// THEN:
verify(SqlScriptVisitor).afterExecute(eq(sqlScriptOne), anyLong(), eq(expectedDurationInMs));
verify(sqlScriptVisitor).afterExecute(eq(sqlScriptOne), anyLong(), eq(expectedDurationInMs));
}


Expand All @@ -269,13 +269,13 @@ public void testSqlExecutionTiming2() throws SQLException {
ResultSet resultSet = mock(ResultSet.class);
when(preparedStatement.executeQuery()).thenReturn(resultSet);

var sqlScriptExecutor = new SqlScriptExecutor(SqlScriptVisitor, dataSource, sqlDialect, connectionResources, clock);
var localSqlScriptExecutor = new SqlScriptExecutor(sqlScriptVisitor, dataSource, sqlDialect, connectionResources, clock);

// WHEN:
sqlScriptExecutor.executeQuery(sqlScriptOne, resultSetProcessor);
localSqlScriptExecutor.executeQuery(sqlScriptOne, resultSetProcessor);

// THEN:
verify(SqlScriptVisitor).afterExecute(anyString(), anyLong(), eq(expectedDurationInMs));
verify(sqlScriptVisitor).afterExecute(anyString(), anyLong(), eq(expectedDurationInMs));
}


Expand All @@ -301,12 +301,12 @@ public void testSqlExecutionTiming3() throws SQLException {
ResultSet resultSet = mock(ResultSet.class);
when(preparedStatement.executeQuery()).thenReturn(resultSet);

var sqlScriptExecutor = new SqlScriptExecutor(SqlScriptVisitor, dataSource, sqlDialect, connectionResources, clock);
var localSqlScriptExecutor = new SqlScriptExecutor(sqlScriptVisitor, dataSource, sqlDialect, connectionResources, clock);

// WHEN:
sqlScriptExecutor.execute(sqlScriptOne, connection, List.of(sqlParameter), dataValueLookup);
localSqlScriptExecutor.execute(sqlScriptOne, connection, List.of(sqlParameter), dataValueLookup);

// THEN:
verify(SqlScriptVisitor).afterExecute(anyString(), anyLong(), eq(expectedDurationInMs));
verify(sqlScriptVisitor).afterExecute(anyString(), anyLong(), eq(expectedDurationInMs));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class TestMergeMatchClause {
private SchemaAndDataChangeVisitor visitor;

@Before
public void setUp() throws Exception {
public void setUp() {
MockitoAnnotations.openMocks(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,7 @@ protected String getSqlFrom(SelectFirstStatement stmt) {
protected String getSqlFrom(SequenceReference sequenceReference) {
StringBuilder result = new StringBuilder();

if (getSchemaName() != null || !getSchemaName().isBlank()) {
if (getSchemaName() != null && !getSchemaName().isBlank()) {
result.append(getSchemaName());
result.append(".");
}
Expand Down
Loading