Skip to content

Commit dc56a7f

Browse files
committed
Allow configuring Apache Calcite properties through QueryContextBuilder#setProperty
1 parent 590340f commit dc56a7f

File tree

5 files changed

+52
-13
lines changed

5 files changed

+52
-13
lines changed

connector/entity-view/src/main/java/com/blazebit/query/connector/view/EntityViewSchemaProvider.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public EntityViewSchemaProvider() {
5050
EntityViewConnectorConfig.ENTITY_VIEW_FILTER.getPropertyName() );
5151
PropertyProvider<EntityManager> entityManagerProvider = configurationProvider.getPropertyProvider(
5252
EntityViewConnectorConfig.ENTITY_MANAGER.getPropertyName() );
53-
PropertyProvider<DataFetchContext> dataContextSupplier = configurationProvider;
5453

5554
final ImmutableMap.Builder<Class<?>, EntityViewTable<?>> builder = ImmutableMap.builder();
5655

@@ -61,7 +60,7 @@ public EntityViewSchemaProvider() {
6160
new EntityViewTable<>(
6261
entityViewManager,
6362
entityManagerProvider,
64-
dataContextSupplier,
63+
configurationProvider,
6564
viewType
6665
)
6766
);

core/api/src/main/java/com/blazebit/query/spi/QueryContextBuilder.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ public interface QueryContextBuilder {
3333
* @param value The property value
3434
* @return {@code this} object for method chaining
3535
*/
36-
default QueryContextBuilder setProperty(String property, Object value) {
37-
return setPropertyProvider(property, (context) -> value);
38-
}
36+
QueryContextBuilder setProperty(String property, Object value);
3937

4038
/**
4139
* Sets the given supplier as value provider for the given property name.
@@ -44,16 +42,17 @@ default QueryContextBuilder setProperty(String property, Object value) {
4442
* @param provider The property value provider
4543
* @return {@code this} object for method chaining
4644
*/
47-
QueryContextBuilder setPropertyProvider(String property, PropertyProvider provider);
45+
QueryContextBuilder setPropertyProvider(String property, PropertyProvider<?> provider);
4846

4947
/**
5048
* Returns the property provider for the property name.
5149
*
5250
* @param property The property name
51+
* @param <X> The expected value type
5352
* @return the property provider for the property name
5453
* @throws IllegalArgumentException If no property provider exists
5554
*/
56-
PropertyProvider getPropertyProvider(String property);
55+
<X> PropertyProvider<X> getPropertyProvider(String property);
5756

5857
/**
5958
* Registers a fully qualified alias for a schema object type.

core/impl/src/main/java/com/blazebit/query/impl/QueryContextBuilderImpl.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.blazebit.query.impl;
1717

1818
import com.blazebit.query.QueryContext;
19+
import com.blazebit.query.spi.DataFetchContext;
1920
import com.blazebit.query.spi.DataFetcher;
2021
import com.blazebit.query.spi.PropertyProvider;
2122
import com.blazebit.query.spi.QueryContextBuilder;
@@ -24,6 +25,7 @@
2425
import java.util.ArrayList;
2526
import java.util.HashMap;
2627
import java.util.Map;
28+
import java.util.Properties;
2729
import java.util.ServiceLoader;
2830

2931
/**
@@ -37,19 +39,26 @@ public class QueryContextBuilderImpl implements QueryContextBuilder {
3739
final Map<String, SchemaObjectTypeImpl<?>> schemaObjects = new HashMap<>();
3840
final Map<String, String> schemaObjectNames = new HashMap<>();
3941

42+
@Override
43+
public QueryContextBuilder setProperty(String property, Object value) {
44+
propertyProviders.put(property, new PropertyProviderImpl<>(value));
45+
return this;
46+
}
47+
4048
@Override
4149
public QueryContextBuilder setPropertyProvider(String property, PropertyProvider provider) {
4250
propertyProviders.put(property, provider);
4351
return this;
4452
}
4553

4654
@Override
47-
public PropertyProvider getPropertyProvider(String property) {
48-
PropertyProvider propertyProvider = propertyProviders.get(property);
55+
public <X> PropertyProvider<X> getPropertyProvider(String property) {
56+
PropertyProvider<?> propertyProvider = propertyProviders.get(property);
4957
if (propertyProvider == null) {
5058
throw new IllegalArgumentException("No property provider found for property: " + property);
5159
}
52-
return propertyProvider;
60+
//noinspection unchecked
61+
return (PropertyProvider<X>) propertyProvider;
5362
}
5463

5564
@Override
@@ -82,4 +91,36 @@ public QueryContextBuilder loadServices() {
8291
public QueryContext build() {
8392
return new QueryContextImpl( this );
8493
}
94+
95+
public Properties getProperties() {
96+
Properties props = new Properties();
97+
for ( Map.Entry<String, PropertyProvider<?>> entry : propertyProviders.entrySet() ) {
98+
PropertyProvider<?> propertyProvider = entry.getValue();
99+
if ( propertyProvider instanceof PropertyProviderImpl ) {
100+
props.put( entry.getKey(), ( (PropertyProviderImpl<?>) propertyProvider ).value );
101+
}
102+
}
103+
return props;
104+
}
105+
106+
/**
107+
* @author Christian Beikov
108+
* @since 1.0.0
109+
*/
110+
private static final class PropertyProviderImpl<X> implements PropertyProvider<X> {
111+
private final X value;
112+
113+
/**
114+
* Creates a new property provider.
115+
* @param value The value
116+
*/
117+
public PropertyProviderImpl(X value) {
118+
this.value = value;
119+
}
120+
121+
@Override
122+
public X provide(DataFetchContext context) {
123+
return value;
124+
}
125+
}
85126
}

core/impl/src/main/java/com/blazebit/query/impl/QueryContextImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class QueryContextImpl implements QueryContext {
5656

5757
public QueryContextImpl(QueryContextBuilderImpl builder) {
5858
this.configurationProvider = new ConfigurationProviderImpl( ImmutableMap.copyOf( builder.propertyProviders ) );
59-
this.calciteDataSource = new CalciteDataSource();
59+
this.calciteDataSource = new CalciteDataSource(builder.getProperties());
6060
this.metamodel = new MetamodelImpl(resolveSchemaObjects( builder, configurationProvider, calciteDataSource ));
6161
}
6262

core/impl/src/main/java/com/blazebit/query/impl/calcite/CalciteDataSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ public class CalciteDataSource extends Driver implements DataSource {
9494
private final JavaTypeFactory typeFactory;
9595
private final CalciteSchema rootSchema;
9696

97-
public CalciteDataSource() {
98-
properties = new Properties();
97+
public CalciteDataSource(Properties properties) {
9998
properties.setProperty( "lex", "JAVA" );
99+
this.properties = properties;
100100

101101
CalciteConnectionConfig cfg = new CalciteConnectionConfigImpl( properties );
102102
RelDataTypeSystem typeSystem = cfg.typeSystem( RelDataTypeSystem.class, RelDataTypeSystem.DEFAULT );

0 commit comments

Comments
 (0)