Skip to content

Commit 5f522fc

Browse files
committed
resolve attributes
1 parent 993da8d commit 5f522fc

6 files changed

Lines changed: 29 additions & 8 deletions

File tree

hyperquery/src/main/java/org/slowcoders/hyperquery/impl/JoinNode.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import java.util.ArrayList;
44
import java.util.HashMap;
5+
import java.util.TreeMap;
56

67
class ViewNode {
78
final HashMap<String, String> usedAttributes = new HashMap<>();
89

9-
final HashMap<String, JoinNode> joins = new HashMap<>();
10+
final TreeMap<String, JoinNode> joins = new TreeMap<>();
1011

1112
protected JoinNode getJoin(String aliasQualifier) {
1213
return joins.get(aliasQualifier);
@@ -31,9 +32,7 @@ class JoinNode {
3132

3233
final boolean addUsedAttribute(String alias, String expr) {
3334
for (int i = attrLevel; --i >= 0; ) {
34-
if (views.get(i).usedAttributes.containsKey(alias)) {
35-
return false;
36-
}
35+
views.get(i).usedAttributes.remove(alias);
3736
}
3837
for (int i = views.size(); --i >= attrLevel; ) {
3938
if (views.get(i).usedAttributes.containsKey(alias)) {

hyperquery/src/main/java/org/slowcoders/hyperquery/impl/QAttribute.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,24 @@
77
public class QAttribute {
88

99
private final String sql;
10+
boolean inProgress = false;
1011
public QAttribute(String sql) {
1112
this.sql = sql;
1213
}
1314

1415
public String getExpression() { return sql; }
1516

16-
public final String inflateStatement(SqlBuilder generator, String paramName) {
17-
return PredicateTranslator.translate(generator, paramName, sql);
17+
public synchronized final String inflateStatement(SqlBuilder generator, String paramName) {
18+
if (inProgress) {
19+
throw new IllegalStateException("Circular attribute reference is found.");
20+
}
21+
try {
22+
inProgress = true;
23+
String expr = PredicateTranslator.translate(generator, paramName, sql);
24+
return expr;
25+
} finally {
26+
inProgress = false;
27+
}
1828
}
1929

2030
public static QAttribute of(String statement) {

hyperquery/src/main/java/org/slowcoders/hyperquery/impl/SqlBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ String resolveLambda(String path, List<String> callArgs) {
9494
});
9595
}
9696

97+
9798
public String resolveProperty(String path) {
9899
return this.resolveQualifiedAlias(path, (model, name) -> {
99100
QAttribute attr = model.getAttribute(name);
@@ -155,7 +156,7 @@ private void genFrom(StringBuilder sb, StringBuilder sbWith) {
155156
genJoin(sb, sbWith, currView.joins);
156157
}
157158

158-
private void genJoin(StringBuilder sb, StringBuilder sbWith, HashMap<String, JoinNode> joinNodes) {
159+
private void genJoin(StringBuilder sb, StringBuilder sbWith, Map<String, JoinNode> joinNodes) {
159160
for (Map.Entry<String, JoinNode> entry : joinNodes.entrySet()) {
160161
String alias = entry.getKey();
161162
JoinNode node = entry.getValue();

sample-app/src/main/java/org/slowcoders/hyperql/sample/hq/bookstore/AuthorDto.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public class AuthorDto implements QRecord<Author> {
2828

2929
@QColumn("bookPriceAvr")
3030
private int bookPriceAvr;
31+
32+
// @QColumn("attr2")
33+
// private int attr2;
3134
}
3235

3336

sample-app/src/main/java/org/slowcoders/hyperql/sample/hq/bookstore/model/Author.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ select sum(price) * @.bookCount from bookstore.book bk where bk.author_id = @.id
2828
@.bookCount * @.bookPriceAvr
2929
""");
3030

31+
static final QAttribute attr1 = Property.formula("""
32+
@.attr2 * 30
33+
""");
34+
static final QAttribute attr2 = Property.formula("""
35+
@.attr1 * 30
36+
""");
37+
38+
3139
@Id
3240
@Column(name = "id", nullable = false)
3341
@PKColumn("id")

tutorial+test/tests/unit/hql.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import axios from "axios";
44

55
describe('Hyper Query operations', () => {
66

7-
initSampleDB();
7+
// initSampleDB();
88

99
const baseUrl = "http://localhost:7007/api/hq/authors"
1010

0 commit comments

Comments
 (0)