Skip to content

Initial Data Model for Schema SQL Generation #1481

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

Merged
merged 8 commits into from
Apr 13, 2023
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.1.0-1478-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.1.0-1478-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.1.0-1478-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.1.0-1478-SNAPSHOT</version>
</parent>

<properties>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-r2dbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-r2dbc</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.1.0-1478-SNAPSHOT</version>

<name>Spring Data R2DBC</name>
<description>Spring Data module for R2DBC</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.1.0-1478-SNAPSHOT</version>
</parent>

<properties>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.1.0-1478-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.1.0-1478-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.relational.core.mapping.schemasqlgeneration.ColumnModel;
import org.springframework.data.relational.core.mapping.schemasqlgeneration.SchemaSQLGenerationDataModel;
import org.springframework.data.relational.core.mapping.schemasqlgeneration.TableModel;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;

import java.util.Iterator;

/**
* {@link MappingContext} implementation.
*
Expand Down Expand Up @@ -102,4 +107,31 @@ public NamingStrategy getNamingStrategy() {
return this.namingStrategy;
}

/**
* Method to build a data model of Tables/Columns and their relationships
* that will be used in SQL Generation.
*/
public SchemaSQLGenerationDataModel generateSchemaSQL() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the mapping context should create a SchemaSqlGenerationDataModel instead a mapping context should be used to create such a model. Either in the constructor of the class or in a factory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

About to push up a change for this. Code definitely looks cleaner with this!


SchemaSQLGenerationDataModel model = new SchemaSQLGenerationDataModel();

for (RelationalPersistentEntity entity : getPersistentEntities()) {
TableModel tableModel = new TableModel();
tableModel.setName(entity.getTableName().getReference());

Iterator<BasicRelationalPersistentProperty> iter =
entity.getPersistentProperties(Column.class).iterator();

while (iter.hasNext()) {
BasicRelationalPersistentProperty p = iter.next();
ColumnModel columnModel = new ColumnModel();
columnModel.setName(p.getColumnName().getReference());
columnModel.setType(p.getActualType());
tableModel.getColumns().add(columnModel);
}
model.getTableData().add(tableModel);
}

return model;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.relational.core.mapping.schemasqlgeneration;

/**
* Class that models a Column for generating SQL for Schema generation.
*
* @author Kurt Niemi
*/
public class ColumnModel {
private String name;
private Class<?> type;
private boolean nullable;

public String getName() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Things like column and table names should be SqlIdentifier since we need to consider quoting and identifier processing.

return name;
}

public void setName(String name) {
this.name = name;
}

public Class<?> getType() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see why we would need a type in the sense of a Class. This information is provided by PersistentProperty. Instead we might need a type like Text or Number which represents database types, while abstracting over the database specific representations of those. Possibly by using Liquibase abstractions, if those exist.

return type;
}

public void setType(Class<?> type) {
this.type = type;
}

public boolean isNullable() {
return nullable;
}

public void setNullable(boolean nullable) {
this.nullable = nullable;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.relational.core.mapping.schemasqlgeneration;

/**
* Class that models a Foreign Key relationship for generating SQL for Schema generation.
*
* @author Kurt Niemi
*/
public class ForeignKeyColumnModel {
private TableModel foreignTable;
private ColumnModel foreignColumn;
private ColumnModel column;

public ForeignKeyColumnModel(TableModel foreignTable, ColumnModel foreignColumn, ColumnModel column) {
this.foreignTable = foreignTable;
this.foreignColumn = foreignColumn;
this.column = column;
}

public TableModel getForeignTable() {
return foreignTable;
}

public ColumnModel getForeignColumn() {
return foreignColumn;
}

public ColumnModel getColumn() {
return column;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.relational.core.mapping.schemasqlgeneration;

import java.util.ArrayList;
import java.util.List;

/**
* Model class that contains Table/Column information that can be used
* to generate SQL for Schema generation.
*
* @author Kurt Niemi
*/
public class SchemaSQLGenerationDataModel {
private List<TableModel> tableData = new ArrayList<TableModel>();

public List<TableModel> getTableData() {
return tableData;
}

public void setTableData(List<TableModel> tableData) {
this.tableData = tableData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.relational.core.mapping.schemasqlgeneration;

import java.util.ArrayList;
import java.util.List;

/**
* Class that models a Table for generating SQL for Schema generation.
*
* @author Kurt Niemi
*/
public class TableModel {
private String schema;
private String name;
private List<ColumnModel> columns = new ArrayList<ColumnModel>();
private List<ColumnModel> keyColumns = new ArrayList<ColumnModel>();
private List<ForeignKeyColumnModel> foreignKeyColumns = new ArrayList<ForeignKeyColumnModel>();

public String getSchema() {
return schema;
}

public void setSchema(String schema) {
this.schema = schema;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<ColumnModel> getColumns() {
return columns;
}

public void setColumns(List<ColumnModel> columns) {
this.columns = columns;
}

public List<ColumnModel> getKeyColumns() {
return keyColumns;
}

public void setKeyColumns(List<ColumnModel> keyColumns) {
this.keyColumns = keyColumns;
}

public List<ForeignKeyColumnModel> getForeignKeyColumns() {
return foreignKeyColumns;
}

public void setForeignKeyColumns(List<ForeignKeyColumnModel> foreignKeyColumns) {
this.foreignKeyColumns = foreignKeyColumns;
}
}
Loading