Skip to content

Commit

Permalink
Merge pull request #134 from alfasoftware/more-dsl-fixes-and-omissions
Browse files Browse the repository at this point in the history
More dsl fixes and omissions
  • Loading branch information
capgen628 authored Dec 31, 2020
2 parents deabc01 + 28a41d5 commit 4504bb6
Show file tree
Hide file tree
Showing 23 changed files with 973 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public Connection getConnection() throws SQLException {
*/
@Override
public Connection getConnection(String username, String password) throws SQLException {
log.info("Opening new database connection to [" + AbstractConnectionResources.this.getJdbcUrl() + "] with username [" + username + "]");
log.info("Opening new database connection to [" + AbstractConnectionResources.this.getJdbcUrl() + "] with username [" + username + "] for schema [" + AbstractConnectionResources.this.getSchemaName() + "]");
loadJdbcDriver();
Connection connection = openConnection(username, password);
return log.isDebugEnabled() ? new LoggingConnection(connection) : connection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -959,13 +959,17 @@ private RealName(String dbName, String realName) {
* The user-friendly camel-case name of the object,
* often derived by looking at the comment of that object,
* or in schema descriptions.
*
* @return user-friendly camel-case name
*/
public String getRealName() {
return realName;
}

/**
* The name as retrieved by the JDBC driver.
*
* @return name as expected in the database
*/
public String getDbName() {
return getAName();
Expand Down
410 changes: 292 additions & 118 deletions morf-core/src/main/java/org/alfasoftware/morf/jdbc/SqlDialect.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.joda.time.LocalDate;

import com.google.common.base.Charsets;

/**
* Standard implementations of {@link ValueConverter}.
*
Expand Down Expand Up @@ -285,7 +287,7 @@ public byte[] byteArrayValue(byte[] value) {
}
@Override
public String stringValue(byte[] value) {
return new String(Base64.getEncoder().encode(value));
return new String(Base64.getEncoder().encode(value), Charsets.US_ASCII);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ public T crossJoin(TableReference toTable) {


/**
* @param toTable
* @return
* @param toTable the table to join to
* @return a new select statement with the change applied.
*
* @deprecated Use {@link #crossJoin(TableReference)} to do a cross join;
* or add join conditions for {@link #innerJoin(SelectStatement, Criterion)}
Expand Down Expand Up @@ -467,8 +467,8 @@ public T crossJoin(SelectStatement subSelect) {
}

/**
* @param subSelect
* @return
* @param subSelect the sub select statement to join on to
* @return a new select statement with the change applied.
*
* @deprecated Use {@link #crossJoin(SelectStatement)} to do a cross join;
* or add join conditions for {@link #innerJoin(SelectStatement, Criterion)}
Expand Down Expand Up @@ -549,6 +549,71 @@ public T leftOuterJoin(SelectStatement subSelect, Criterion criterion) {
}


/**
* Specifies a full outer join to a table:
*
* <blockquote><pre>
* TableReference foo = tableRef("Foo");
* TableReference bar = tableRef("Bar");
* SelectStatement statement = select()
* .from(foo)
* .fullOuterJoin(bar, foo.field(&quot;id&quot;).eq(bar.field(&quot;fooId&quot;)));</pre>
* </blockquote>
*
* @param toTable the table to join to
* @param criterion the criteria on which to join the tables
* @return a new select statement with the change applied.
*/
public T fullOuterJoin(TableReference toTable, Criterion criterion) {
return copyOnWriteOrMutate(
b -> b.fullOuterJoin(toTable, criterion),
() -> joins.add(new Join(JoinType.FULL_OUTER_JOIN, toTable, criterion))
);
}


/**
* Specifies an full outer join to a subselect:
*
* <blockquote><pre>
* TableReference sale = tableRef("Sale");
* TableReference customer = tableRef("Customer");
*
* // Define the subselect - a group by showing total sales by age in the
* // previous month.
* SelectStatement amountsByAgeLastMonth = select(field("age"), sum(field("amount")))
* .from(sale)
* .innerJoin(customer, sale.field("customerId").eq(customer.field("id")))
* .where(sale.field("month").eq(5))
* .groupBy(customer.field("age")
* .alias("amountByAge");
*
* // The outer select, showing each sale this month as a percentage of the sales
* // to that age the previous month
* SelectStatement outer = select(
* sale.field("id"),
* sale.field("amount")
* // May cause division by zero (!)
* .divideBy(isNull(amountsByAgeLastMonth.asTable().field("amount"), 0))
* .multiplyBy(literal(100))
* )
* .from(sale)
* .innerJoin(customer, sale.field("customerId").eq(customer.field("id")))
* .fullOuterJoin(amountsByAgeLastMonth, amountsByAgeLastMonth.asTable().field("age").eq(customer.field("age")));
* </pre></blockquote>
*
* @param subSelect the sub select statement to join on to
* @param criterion the criteria on which to join the tables
* @return a new select statement with the change applied.
*/
public T fullOuterJoin(SelectStatement subSelect, Criterion criterion) {
return copyOnWriteOrMutate(
b -> b.fullOuterJoin(subSelect, criterion),
() -> joins.add(new Join(JoinType.FULL_OUTER_JOIN, subSelect, criterion))
);
}


/**
* Specifies the where criteria:
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ public T crossJoin(TableReference toTable) {


/**
* @param toTable
* @return
* @param toTable the table to join to
* @return this, for method chaining.
*
* @deprecated Use {@link #crossJoin(TableReference)} to do a cross join;
* or add join conditions for {@link #innerJoin(TableReference, Criterion)}
Expand Down Expand Up @@ -352,8 +352,8 @@ public T crossJoin(SelectStatement subSelect) {


/**
* @param subSelect
* @return
* @param subSelect the sub select statement to join on to
* @return this, for method chaining.
*
* @deprecated Use {@link #crossJoin(SelectStatement)} to do a cross join;
* or add join conditions for {@link #innerJoin(SelectStatement, Criterion)}
Expand Down Expand Up @@ -426,6 +426,67 @@ public T leftOuterJoin(SelectStatement subSelect, Criterion criterion) {
}


/**
* Specifies a full outer join to a table:
*
* <blockquote><pre>
* TableReference foo = tableRef("Foo");
* TableReference bar = tableRef("Bar");
* SelectStatement statement = select()
* .from(foo)
* .fullOuterJoin(bar, foo.field(&quot;id&quot;).eq(bar.field(&quot;fooId&quot;)));</pre>
* </blockquote>
*
* @param toTable the table to join to
* @param criterion the criteria on which to join the tables
* @return this, for method chaining.
*/
public T fullOuterJoin(TableReference toTable, Criterion criterion) {
joins.add(new Join(JoinType.FULL_OUTER_JOIN, toTable, criterion));
return castToChild(this);
}


/**
* Specifies an full outer join to a subselect:
*
* <blockquote><pre>
* TableReference sale = tableRef("Sale");
* TableReference customer = tableRef("Customer");
*
* // Define the subselect - a group by showing total sales by age in the
* // previous month.
* SelectStatement amountsByAgeLastMonth = select(field("age"), sum(field("amount")))
* .from(sale)
* .innerJoin(customer, sale.field("customerId").eq(customer.field("id")))
* .where(sale.field("month").eq(5))
* .groupBy(customer.field("age")
* .alias("amountByAge");
*
* // The outer select, showing each sale this month as a percentage of the sales
* // to that age the previous month
* SelectStatement outer = select(
* sale.field("id"),
* sale.field("amount")
* // May cause division by zero (!)
* .divideBy(isNull(amountsByAgeLastMonth.asTable().field("amount"), 0))
* .multiplyBy(literal(100))
* )
* .from(sale)
* .innerJoin(customer, sale.field("customerId").eq(customer.field("id")))
* .fullOuterJoin(amountsByAgeLastMonth, amountsByAgeLastMonth.asTable().field("age").eq(customer.field("age")));
* </pre></blockquote>
*
* @param subSelect the sub select statement to join on to
* @param criterion the criteria on which to join the tables
* @return this, for method chaining.
*/
public T fullOuterJoin(SelectStatement subSelect, Criterion criterion) {
joins.add(new Join(JoinType.FULL_OUTER_JOIN, subSelect, criterion));
return castToChild(this);
}


/**
* Specifies the where criteria:
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,32 @@ public SelectStatement groupBy(AliasedFieldBuilder field, AliasedFieldBuilder...
}


/**
* Specifies that the records should be grouped by the specified fields.
*
* <blockquote><pre>
* select()
* .from(tableRef("Foo"))
* .groupBy(groupByFields);</pre></blockquote>
*
* @param fields the fields to group by
* @return a new select statement with the change applied.
*/
public SelectStatement groupBy(Iterable<? extends AliasedFieldBuilder> fields) {
return copyOnWriteOrMutate(
(SelectStatementBuilder b) -> b.groupBy(fields),
() -> {
if (fields == null) {
throw new IllegalArgumentException("Field was null in group by clause");
}

// Add the list
groupBys.addAll(Builder.Helper.buildAll(fields));
}
);
}


/**
* Filters the grouped records by some criteria.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,25 @@ public SelectStatementBuilder groupBy(AliasedFieldBuilder field, AliasedFieldBui
}


/**
* Specifies that the records should be grouped by the specified fields.
*
* <blockquote><pre>
* select()
* .from(tableRef("Foo"))
* .groupBy(field("age"));</pre></blockquote>
*
* @param fields the fields to group by
* @return this, for method chaining.
*/
public SelectStatementBuilder groupBy(Iterable<? extends AliasedFieldBuilder> fields) {
// Add the list
groupBys.addAll(Builder.Helper.buildAll(Lists.newArrayList(fields)));

return this;
}


/**
* Filters the grouped records by some criteria.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public Criterion(Operator operator, Iterable<Criterion> criteria) {
* @param criterion the first criterion in this criterion
* @param criteria subsequent criteria
*/
public Criterion(Operator operator, Criterion criterion, Criterion...criteria) {
public Criterion(Operator operator, Criterion criterion, Criterion... criteria) {
this(
operator,
criterion == null
Expand Down Expand Up @@ -222,6 +222,21 @@ public static Criterion and(Criterion criterion, Criterion... criteria) {
return new Criterion(Operator.AND, criterion, criteria);
}


/**
* Helper method to create a new "AND" expression.
*
* <blockquote><pre>
* Criterion.and(listOfCriterions);</pre></blockquote>
*
* @param criteria the criteria
* @return a new Criterion object
*/
public static Criterion and(Iterable<Criterion> criteria) {
return new Criterion(Operator.AND, criteria);
}


/**
* Helper method to create a new "OR" expression.
*
Expand All @@ -236,6 +251,20 @@ public static Criterion or(Criterion criterion, Criterion... criteria) {
return new Criterion(Operator.OR, criterion, criteria);
}


/**
* Helper method to create a new "OR" expression.
*
* <blockquote><pre>
* Criterion.or(listOfCriterions);</pre></blockquote>
*
* @param criteria the criteria
* @return a new Criterion object
*/
public static Criterion or(Iterable<Criterion> criteria) {
return new Criterion(Operator.OR, criteria);
}

/**
* Helper method to create a new "NOT" expression.
*
Expand Down Expand Up @@ -338,8 +367,8 @@ public static Criterion in(AliasedField field, Object... values) {
* @param values the list of values (the right-hand side of the expression)
* @return a new Criterion object
*/
public static Criterion in(AliasedField field, List<Object> values) {
return new Criterion(Operator.IN, field, values);
public static Criterion in(AliasedField field, Iterable<? extends Object> values) {
return new Criterion(Operator.IN, field, ImmutableList.copyOf(values));
}


Expand Down
Loading

0 comments on commit 4504bb6

Please sign in to comment.