Skip to content

Commit

Permalink
collection insertion works now correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
flurfis committed Dec 2, 2023
1 parent ef57f8f commit 8e0f856
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.polypheny.db.catalog.logistic.DataModel;
import org.polypheny.db.catalog.logistic.EntityType;
import org.polypheny.db.util.Pair;

Expand Down Expand Up @@ -237,8 +238,8 @@ public ImmutableMap<Long, List<BackupEntityWrapper<LogicalEntity>>> wrapLogicalE
bupEntityList.add( tempBupEntity );


// create entityReferences for each table (if there is a reference) with tableDependencies, and add entityReferences to the backupinformationobject
if (entity.getEntityType().equals( EntityType.ENTITY)) {
// create entityReferences for each table (if there is a reference) with tableDependencies, and add entityReferences to the backupinformationobject, but only for relational entities
if (entity.getEntityType().equals( EntityType.ENTITY) && !(entity.getDataModel().equals( DataModel.DOCUMENT ) || entity.getDataModel().equals( DataModel.GRAPH ))) {
EntityReferencer entityReferencer = new EntityReferencer( entity.getId(), BackupEntityType.TABLE );
if (tableDependencies.containsKey( entity.getId() )) {
entityReferencer.setReferencerTables( tableDependencies.get( entity.getId() ) );
Expand Down
5 changes: 5 additions & 0 deletions dbms/src/main/java/org/polypheny/db/backup/BackupManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ private void wrapEntities() {
Map<Long, List<Long>> tableDependencies = new HashMap<>(); // key: tableId, value: referencedKeyTableId
Map<Long, List<Pair<Long, Long>>> namespaceTableDependendencies = new HashMap<>(); // key: namespaceId, value: <namespaceId, referencedKeyTableId>
Map<Long, List<Long>> viewDependencies = new HashMap<>();
//TODO(FF): are there dependencies for collections? (views/indexes from collections?)

//go through all foreign keys, and check if the namespaceId equals the referencedKeySchemaId, and if not, add it to the namespaceDependencies map, with the namespaceId as key and the referencedKeySchemaId as value
for ( Map.Entry<Long, List<LogicalForeignKey>> entry : foreignKeysPerTable.entrySet() ) {
Expand Down Expand Up @@ -181,6 +182,10 @@ private void wrapEntities() {
ImmutableMap<Long, List<BackupEntityWrapper<LogicalEntity>>> wrappedTables = backupInformationObject.wrapLogicalEntities( backupInformationObject.getTables(), tableDependencies, namespaceTableDependendencies, true);
backupInformationObject.setWrappedTables( wrappedTables );

// wrap all collections with BackupEntityWrapper
ImmutableMap<Long, List<BackupEntityWrapper<LogicalEntity>>> wrappedCollections = backupInformationObject.wrapLogicalEntities( backupInformationObject.getCollections(), null, namespaceTableDependendencies, true);
backupInformationObject.setWrappedCollections( wrappedCollections );

/*
ArrayList<LogicalTable> lol = new ArrayList<>();
lol.add( (LogicalTable) backupInformationObject.getTables().get( 0 ));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public void start( BackupInformationObject backupInformationObject ) {
insertCreateTable( backupInformationObject.getWrappedTables() );

// alter table - add unique constraint
//TODO(FF): only call if there are any relational schemas (machts senn??)
insertAlterTableUQ( backupInformationObject.getWrappedTables(), backupInformationObject.getConstraints() );
insertAlterTableFK( backupInformationObject.getWrappedTables(), backupInformationObject.getForeignKeysPerTable() );

Expand Down Expand Up @@ -302,41 +303,48 @@ private void insertAlterTableUQ( ImmutableMap<Long, List<BackupEntityWrapper<Log

private void insertAlterTableFK( ImmutableMap<Long, List<BackupEntityWrapper<LogicalEntity>>> bupTables, ImmutableMap<Long, List<LogicalForeignKey>> foreignKeysPerTable ) {
String query = new String();

// go through foreign key constraints and collect the necessary data
for ( Map.Entry<Long, List<LogicalForeignKey>> fkListPerTable : foreignKeysPerTable.entrySet() ) {
Long tableId = fkListPerTable.getKey();

for ( LogicalForeignKey foreignKey : fkListPerTable.getValue() ) {
// get the table where the foreign key is saved
Long nsId = foreignKey.namespaceId;
BackupEntityWrapper<LogicalEntity> table = backupInformationObject.getWrappedTables().get( nsId ).stream().filter( e -> e.getEntityObject().unwrap( LogicalTable.class ).getId() == tableId ).findFirst().get();
//boolean lol = table.getToBeInserted();
// check if the table is marked to be inserted
if (table.getToBeInserted()) {
String namespaceName = backupInformationObject.getWrappedNamespaces().get( foreignKey.namespaceId ).getNameForQuery();
String tableName = backupInformationObject.getWrappedTables().get( foreignKey.namespaceId ).stream().filter( e -> e.getEntityObject().unwrap( LogicalTable.class ).getId() == foreignKey.tableId ).findFirst().get().getNameForQuery();
String constraintName = foreignKey.name;
String listOfCols = getListOfCol( foreignKey.columnIds, backupInformationObject.getColumns().get( foreignKey.tableId ) );
String referencedNamespaceName = backupInformationObject.getWrappedNamespaces().get( foreignKey.referencedKeySchemaId ).getNameForQuery();
String referencedTableName = backupInformationObject.getWrappedTables().get( foreignKey.referencedKeySchemaId ).stream().filter( e -> e.getEntityObject().unwrap( LogicalTable.class ).getId() == foreignKey.referencedKeyTableId ).findFirst().get().getNameForQuery();
String referencedListOfCols = getListOfCol( foreignKey.referencedKeyColumnIds, backupInformationObject.getColumns().get( foreignKey.referencedKeyTableId ) );
String updateAction = foreignKey.updateRule.foreignKeyOptionToString();
String deleteAction = foreignKey.deleteRule.foreignKeyOptionToString();
//enforcementTime (on commit) - right now is manually set to the same thing everywhere (in the rest of polypheny)

query = String.format( "ALTER TABLE %s11.%s11 ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s11.%s11 (%s) ON UPDATE %s ON DELETE %s", namespaceName, tableName, constraintName, listOfCols, referencedNamespaceName, referencedTableName, referencedListOfCols, updateAction, deleteAction );
log.info( query );
executeStatementInPolypheny( query, nsId, DataModel.RELATIONAL );
//if (!foreignKeysPerTable.isEmpty()) {
// go through foreign key constraints and collect the necessary data
for ( Map.Entry<Long, List<LogicalForeignKey>> fkListPerTable : foreignKeysPerTable.entrySet() ) {
if (!(fkListPerTable.getValue().isEmpty())) {
Long tableId = fkListPerTable.getKey();

for ( LogicalForeignKey foreignKey : fkListPerTable.getValue() ) {
// get the table where the foreign key is saved
Long nsId = foreignKey.namespaceId;
BackupEntityWrapper<LogicalEntity> table = backupInformationObject.getWrappedTables().get( nsId ).stream().filter( e -> e.getEntityObject().unwrap( LogicalTable.class ).getId() == tableId ).findFirst().get();
//boolean lol = table.getToBeInserted();
// check if the table is marked to be inserted
if (table.getToBeInserted()) {
String namespaceName = backupInformationObject.getWrappedNamespaces().get( foreignKey.namespaceId ).getNameForQuery();
String tableName = backupInformationObject.getWrappedTables().get( foreignKey.namespaceId ).stream().filter( e -> e.getEntityObject().unwrap( LogicalTable.class ).getId() == foreignKey.tableId ).findFirst().get().getNameForQuery();
String constraintName = foreignKey.name;
String listOfCols = getListOfCol( foreignKey.columnIds, backupInformationObject.getColumns().get( foreignKey.tableId ) );
String referencedNamespaceName = backupInformationObject.getWrappedNamespaces().get( foreignKey.referencedKeySchemaId ).getNameForQuery();
String referencedTableName = backupInformationObject.getWrappedTables().get( foreignKey.referencedKeySchemaId ).stream().filter( e -> e.getEntityObject().unwrap( LogicalTable.class ).getId() == foreignKey.referencedKeyTableId ).findFirst().get().getNameForQuery();
String referencedListOfCols = getListOfCol( foreignKey.referencedKeyColumnIds, backupInformationObject.getColumns().get( foreignKey.referencedKeyTableId ) );
String updateAction = foreignKey.updateRule.foreignKeyOptionToString();
String deleteAction = foreignKey.deleteRule.foreignKeyOptionToString();
//enforcementTime (on commit) - right now is manually set to the same thing everywhere (in the rest of polypheny)

query = String.format( "ALTER TABLE %s11.%s11 ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s11.%s11 (%s) ON UPDATE %s ON DELETE %s", namespaceName, tableName, constraintName, listOfCols, referencedNamespaceName, referencedTableName, referencedListOfCols, updateAction, deleteAction );
log.info( query );
executeStatementInPolypheny( query, nsId, DataModel.RELATIONAL );
}
}
}

}
}
//}


}


private void insertCreateCollection(ImmutableMap<Long, List<BackupEntityWrapper<LogicalEntity>>> wrappedCollections) {
String query = new String();

//FIXME(FF): collections are not wrapped yet!!
// go through all collections per namespace and create and execute a query
for ( Map.Entry<Long, List<BackupEntityWrapper<LogicalEntity>>> collectionsPerNs : wrappedCollections.entrySet() ) {
Long nsID = collectionsPerNs.getKey();
Expand All @@ -349,14 +357,14 @@ private void insertCreateCollection(ImmutableMap<Long, List<BackupEntityWrapper<
// only create collections that should be inserted
if ( collection.getToBeInserted()) {
// only create tables that don't (exist by default in polypheny)
query = String.format( "db.createCollection(\"%s\")", collection.getNameForQuery() );
query = String.format( "db.createCollection(\"%s11\")", collection.getNameForQuery() );
log.info( query );
executeStatementInPolypheny( query, nsID, DataModel.DOCUMENT );

}
}
}
//db.createCollection('users')
executeStatementInPolypheny( "db.createCollection(\"users\")", Catalog.defaultNamespaceId, DataModel.DOCUMENT );
//executeStatementInPolypheny( "db.createCollection(\"users\")", Catalog.defaultNamespaceId, DataModel.DOCUMENT );
}


Expand Down Expand Up @@ -596,6 +604,7 @@ private String nullableBoolToString (boolean nullable) {


private void executeStatementInPolypheny( String query, Long namespaceId, DataModel dataModel ) {
log.info( "entered execution with query:"+query );
Transaction transaction;
Statement statement = null;
PolyImplementation result;
Expand Down

0 comments on commit 8e0f856

Please sign in to comment.