use of mil.nga.geopackage.user.custom.UserCustomResultSet in project geopackage-java by ngageoint.
the class StyleMappingDao method queryByBaseFeatureId.
/**
* Query for style mappings by base id
*
* @param id
* base id, feature contents id or feature geometry id
* @return style mappings rows
*/
public List<StyleMappingRow> queryByBaseFeatureId(long id) {
List<StyleMappingRow> rows = new ArrayList<>();
UserCustomResultSet resultSet = queryByBaseId(id);
try {
while (resultSet.moveToNext()) {
rows.add(getRow(resultSet));
}
} finally {
resultSet.close();
}
return rows;
}
use of mil.nga.geopackage.user.custom.UserCustomResultSet in project geopackage-java by ngageoint.
the class RelatedTablesExtension method getMappingsForRelated.
/**
* Get the base id mappings for the related id
*
* @param tableName
* mapping table name
* @param relatedId
* related id
* @return IDs representing the matching base IDs
*/
public List<Long> getMappingsForRelated(String tableName, long relatedId) {
List<Long> baseIds = new ArrayList<>();
UserMappingDao userMappingDao = getMappingDao(tableName);
UserCustomResultSet resultSet = userMappingDao.queryByRelatedId(relatedId);
try {
while (resultSet.moveToNext()) {
UserMappingRow row = userMappingDao.getRow(resultSet);
baseIds.add(row.getBaseId());
}
} finally {
resultSet.close();
}
return baseIds;
}
use of mil.nga.geopackage.user.custom.UserCustomResultSet in project geopackage-java by ngageoint.
the class RelatedTablesExtension method getMappingsForBase.
/**
* Get the related id mappings for the base id
*
* @param tableName
* mapping table name
* @param baseId
* base id
* @return IDs representing the matching related IDs
*/
public List<Long> getMappingsForBase(String tableName, long baseId) {
List<Long> relatedIds = new ArrayList<>();
UserMappingDao userMappingDao = getMappingDao(tableName);
UserCustomResultSet resultSet = userMappingDao.queryByBaseId(baseId);
try {
while (resultSet.moveToNext()) {
UserMappingRow row = userMappingDao.getRow(resultSet);
relatedIds.add(row.getRelatedId());
}
} finally {
resultSet.close();
}
return relatedIds;
}
use of mil.nga.geopackage.user.custom.UserCustomResultSet in project geopackage-java by ngageoint.
the class RelatedTablesWriteTest method testWriteRelationshipsToAttributes.
/**
* Test write relationships to attributes
*
* @throws SQLException
* @throws IOException
*/
@Test
public void testWriteRelationshipsToAttributes() throws SQLException, IOException {
RelatedTablesExtension rte = new RelatedTablesExtension(geoPackage);
if (rte.has()) {
rte.removeExtension();
}
// 1. Has extension
TestCase.assertFalse(rte.has());
// 4. Get relationships
List<ExtendedRelation> extendedRelations = rte.getRelationships();
TestCase.assertTrue(extendedRelations.isEmpty());
// 2. Add extension
// 5. Add relationship between "geometry2d" and "attributes"
final String baseTableName = "geometry1";
final String relatedTableName = "attributes";
final String mappingTableName = "g1_a";
List<UserCustomColumn> additionalColumns = RelatedTablesUtils.createAdditionalUserColumns();
UserMappingTable userMappingTable = UserMappingTable.create(mappingTableName, additionalColumns);
TestCase.assertFalse(rte.has(userMappingTable.getTableName()));
TestCase.assertEquals(UserMappingTable.numRequiredColumns() + additionalColumns.size(), userMappingTable.getColumns().size());
UserCustomColumn baseIdColumn = userMappingTable.getBaseIdColumn();
TestCase.assertNotNull(baseIdColumn);
TestCase.assertTrue(baseIdColumn.isNamed(UserMappingTable.COLUMN_BASE_ID));
TestCase.assertTrue(baseIdColumn.isNotNull());
TestCase.assertFalse(baseIdColumn.isPrimaryKey());
UserCustomColumn relatedIdColumn = userMappingTable.getRelatedIdColumn();
TestCase.assertNotNull(relatedIdColumn);
TestCase.assertTrue(relatedIdColumn.isNamed(UserMappingTable.COLUMN_RELATED_ID));
TestCase.assertTrue(relatedIdColumn.isNotNull());
TestCase.assertFalse(relatedIdColumn.isPrimaryKey());
TestCase.assertFalse(rte.has(userMappingTable.getTableName()));
ExtendedRelation extendedRelation = rte.addAttributesRelationship(baseTableName, relatedTableName, userMappingTable);
TestCase.assertTrue(rte.has());
TestCase.assertTrue(rte.has(userMappingTable.getTableName()));
TestCase.assertNotNull(extendedRelation);
extendedRelations = rte.getRelationships();
TestCase.assertEquals(1, extendedRelations.size());
TestCase.assertTrue(geoPackage.isTable(mappingTableName));
// 7. Add mappings
FeatureDao baseDao = geoPackage.getFeatureDao(baseTableName);
AttributesDao relatedDao = geoPackage.getAttributesDao(relatedTableName);
FeatureResultSet baseFrs = baseDao.queryForAll();
int baseCount = baseFrs.getCount();
long[] baseIds = new long[baseCount];
int inx = 0;
while (baseFrs.moveToNext()) {
baseIds[inx++] = baseFrs.getRow().getId();
}
baseFrs.close();
AttributesResultSet relatedArs = relatedDao.queryForAll();
int relatedCount = relatedArs.getCount();
long[] relatedIds = new long[relatedCount];
inx = 0;
while (relatedArs.moveToNext()) {
relatedIds[inx++] = relatedArs.getRow().getId();
}
relatedArs.close();
UserMappingDao dao = rte.getMappingDao(mappingTableName);
UserMappingRow userMappingRow = null;
for (inx = 0; inx < 10; inx++) {
userMappingRow = dao.newRow();
userMappingRow.setBaseId(baseIds[(int) (Math.random() * baseCount)]);
userMappingRow.setRelatedId(relatedIds[(int) (Math.random() * relatedCount)]);
RelatedTablesUtils.populateUserRow(userMappingTable, userMappingRow, UserMappingTable.requiredColumns());
TestCase.assertTrue(dao.create(userMappingRow) > 0);
}
TestCase.assertEquals(10, dao.count());
userMappingTable = dao.getTable();
String[] columns = userMappingTable.getColumnNames();
UserCustomResultSet resultSet = dao.queryForAll();
int count = resultSet.getCount();
TestCase.assertEquals(10, count);
int manualCount = 0;
while (resultSet.moveToNext()) {
UserMappingRow resultRow = dao.getRow(resultSet);
TestCase.assertFalse(resultRow.hasId());
RelatedTablesUtils.validateUserRow(columns, resultRow);
RelatedTablesUtils.validateDublinCoreColumns(resultRow);
manualCount++;
}
TestCase.assertEquals(count, manualCount);
resultSet.close();
// 8. Remove mappings (note: it is plausible and allowed
// to have duplicate entries)
TestCase.assertTrue(dao.deleteByIds(userMappingRow) > 0);
// 6. Remove relationship
rte.removeRelationship(extendedRelation);
TestCase.assertFalse(rte.has(userMappingTable.getTableName()));
extendedRelations = rte.getRelationships();
TestCase.assertEquals(0, extendedRelations.size());
TestCase.assertFalse(geoPackage.isTable(mappingTableName));
// 3. Remove extension
rte.removeExtension();
TestCase.assertFalse(rte.has());
}
use of mil.nga.geopackage.user.custom.UserCustomResultSet in project geopackage-java by ngageoint.
the class RelatedMediaUtils method testMedia.
/**
* Test related media tables
*
* @param geoPackage
* @throws SQLException
*/
public static void testMedia(GeoPackage geoPackage) throws Exception {
// Create a related tables extension
RelatedTablesExtension rte = new RelatedTablesExtension(geoPackage);
if (rte.has()) {
rte.removeExtension();
}
TestCase.assertFalse(rte.has());
TestCase.assertTrue(rte.getRelationships().isEmpty());
// Choose a random feature table
List<String> featureTables = geoPackage.getFeatureTables();
if (featureTables.isEmpty()) {
// pass with no testing
return;
}
final String baseTableName = featureTables.get((int) (Math.random() * featureTables.size()));
// Populate and validate a media table
List<UserCustomColumn> additionalMediaColumns = RelatedTablesUtils.createAdditionalUserColumns();
MediaTable mediaTable = MediaTable.create(MediaTableMetadata.create("media_table", additionalMediaColumns));
String[] mediaColumns = mediaTable.getColumnNames();
TestCase.assertEquals(MediaTable.numRequiredColumns() + additionalMediaColumns.size(), mediaColumns.length);
UserCustomColumn idColumn = mediaTable.getIdColumn();
TestCase.assertNotNull(idColumn);
TestCase.assertTrue(idColumn.isNamed(MediaTableMetadata.DEFAULT_ID_COLUMN_NAME));
TestCase.assertEquals(GeoPackageDataType.INTEGER, idColumn.getDataType());
TestCase.assertTrue(idColumn.isNotNull());
TestCase.assertTrue(idColumn.isPrimaryKey());
UserCustomColumn dataColumn = mediaTable.getDataColumn();
TestCase.assertNotNull(dataColumn);
TestCase.assertTrue(dataColumn.isNamed(MediaTable.COLUMN_DATA));
TestCase.assertEquals(GeoPackageDataType.BLOB, dataColumn.getDataType());
TestCase.assertTrue(dataColumn.isNotNull());
TestCase.assertFalse(dataColumn.isPrimaryKey());
UserCustomColumn contentTypeColumn = mediaTable.getContentTypeColumn();
TestCase.assertNotNull(contentTypeColumn);
TestCase.assertTrue(contentTypeColumn.isNamed(MediaTable.COLUMN_CONTENT_TYPE));
TestCase.assertEquals(GeoPackageDataType.TEXT, contentTypeColumn.getDataType());
TestCase.assertTrue(contentTypeColumn.isNotNull());
TestCase.assertFalse(contentTypeColumn.isPrimaryKey());
// Create and validate a mapping table
List<UserCustomColumn> additionalMappingColumns = RelatedTablesUtils.createAdditionalUserColumns();
final String mappingTableName = "features_media";
UserMappingTable userMappingTable = UserMappingTable.create(mappingTableName, additionalMappingColumns);
TestCase.assertFalse(rte.has(userMappingTable.getTableName()));
TestCase.assertEquals(UserMappingTable.numRequiredColumns() + additionalMappingColumns.size(), userMappingTable.getColumns().size());
UserCustomColumn baseIdColumn = userMappingTable.getBaseIdColumn();
TestCase.assertNotNull(baseIdColumn);
TestCase.assertTrue(baseIdColumn.isNamed(UserMappingTable.COLUMN_BASE_ID));
TestCase.assertEquals(GeoPackageDataType.INTEGER, baseIdColumn.getDataType());
TestCase.assertTrue(baseIdColumn.isNotNull());
TestCase.assertFalse(baseIdColumn.isPrimaryKey());
UserCustomColumn relatedIdColumn = userMappingTable.getRelatedIdColumn();
TestCase.assertNotNull(relatedIdColumn);
TestCase.assertTrue(relatedIdColumn.isNamed(UserMappingTable.COLUMN_RELATED_ID));
TestCase.assertEquals(GeoPackageDataType.INTEGER, relatedIdColumn.getDataType());
TestCase.assertTrue(relatedIdColumn.isNotNull());
TestCase.assertFalse(relatedIdColumn.isPrimaryKey());
TestCase.assertFalse(rte.has(userMappingTable.getTableName()));
// Create the media table, content row, and relationship between the
// feature table and media table
ContentsDao contentsDao = geoPackage.getContentsDao();
TestCase.assertFalse(contentsDao.getTables().contains(mediaTable.getTableName()));
ExtendedRelation extendedRelation = rte.addMediaRelationship(baseTableName, mediaTable, userMappingTable);
validateContents(mediaTable, mediaTable.getContents());
TestCase.assertTrue(rte.has());
TestCase.assertTrue(rte.has(userMappingTable.getTableName()));
TestCase.assertNotNull(extendedRelation);
List<ExtendedRelation> extendedRelations = rte.getRelationships();
TestCase.assertEquals(1, extendedRelations.size());
TestCase.assertTrue(geoPackage.isTable(mappingTableName));
TestCase.assertTrue(geoPackage.isTable(mediaTable.getTableName()));
TestCase.assertTrue(contentsDao.getTables().contains(mediaTable.getTableName()));
validateContents(mediaTable, contentsDao.queryForId(mediaTable.getTableName()));
TestCase.assertEquals(MediaTable.RELATION_TYPE.getDataType(), geoPackage.getTableType(mediaTable.getTableName()));
TestCase.assertTrue(geoPackage.isTableType(mediaTable.getTableName(), MediaTable.RELATION_TYPE.getDataType()));
// Validate the media DAO
MediaDao mediaDao = rte.getMediaDao(mediaTable);
TestCase.assertNotNull(mediaDao);
mediaTable = mediaDao.getTable();
TestCase.assertNotNull(mediaTable);
validateContents(mediaTable, mediaTable.getContents());
// Insert media table rows
byte[] mediaData = TestUtils.getTileBytes();
String contentType = "image/png";
BufferedImage mediaImage = ImageUtils.getImage(mediaData);
int imageWidth = mediaImage.getWidth();
int imageHeight = mediaImage.getHeight();
int mediaCount = 2 + (int) (Math.random() * 9);
long mediaRowId = 0;
// Create and insert the first mediaCount - 1 rows
for (int i = 0; i < mediaCount - 1; i++) {
MediaRow mediaRow = mediaDao.newRow();
mediaRow.setData(mediaData);
mediaRow.setContentType(contentType);
RelatedTablesUtils.populateUserRow(mediaTable, mediaRow, MediaTable.requiredColumns());
mediaRowId = mediaDao.create(mediaRow);
TestCase.assertTrue(mediaRowId > 0);
}
// Copy the last row insert and insert the final media row
MediaRow mediaRowToCopy = new MediaRow(mediaDao.queryForIdRow(mediaRowId));
MediaRow mediaRowCopy = mediaRowToCopy.copy();
long copyMediaRowId = mediaDao.create(mediaRowCopy);
TestCase.assertTrue(copyMediaRowId > 0);
TestCase.assertEquals(mediaRowId + 1, copyMediaRowId);
TestCase.assertEquals(mediaCount, mediaDao.count());
// Build the Feature ids
FeatureDao featureDao = geoPackage.getFeatureDao(baseTableName);
FeatureResultSet featureResultSet = featureDao.queryForAll();
int featureCount = featureResultSet.getCount();
List<Long> featureIds = new ArrayList<>();
while (featureResultSet.moveToNext()) {
featureIds.add(featureResultSet.getRow().getId());
}
featureResultSet.close();
// Build the Media ids
UserCustomResultSet mediaResultSet = mediaDao.queryForAll();
mediaCount = mediaResultSet.getCount();
List<Long> mediaIds = new ArrayList<>();
while (mediaResultSet.moveToNext()) {
mediaIds.add(mediaResultSet.getRow().getId());
}
mediaResultSet.close();
// Insert user mapping rows between feature ids and media ids
UserMappingDao dao = rte.getMappingDao(mappingTableName);
UserMappingRow userMappingRow = null;
for (int i = 0; i < 10; i++) {
userMappingRow = dao.newRow();
userMappingRow.setBaseId(featureIds.get((int) (Math.random() * featureCount)));
userMappingRow.setRelatedId(mediaIds.get((int) (Math.random() * mediaCount)));
RelatedTablesUtils.populateUserRow(userMappingTable, userMappingRow, UserMappingTable.requiredColumns());
TestCase.assertTrue(dao.create(userMappingRow) > 0);
}
TestCase.assertEquals(10, dao.count());
// Validate the user mapping rows
userMappingTable = dao.getTable();
String[] mappingColumns = userMappingTable.getColumnNames();
UserCustomResultSet resultSet = dao.queryForAll();
int count = resultSet.getCount();
TestCase.assertEquals(10, count);
int manualCount = 0;
while (resultSet.moveToNext()) {
UserMappingRow resultRow = dao.getRow(resultSet);
TestCase.assertFalse(resultRow.hasId());
TestCase.assertTrue(featureIds.contains(resultRow.getBaseId()));
TestCase.assertTrue(mediaIds.contains(resultRow.getRelatedId()));
RelatedTablesUtils.validateUserRow(mappingColumns, resultRow);
RelatedTablesUtils.validateDublinCoreColumns(resultRow);
manualCount++;
}
TestCase.assertEquals(count, manualCount);
resultSet.close();
ExtendedRelationsDao extendedRelationsDao = rte.getExtendedRelationsDao();
// Get the relations starting from the feature table
List<ExtendedRelation> featureExtendedRelations = extendedRelationsDao.getBaseTableRelations(featureDao.getTableName());
List<ExtendedRelation> featureExtendedRelations2 = extendedRelationsDao.getTableRelations(featureDao.getTableName());
TestCase.assertEquals(1, featureExtendedRelations.size());
TestCase.assertEquals(1, featureExtendedRelations2.size());
TestCase.assertEquals(featureExtendedRelations.get(0).getId(), featureExtendedRelations2.get(0).getId());
TestCase.assertTrue(extendedRelationsDao.getRelatedTableRelations(featureDao.getTableName()).isEmpty());
// Test the feature table relations
for (ExtendedRelation featureRelation : featureExtendedRelations) {
// Test the relation
TestCase.assertTrue(featureRelation.getId() >= 0);
TestCase.assertEquals(featureDao.getTableName(), featureRelation.getBaseTableName());
TestCase.assertEquals(featureDao.getPkColumnName(), featureRelation.getBasePrimaryColumn());
TestCase.assertEquals(mediaDao.getTableName(), featureRelation.getRelatedTableName());
TestCase.assertEquals(mediaDao.getPkColumnName(), featureRelation.getRelatedPrimaryColumn());
TestCase.assertEquals(MediaTable.RELATION_TYPE.getName(), featureRelation.getRelationName());
TestCase.assertEquals(mappingTableName, featureRelation.getMappingTableName());
// Test the user mappings from the relation
UserMappingDao userMappingDao = rte.getMappingDao(featureRelation);
int totalMappedCount = userMappingDao.count();
UserCustomResultSet mappingResultSet = userMappingDao.queryForAll();
while (mappingResultSet.moveToNext()) {
userMappingRow = userMappingDao.getRow(mappingResultSet);
TestCase.assertTrue(featureIds.contains(userMappingRow.getBaseId()));
TestCase.assertTrue(mediaIds.contains(userMappingRow.getRelatedId()));
RelatedTablesUtils.validateUserRow(mappingColumns, userMappingRow);
RelatedTablesUtils.validateDublinCoreColumns(userMappingRow);
}
mappingResultSet.close();
// Get and test the media DAO
mediaDao = rte.getMediaDao(featureRelation);
TestCase.assertNotNull(mediaDao);
mediaTable = mediaDao.getTable();
TestCase.assertNotNull(mediaTable);
validateContents(mediaTable, mediaTable.getContents());
// Get and test the Media Rows mapped to each Feature Row
featureResultSet = featureDao.queryForAll();
int totalMapped = 0;
while (featureResultSet.moveToNext()) {
FeatureRow featureRow = featureResultSet.getRow();
List<Long> mappedIds = rte.getMappingsForBase(featureRelation, featureRow.getId());
List<MediaRow> mediaRows = mediaDao.getRows(mappedIds);
TestCase.assertEquals(mappedIds.size(), mediaRows.size());
for (MediaRow mediaRow : mediaRows) {
TestCase.assertTrue(mediaRow.hasId());
TestCase.assertTrue(mediaRow.getId() >= 0);
TestCase.assertTrue(mediaIds.contains(mediaRow.getId()));
TestCase.assertTrue(mappedIds.contains(mediaRow.getId()));
GeoPackageGeometryDataUtils.compareByteArrays(mediaData, mediaRow.getData());
TestCase.assertEquals(contentType, mediaRow.getContentType());
RelatedTablesUtils.validateUserRow(mediaColumns, mediaRow);
RelatedTablesUtils.validateDublinCoreColumns(mediaRow);
validateDublinCoreColumns(mediaRow);
BufferedImage image = mediaRow.getDataImage();
TestCase.assertNotNull(image);
TestCase.assertEquals(imageWidth, image.getWidth());
TestCase.assertEquals(imageHeight, image.getHeight());
}
totalMapped += mappedIds.size();
}
featureResultSet.close();
TestCase.assertEquals(totalMappedCount, totalMapped);
}
// Get the relations starting from the media table
List<ExtendedRelation> mediaExtendedRelations = extendedRelationsDao.getRelatedTableRelations(mediaTable.getTableName());
List<ExtendedRelation> mediaExtendedRelations2 = extendedRelationsDao.getTableRelations(mediaTable.getTableName());
TestCase.assertEquals(1, mediaExtendedRelations.size());
TestCase.assertEquals(1, mediaExtendedRelations2.size());
TestCase.assertEquals(mediaExtendedRelations.get(0).getId(), mediaExtendedRelations2.get(0).getId());
TestCase.assertTrue(extendedRelationsDao.getBaseTableRelations(mediaTable.getTableName()).isEmpty());
// Test the media table relations
for (ExtendedRelation mediaRelation : mediaExtendedRelations) {
// Test the relation
TestCase.assertTrue(mediaRelation.getId() >= 0);
TestCase.assertEquals(featureDao.getTableName(), mediaRelation.getBaseTableName());
TestCase.assertEquals(featureDao.getPkColumnName(), mediaRelation.getBasePrimaryColumn());
TestCase.assertEquals(mediaDao.getTableName(), mediaRelation.getRelatedTableName());
TestCase.assertEquals(mediaDao.getPkColumnName(), mediaRelation.getRelatedPrimaryColumn());
TestCase.assertEquals(MediaTable.RELATION_TYPE.getName(), mediaRelation.getRelationName());
TestCase.assertEquals(mappingTableName, mediaRelation.getMappingTableName());
// Test the user mappings from the relation
UserMappingDao userMappingDao = rte.getMappingDao(mediaRelation);
int totalMappedCount = userMappingDao.count();
UserCustomResultSet mappingResultSet = userMappingDao.queryForAll();
while (mappingResultSet.moveToNext()) {
userMappingRow = userMappingDao.getRow(mappingResultSet);
TestCase.assertTrue(featureIds.contains(userMappingRow.getBaseId()));
TestCase.assertTrue(mediaIds.contains(userMappingRow.getRelatedId()));
RelatedTablesUtils.validateUserRow(mappingColumns, userMappingRow);
RelatedTablesUtils.validateDublinCoreColumns(userMappingRow);
}
mappingResultSet.close();
// Get and test the feature DAO
featureDao = geoPackage.getFeatureDao(featureDao.getTableName());
TestCase.assertNotNull(featureDao);
FeatureTable featureTable = featureDao.getTable();
TestCase.assertNotNull(featureTable);
Contents featureContents = featureDao.getContents();
TestCase.assertNotNull(featureContents);
TestCase.assertEquals(ContentsDataType.FEATURES, featureContents.getDataType());
TestCase.assertEquals(ContentsDataType.FEATURES.getName(), featureContents.getDataTypeName());
TestCase.assertEquals(featureTable.getTableName(), featureContents.getTableName());
TestCase.assertNotNull(featureContents.getLastChange());
// Get and test the Feature Rows mapped to each Media Row
mediaResultSet = mediaDao.queryForAll();
int totalMapped = 0;
while (mediaResultSet.moveToNext()) {
MediaRow mediaRow = mediaDao.getRow(mediaResultSet);
List<Long> mappedIds = rte.getMappingsForRelated(mediaRelation, mediaRow.getId());
for (long mappedId : mappedIds) {
FeatureRow featureRow = featureDao.queryForIdRow(mappedId);
TestCase.assertNotNull(featureRow);
TestCase.assertTrue(featureRow.hasId());
TestCase.assertTrue(featureRow.getId() >= 0);
TestCase.assertTrue(featureIds.contains(featureRow.getId()));
TestCase.assertTrue(mappedIds.contains(featureRow.getId()));
if (featureRow.getValue(featureRow.getGeometryColumnIndex()) != null) {
GeoPackageGeometryData geometryData = featureRow.getGeometry();
TestCase.assertNotNull(geometryData);
if (!geometryData.isEmpty()) {
TestCase.assertNotNull(geometryData.getGeometry());
}
}
}
totalMapped += mappedIds.size();
}
mediaResultSet.close();
TestCase.assertEquals(totalMappedCount, totalMapped);
}
// Add more columns to the media table
int existingColumns = mediaTable.getColumns().size();
UserCustomColumn mediaIdColumn = mediaTable.getIdColumn();
UserCustomColumn mediaDataColumn = mediaTable.getDataColumn();
UserCustomColumn mediaContentTypeColumn = mediaTable.getContentTypeColumn();
int newColumns = 0;
String newColumnName = "new_column";
mediaDao.addColumn(UserCustomColumn.createColumn(newColumnName + ++newColumns, GeoPackageDataType.TEXT));
mediaDao.addColumn(UserCustomColumn.createColumn(newColumnName + ++newColumns, GeoPackageDataType.BLOB));
TestCase.assertEquals(existingColumns + 2, mediaTable.getColumns().size());
for (int index = existingColumns; index < mediaTable.getColumns().size(); index++) {
String name = newColumnName + (index - existingColumns + 1);
TestCase.assertEquals(name, mediaTable.getColumnName(index));
TestCase.assertEquals(index, mediaTable.getColumnIndex(name));
TestCase.assertEquals(name, mediaTable.getColumn(index).getName());
TestCase.assertEquals(index, mediaTable.getColumn(index).getIndex());
TestCase.assertEquals(name, mediaTable.getColumnNames()[index]);
TestCase.assertEquals(name, mediaTable.getColumns().get(index).getName());
try {
mediaTable.getColumn(index).setIndex(index - 1);
TestCase.fail("Changed index on a created table column");
} catch (Exception e) {
}
mediaTable.getColumn(index).setIndex(index);
}
TestCase.assertEquals(mediaIdColumn, mediaTable.getIdColumn());
TestCase.assertEquals(mediaDataColumn, mediaTable.getDataColumn());
TestCase.assertEquals(mediaContentTypeColumn, mediaTable.getContentTypeColumn());
// Add another row with the new columns and read it
MediaRow mediaRow = mediaDao.newRow();
mediaRow.setData(mediaData);
mediaRow.setContentType(contentType);
RelatedTablesUtils.populateUserRow(mediaTable, mediaRow, MediaTable.requiredColumns());
String newValue = UUID.randomUUID().toString();
mediaRow.setValue(existingColumns, newValue);
mediaRow.setValue(existingColumns + 1, mediaRow.getData());
mediaRowId = mediaDao.create(mediaRow);
TestCase.assertTrue(mediaRowId > 0);
MediaRow newMediaRow = mediaDao.getRow(mediaDao.queryForIdRow(mediaRowId));
TestCase.assertNotNull(newMediaRow);
TestCase.assertEquals(newValue, newMediaRow.getValue(existingColumns));
GeoPackageGeometryDataUtils.compareByteArrays(mediaRow.getData(), (byte[]) newMediaRow.getValue(existingColumns + 1));
// Delete a single mapping
int countOfIds = dao.countByIds(userMappingRow);
TestCase.assertEquals(countOfIds, dao.deleteByIds(userMappingRow));
TestCase.assertEquals(10 - countOfIds, dao.count());
// Delete the relationship and user mapping table
rte.removeRelationship(extendedRelation);
TestCase.assertFalse(rte.has(userMappingTable.getTableName()));
extendedRelations = rte.getRelationships();
TestCase.assertEquals(0, extendedRelations.size());
TestCase.assertFalse(geoPackage.isTable(mappingTableName));
// Delete the media table and contents row
TestCase.assertTrue(geoPackage.isTable(mediaTable.getTableName()));
TestCase.assertNotNull(contentsDao.queryForId(mediaTable.getTableName()));
geoPackage.deleteTable(mediaTable.getTableName());
TestCase.assertFalse(geoPackage.isTable(mediaTable.getTableName()));
TestCase.assertNull(contentsDao.queryForId(mediaTable.getTableName()));
// Delete the related tables extension
rte.removeExtension();
TestCase.assertFalse(rte.has());
}
Aggregations