mil.nga.geopackage.db.AlterTable Java Examples

The following examples show how to use mil.nga.geopackage.db.AlterTable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GeoPackageCoreImpl.java    From geopackage-core-java with MIT License 6 votes vote down vote up
/**
 * Copy the user table
 * 
 * @param tableName
 *            table name
 * @param newTableName
 *            new table name
 * @param transferContent
 *            transfer user table content flag
 * @param validateContents
 *            true to validate a contents was copied
 * @return copied contents
 * @since 3.3.0
 */
protected Contents copyUserTable(String tableName, String newTableName,
		boolean transferContent, boolean validateContents) {

	AlterTable.copyTable(database, tableName, newTableName,
			transferContent);

	Contents contents = copyContents(tableName, newTableName);

	if (contents == null && validateContents) {
		throw new GeoPackageException(
				"No table contents found for table: " + tableName);
	}

	return contents;
}
 
Example #2
Source File: CrsWktExtension.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * Create the extension column
 */
private void createColumn() {

	AlterTable.addColumn(connection, SpatialReferenceSystem.TABLE_NAME,
			COLUMN_NAME, COLUMN_DEF);

	// Update the existing known SRS values
	updateDefinition(GeoPackageProperties.getIntegerProperty(
			PropertyConstants.WGS_84, PropertyConstants.SRS_ID),
			GeoPackageProperties.getProperty(PropertyConstants.WGS_84,
					PropertyConstants.DEFINITION_12_063));
	updateDefinition(
			GeoPackageProperties.getIntegerProperty(
					PropertyConstants.UNDEFINED_CARTESIAN,
					PropertyConstants.SRS_ID),
			GeoPackageProperties.getProperty(
					PropertyConstants.UNDEFINED_CARTESIAN,
					PropertyConstants.DEFINITION_12_063));
	updateDefinition(GeoPackageProperties.getIntegerProperty(
			PropertyConstants.UNDEFINED_GEOGRAPHIC,
			PropertyConstants.SRS_ID), GeoPackageProperties.getProperty(
			PropertyConstants.UNDEFINED_GEOGRAPHIC,
			PropertyConstants.DEFINITION_12_063));
	updateDefinition(GeoPackageProperties.getIntegerProperty(
			PropertyConstants.WEB_MERCATOR, PropertyConstants.SRS_ID),
			GeoPackageProperties.getProperty(
					PropertyConstants.WEB_MERCATOR,
					PropertyConstants.DEFINITION_12_063));
}
 
Example #3
Source File: GeoPackageCoreImpl.java    From geopackage-core-java with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void renameTable(String tableName, String newTableName) {
	if (getTableDataType(tableName) != null) {
		copyTable(tableName, newTableName);
		deleteTable(tableName);
	} else {
		AlterTable.renameTable(database, tableName, newTableName);
	}
}
 
Example #4
Source File: SQLExecAlterTable.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Check for a drop column statement and execute
 * 
 * @param database
 *            database
 * @param sql
 *            SQL statement
 * @return result if dropped column, null if not
 */
private static SQLExecResult dropColumn(GeoPackage database, String sql) {

	SQLExecResult result = null;

	Matcher matcher = DROP_COLUMN_PATTERN.matcher(sql);
	if (matcher.find()) {
		String tableName = CoreSQLUtils
				.quoteUnwrap(matcher.group(TABLE_NAME_GROUP));
		if (tableName != null) {
			tableName = tableName.trim();
		}
		String columnName = CoreSQLUtils
				.quoteUnwrap(matcher.group(COLUMN_NAME_GROUP));
		if (columnName != null) {
			columnName = columnName.trim();
		}

		if (tableName != null && columnName != null) {
			AlterTable.dropColumn(database.getDatabase(), tableName,
					columnName);
			result = new SQLExecResult();
		}

	}

	return result;
}
 
Example #5
Source File: SQLExecAlterTable.java    From geopackage-java with MIT License 5 votes vote down vote up
/**
 * Check for a copy table statement and execute
 * 
 * @param database
 *            database
 * @param sql
 *            SQL statement
 * @return result if copied table, null if not
 */
private static SQLExecResult copyTable(GeoPackage database, String sql) {

	SQLExecResult result = null;

	Matcher matcher = COPY_TABLE_PATTERN.matcher(sql);
	if (matcher.find()) {
		String tableName = CoreSQLUtils
				.quoteUnwrap(matcher.group(TABLE_NAME_GROUP));
		if (tableName != null) {
			tableName = tableName.trim();
		}
		String newTableName = CoreSQLUtils
				.quoteUnwrap(matcher.group(NEW_TABLE_NAME_GROUP));
		if (newTableName != null) {
			newTableName = newTableName.trim();
		}

		if (tableName != null && newTableName != null) {

			if (SQLExec.isGeoPackage(database)) {
				database.copyTable(tableName, newTableName);
			} else {
				AlterTable.copyTable(database.getDatabase(), tableName,
						newTableName);
			}

			result = new SQLExecResult();

		}

	}

	return result;
}
 
