Skip to content
Open
64 changes: 44 additions & 20 deletions morf-core/src/main/java/org/alfasoftware/morf/jdbc/SqlDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public SqlDialect(String schemaName) {
* @return The statements required to deploy the table and its indexes.
*/
public Collection<String> tableDeploymentStatements(Table table) {
Builder<String> statements = ImmutableList.<String>builder();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary type

Builder<String> statements = ImmutableList.builder();

statements.addAll(internalTableDeploymentStatements(table));

Expand Down Expand Up @@ -215,16 +215,15 @@ public Collection<String> viewDeploymentStatements(View view) {
List<String> statements = new ArrayList<>();

// Create the table deployment statement
StringBuilder createTableStatement = new StringBuilder();
createTableStatement.append("CREATE ");
createTableStatement.append("VIEW ");
createTableStatement.append(schemaNamePrefix());
createTableStatement.append(view.getName());
createTableStatement.append(" AS (");
createTableStatement.append(convertStatementToSQL(view.getSelectStatement()));
createTableStatement.append(")");
String createTableStatement = "CREATE " +
"VIEW " +
schemaNamePrefix() +
view.getName() +
" AS (" +
convertStatementToSQL(view.getSelectStatement()) +
")";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary StringBuilder.


statements.add(createTableStatement.toString());
statements.add(createTableStatement);

return statements;
}
Expand Down Expand Up @@ -1195,10 +1194,7 @@ protected String getSqlForOrderByField(AliasedField currentOrderByField) {
return getSqlForOrderByField((FieldReference) currentOrderByField);
}

StringBuilder result = new StringBuilder(getSqlFrom(currentOrderByField));
result.append(" ").append(defaultNullOrder());

return result.toString().trim();
return (getSqlFrom(currentOrderByField) + " " + defaultNullOrder()).trim();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary StringBuilder.

}


Expand Down Expand Up @@ -1325,7 +1321,7 @@ protected void appendJoin(StringBuilder result, Join join, String innerJoinKeywo
} else {
// MySql supports no ON criteria and ON TRUE, but the other platforms
// don't, so just keep things simple.
result.append(String.format(" ON 1=1"));
result.append(" ON 1=1");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove redundant String.format

}
}

