Skip to content

Commit

Permalink
Distinguish spatial queries for MariaDB and Postgres as the specific …
Browse files Browse the repository at this point in the history
…geometric functions differ. The ExpressionHandler class contains the functions similar for all database dialects. Specific differences in the dialects are implemented in PgExpressionHandler and MariaDbExpressionHandler. (#2036)
  • Loading branch information
T-Hellmund authored and hylkevds committed Oct 10, 2024
1 parent 25880dd commit 64d96f1
Show file tree
Hide file tree
Showing 8 changed files with 1,296 additions and 1,246 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Development version 2.4.3

**Internal changes & Bugfixes**
* Added support for spatial queries on MariaDB.


## Release version 2.4.2
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public interface JooqPersistenceManager extends LiquibaseUser, PersistenceManage

void generateLiquibaseVariables(Map<String, Object> target, String entity, String type);

ExpressionHandler createExpressionHandler(QueryBuilder queryBuilder);

Entity get(EntityType entityType, PkValue id, Query query);

ConnectionUtils.ConnectionWrapper getConnectionProvider();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2024 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
* Karlsruhe, Germany.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq;

import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.bindings.PostGisGeometryBinding;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.fieldwrapper.FieldWrapper;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.fieldwrapper.SimpleFieldWrapper;
import de.fraunhofer.iosb.ilt.frostserver.query.expression.constant.GeoJsonConstant;
import de.fraunhofer.iosb.ilt.frostserver.query.expression.constant.LineStringConstant;
import de.fraunhofer.iosb.ilt.frostserver.query.expression.constant.PointConstant;
import de.fraunhofer.iosb.ilt.frostserver.query.expression.constant.PolygonConstant;
import de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings;
import org.geojson.GeoJsonObject;
import org.geolatte.geom.Geometry;
import org.geolatte.geom.codec.Wkt;
import org.jooq.impl.DSL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author Hylke van der Schaaf
*/
public class MariaDBExpressionHandler extends ExpressionHandler {

private static final String ST_GeomFromText = "ST_GeomFromText(?)";

private static final Logger LOGGER = LoggerFactory.getLogger(MariaDBExpressionHandler.class);

public MariaDBExpressionHandler(CoreSettings settings, QueryBuilder queryBuilder) {
super(settings, queryBuilder);
}

@Override
public FieldWrapper visit(LineStringConstant node) {
Geometry geom = fromGeoJsonConstant(node);
return new SimpleFieldWrapper(DSL.field(ST_GeomFromText, PostGisGeometryBinding.dataType(), geom.asText()));
}

@Override
public FieldWrapper visit(PointConstant node) {
Geometry geom = fromGeoJsonConstant(node);
return new SimpleFieldWrapper(DSL.field(ST_GeomFromText, PostGisGeometryBinding.dataType(), geom.asText()));
}

@Override
public FieldWrapper visit(PolygonConstant node) {
Geometry geom = fromGeoJsonConstant(node);
return new SimpleFieldWrapper(DSL.field(ST_GeomFromText, PostGisGeometryBinding.dataType(), geom.asText()));
}

public Geometry fromGeoJsonConstant(GeoJsonConstant<? extends GeoJsonObject> node) {
if (node.getValue().getCrs() == null) {
return Wkt.fromWkt(node.getSource());
}
return Wkt.fromWkt(node.getSource());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,11 @@ public void generateLiquibaseVariables(Map<String, Object> target, String entity
}
}

@Override
public ExpressionHandler createExpressionHandler(QueryBuilder queryBuilder) {
return new MariaDBExpressionHandler(settings, queryBuilder);
}

@Override
public String checkForUpgrades() {
Map<String, Object> props = CollectionsHelper.LinkedHashMapBuilder()
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,11 @@ public void generateLiquibaseVariables(Map<String, Object> target, String entity
}
}

@Override
public ExpressionHandler createExpressionHandler(QueryBuilder queryBuilder) {
return new PgExpressionHandler(settings, queryBuilder);
}

@Override
public String checkForUpgrades() {
Map<String, Object> props = CollectionsHelper.LinkedHashMapBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ private void handleEntityExpand(QueryState sqlState, Expand expand, NavigationPr

private void parseOrder(Query query) {
if (query != null) {
PgExpressionHandler handler = new PgExpressionHandler(coreSettings, this);
ExpressionHandler handler = getPersistenceManager().createExpressionHandler(this);
for (OrderBy ob : query.getOrderBy()) {
handler.addOrderbyToQuery(ob, queryState.getSqlSortFields());
}
Expand All @@ -440,7 +440,7 @@ public void parseFilter(Query query) {
queryState.setFilter(true);
final Expression filter = query.getFilter();
final Expression skipFilter = query.getSkipFilter();
PgExpressionHandler handler = new PgExpressionHandler(coreSettings, this);
ExpressionHandler handler = getPersistenceManager().createExpressionHandler(this);
if (filter != null) {
queryState.setSqlWhere(handler.addFilterToWhere(filter, queryState.getSqlWhere()));
}
Expand Down

0 comments on commit 64d96f1

Please sign in to comment.