Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement backend data field filter for H2 & Postgresql (similar to MongoDB) #3516

Merged

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

countDevicesOfTenantWithFilter: |
SELECT COUNT(*) AS deviceCount FROM %1$s
WHERE
tenant_id=:tenant_id
AND
LOCATE(CONCAT_WS(':', :field, REPLACE(:value, '"')), REPLACE(data, '"'))
OR
REPLACE(data, '"') LIKE CONCAT('%%', :field, ':', REPLACE(:value, '"'))


findDevicesOfTenantWithFilter: |
SELECT *
FROM %s
WHERE
tenant_id=:tenant_id
AND
LOCATE(CONCAT_WS(':', :field, REPLACE(:value, '"')), REPLACE(data, '"'))
OR
REPLACE(data, '"') LIKE CONCAT('%%', :field, ':', REPLACE(:value, '"'))
ORDER BY device_id ASC
LIMIT :page_size
OFFSET :page_offset
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,23 @@ resolveGroups: |
tenant_id=:tenant_id
AND
group_id in (select unnest((string_to_array(:group_ids,','))::varchar[]))

countDevicesOfTenantWithFilter: |
SELECT COUNT(*) AS deviceCount FROM %1$s
WHERE
tenant_id=:tenant_id
AND
data->>:field LIKE CAST(:value AS VARCHAR) OR
jsonb_extract_path(data,'ext')->>:field LIKE CAST(:value as varchar)

findDevicesOfTenantWithFilter: |
SELECT *
FROM %s
WHERE
tenant_id=:tenant_id
AND
data->>:field LIKE CAST(:value AS VARCHAR) OR
jsonb_extract_path(data,'ext')->>:field LIKE CAST(:value as varchar)
ORDER BY device_id ASC
LIMIT :page_size
OFFSET :page_offset
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,6 @@ findDevicesOfTenant: |
FROM %s
WHERE
tenant_id=:tenant_id
ORDER BY device_id
ORDER BY device_id ASC
LIMIT :page_size
OFFSET :page_offset
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected Future<OperationResult<Id>> processCreateDevice(final DeviceKey key, f
Optional.of(r.getVersion()))
)

.recover(e -> Services.recover(e));
.recover(Services::recover);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 👍


}

Expand Down Expand Up @@ -112,7 +112,7 @@ protected Future<OperationResult<Id>> processUpdateDevice(final DeviceKey key, f
Optional.empty(),
Optional.of(r.getVersion())))

.recover(e -> Services.recover(e));
.recover(Services::recover);

}

Expand All @@ -131,7 +131,7 @@ protected Future<Result<Void>> processDeleteDevice(final DeviceKey key, final Op
return Result.<Void>from(HttpURLConnection.HTTP_NO_CONTENT);
}
})
.recover(e -> Services.recover(e));
.recover(Services::recover);

}

Expand All @@ -141,7 +141,7 @@ protected Future<Result<Void>> processDeleteDevicesOfTenant(final String tenantI
return this.store
.dropTenant(tenantId, span.context())
.map(r -> Result.<Void>from(HttpURLConnection.HTTP_NO_CONTENT))
.recover(e -> Services.recover(e));
.recover(Services::recover);
}

@Override
Expand All @@ -151,22 +151,22 @@ protected String generateDeviceId(final String tenantId) {

@Override
protected Future<OperationResult<SearchResult<DeviceWithId>>> processSearchDevices(
final String tenantId,
final int pageSize,
final int pageOffset,
final List<Filter> filters,
final List<Sort> sortOptions,
final Span span) {
final String tenantId,
final int pageSize,
final int pageOffset,
final List<Filter> filters,
final List<Sort> sortOptions,
final Span span) {

Objects.requireNonNull(tenantId);
Objects.requireNonNull(span);

return store.findDevices(tenantId, pageSize, pageOffset, span.context())
.map(result -> OperationResult.ok(
HttpURLConnection.HTTP_OK,
result,
Optional.empty(),
Optional.empty()))
.recover(e -> Services.recover(e));
return store.findDevices(tenantId, pageSize, pageOffset, filters, span.context())
.map(result -> OperationResult.ok(
HttpURLConnection.HTTP_OK,
result,
Optional.empty(),
Optional.empty()))
.recover(Services::recover);
Comment on lines +154 to +170
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this additional indentation due to Hono's formatter configuration in the eclipse folder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ enum DatabaseType {
}
protected static final Span SPAN = NoopSpan.INSTANCE;

private static final DatabaseType DEFAULT_DATABASE_TYPE = DatabaseType.H2;
private static final DatabaseType DATABASE_TYPE = DatabaseType.valueOf(System.getProperty(AbstractJdbcRegistryTest.class.getSimpleName()
protected static DatabaseType DEFAULT_DATABASE_TYPE = DatabaseType.H2;
protected static DatabaseType DATABASE_TYPE = DatabaseType.valueOf(System.getProperty(AbstractJdbcRegistryTest.class.getSimpleName()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like these changes can be reverted as well now, can't they?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i change it back to private final

+ ".databaseType", DEFAULT_DATABASE_TYPE.name()).toUpperCase());
private static final Map<DatabaseType, JdbcDatabaseContainer<?>> DATABASE_CONTAINER_CACHE = new ConcurrentHashMap<>();
private static final String POSTGRESQL_IMAGE_NAME = System.getProperty(AbstractJdbcRegistryTest.class.getSimpleName()
Expand All @@ -86,7 +86,7 @@ enum DatabaseType {
private static final AtomicLong UNIQUE_ID_GENERATOR = new AtomicLong(System.currentTimeMillis());

private static final Tracer TRACER = NoopTracerFactory.create();
private static final Path EXAMPLE_SQL_BASE = Path.of("..", "base-jdbc", "src", "main", "resources", "sql", DATABASE_TYPE.name().toLowerCase());
protected static Path EXAMPLE_SQL_BASE = Path.of("..", "base-jdbc", "src", "main", "resources", "sql", DATABASE_TYPE.name().toLowerCase());

private static final Path BASE_DIR = Path.of("target").toAbsolutePath();

Expand Down
Loading