Skip to content

Commit

Permalink
Merge pull request #1678 from lat-lon/feature/dbSchemaGmlTool-9651-29
Browse files Browse the repository at this point in the history
Enhanced SqlFeatureStoreConfigCreator to support new parameter dbschema (3.5)
  • Loading branch information
stephanr authored May 22, 2024
2 parents 3e4f517 + 7d7777c commit 07b50f6
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public abstract class DDLCreator {

protected TableName currentFtTable;

protected List<String> createdSchemas = new ArrayList<>();

/**
* Creates a new {@link DDLCreator} instance for the given {@link MappedAppSchema}.
* @param schema mapped application schema, must not be <code>null</code>
Expand Down Expand Up @@ -121,7 +123,9 @@ private List<StringBuffer> process(FeatureTypeMapping ftMapping) {

currentFtTable = ftMapping.getFtTable();

StringBuffer sql = new StringBuffer("CREATE TABLE ");
StringBuffer sql = new StringBuffer();
appendCreateSchema(currentFtTable.getSchema(), sql);
sql.append("CREATE TABLE ");
ddls.add(sql);
sql.append(currentFtTable);
sql.append(" (");
Expand Down Expand Up @@ -168,6 +172,13 @@ private List<StringBuffer> process(FeatureTypeMapping ftMapping) {
return ddls;
}

protected void appendCreateSchema(String schema, StringBuffer sql) {
if (schema != null && !createdSchemas.contains(schema)) {
createSchemaSnippet(schema, sql);
createdSchemas.add(schema);
}
}

private String getPkConstraintName(TableName ftTable) {
String s = null;
String table = ftTable.getTable();
Expand All @@ -180,6 +191,10 @@ private String getPkConstraintName(TableName ftTable) {
return s;
}

protected void createSchemaSnippet(String schema, StringBuffer sql) {
sql.append("CREATE SCHEMA ").append(schema).append(";\n");
}

protected abstract void primitiveMappingSnippet(StringBuffer sql, PrimitiveMapping mapping);

protected abstract void geometryMappingSnippet(StringBuffer sql, GeometryMapping mapping, List<StringBuffer> ddls,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ private List<StringBuffer> getGeometryCreate(GeometryMapping mapping, DBField db
StringBuffer indexSql = new StringBuffer("CREATE INDEX ");
String idxName = createIdxName(table.getTable(), column);
indexSql.append(idxName);
indexSql.append(" ON ").append(table.getTable().toLowerCase());
indexSql.append(" ON ");
indexSql.append(table.toString().toLowerCase());
indexSql.append(" USING GIST (").append(column).append(" ); ");
ddls.add(indexSql);
return ddls;
Expand Down Expand Up @@ -187,7 +188,9 @@ protected void featureMappingSnippet(StringBuffer sql, FeatureMapping mapping) {
@Override
protected StringBuffer createJoinedTable(TableName fromTable, TableJoin jc, List<StringBuffer> ddls,
FIDMapping fidMapping) {
StringBuffer sb = new StringBuffer("CREATE TABLE ");
StringBuffer sb = new StringBuffer();
appendCreateSchema(jc.getToTable().getSchema(), sb);
sb.append("CREATE TABLE ");
sb.append(jc.getToTable());
sb.append(" (\n ");
sb.append("id serial PRIMARY KEY,\n ");
Expand Down Expand Up @@ -238,6 +241,10 @@ protected String getDBType(BaseType type) {
return postgresqlType;
}

protected void createSchemaSnippet(String schema, StringBuffer sql) {
sql.append("CREATE SCHEMA IF NOT EXISTS ").append(schema).append(";\n");
}

private String retrieveTypeOfPrimaryKey(TableName fromTable, SQLIdentifier toColumn, FIDMapping fidMapping) {
if (fidMapping != null) {
for (Pair<SQLIdentifier, BaseType> column : fidMapping.getColumns()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public class AppSchemaMapper {

private final boolean useRefDataProps;

private final String dbSchema;

/**
* Creates a new {@link AppSchemaMapper} instance for the given schema.
* @param appSchema application schema to be mapped, must not be <code>null</code>
Expand Down Expand Up @@ -180,7 +182,7 @@ public AppSchemaMapper(AppSchema appSchema, boolean createBlobMapping, boolean c
GeometryStorageParams geometryParams, int maxLength, boolean usePrefixedSQLIdentifiers,
boolean useIntegerFids, int allowedCycleDepth) {
this(appSchema, createBlobMapping, createRelationalMapping, geometryParams, maxLength,
usePrefixedSQLIdentifiers, useIntegerFids, allowedCycleDepth, null, false);
usePrefixedSQLIdentifiers, useIntegerFids, allowedCycleDepth, null, false, null);
}

/**
Expand All @@ -203,14 +205,16 @@ public AppSchemaMapper(AppSchema appSchema, boolean createBlobMapping, boolean c
*/
public AppSchemaMapper(AppSchema appSchema, boolean createBlobMapping, boolean createRelationalMapping,
GeometryStorageParams geometryParams, int maxLength, boolean usePrefixedSQLIdentifiers,
boolean useIntegerFids, int allowedCycleDepth, ReferenceData referenceData, boolean useRefDataProps) {
boolean useIntegerFids, int allowedCycleDepth, ReferenceData referenceData, boolean useRefDataProps,
String dbSchema) {
this.appSchema = appSchema;
this.geometryParams = geometryParams;
this.useIntegerFids = useIntegerFids;
this.allowedCycleDepth = allowedCycleDepth;
this.maxComplexityIndex = DEFAULT_COMPLEXITY_INDEX * (allowedCycleDepth + 1);
this.referenceData = referenceData;
this.useRefDataProps = useRefDataProps;
this.dbSchema = dbSchema;

List<FeatureType> ftList = appSchema.getFeatureTypes(null, false, false);
List<FeatureType> blackList = new ArrayList<FeatureType>();
Expand Down Expand Up @@ -285,10 +289,7 @@ private FeatureTypeMapping generateFtMapping(FeatureType ft) {
LOG.info("Mapping feature type '" + ft.getName() + "'");
MappingContext mc = mcManager.newContext(ft.getName(), detectPrimaryKeyColumnName());

// TODO
TableName table = new TableName(mc.getTable());
// TODO

TableName table = new TableName(mc.getTable(), dbSchema);
FIDMapping fidMapping = generateFidMapping(ft);

List<Mapping> mappings = new ArrayList<>();
Expand Down Expand Up @@ -563,8 +564,8 @@ private CompoundMapping generatePropMapping(CodePropertyType pt, MappingContext
}

private List<TableJoin> generateJoinChain(MappingContext from, MappingContext to) {
TableName fromTable = new TableName(from.getTable());
TableName toTable = new TableName(to.getTable());
TableName fromTable = new TableName(from.getTable(), dbSchema);
TableName toTable = new TableName(to.getTable(), dbSchema);
List<String> fromColumns = Collections.singletonList(from.getIdColumn());
List<String> toColumns = Collections.singletonList("parentfk");
List<String> orderColumns = Collections.singletonList("num");
Expand All @@ -579,7 +580,7 @@ private TableJoin generateFtJoin(MappingContext from, FeatureType valueFt) {
if (valueFt != null && valueFt.getSchema().getSubtypes(valueFt).length == 1) {
LOG.warn("Ambiguous feature join.");
}
TableName fromTable = new TableName(from.getTable());
TableName fromTable = new TableName(from.getTable(), dbSchema);
TableName toTable = new TableName("?");
List<String> fromColumns = Collections.singletonList(from.getColumn());
List<String> toColumns = Collections.singletonList(detectPrimaryKeyColumnName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public void testWithoutReferenceData() throws Exception {
CRSRef storageCrs = CRSManager.getCRSRef("EPSG:4326");
GeometryStorageParams geometryParams = new GeometryStorageParams(storageCrs, "0", DIM_2);
AppSchemaMapper mapper = new AppSchemaMapper(appSchema, false, true, geometryParams, 63, true, true, 0, null,
false);
false, null);

MappedAppSchema mappedSchema = mapper.getMappedSchema();

Expand Down Expand Up @@ -498,7 +498,7 @@ public void testWithReferenceData() throws Exception {
CRSRef storageCrs = CRSManager.getCRSRef("EPSG:4326");
GeometryStorageParams geometryParams = new GeometryStorageParams(storageCrs, "0", DIM_2);
AppSchemaMapper mapper = new AppSchemaMapper(appSchema, false, true, geometryParams, 63, true, true, 0,
referenceData, false);
referenceData, false, null);

MappedAppSchema mappedSchema = mapper.getMappedSchema();

Expand Down Expand Up @@ -539,7 +539,7 @@ public void testWithReferenceData_ComplexData_ComplexN() throws Exception {
CRSRef storageCrs = CRSManager.getCRSRef("EPSG:4326");
GeometryStorageParams geometryParams = new GeometryStorageParams(storageCrs, "0", DIM_2);
AppSchemaMapper mapper = new AppSchemaMapper(appSchema, false, true, geometryParams, 63, true, true, 0,
referenceData, false);
referenceData, false, null);

MappedAppSchema mappedSchema = mapper.getMappedSchema();

Expand Down Expand Up @@ -586,7 +586,7 @@ public void testWithReferenceData_ComplexData_Complex1() throws Exception {
CRSRef storageCrs = CRSManager.getCRSRef("EPSG:4326");
GeometryStorageParams geometryParams = new GeometryStorageParams(storageCrs, "0", DIM_2);
AppSchemaMapper mapper = new AppSchemaMapper(appSchema, false, true, geometryParams, 63, true, true, 0,
referenceData, false);
referenceData, false, null);

MappedAppSchema mappedSchema = mapper.getMappedSchema();

Expand Down Expand Up @@ -632,7 +632,7 @@ public void testWithReferenceData_ReferencedFeature_RefN() throws Exception {
CRSRef storageCrs = CRSManager.getCRSRef("EPSG:4326");
GeometryStorageParams geometryParams = new GeometryStorageParams(storageCrs, "0", DIM_2);
AppSchemaMapper mapper = new AppSchemaMapper(appSchema, false, true, geometryParams, 63, true, true, 0,
referenceData, false);
referenceData, false, null);

MappedAppSchema mappedSchema = mapper.getMappedSchema();

Expand Down Expand Up @@ -680,7 +680,7 @@ public void testWithReferenceData_ReferencedFeature_Ref1() throws Exception {
CRSRef storageCrs = CRSManager.getCRSRef("EPSG:4326");
GeometryStorageParams geometryParams = new GeometryStorageParams(storageCrs, "0", DIM_2);
AppSchemaMapper mapper = new AppSchemaMapper(appSchema, false, true, geometryParams, 63, true, true, 0,
referenceData, false);
referenceData, false, null);

MappedAppSchema mappedSchema = mapper.getMappedSchema();

Expand Down Expand Up @@ -719,7 +719,7 @@ public void testWithReferenceDataAndConsiderProperties() throws Exception {
CRSRef storageCrs = CRSManager.getCRSRef("EPSG:4326");
GeometryStorageParams geometryParams = new GeometryStorageParams(storageCrs, "0", DIM_2);
AppSchemaMapper mapper = new AppSchemaMapper(appSchema, false, true, geometryParams, 63, true, true, 0,
referenceData, true);
referenceData, true, null);

MappedAppSchema mappedSchema = mapper.getMappedSchema();

Expand Down Expand Up @@ -752,7 +752,7 @@ public void testWithReferenceDataAndNilProperties() throws Exception {
CRSRef storageCrs = CRSManager.getCRSRef("EPSG:4326");
GeometryStorageParams geometryParams = new GeometryStorageParams(storageCrs, "0", DIM_2);
AppSchemaMapper mapper = new AppSchemaMapper(appSchema, false, true, geometryParams, 63, true, true, 0,
referenceData, true);
referenceData, true, null);

MappedAppSchema mappedSchema = mapper.getMappedSchema();

Expand All @@ -775,7 +775,7 @@ public void testWithTimeProperties() throws Exception {
CRSRef storageCrs = CRSManager.getCRSRef("EPSG:4326");
GeometryStorageParams geometryParams = new GeometryStorageParams(storageCrs, "0", DIM_2);
AppSchemaMapper mapper = new AppSchemaMapper(appSchema, false, true, geometryParams, 63, true, true, 0, null,
false);
false, null);

MappedAppSchema mappedSchema = mapper.getMappedSchema();

Expand Down Expand Up @@ -817,6 +817,27 @@ public void testWithTimeProperties() throws Exception {
assertThat(getPrimitive(timePeriodOfTimeObject.getParticles(), "@gml:id"), is(notNullValue()));
}

@Test
public void testWithoutDbSchema() throws Exception {
GMLAppSchemaReader xsdDecoder = new GMLAppSchemaReader(null, null, schemaForSampleValues);
AppSchema appSchema = xsdDecoder.extractAppSchema();

CRSRef storageCrs = CRSManager.getCRSRef("EPSG:4326");
GeometryStorageParams geometryParams = new GeometryStorageParams(storageCrs, "0", DIM_2);
String dbSchema = "test";
AppSchemaMapper mapper = new AppSchemaMapper(appSchema, false, true, geometryParams, 63, true, true, 0, null,
false, dbSchema);

MappedAppSchema mappedSchema = mapper.getMappedSchema();

Map<QName, FeatureTypeMapping> ftMappings = mappedSchema.getFtMappings();
assertThat(ftMappings.size(), is(2));

FeatureTypeMapping featureA = mappedSchema.getFtMapping(FEATURE_A);

assertThat(featureA.getFtTable().getSchema(), is(dbSchema));
}

private CompoundMapping getFeatureC(List<Mapping> mappings) {
return getCompound(mappings, "FeatureC");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void write(List<? extends AppSchema> appSchemas) throws Exception {
AppSchemaMapper mapper = new AppSchemaMapper(appSchema, !loadParameter.isRelationalMapping(),
loadParameter.isRelationalMapping(), geometryParams, sqlDialect.getMaxColumnNameLength(), true,
loadParameter.isUseIntegerFids(), loadParameter.getDepth(), loadParameter.getReferenceData(),
loadParameter.isUseRefDataProps());
loadParameter.isUseRefDataProps(), loadParameter.getDbSchema());
MappedAppSchema mappedSchema = mapper.getMappedSchema();
SQLFeatureStoreConfigWriter configWriter = new SQLFeatureStoreConfigWriter(mappedSchema,
loadParameter.getPropertiesWithPrimitiveHref());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@

import org.deegree.feature.persistence.sql.mapper.ReferenceData;

import java.util.List;

import javax.xml.namespace.QName;
import java.util.List;

/**
* Load parameter.
Expand Down Expand Up @@ -54,6 +53,8 @@ public class LoadParameter {

private boolean useRefDataProps;

private String dbSchema;

LoadParameter() {
}

Expand Down Expand Up @@ -121,6 +122,14 @@ public void setDialect(String dialect) {
this.dialect = dialect;
}

public void setDbSchema(String dbSchema) {
this.dbSchema = dbSchema;
}

public String getDbSchema() {
return dbSchema;
}

public void setDepth(int depth) {
this.depth = depth;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ public LoadParameterBuilder setDialect(String dialect) {
return this;
}

public LoadParameterBuilder setDbSchema(String dbSchema) {
if (dbSchema != null && dbSchema.isEmpty())
loadParameter.setDbSchema(null);
else
loadParameter.setDbSchema(dbSchema);
return this;
}

public LoadParameterBuilder setSrid(String srid) {
if (srid == null)
loadParameter.setSrid(DEFAULT_SRID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public class SqlFeatureStoreConfigCreatorConfiguration {
public LoadParameter parseJobParameter(@Value("#{jobParameters[schemaUrl]}") String schemaUrl,
@Value("#{jobParameters[format]}") String format, @Value("#{jobParameters[srid]}") String srid,
@Value("#{jobParameters[idtype]}") String idtype, @Value("#{jobParameters[mapping]}") String mapping,
@Value("#{jobParameters[dialect]}") String dialect, @Value("#{jobParameters[cycledepth]}") String depth,
@Value("#{jobParameters[dialect]}") String dialect, @Value("#{jobParameters[dbschema]}") String dbSchema,
@Value("#{jobParameters[cycledepth]}") String depth,
@Value("#{jobParameters[listOfPropertiesWithPrimitiveHref]}") String listOfPropertiesWithPrimitiveHref,
@Value("#{jobParameters[referenceData]}") String referenceData,
@Value("#{jobParameters[useRefDataProps]}") String useRefDataProps) {
Expand All @@ -64,6 +65,7 @@ public LoadParameter parseJobParameter(@Value("#{jobParameters[schemaUrl]}") Str
.setIdType(idtype)
.setMappingType(mapping)
.setDialect(dialect)
.setDbSchema(dbSchema)
.setDepth(depth)
.setListOfPropertiesWithPrimitiveHref(listOfPropertiesWithPrimitiveHref)
.setReferenceData(referenceData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static void printUsage() {
System.out.println(" -idtype={int|uuid}, default=int");
System.out.println(" -mapping={relational|blob}, default=relational");
System.out.println(" -dialect={postgis|oracle}, default=postgis");
System.out.println(" -dbschema=STRING, name of the db schema to use, default: no schema");
System.out.println(" -cycledepth=INT, positive integer value to specify the depth of cycles, default=0");
System.out.println(" -listOfPropertiesWithPrimitiveHref=<path/to/file>, not set by default");
System.out.println(
Expand Down

0 comments on commit 07b50f6

Please sign in to comment.