19
19
import com .amazonaws .services .dynamodbv2 .model .ComparisonOperator ;
20
20
import org .socialsignin .spring .data .dynamodb .core .DynamoDBOperations ;
21
21
import org .socialsignin .spring .data .dynamodb .query .Query ;
22
+ import org .socialsignin .spring .data .dynamodb .repository .ExpressionAttribute ;
22
23
import org .socialsignin .spring .data .dynamodb .repository .QueryConstants ;
23
24
import org .socialsignin .spring .data .dynamodb .repository .support .DynamoDBEntityInformation ;
24
25
import org .socialsignin .spring .data .dynamodb .repository .support .DynamoDBIdIsHashAndRangeKeyEntityInformation ;
25
26
import org .springframework .data .mapping .PropertyPath ;
27
+ import org .springframework .data .repository .query .Parameter ;
26
28
import org .springframework .data .repository .query .ParameterAccessor ;
29
+ import org .springframework .data .repository .query .ParametersParameterAccessor ;
27
30
import org .springframework .data .repository .query .parser .AbstractQueryCreator ;
28
31
import org .springframework .data .repository .query .parser .Part ;
29
32
import org .springframework .data .repository .query .parser .Part .IgnoreCaseType ;
30
33
import org .springframework .data .repository .query .parser .PartTree ;
31
34
import org .springframework .util .Assert ;
32
35
import org .springframework .util .ClassUtils ;
33
36
import org .springframework .util .ObjectUtils ;
37
+ import org .springframework .util .StringUtils ;
34
38
35
39
import java .util .Arrays ;
40
+ import java .util .HashMap ;
36
41
import java .util .Iterator ;
42
+ import java .util .Map ;
37
43
import java .util .Optional ;
38
44
39
45
/**
@@ -48,35 +54,71 @@ public abstract class AbstractDynamoDBQueryCreator<T, ID, R>
48
54
protected final DynamoDBOperations dynamoDBOperations ;
49
55
protected final Optional <String > projection ;
50
56
protected final Optional <Integer > limit ;
57
+ protected final Optional <String > filterExpression ;
58
+ protected final ExpressionAttribute [] expressionAttributeNames ;
59
+ protected final ExpressionAttribute [] expressionAttributeValues ;
60
+ protected final Map <String , String > mappedExpressionValues = new HashMap <>();
51
61
protected final QueryConstants .ConsistentReadMode consistentReads ;
52
62
53
63
public AbstractDynamoDBQueryCreator (PartTree tree , DynamoDBEntityInformation <T , ID > entityMetadata ,
54
- Optional <String > projection , Optional <Integer > limitResults , QueryConstants .ConsistentReadMode consistentReads , DynamoDBOperations dynamoDBOperations ) {
64
+ Optional <String > projection , Optional <Integer > limitResults , QueryConstants .ConsistentReadMode consistentReads ,
65
+ Optional <String > filterExpression , ExpressionAttribute [] names , ExpressionAttribute [] values , DynamoDBOperations dynamoDBOperations ) {
55
66
super (tree );
56
67
this .entityMetadata = entityMetadata ;
57
68
this .projection = projection ;
58
69
this .limit = limitResults ;
59
70
this .consistentReads = consistentReads ;
71
+ this .filterExpression = filterExpression ;
72
+ if (names != null ) {
73
+ this .expressionAttributeNames = names .clone ();
74
+ } else {
75
+ this .expressionAttributeNames = null ;
76
+ }
77
+ if (values != null ) {
78
+ this .expressionAttributeValues = values .clone ();
79
+ } else {
80
+ this .expressionAttributeValues = null ;
81
+ }
60
82
this .dynamoDBOperations = dynamoDBOperations ;
61
83
}
62
84
63
85
public AbstractDynamoDBQueryCreator (PartTree tree , ParameterAccessor parameterAccessor ,
64
86
DynamoDBEntityInformation <T , ID > entityMetadata , Optional <String > projection ,
65
- Optional <Integer > limitResults , QueryConstants .ConsistentReadMode consistentReads , DynamoDBOperations dynamoDBOperations ) {
87
+ Optional <Integer > limitResults , QueryConstants .ConsistentReadMode consistentReads , Optional < String > filterExpression , ExpressionAttribute [] names , ExpressionAttribute [] values , DynamoDBOperations dynamoDBOperations ) {
66
88
super (tree , parameterAccessor );
67
89
this .entityMetadata = entityMetadata ;
68
90
this .projection = projection ;
69
91
this .limit = limitResults ;
92
+ this .filterExpression = filterExpression ;
70
93
this .consistentReads = consistentReads ;
94
+ if (names != null ) {
95
+ this .expressionAttributeNames = names .clone ();
96
+ } else {
97
+ this .expressionAttributeNames = null ;
98
+ }
99
+ if (values != null ) {
100
+ this .expressionAttributeValues = values .clone ();
101
+ for (ExpressionAttribute value : expressionAttributeValues ) {
102
+ if (!StringUtils .isEmpty (value .parameterName ())) {
103
+ for (Parameter p : ((ParametersParameterAccessor )parameterAccessor ).getParameters ()) {
104
+ if (p .getName ().isPresent () && p .getName ().get ().equals (value .parameterName ())) {
105
+ mappedExpressionValues .put (value .parameterName (), (String ) parameterAccessor .getBindableValue (p .getIndex ()));
106
+ }
107
+ }
108
+ }
109
+ }
110
+ } else {
111
+ this .expressionAttributeValues = null ;
112
+ }
71
113
this .dynamoDBOperations = dynamoDBOperations ;
72
114
}
73
115
74
116
@ Override
75
117
protected DynamoDBQueryCriteria <T , ID > create (Part part , Iterator <Object > iterator ) {
76
118
final DynamoDBMapperTableModel <T > tableModel = dynamoDBOperations .getTableModel (entityMetadata .getJavaType ());
77
119
DynamoDBQueryCriteria <T , ID > criteria = entityMetadata .isRangeKeyAware ()
78
- ? new DynamoDBEntityWithHashAndRangeKeyCriteria <T , ID >(
79
- (DynamoDBIdIsHashAndRangeKeyEntityInformation <T , ID >) entityMetadata , tableModel )
120
+ ? new DynamoDBEntityWithHashAndRangeKeyCriteria <>(
121
+ (DynamoDBIdIsHashAndRangeKeyEntityInformation <T , ID >) entityMetadata , tableModel )
80
122
: new DynamoDBEntityWithHashKeyOnlyCriteria <>(entityMetadata , tableModel );
81
123
return addCriteria (criteria , part , iterator );
82
124
}
@@ -90,7 +132,7 @@ protected DynamoDBQueryCriteria<T, ID> addCriteria(DynamoDBQueryCriteria<T, ID>
90
132
91
133
PropertyPath leafNodePropertyPath = part .getProperty ().getLeafProperty ();
92
134
String leafNodePropertyName = leafNodePropertyPath .toDotPath ();
93
- if (leafNodePropertyName .indexOf ("." ) != - 1 ) {
135
+ if (leafNodePropertyName .contains ("." )) {
94
136
int index = leafNodePropertyName .lastIndexOf ("." );
95
137
leafNodePropertyName = leafNodePropertyName .substring (index );
96
138
}
0 commit comments