|
46 | 46 | import static com.couchbase.client.java.manager.query.GetAllQueryIndexesOptions.getAllQueryIndexesOptions; |
47 | 47 | import static com.couchbase.client.java.manager.query.WatchQueryIndexesOptions.watchQueryIndexesOptions; |
48 | 48 | import static com.couchbase.client.java.manager.search.UpsertSearchIndexOptions.upsertSearchIndexOptions; |
49 | | -import static com.google.common.collect.ImmutableMap.of; |
50 | 49 | import static java.lang.String.format; |
51 | 50 | import static lombok.AccessLevel.PACKAGE; |
52 | 51 | import static org.slf4j.LoggerFactory.getLogger; |
@@ -79,11 +78,18 @@ public class CouchbaseRepositoryImpl<E extends CouchbaseEntity> implements Couch |
79 | 78 |
|
80 | 79 | private final Class<E> entityClass; |
81 | 80 |
|
| 81 | + private final Map<String, String> variables; |
| 82 | + |
82 | 83 | public CouchbaseRepositoryImpl(Cluster cluster, Collection collection, Class<E> entityClass) { |
| 84 | + this(cluster, collection, entityClass, Collections.emptyMap()); |
| 85 | + } |
| 86 | + |
| 87 | + public CouchbaseRepositoryImpl(Cluster cluster, Collection collection, Class<E> entityClass, Map<String, String> customVariables) { |
83 | 88 | this.cluster = cluster; |
84 | 89 | this.bucket = cluster.bucket(collection.bucketName()); |
85 | 90 | this.collection = getOrCreate(collection); |
86 | 91 | this.entityClass = entityClass; |
| 92 | + this.variables = createVariables(customVariables, this.collection); |
87 | 93 | } |
88 | 94 |
|
89 | 95 | private Collection getOrCreate(Collection collection) { |
@@ -112,21 +118,35 @@ private Collection getOrCreate(Collection collection) { |
112 | 118 | return collection; |
113 | 119 | } |
114 | 120 |
|
| 121 | + private Map<String, String> createVariables(Map<String, String> customVariables, Collection collection) { |
| 122 | + Map<String, String> result = new HashMap<>(customVariables); |
| 123 | + if(collection != null) { |
| 124 | + result.put(BUCKET_PARAM, collection.bucketName()); |
| 125 | + result.put(SCOPE_PARAM, collection.scopeName()); |
| 126 | + } |
| 127 | + return Collections.unmodifiableMap(result); |
| 128 | + } |
| 129 | + |
115 | 130 | public CouchbaseRepositoryImpl(Cluster cluster, Bucket bucket, Class<E> entityClass) { |
| 131 | + this(cluster, bucket, entityClass, Collections.emptyMap()); |
| 132 | + } |
| 133 | + |
| 134 | + public CouchbaseRepositoryImpl(Cluster cluster, Bucket bucket, Class<E> entityClass, Map<String, String> customVariables) { |
116 | 135 | this.cluster = cluster; |
117 | 136 | this.bucket = bucket; |
118 | 137 | this.collection = bucket.defaultCollection(); |
119 | 138 | this.entityClass = entityClass; |
| 139 | + this.variables = createVariables(customVariables, this.collection); |
120 | 140 | } |
121 | 141 |
|
122 | 142 | @Override |
123 | 143 | public CouchbaseRepositoryImpl<E> withCollection(String collection) { |
124 | | - return new CouchbaseRepositoryImpl<>(cluster, bucket.scope(this.collection.scopeName()).collection(collection), entityClass); |
| 144 | + return new CouchbaseRepositoryImpl<>(cluster, bucket.scope(this.collection.scopeName()).collection(collection), entityClass, variables); |
125 | 145 | } |
126 | 146 |
|
127 | 147 | @Override |
128 | 148 | public CouchbaseRepositoryImpl<E> withCollection(String scope, String collection) { |
129 | | - return new CouchbaseRepositoryImpl<>(cluster, bucket.scope(scope).collection(collection), entityClass); |
| 149 | + return new CouchbaseRepositoryImpl<>(cluster, bucket.scope(scope).collection(collection), entityClass, variables); |
130 | 150 | } |
131 | 151 |
|
132 | 152 | @Override |
@@ -275,10 +295,7 @@ public boolean isEventingFunctionExists(String name) { |
275 | 295 | } |
276 | 296 |
|
277 | 297 | String injectParameters(String statement) { |
278 | | - return StrSubstitutor.replace(statement, of( |
279 | | - BUCKET_PARAM, getBucketName(), |
280 | | - SCOPE_PARAM, getScopeName() |
281 | | - )); |
| 298 | + return StrSubstitutor.replace(statement, variables); |
282 | 299 | } |
283 | 300 |
|
284 | 301 | @Override |
|
0 commit comments