Expand Down Expand Up @@ -1568,7 +1564,7 @@ protected String getSqlFrom(Criterion criterion) {
result.append(getOperatorLine(criterion, "<="));
break;
case LIKE:
result.append(getOperatorLine(criterion, "LIKE") + likeEscapeSuffix());
result.append(getOperatorLine(criterion, "LIKE")).append(likeEscapeSuffix());
break;
case ISNULL:
result.append(String.format("%s IS NULL", getSqlFrom(criterion.getField())));
Expand Down Expand Up @@ -2078,6 +2074,18 @@ protected String getSqlFrom(Function function) {
}
return getSqlForRowNumber();

case CURRENT_UNIX_TIME_MILLISECONDS:
if (!function.getArguments().isEmpty()) {
throw new IllegalArgumentException("The CURRENT_UNIX_TIME_MILLISECONDS function should have zero arguments. This function has " + function.getArguments().size());
}
return getSqlForCurrentUnixTimeMilliseconds();

case CLIENT_HOST:
if (!function.getArguments().isEmpty()) {
throw new IllegalArgumentException("The CLIENT_HOST function should have zero arguments. This function has " + function.getArguments().size());
}
return getSqlForClientHost();

default:
throw new UnsupportedOperationException("This database does not currently support the [" + function.getType() + "] function");
}
Expand Down Expand Up @@ -2189,7 +2197,7 @@ protected String getSqlForCoalesce(Function function) {
* @return a string representation of the SQL
*/
protected String getSqlForGreatest(Function function) {
return getGreatestFunctionName() + '(' + Joiner.on(", ").join(function.getArguments().stream().map(f -> getSqlFrom(f)).iterator()) + ')';
return getGreatestFunctionName() + '(' + Joiner.on(", ").join(function.getArguments().stream().map(this::getSqlFrom).iterator()) + ')';
}


Expand All @@ -2200,7 +2208,7 @@ protected String getSqlForGreatest(Function function) {
* @return a string representation of the SQL
*/
protected String getSqlForLeast(Function function) {
return getLeastFunctionName() + '(' + Joiner.on(", ").join(function.getArguments().stream().map(f -> getSqlFrom(f)).iterator()) + ')';
return getLeastFunctionName() + '(' + Joiner.on(", ").join(function.getArguments().stream().map(this::getSqlFrom).iterator()) + ')';
}


Expand Down Expand Up @@ -2417,13 +2425,29 @@ protected String getLeastFunctionName() {
/**
* Produce SQL for getting the row number of the row in the partition
*
* @return a string representation of the SQL for finding the last day of the month.
* @return a string representation of the SQL for getting the row number of the row in the partition.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous change had cut and pasted the wrong JavaDocs without updating it.

*/
protected String getSqlForRowNumber(){
return "ROW_NUMBER()";
}


/**
* Produce SQL for getting the current unix time in milliseconds
*
* @return A string representation of the SQL for the current unix time in milliseconds
*/
protected abstract String getSqlForCurrentUnixTimeMilliseconds();


/**
* Produce SQL for getting the client host
*
* @return A string representation of the SQL for the client host
*/
protected abstract String getSqlForClientHost();


/**
* Gets the function name required to perform a substring command.
* <p>
Expand Down Expand Up @@ -4192,7 +4216,7 @@ protected Iterable<AliasedField> getMergeStatementUpdateExpressions(MergeStateme
.toSet();

List<String> listOfKeyFieldsWithUpdateExpression = FluentIterable.from(onUpdateExpressions.keySet())
.filter(a -> keyFields.contains(a))
.filter(keyFields::contains)
.toList();

if (!listOfKeyFieldsWithUpdateExpression.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,11 @@ public static Function addMonths(AliasedField expression, AliasedField number) {
* <table>
* <caption>Database rounding references</caption>
* <tr><th>Database</th><th>Database Manual</th></tr>
* <tr><td>Oracle</td><td>http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions135.htm</td></tr>
* <tr><td>MySQL</td><td>http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_round</td></tr>
* <tr><td>SQLServer</td><td>http://technet.microsoft.com/en-us/library/ms175003.aspx</td></tr>
* <tr><td>Db2400</td><td>http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=%2Fcom.ibm.db2.udb.admin.doc%2Fdoc%2Fr0000845.htm</td></tr>
* <tr><td>H2</td><td>http://www.h2database.com/html/functions.html#round</td></tr>
* <tr><td>Oracle</td><td><a href="http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions135.htm">Manual</a></td></tr>
* <tr><td>MySQL</td><td><a href="http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_round">Manual</a></td></tr>
* <tr><td>SQLServer</td><td><a href="http://technet.microsoft.com/en-us/library/ms175003.aspx">Manual</a></td></tr>
* <tr><td>Db2400</td><td><a href="http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=%2Fcom.ibm.db2.udb.admin.doc%2Fdoc%2Fr0000845.htm">Manual</a></td></tr>
* <tr><td>H2</td><td><a href="http://www.h2database.com/html/functions.html#round">Manual</a></td></tr>
* </table>
*
* @param expression the expression to evaluate
Expand Down Expand Up @@ -645,6 +645,26 @@ public FunctionType getType() {
}



/**
* Helper method to create an instance of the "currentUnixTimeMilliseconds" SQL function.
*
* @return an instance of a unixtime function
*/
public static Function currentUnixTimeMilliseconds() {
return new Function(FunctionType.CURRENT_UNIX_TIME_MILLISECONDS);
}

/**
* Helper method to create an instance of the "clientHost" SQL function.
*
* @return an instance of a clientHost function
*/
public static Function clientHost() {
return new Function(FunctionType.CLIENT_HOST);
}


/**
* @see org.alfasoftware.morf.sql.element.AliasedField#deepCopyInternal(DeepCopyTransformation)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,16 @@ public enum FunctionType {
/**
* Calculates the row number on a partition. Generally used as a Window function
*/
ROW_NUMBER
ROW_NUMBER,

/**
* Unix time, milliseconds since 1st January 1970 UTC
*/

CURRENT_UNIX_TIME_MILLISECONDS,

/**
* Identifiable label for the client machine
*/
CLIENT_HOST
}
Loading