Example #6
Source File: NGAExtensions.java    From geopackage-core-java with MIT License 4 votes vote down vote up
/**
 * Copy the feature table style
 * 
 * @param featureStyleExtension
 *            feature style extension
 * @param mappingTablePrefix
 *            mapping table prefix
 * @param table
 *            table name
 * @param newTable
 *            new table name
 * @param contentsId
 *            contents id
 * @param newContentsId
 *            new contents id
 */
private static void copyFeatureTableStyle(
		FeatureCoreStyleExtension featureStyleExtension,
		String mappingTablePrefix, String table, String newTable,
		long contentsId, long newContentsId) throws SQLException {

	GeoPackageCore geoPackage = featureStyleExtension.getGeoPackage();

	String mappingTableName = featureStyleExtension
			.getMappingTableName(mappingTablePrefix, table);

	ExtensionsDao extensionsDao = featureStyleExtension.getExtensionsDao();
	List<Extensions> extensions = extensionsDao.queryByExtension(
			RelatedTablesCoreExtension.EXTENSION_NAME, mappingTableName);

	if (!extensions.isEmpty()) {

		String newMappingTableName = featureStyleExtension
				.getMappingTableName(mappingTablePrefix, newTable);

		UserCustomTable userTable = UserCustomTableReader
				.readTable(geoPackage.getDatabase(), mappingTableName);
		AlterTable.copyTable(geoPackage.getDatabase(), userTable,
				newMappingTableName, false);

		TableMapping mappingTableTableMapping = new TableMapping(userTable,
				newMappingTableName);
		MappedColumn baseIdColumn = mappingTableTableMapping
				.getColumn(UserMappingTable.COLUMN_BASE_ID);
		baseIdColumn.setConstantValue(newContentsId);
		baseIdColumn.setWhereValue(contentsId);
		CoreSQLUtils.transferTableContent(geoPackage.getDatabase(),
				mappingTableTableMapping);

		Extensions extension = extensions.get(0);
		extension.setTableName(newMappingTableName);
		extensionsDao.create(extension);

		TableMapping extendedRelationTableMapping = new TableMapping(
				geoPackage.getDatabase(), ExtendedRelation.TABLE_NAME);
		extendedRelationTableMapping
				.removeColumn(ExtendedRelation.COLUMN_ID);
		MappedColumn baseTableNameColumn = extendedRelationTableMapping
				.getColumn(ExtendedRelation.COLUMN_BASE_TABLE_NAME);
		baseTableNameColumn.setWhereValue(ContentsId.TABLE_NAME);
		MappedColumn mappingTableNameColumn = extendedRelationTableMapping
				.getColumn(ExtendedRelation.COLUMN_MAPPING_TABLE_NAME);
		mappingTableNameColumn.setConstantValue(newMappingTableName);
		mappingTableNameColumn.setWhereValue(mappingTableName);
		CoreSQLUtils.transferTableContent(geoPackage.getDatabase(),
				extendedRelationTableMapping);

	}

}
 
Example #7
Source File: GeoPackageExtensions.java    From geopackage-core-java with MIT License 4 votes vote down vote up
/**
 * Copy the Related Tables extensions for the table
 * 
 * @param geoPackage
 *            GeoPackage
 * @param table
 *            table name
 * @param newTable
 *            new table name
 * @since 3.3.0
 */
public static void copyRelatedTables(GeoPackageCore geoPackage,
		String table, String newTable) {

	try {

		RelatedTablesCoreExtension relatedTablesExtension = getRelatedTableExtension(
				geoPackage);
		if (relatedTablesExtension.has()) {

			ExtendedRelationsDao extendedRelationsDao = relatedTablesExtension
					.getExtendedRelationsDao();
			ExtensionsDao extensionsDao = geoPackage.getExtensionsDao();

			List<ExtendedRelation> extendedRelations = extendedRelationsDao
					.getBaseTableRelations(table);
			for (ExtendedRelation extendedRelation : extendedRelations) {

				String mappingTableName = extendedRelation
						.getMappingTableName();

				List<Extensions> extensions = extensionsDao
						.queryByExtension(
								RelatedTablesCoreExtension.EXTENSION_NAME,
								mappingTableName);

				if (!extensions.isEmpty()) {

					String newMappingTableName = CoreSQLUtils.createName(
							geoPackage.getDatabase(), mappingTableName,
							table, newTable);

					UserCustomTable userTable = UserCustomTableReader
							.readTable(geoPackage.getDatabase(),
									mappingTableName);
					AlterTable.copyTable(geoPackage.getDatabase(),
							userTable, newMappingTableName);

					Extensions extension = extensions.get(0);
					extension.setTableName(newMappingTableName);
					extensionsDao.create(extension);

					TableMapping extendedRelationTableMapping = new TableMapping(
							geoPackage.getDatabase(),
							ExtendedRelation.TABLE_NAME);
					extendedRelationTableMapping
							.removeColumn(ExtendedRelation.COLUMN_ID);
					MappedColumn baseTableNameColumn = extendedRelationTableMapping
							.getColumn(
									ExtendedRelation.COLUMN_BASE_TABLE_NAME);
					baseTableNameColumn.setConstantValue(newTable);
					baseTableNameColumn.setWhereValue(table);
					MappedColumn mappingTableNameColumn = extendedRelationTableMapping
							.getColumn(
									ExtendedRelation.COLUMN_MAPPING_TABLE_NAME);
					mappingTableNameColumn
							.setConstantValue(newMappingTableName);
					mappingTableNameColumn.setWhereValue(mappingTableName);
					CoreSQLUtils.transferTableContent(
							geoPackage.getDatabase(),
							extendedRelationTableMapping);

				}
			}
		}

	} catch (Exception e) {
		logger.log(Level.WARNING,
				"Failed to create Related Tables for table: " + newTable
						+ ", copied from table: " + table,
				e);
	}

}
 
