Skip to content

Commit

Permalink
User Column integrated Data Columns Schema support
Browse files Browse the repository at this point in the history
  • Loading branch information
bosborn committed Feb 6, 2024
1 parent e0e0eb6 commit c0b03d4
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 77 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Adheres to [Semantic Versioning](http://semver.org/).
* GeoPackage user version 1.4.0
* R-tree update trigger modifications
* DAO column range support (including geometry envelopes & bounding boxes) to build where clauses & args for queries
* User Column integrated Data Columns Schema support
* oapi-features-json version 2.3.3

## [6.6.5](https://github.com/ngageoint/geopackage-core-java/releases/tag/6.6.5) (11-29-2023)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import mil.nga.geopackage.GeoPackageCore;
import mil.nga.geopackage.contents.Contents;
import mil.nga.geopackage.contents.ContentsDao;
import mil.nga.geopackage.db.GeoPackageCoreConnection;
import mil.nga.geopackage.db.GeoPackageDao;
import mil.nga.geopackage.db.TableColumnKey;
Expand Down Expand Up @@ -52,6 +53,11 @@ public static DataColumnsDao create(GeoPackageCoreConnection db) {
return GeoPackageDao.createDao(db, DataColumns.class);
}

/**
* Contents DAO
*/
private ContentsDao contentsDao;

/**
* Constructor, required by ORMLite
*
Expand Down Expand Up @@ -162,6 +168,24 @@ public int deleteById(TableColumnKey id) throws SQLException {
return count;
}

/**
* Delete by id with table name and column name
*
* @param tableName
* table name
* @param columnName
* column name
* @return number of rows deleted
* @throws SQLException
* upon failure
* @since 6.6.7
*/
public int deleteById(String tableName, String columnName)
throws SQLException {
TableColumnKey id = new TableColumnKey(tableName, columnName);
return deleteById(id);
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -270,121 +294,126 @@ public int deleteByTableName(String tableName) throws SQLException {
}

/**
* Save the column titles as data columns
* Save the table schema
*
* @param table
* user table
* @throws SQLException
* upon failure
* @since 6.6.7
*/
public void saveColumnTitles(UserTable<? extends UserColumn> table)
public void saveSchema(UserTable<? extends UserColumn> table)
throws SQLException {
saveColumnTitles(table.getContents(), table.getUserColumns());
saveSchema(table.getUserColumns());
}

/**
* Save the column titles as data columns
* Save the columns schema
*
* @param contents
* user table contents
* @param columns
* user columns
* @throws SQLException
* upon failure
* @since 6.6.7
*/
public void saveColumnTitles(Contents contents,
UserColumns<? extends UserColumn> columns) throws SQLException {
saveColumnTitles(contents, columns.getColumns());
public void saveSchema(UserColumns<? extends UserColumn> columns)
throws SQLException {
saveSchema(columns.getTableName(), columns.getColumns());
}

/**
* Save the column titles as data columns
* Save the columns schema
*
* @param contents
* user table contents
* @param table
* table name
* @param columns
* user columns
* @throws SQLException
* upon failure
* @since 6.6.7
*/
public void saveColumnTitles(Contents contents,
List<? extends UserColumn> columns) throws SQLException {
public void saveSchema(String table, List<? extends UserColumn> columns)
throws SQLException {

for (UserColumn column : columns) {

saveColumnTitle(contents, column);

saveSchema(table, column);
}

}

/**
* Save the column title as a data column
* Save the column schema
*
* @param contents
* user table contents
* @param table
* table name
* @param column
* user column
* @throws SQLException
* upon failure
* @since 6.6.7
*/
public void saveColumnTitle(Contents contents, UserColumn column)
public void saveSchema(String table, UserColumn column)
throws SQLException {

String table = contents.getTableName();
String name = column.getName();
String title = column.getTitle();

DataColumns dataColumns = getDataColumn(table, name);
if (dataColumns != null) {
dataColumns.setName(title);
dataColumns.setTitle(title);
update(dataColumns);
} else if (title != null) {
dataColumns = new DataColumns();
dataColumns.setContents(contents);
dataColumns.setColumnName(name);
dataColumns.setName(title);
dataColumns.setTitle(title);
create(dataColumns);
String columnName = column.getName();

DataColumns schema = column.getSchema();
DataColumns existing = getSchema(table, columnName);

if (schema != null) {

schema.setColumnName(columnName);

if (existing != null) {
schema.setContents(existing.getContents());
update(schema);
} else {
if (schema.getContents() == null) {
ContentsDao contentsDao = getContentsDao();
Contents contents = contentsDao.queryForId(table);
schema.setContents(contents);
}
if (schema.getContents() != null) {
create(schema);
}
}

} else if (existing != null) {
deleteById(table, columnName);
}

}

/**
* Load the column titles from data columns
* Load the table schema
*
* @param table
* user table
* @throws SQLException
* upon failure
* @since 6.6.7
*/
public void loadColumnTitles(UserTable<? extends UserColumn> table)
public void loadSchema(UserTable<? extends UserColumn> table)
throws SQLException {
loadColumnTitles(table.getUserColumns());
loadSchema(table.getUserColumns());
}

/**
* Load the column titles from data columns
* Load the columns schema
*
* @param columns
* user columns
* @throws SQLException
* upon failure
* @since 6.6.7
*/
public void loadColumnTitles(UserColumns<? extends UserColumn> columns)
public void loadSchema(UserColumns<? extends UserColumn> columns)
throws SQLException {
loadColumnTitles(columns.getTableName(), columns.getColumns());
loadSchema(columns.getTableName(), columns.getColumns());
}

/**
* Load the column titles from data columns
* Load the columns schema
*
* @param table
* table name
Expand All @@ -394,14 +423,14 @@ public void loadColumnTitles(UserColumns<? extends UserColumn> columns)
* upon failure
* @since 6.6.7
*/
public void loadColumnTitles(String table,
List<? extends UserColumn> columns) throws SQLException {
public void loadSchema(String table, List<? extends UserColumn> columns)
throws SQLException {

if (isTableExists()) {

for (UserColumn column : columns) {

loadColumnTitle(table, column);
loadSchema(table, column);

}

Expand All @@ -410,7 +439,7 @@ public void loadColumnTitles(String table,
}

/**
* Load the column title from a data column
* Load the column schema
*
* @param table
* table name
Expand All @@ -420,43 +449,47 @@ public void loadColumnTitles(String table,
* upon failure
* @since 6.6.7
*/
public void loadColumnTitle(String table, UserColumn column)
public void loadSchema(String table, UserColumn column)
throws SQLException {

column.setTitle(getColumnTitle(table, column.getName()));
column.setSchema(getSchema(table, column.getName()));

}

/**
* Get the column title from a data column
* Get the column schema
*
* @param table
* table name
* @param column
* column name
* @return column title or null
* @return column schema or null
* @throws SQLException
* upon failure
* @since 6.6.7
*/
public String getColumnTitle(String table, String column)
public DataColumns getSchema(String table, String column)
throws SQLException {

String title = null;
DataColumns schema = null;

if (isTableExists()) {
schema = getDataColumn(table, column);
}

DataColumns dataColumns = getDataColumn(table, column);
if (dataColumns != null) {
title = dataColumns.getName();
if (title == null) {
title = dataColumns.getTitle();
}
}
return schema;
}

/**
* Get or create a Contents DAO
*
* @return contents dao
*/
private ContentsDao getContentsDao() {
if (contentsDao == null) {
contentsDao = ContentsDao.create(db);
}

return title;
return contentsDao;
}

}
31 changes: 17 additions & 14 deletions src/main/java/mil/nga/geopackage/user/UserColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import mil.nga.geopackage.db.table.Constraints;
import mil.nga.geopackage.db.table.RawConstraint;
import mil.nga.geopackage.db.table.TableColumn;
import mil.nga.geopackage.extension.schema.columns.DataColumns;
import mil.nga.geopackage.extension.schema.columns.DataColumnsDao;

/**
Expand Down Expand Up @@ -117,12 +118,12 @@ public abstract class UserColumn implements Comparable<UserColumn> {
private final Constraints constraints;

/**
* Column title, not saved as part of the column, saved using
* Column schema, not saved as part of the column, saved using
* {@link DataColumnsDao}
*
* @since 6.6.7
*/
private String title;
private DataColumns schema;

/**
* Constructor
Expand Down Expand Up @@ -229,7 +230,9 @@ protected UserColumn(UserColumn userColumn) {
this.type = userColumn.type;
this.dataType = userColumn.dataType;
this.constraints = userColumn.constraints.copy();
this.title = userColumn.title;
if (userColumn.schema != null) {
this.schema = new DataColumns(userColumn.schema);
}
}

/**
Expand Down Expand Up @@ -959,26 +962,26 @@ public String buildConstraintSql(Constraint constraint) {
}

/**
* Get the column title. The title is not saved as part of the column but
* can be loaded using {@link DataColumnsDao}.
* Get the data columns schema. Not saved as part of the column, loaded
* using {@link DataColumnsDao}.
*
* @return column title or null
* @return column schema or null
* @since 6.6.7
*/
public String getTitle() {
return title;
public DataColumns getSchema() {
return schema;
}

/**
* Set the column title. The title is not saved as part of the column but
* can be saved using {@link DataColumnsDao}.
* Set the data columns schema. Not saved as part of the column, saved using
* {@link DataColumnsDao}.
*
* @param title
* column title
* @param schema
* data columns schema
* @since 6.6.7
*/
public void setTitle(String title) {
this.title = title;
public void setSchema(DataColumns schema) {
this.schema = schema;
}

/**
Expand Down

0 comments on commit c0b03d4

Please sign in to comment.