Skip to content

Commit c12fd87

Browse files
committed
QuotingAppendable refactoring:
0. Rename to SQLAppendable (it is not just quoting, it is a full SQL buffer) 1. Do not extend Java Appendable. It is better for us to control the signature 2. Take String instead of CharSequence. This is a cheaper operation on the StringBuilder side 3. Explicit method for result extraction instead of toString() 4. Collapse SQLAppendable class hierarchy to a single class
1 parent b88011a commit c12fd87

120 files changed

Lines changed: 394 additions & 431 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.

cayenne/src/main/java/org/apache/cayenne/access/sqlbuilder/StringBuilderAppendable.java renamed to cayenne/src/main/java/org/apache/cayenne/access/sqlbuilder/DefaultSQLAppendable.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,57 +19,70 @@
1919

2020
package org.apache.cayenne.access.sqlbuilder;
2121

22+
import org.apache.cayenne.dba.QuotingStrategy;
23+
2224
/**
2325
* @since 4.2
2426
*/
25-
public class StringBuilderAppendable implements QuotingAppendable {
27+
public class DefaultSQLAppendable implements SQLAppendable {
2628

2729
protected final StringBuilder builder;
30+
protected final SQLGenerationContext context;
31+
protected final QuotingStrategy quotingStrategy;
2832

29-
public StringBuilderAppendable() {
33+
public DefaultSQLAppendable(SQLGenerationContext context) {
3034
this.builder = new StringBuilder();
35+
this.context = context;
36+
this.quotingStrategy = context == null ? null : context.getAdapter().getQuotingStrategy();
3137
}
3238

3339
@Override
34-
public QuotingAppendable append(CharSequence csq) {
35-
builder.append(csq);
40+
public SQLAppendable append(String str) {
41+
builder.append(str);
3642
return this;
3743
}
3844

3945
@Override
40-
public QuotingAppendable append(CharSequence csq, int start, int end) {
41-
builder.append(csq, start, end);
46+
public SQLAppendable append(char c) {
47+
builder.append(c);
4248
return this;
4349
}
4450

4551
@Override
46-
public QuotingAppendable append(char c) {
52+
public SQLAppendable append(int c) {
4753
builder.append(c);
4854
return this;
4955
}
5056

5157
@Override
52-
public QuotingAppendable append(int c) {
53-
builder.append(c);
58+
public SQLAppendable appendQuoted(String str) {
59+
if (quotingStrategy == null) {
60+
builder.append(str);
61+
} else {
62+
quotingStrategy.quotedIdentifier(context.getRootDbEntity(), str, builder);
63+
}
5464
return this;
5565
}
5666

5767
@Override
58-
public QuotingAppendable appendQuoted(CharSequence csq) {
59-
builder.append(csq);
60-
return this;
68+
public SQLGenerationContext getContext() {
69+
return context;
6170
}
6271

6372
@Override
64-
public SQLGenerationContext getContext() {
65-
return null;
73+
public String getSql() {
74+
return builder.toString();
6675
}
6776

6877
@Override
6978
public String toString() {
70-
return builder.toString();
79+
return getSql();
7180
}
7281

82+
/**
83+
* @deprecated unused
84+
*/
85+
@Deprecated(since = "5.0", forRemoval = true)
7386
public StringBuilder unwrap() {
7487
return builder;
7588
}

cayenne/src/main/java/org/apache/cayenne/access/sqlbuilder/QuotingAppendable.java renamed to cayenne/src/main/java/org/apache/cayenne/access/sqlbuilder/SQLAppendable.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,28 @@
2020
package org.apache.cayenne.access.sqlbuilder;
2121

2222
/**
23-
* @since 4.2
23+
* @since 5.0
2424
*/
25-
public interface QuotingAppendable extends Appendable {
25+
public interface SQLAppendable {
2626

27-
@Override
28-
QuotingAppendable append(CharSequence csq);
27+
SQLAppendable append(String str);
2928

30-
@Override
31-
QuotingAppendable append(CharSequence csq, int start, int end);
29+
/**
30+
* @deprecated unused; no SQL tree node appends a sub-range. Kept for binary compatibility,
31+
* now delegating to {@link #append(String)}.
32+
*/
33+
@Deprecated(since = "5.0", forRemoval = true)
34+
default SQLAppendable append(CharSequence csq, int start, int end) {
35+
return append(csq.subSequence(start, end).toString());
36+
}
3237

33-
@Override
34-
QuotingAppendable append(char c);
38+
SQLAppendable append(char c);
3539

36-
QuotingAppendable append(int i);
40+
SQLAppendable append(int i);
3741

38-
QuotingAppendable appendQuoted(CharSequence csq);
42+
SQLAppendable appendQuoted(String str);
3943

4044
SQLGenerationContext getContext();
45+
46+
String getSql();
4147
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919

2020
package org.apache.cayenne.access.sqlbuilder;
2121

22-
import java.util.Collection;
23-
2422
import org.apache.cayenne.access.translator.ParameterBinding;
2523
import org.apache.cayenne.dba.DbAdapter;
2624
import org.apache.cayenne.dba.QuotingStrategy;
2725
import org.apache.cayenne.map.DbEntity;
2826

27+
import java.util.Collection;
28+
2929
/**
3030
* @since 4.2
3131
*/
@@ -35,7 +35,13 @@ public interface SQLGenerationContext {
3535

3636
Collection<ParameterBinding> getBindings();
3737

38-
QuotingStrategy getQuotingStrategy();
38+
/**
39+
* @deprecated use {@link DbAdapter#getQuotingStrategy()}
40+
*/
41+
@Deprecated(since = "5.0", forRemoval = true)
42+
default QuotingStrategy getQuotingStrategy() {
43+
return getAdapter().getQuotingStrategy();
44+
}
3945

4046
DbEntity getRootDbEntity();
4147
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
*/
2727
public class SQLGenerationVisitor implements NodeTreeVisitor {
2828

29-
private final QuotingAppendable appendable;
29+
private final SQLAppendable appendable;
3030

31-
public SQLGenerationVisitor(QuotingAppendable appendable) {
31+
public SQLGenerationVisitor(SQLAppendable appendable) {
3232
this.appendable = appendable;
3333
}
3434

@@ -57,7 +57,7 @@ public void onNodeEnd(Node node) {
5757
}
5858

5959
public String getSQLString() {
60-
return appendable.toString();
60+
return appendable.getSql();
6161
}
6262

6363
}

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

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

2222
import org.apache.cayenne.access.sqlbuilder.NodeTreeVisitor;
23-
import org.apache.cayenne.access.sqlbuilder.QuotingAppendable;
23+
import org.apache.cayenne.access.sqlbuilder.SQLAppendable;
2424

2525
import java.util.Objects;
2626

@@ -41,7 +41,7 @@ public Node copy() {
4141
}
4242

4343
@Override
44-
public QuotingAppendable append(QuotingAppendable buffer) {
44+
public SQLAppendable append(SQLAppendable buffer) {
4545
if(skipContent()) {
4646
buffer.append(' ').append(alias);
4747
}
@@ -59,7 +59,7 @@ public void visit(NodeTreeVisitor visitor) {
5959
}
6060

6161
@Override
62-
public void appendChildrenEnd(QuotingAppendable buffer) {
62+
public void appendChildrenEnd(SQLAppendable buffer) {
6363
if(skipContent()){
6464
return;
6565
}

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

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

2222

23-
import org.apache.cayenne.access.sqlbuilder.QuotingAppendable;
23+
import org.apache.cayenne.access.sqlbuilder.SQLAppendable;
2424

2525
import java.util.Objects;
2626

@@ -36,7 +36,7 @@ public BetweenNode(boolean not) {
3636
}
3737

3838
@Override
39-
public void appendChildrenSeparator(QuotingAppendable buffer, int childIdx) {
39+
public void appendChildrenSeparator(SQLAppendable buffer, int childIdx) {
4040
if (childIdx == 0) {
4141
if (not) {
4242
buffer.append(" NOT");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919

2020
package org.apache.cayenne.access.sqlbuilder.sqltree;
2121

22-
import org.apache.cayenne.access.sqlbuilder.QuotingAppendable;
22+
import org.apache.cayenne.access.sqlbuilder.SQLAppendable;
2323

2424
/**
2525
* @since 4.2
2626
*/
2727
public class BitwiseNotNode extends ExpressionNode {
2828

2929
@Override
30-
public QuotingAppendable append(QuotingAppendable buffer) {
30+
public SQLAppendable append(SQLAppendable buffer) {
3131
return buffer.append('~');
3232
}
3333

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919

2020
package org.apache.cayenne.access.sqlbuilder.sqltree;
2121

22-
import org.apache.cayenne.access.sqlbuilder.QuotingAppendable;
22+
import org.apache.cayenne.access.sqlbuilder.SQLAppendable;
2323

2424
/**
2525
* @since 5.0
2626
*/
2727
public class CaseNode extends Node {
2828

2929
@Override
30-
public QuotingAppendable append(QuotingAppendable buffer) {
30+
public SQLAppendable append(SQLAppendable buffer) {
3131
return buffer.append(" CASE");
3232
}
3333

@@ -37,7 +37,7 @@ public Node copy() {
3737
}
3838

3939
@Override
40-
public void appendChildrenEnd(QuotingAppendable buffer) {
40+
public void appendChildrenEnd(SQLAppendable buffer) {
4141
buffer.append(" END");
4242
}
4343
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package org.apache.cayenne.access.sqlbuilder.sqltree;
2121

22-
import org.apache.cayenne.access.sqlbuilder.QuotingAppendable;
22+
import org.apache.cayenne.access.sqlbuilder.SQLAppendable;
2323
import org.apache.cayenne.map.DbAttribute;
2424

2525
import java.util.Objects;
@@ -43,7 +43,7 @@ public ColumnNode(String table, String column, String alias, DbAttribute attribu
4343
}
4444

4545
@Override
46-
public QuotingAppendable append(QuotingAppendable buffer) {
46+
public SQLAppendable append(SQLAppendable buffer) {
4747
buffer.append(' ');
4848
if (table != null) {
4949
buffer.appendQuoted(table).append('.');

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
****************************************************************/
1919
package org.apache.cayenne.access.sqlbuilder.sqltree;
2020

21-
import org.apache.cayenne.access.sqlbuilder.QuotingAppendable;
21+
import org.apache.cayenne.access.sqlbuilder.SQLAppendable;
2222

2323
/**
2424
* @since 4.2
@@ -30,7 +30,7 @@ public Node copy() {
3030
}
3131

3232
@Override
33-
public QuotingAppendable append(QuotingAppendable buffer) {
33+
public SQLAppendable append(SQLAppendable buffer) {
3434
return buffer.append("DELETE FROM");
3535
}
3636
}

0 commit comments

Comments
 (0)