Skip to content

Commit 2123f2f

Browse files
committed
adds SqlBuilder
1 parent 4942731 commit 2123f2f

15 files changed

Lines changed: 729 additions & 421 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
grammar Selection;
2+
3+
/* =======================
4+
* Parser rules
5+
* ======================= */
6+
expr
7+
: (macroInvocation
8+
| mapperParameter
9+
| property
10+
| tuple
11+
| trivia
12+
| string
13+
| atom)+
14+
;
15+
16+
macroInvocation
17+
: property tuple
18+
;
19+
20+
mapperParameter
21+
: '#{' Identifier ('.' Identifier)* '}'
22+
;
23+
24+
property
25+
: PropertyLiteral
26+
;
27+
28+
trivia
29+
: Tribia
30+
;
31+
32+
tuple
33+
: '(' expr (',' expr)* ')'
34+
;
35+
36+
atom
37+
: Identifier
38+
;
39+
40+
string
41+
: StringLiteral
42+
;
43+
44+
/* =======================
45+
* Lexer rules
46+
* ======================= */
47+
48+
PropertyLiteral
49+
: Alias '.' Identifier
50+
;
51+
52+
Alias
53+
: '@' | ('@' Identifier)+
54+
;
55+
56+
Identifier
57+
: [a-zA-Z_][a-zA-Z0-9_]*
58+
;
59+
60+
Tribia
61+
: ~[a-zA-Z,?@()]+
62+
;
63+
64+
StringLiteral
65+
: '"' ( ~["\\] | '\\' . )* '"'
66+
| '\'' ( ~['\\] | '\\' . )* '\''
67+
;
68+

hyperquery/src/main/java/org/slowcoders/hyperquery/core/QFilter.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.slowcoders.hyperquery.core;
22

3-
import org.slowcoders.hyperquery.impl.HSchema;
43
import org.slowcoders.hyperquery.impl.QCriteria;
4+
import org.slowcoders.hyperquery.impl.SqlBuilder;
55

66
import java.lang.annotation.Repeatable;
77
import java.lang.annotation.Retention;
@@ -49,21 +49,4 @@ public class QFilter<T extends QEntity<?>> {
4949
}
5050
}
5151

52-
//==============================================================================//
53-
54-
private String __sql__;
55-
56-
/*internal*/ public final void setSql(String sql) {
57-
this.__sql__ = sql;
58-
}
59-
60-
public QCriteria buildCriteria() {
61-
return QCriteria.buildCriteria(this, "@");
62-
}
63-
64-
65-
public String toString() {
66-
QCriteria criteria = buildCriteria();
67-
return criteria.toString();
68-
}
6952
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.slowcoders.hyperquery.impl;
2+
3+
class ColumnMapping {
4+
final String columnName;
5+
final String fieldName;
6+
7+
public ColumnMapping(String columnName, String fieldName) {
8+
this.columnName = columnName;
9+
this.fieldName = fieldName;
10+
}
11+
}

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

Lines changed: 0 additions & 211 deletions
This file was deleted.

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

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
package org.slowcoders.hyperquery.impl;
22

3+
import jakarta.persistence.Column;
4+
import org.slowcoders.hyperquery.core.QColumn;
5+
import org.slowcoders.hyperquery.core.QEntity;
36
import org.slowcoders.hyperquery.core.QJoin;
7+
import org.slowcoders.hyperquery.core.QRecord;
8+
9+
import java.lang.reflect.Field;
10+
import java.lang.reflect.ParameterizedType;
11+
import java.lang.reflect.Type;
12+
import java.util.Collection;
413

514
public abstract class HModel {
615

@@ -13,8 +22,8 @@ public void initialize() {
1322

1423
protected abstract String getTableName();
1524

16-
protected String translateProperty(String property) {
17-
return property;
25+
protected QAttribute getAttribute(String property) {
26+
return null;
1827
}
1928

2029
protected QJoin getJoin(String alias) {
@@ -24,4 +33,44 @@ protected QJoin getJoin(String alias) {
2433
protected QLambda getLambda(String alias) {
2534
return null;
2635
}
36+
37+
static Class<? extends QRecord<?>> getElementType(Field f) {
38+
if (f.getType().isArray()) return (Class<? extends QRecord<?>>) f.getType().getComponentType();
39+
40+
Type type = f.getGenericType();
41+
if (type instanceof ParameterizedType) {
42+
ParameterizedType pType = (ParameterizedType) type;
43+
Type[] typeArguments = pType.getActualTypeArguments();
44+
if (typeArguments.length > 0 && typeArguments[0] instanceof Class) {
45+
return (Class<? extends QRecord<?>>) typeArguments[0];
46+
}
47+
}
48+
return (Class<? extends QRecord<?>>) f.getType();
49+
}
50+
51+
static class Helper implements QEntity<Helper> {
52+
static String getColumnName(Field f) {
53+
QColumn anno = f.getAnnotation(QColumn.class);
54+
if (anno != null) return anno.value();
55+
56+
PKColumn pk = f.getAnnotation(PKColumn.class);
57+
if (pk != null) return pk.value();
58+
59+
TColumn tcol = f.getAnnotation(TColumn.class);
60+
if (tcol != null) return tcol.value();
61+
62+
Column col = f.getAnnotation(Column.class);
63+
if (col != null) return col.name();
64+
65+
return null;
66+
}
67+
68+
public static boolean isUniqueKey(Field f) {
69+
return f.getAnnotation(PKColumn.class) != null;
70+
}
71+
72+
public static boolean isCollectionType(Field f) {
73+
return f.getType().isArray() || Collection.class.isAssignableFrom(f.getType());
74+
}
75+
}
2776
}

0 commit comments

Comments
 (0)