Skip to content

Commit 5ccf997

Browse files
committed
CAY-2975 Mnemonic table aliases in generated SQL
(also required a great deal of refactoring between SQLAppendable and SQLGenerationContext)
1 parent e476fc6 commit 5ccf997

68 files changed

Lines changed: 558 additions & 290 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

RELEASE-NOTES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ CAY-2970 Tighten deferred value resolution contract on commit
2424
CAY-2971 Remove extra spaces within SQL parenthesis
2525
CAY-2972 Fewer parentheses in generated SQL
2626
CAY-2974 CayenneSqlException with a reference to translated query
27+
CAY-2975 Mnemonic table aliases in generated SQL
2728

2829
Bug Fixes:
2930

cayenne/src/main/java/org/apache/cayenne/access/sqlbuilder/DefaultSQLAppendable.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,12 @@
2727
public class DefaultSQLAppendable implements SQLAppendable {
2828

2929
final StringBuilder builder;
30-
private final SQLGenerationContext context;
3130
private final QuotingStrategy quotingStrategy;
3231
private boolean suppressNextSeparator;
3332

34-
public DefaultSQLAppendable(SQLGenerationContext context) {
33+
public DefaultSQLAppendable(QuotingStrategy quotingStrategy) {
3534
this.builder = new StringBuilder();
36-
this.context = context;
37-
this.quotingStrategy = resolveQuotes(context);
38-
}
39-
40-
private static QuotingStrategy resolveQuotes(SQLGenerationContext context) {
41-
return context == null
42-
? QuotingStrategy.NONE
43-
: context.getAdapter().getQuotingStrategy(context.getRootDbEntity());
35+
this.quotingStrategy = quotingStrategy == null ? QuotingStrategy.NONE : quotingStrategy;
4436
}
4537

4638
@Override
@@ -89,11 +81,6 @@ public SQLAppendable suppressNextTokenSeparator() {
8981
return this;
9082
}
9183

92-
@Override
93-
public SQLGenerationContext getContext() {
94-
return context;
95-
}
96-
9784
@Override
9885
public String getSql() {
9986
return builder.toString();

cayenne/src/main/java/org/apache/cayenne/access/sqlbuilder/SQLAppendable.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,5 @@ default SQLAppendable append(CharSequence csq, int start, int end) {
5454

5555
SQLAppendable appendQuoted(String str);
5656

57-
SQLGenerationContext getContext();
58-
5957
String getSql();
6058
}

cayenne/src/main/java/org/apache/cayenne/access/sqlbuilder/SQLGenerationContext.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.apache.cayenne.dba.DbAdapter;
2424
import org.apache.cayenne.map.DbEntity;
2525

26-
import java.util.Collection;
26+
import java.util.List;
2727

2828
/**
2929
* @since 4.2
@@ -32,7 +32,16 @@ public interface SQLGenerationContext {
3232

3333
DbAdapter getAdapter();
3434

35-
Collection<PSParameter> getBindings();
35+
List<PSParameter<?>> getBindings();
3636

3737
DbEntity getRootDbEntity();
38+
39+
/**
40+
* Returns true if the statement being generated uses a single table, in which case table nodes
41+
* and column nodes omit the table alias / prefix for readability (e.g. {@code SELECT NAME FROM
42+
* ARTIST} rather than {@code SELECT a.NAME FROM ARTIST a}).
43+
*
44+
* @since 5.0
45+
*/
46+
boolean isSingleTableSQL();
3847
}

cayenne/src/main/java/org/apache/cayenne/access/sqlbuilder/SQLGenerationVisitor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@
2727
public class SQLGenerationVisitor implements NodeTreeVisitor {
2828

2929
private final SQLAppendable appendable;
30+
private final SQLGenerationContext context;
3031

31-
public SQLGenerationVisitor(SQLAppendable appendable) {
32+
public SQLGenerationVisitor(SQLAppendable appendable, SQLGenerationContext context) {
3233
this.appendable = appendable;
34+
this.context = context;
3335
}
3436

3537
@Override
3638
public boolean onNodeStart(Node node) {
37-
node.append(appendable);
39+
node.append(appendable, context);
3840
node.appendChildrenStart(appendable);
3941
return true;
4042
}

cayenne/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/AliasedNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.cayenne.access.sqlbuilder.NodeTreeVisitor;
2323
import org.apache.cayenne.access.sqlbuilder.SQLAppendable;
24+
import org.apache.cayenne.access.sqlbuilder.SQLGenerationContext;
2425

2526
import java.util.Objects;
2627

@@ -41,7 +42,7 @@ public Node copy() {
4142
}
4243

4344
@Override
44-
public SQLAppendable append(SQLAppendable buffer) {
45+
public SQLAppendable append(SQLAppendable buffer, SQLGenerationContext context) {
4546
if(skipContent()) {
4647
buffer.appendTokenSeparator().append(alias);
4748
}

cayenne/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/BitwiseNotNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
package org.apache.cayenne.access.sqlbuilder.sqltree;
2121

2222
import org.apache.cayenne.access.sqlbuilder.SQLAppendable;
23+
import org.apache.cayenne.access.sqlbuilder.SQLGenerationContext;
2324

2425
/**
2526
* @since 4.2
2627
*/
2728
public class BitwiseNotNode extends ExpressionNode {
2829

2930
@Override
30-
public SQLAppendable append(SQLAppendable buffer) {
31+
public SQLAppendable append(SQLAppendable buffer, SQLGenerationContext context) {
3132
return buffer.append('~');
3233
}
3334

cayenne/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/CaseNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
package org.apache.cayenne.access.sqlbuilder.sqltree;
2121

2222
import org.apache.cayenne.access.sqlbuilder.SQLAppendable;
23+
import org.apache.cayenne.access.sqlbuilder.SQLGenerationContext;
2324

2425
/**
2526
* @since 5.0
2627
*/
2728
public class CaseNode extends Node {
2829

2930
@Override
30-
public SQLAppendable append(SQLAppendable buffer) {
31+
public SQLAppendable append(SQLAppendable buffer, SQLGenerationContext context) {
3132
return buffer.appendTokenSeparator().append("CASE");
3233
}
3334

cayenne/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/ColumnNode.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.cayenne.access.sqlbuilder.sqltree;
2121

2222
import org.apache.cayenne.access.sqlbuilder.SQLAppendable;
23+
import org.apache.cayenne.access.sqlbuilder.SQLGenerationContext;
2324
import org.apache.cayenne.map.DbAttribute;
2425

2526
import java.util.Objects;
@@ -43,9 +44,10 @@ public ColumnNode(String table, String column, String alias, DbAttribute attribu
4344
}
4445

4546
@Override
46-
public SQLAppendable append(SQLAppendable buffer) {
47+
public SQLAppendable append(SQLAppendable buffer, SQLGenerationContext context) {
4748
buffer.appendTokenSeparator();
48-
if (table != null) {
49+
// omit the table prefix for a single-table statement
50+
if (table != null && (context == null || !context.isSingleTableSQL())) {
4951
buffer.appendQuoted(table).append('.');
5052
}
5153
buffer.appendQuoted(column);

cayenne/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/DeleteNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.cayenne.access.sqlbuilder.sqltree;
2020

2121
import org.apache.cayenne.access.sqlbuilder.SQLAppendable;
22+
import org.apache.cayenne.access.sqlbuilder.SQLGenerationContext;
2223

2324
/**
2425
* @since 4.2
@@ -30,7 +31,7 @@ public Node copy() {
3031
}
3132

3233
@Override
33-
public SQLAppendable append(SQLAppendable buffer) {
34+
public SQLAppendable append(SQLAppendable buffer, SQLGenerationContext context) {
3435
return buffer.append("DELETE FROM");
3536
}
3637
}

0 commit comments

Comments
 (0)