Skip to content

Commit

Permalink
save and load column titles
Browse files Browse the repository at this point in the history
  • Loading branch information
bosborn committed Feb 5, 2024
1 parent 88f8cd3 commit e0e0eb6
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
import com.j256.ormlite.support.ConnectionSource;

import mil.nga.geopackage.GeoPackageCore;
import mil.nga.geopackage.contents.Contents;
import mil.nga.geopackage.db.GeoPackageCoreConnection;
import mil.nga.geopackage.db.GeoPackageDao;
import mil.nga.geopackage.db.TableColumnKey;
import mil.nga.geopackage.user.UserColumn;
import mil.nga.geopackage.user.UserColumns;
import mil.nga.geopackage.user.UserTable;

/**
* Data Columns Data Access Object
Expand Down Expand Up @@ -265,4 +269,194 @@ public int deleteByTableName(String tableName) throws SQLException {
return deleted;
}

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

/**
* Save the column titles as data columns
*
* @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());
}

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

for (UserColumn column : columns) {

saveColumnTitle(contents, column);

}

}

/**
* Save the column title as a data column
*
* @param contents
* user table contents
* @param column
* user column
* @throws SQLException
* upon failure
* @since 6.6.7
*/
public void saveColumnTitle(Contents contents, 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);
}

}

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

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

/**
* Load the column titles from data columns
*
* @param table
* table name
* @param columns
* user columns
* @throws SQLException
* upon failure
* @since 6.6.7
*/
public void loadColumnTitles(String table,
List<? extends UserColumn> columns) throws SQLException {

if (isTableExists()) {

for (UserColumn column : columns) {

loadColumnTitle(table, column);

}

}

}

/**
* Load the column title from a data column
*
* @param table
* table name
* @param column
* user column
* @throws SQLException
* upon failure
* @since 6.6.7
*/
public void loadColumnTitle(String table, UserColumn column)
throws SQLException {

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

}

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

String title = null;

if (isTableExists()) {

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

}

return title;
}

}
33 changes: 33 additions & 0 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.DataColumnsDao;

/**
* Metadata about a single column from a user table
Expand Down Expand Up @@ -115,6 +116,14 @@ public abstract class UserColumn implements Comparable<UserColumn> {
*/
private final Constraints constraints;

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

/**
* Constructor
*
Expand Down Expand Up @@ -220,6 +229,7 @@ protected UserColumn(UserColumn userColumn) {
this.type = userColumn.type;
this.dataType = userColumn.dataType;
this.constraints = userColumn.constraints.copy();
this.title = userColumn.title;
}

/**
Expand Down Expand Up @@ -948,6 +958,29 @@ public String buildConstraintSql(Constraint constraint) {
return sql;
}

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

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

/**
* {@inheritDoc}
* <p>
Expand Down

0 comments on commit e0e0eb6

Please sign in to comment.