Example #8
Source File: GeoPackageExtensions.java    From geopackage-core-java with MIT License 4 votes vote down vote up
/**
 * Copy the Schema extensions for the table
 * 
 * @param geoPackage
 *            GeoPackage
 * @param table
 *            table name
 * @param newTable
 *            new table name
 * @since 3.3.0
 */
public static void copySchema(GeoPackageCore geoPackage, String table,
		String newTable) {

	try {

		if (geoPackage.isTable(DataColumns.TABLE_NAME)) {

			UserCustomTable dataColumnsTable = UserCustomTableReader
					.readTable(geoPackage.getDatabase(),
							DataColumns.TABLE_NAME);
			UserCustomColumn nameColumn = dataColumnsTable
					.getColumn(DataColumns.COLUMN_NAME);
			if (nameColumn.hasConstraints()) {
				nameColumn.clearConstraints();
				if (dataColumnsTable.hasConstraints()) {
					dataColumnsTable.clearConstraints();
					String constraintSql = GeoPackageTableCreator
							.readSQLScript(
									GeoPackageTableCreator.DATA_COLUMNS)
							.get(0);
					TableConstraints constraints = ConstraintParser
							.getConstraints(constraintSql);
					dataColumnsTable.addConstraints(
							constraints.getTableConstraints());
				}
				AlterTable.alterColumn(geoPackage.getDatabase(),
						dataColumnsTable, nameColumn);
			}

			CoreSQLUtils.transferTableContent(geoPackage.getDatabase(),
					DataColumns.TABLE_NAME, DataColumns.COLUMN_TABLE_NAME,
					newTable, table);

		}

	} catch (Exception e) {
		logger.log(Level.WARNING, "Failed to create Schema for table: "
				+ newTable + ", copied from table: " + table, e);
	}

}
 
Example #9
Source File: UserDao.java    From geopackage-android with MIT License 3 votes vote down vote up
/**
 * {@inheritDoc}
 * Alter Table in SQLite does not support renaming columns until version 3.25.0
 * Once Android supports column rename alter table statements, this method override can be removed.
 */
@Override
protected void renameTableColumn(String columnName, String newColumnName) {

    UserTable<? extends UserColumn> newTable = getTable().copy();

    newTable.renameColumn(columnName, newColumnName);

    TableMapping tableMapping = new TableMapping(newTable);
    tableMapping.getColumn(newColumnName).setFromColumn(columnName);

    AlterTable.alterTable(getDb(), newTable, tableMapping);
}
 
Example #10
Source File: UserCoreDao.java    From geopackage-core-java with MIT License 2 votes vote down vote up
/**
 * Rename a table column
 * 
 * @param columnName
 *            column name
 * @param newColumnName
 *            new column name
 * @since 3.3.0
 */
protected void renameTableColumn(String columnName, String newColumnName) {
	AlterTable.renameColumn(db, table.getTableName(), columnName,
			newColumnName);
}
 
Example #11
Source File: UserCoreDao.java    From geopackage-core-java with MIT License 2 votes vote down vote up
/**
 * Drop a column
 * 
 * @param columnName
 *            column name
 * @since 3.3.0
 */
public void dropColumn(String columnName) {
	AlterTable.dropColumn(db, table, columnName);
}
 
Example #12
Source File: UserCoreDao.java    From geopackage-core-java with MIT License 2 votes vote down vote up
/**
 * Drop columns
 * 
 * @param columnNames
 *            column names
 * @since 3.3.0
 */
public void dropColumnNames(Collection<String> columnNames) {
	AlterTable.dropColumns(db, table, columnNames);
}
 
Example #13
Source File: UserCoreDao.java    From geopackage-core-java with MIT License 2 votes vote down vote up
/**
 * Alter a column
 * 
 * @param column
 *            column
 * @since 3.3.0
 */
public void alterColumn(TColumn column) {
	AlterTable.alterColumn(db, table, column);
}
 
Example #14
Source File: UserCoreDao.java    From geopackage-core-java with MIT License 2 votes vote down vote up
/**
 * Alter columns
 * 
 * @param columns
 *            columns
 * @since 3.3.0
 */
public void alterColumns(Collection<TColumn> columns) {
	AlterTable.alterColumns(db, table, columns);
}