From 152ea92fe25c685a77e98c8d1234eaf594163de0 Mon Sep 17 00:00:00 2001 From: Flurina Fischer Date: Thu, 23 Nov 2023 10:52:03 +0100 Subject: [PATCH] default works now, changed for LogicalTable that polymorphism works now --- .../catalog/entity/logical/LogicalIndex.java | 14 +- .../db/catalog/logistic/Collation.java | 3 +- .../db/backup/BackupEntityWrapper.java | 18 ++- .../db/backup/BackupInformationObject.java | 66 +++++++-- .../polypheny/db/backup/BackupManager.java | 15 +- .../db/backup/datagatherer/GatherSchema.java | 7 +- .../db/backup/datainserter/InsertSchema.java | 78 +++++++---- .../org/polypheny/db/backup/GeneralTest.java | 132 ++++++++++++++++++ .../java/org/polypheny/db/webui/Crud.java | 3 +- 9 files changed, 283 insertions(+), 53 deletions(-) create mode 100644 dbms/src/test/java/org/polypheny/db/backup/GeneralTest.java diff --git a/core/src/main/java/org/polypheny/db/catalog/entity/logical/LogicalIndex.java b/core/src/main/java/org/polypheny/db/catalog/entity/logical/LogicalIndex.java index 078ee67b8e..98bca9668d 100644 --- a/core/src/main/java/org/polypheny/db/catalog/entity/logical/LogicalIndex.java +++ b/core/src/main/java/org/polypheny/db/catalog/entity/logical/LogicalIndex.java @@ -36,7 +36,7 @@ @EqualsAndHashCode(callSuper = false) @Value @SuperBuilder(toBuilder = true) -public class LogicalIndex implements Serializable { +public class LogicalIndex implements LogicalObject { private static final long serialVersionUID = -318228681682792406L; @@ -118,6 +118,18 @@ public Serializable[] getParameterArray( int ordinalPosition, String columnName } + @Override + public Serializable[] getParameterArray() { + return new Serializable[0]; + } + + + @Override + public Visibility getVisibility() { + return LogicalObject.super.getVisibility(); + } + + // Used for creating ResultSets @RequiredArgsConstructor @Value diff --git a/core/src/main/java/org/polypheny/db/catalog/logistic/Collation.java b/core/src/main/java/org/polypheny/db/catalog/logistic/Collation.java index 6a8136b2e1..56251e9302 100644 --- a/core/src/main/java/org/polypheny/db/catalog/logistic/Collation.java +++ b/core/src/main/java/org/polypheny/db/catalog/logistic/Collation.java @@ -17,6 +17,7 @@ package org.polypheny.db.catalog.logistic; import lombok.NonNull; +import org.polypheny.db.catalog.exceptions.GenericRuntimeException; import org.polypheny.db.config.RuntimeConfig; public enum Collation { @@ -64,7 +65,7 @@ public String collationToString( ) { return "CASE INSENSITIVE"; } else { - throw new RuntimeException( "Collation not supported" ); + throw new GenericRuntimeException( "Collation not supported" ); } } catch ( Exception e ) { throw new RuntimeException( e ); diff --git a/dbms/src/main/java/org/polypheny/db/backup/BackupEntityWrapper.java b/dbms/src/main/java/org/polypheny/db/backup/BackupEntityWrapper.java index 7b48502564..560d6ed9c2 100644 --- a/dbms/src/main/java/org/polypheny/db/backup/BackupEntityWrapper.java +++ b/dbms/src/main/java/org/polypheny/db/backup/BackupEntityWrapper.java @@ -18,9 +18,11 @@ import lombok.Getter; import lombok.Setter; +import org.polypheny.db.catalog.entity.LogicalObject; +import org.polypheny.db.catalog.entity.logical.LogicalEntity; @Getter @Setter -public class BackupEntityWrapper { +public class BackupEntityWrapper { private E entityObject; @@ -31,4 +33,18 @@ public class BackupEntityWrapper { private EntityReferencer entityReferencer; + + public BackupEntityWrapper( E entity, Boolean toBeInserted, String nameForQuery, EntityReferencer entityReferencer ) { + this.entityObject = entity; + this.toBeInserted = toBeInserted; + this.nameForQuery = nameForQuery; + this.entityReferencer = entityReferencer; + } + + public BackupEntityWrapper( E entity, String nameForQuery, EntityReferencer entityReferencer ) { + this.entityObject = entity; + this.nameForQuery = nameForQuery; + this.entityReferencer = entityReferencer; + } + } diff --git a/dbms/src/main/java/org/polypheny/db/backup/BackupInformationObject.java b/dbms/src/main/java/org/polypheny/db/backup/BackupInformationObject.java index af39decc7b..3ee72f88ed 100644 --- a/dbms/src/main/java/org/polypheny/db/backup/BackupInformationObject.java +++ b/dbms/src/main/java/org/polypheny/db/backup/BackupInformationObject.java @@ -63,9 +63,9 @@ public class BackupInformationObject { private ImmutableMap>> wrappedMaterializedViews; - private ImmutableMap> tables; + private ImmutableMap> tables; //TODO: cast all to logicaltable - LogicalEntity.unwrap(logicalTable.class) to get logicalTAble, for everything here logicalEntity - private ImmutableMap>> wrappedTables; + private ImmutableMap>> wrappedTables; private ImmutableMap> collections; @@ -100,12 +100,16 @@ public ImmutableMap> wrapNamespaces( ImmutableMap> resultMap; Map> tempNS = new HashMap<>(); - BackupEntityWrapper nsBupObj = new BackupEntityWrapper<>(); + //BackupEntityWrapper nsBupObj = new BackupEntityWrapper<>(); for ( LogicalNamespace ns : namespaces ) { + /* nsBupObj.setEntityObject( ns ); nsBupObj.setToBeInserted( toBeInserted ); nsBupObj.setNameForQuery( ns.name ); + */ + //E entity, Boolean toBeInserted, String nameForQuery, EntityReferencer entityReferencer + BackupEntityWrapper nsBupObj = new BackupEntityWrapper<>(ns, toBeInserted, ns.name, null); tempNS.put( ns.id, nsBupObj ); } @@ -120,9 +124,11 @@ public ImmutableMap> wrapNamespaces( Map> tempNS = new HashMap<>(); for ( LogicalNamespace ns : namespaces ) { + /* BackupEntityWrapper nsBupObj = new BackupEntityWrapper<>(); nsBupObj.setEntityObject( ns ); nsBupObj.setNameForQuery( ns.name ); + */ // create entityReferences for each namespace (if there is a reference) with namespacedependencies, and add entityReferences to the backupinformationobject if ( namespaceDependencies.containsKey( ns.id ) || namespaceTableDependencies.containsKey( ns.id ) ) { @@ -140,17 +146,21 @@ public ImmutableMap> wrapNamespaces( } entityReferencer.setReferencerTables( tempReferencerTables ); } - nsBupObj.setEntityReferencer( entityReferencer ); + BackupEntityWrapper nsBupObj = new BackupEntityWrapper<>(ns, ns.name, entityReferencer); + tempNS.put( ns.id, nsBupObj ); + //nsBupObj.setEntityReferencer( entityReferencer ); + } else { + //E entity, Boolean toBeInserted, String nameForQuery, EntityReferencer entityReferencer + BackupEntityWrapper nsBupObj = new BackupEntityWrapper<>(ns, ns.name, null); + tempNS.put( ns.id, nsBupObj ); } - - tempNS.put( ns.id, nsBupObj ); } resultMap = ImmutableMap.copyOf( tempNS ); return resultMap; } - + /* public ImmutableMap>> wrapLogicalEntities( ImmutableMap> entityMap, Boolean toBeInserted ) { ImmutableMap>> resultMap; @@ -202,24 +212,29 @@ public ImmutableMap>> wrapLogicalE return resultMap; } + */ - public ImmutableMap>> tempWrapLogicalTables( ImmutableMap> entityMap, Map> tableDependencies, Map>> namespaceTableDependendencies, Boolean toBeInserted ) { + public ImmutableMap>> wrapLogicalEntities( Map> entityMap, Map> tableDependencies, Map>> namespaceTableDependendencies, Boolean toBeInserted ) { - ImmutableMap>> resultMap; - Map>> tempMap = new HashMap<>(); + ImmutableMap>> resultMap; + Map>> tempMap = new HashMap<>(); //go through each element from entityMap, and for each list go through each element and transform it to a BupSuperEntity - for ( Map.Entry> entry : entityMap.entrySet() ) { - List entityList = entry.getValue(); - List> bupEntityList = new ArrayList<>(); + for ( Map.Entry> entry : entityMap.entrySet() ) { + List entityList = entry.getValue(); + List> bupEntityList = new ArrayList<>(); - for ( LogicalTable entity : entityList ) { - BackupEntityWrapper tempBupEntity = new BackupEntityWrapper<>(); + for ( LogicalEntity entity : entityList ) { + BackupEntityWrapper tempBupEntity = new BackupEntityWrapper<>(entity, toBeInserted, entity.name, null); + /* tempBupEntity.setEntityObject( entity ); tempBupEntity.setToBeInserted( toBeInserted ); tempBupEntity.setNameForQuery( entity.name ); + + */ 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)) { EntityReferencer entityReferencer = new EntityReferencer( entity.getId(), BackupEntityType.TABLE ); @@ -251,6 +266,7 @@ public void transformationManager() { } + /* public List> wrapLogicalEntity( List entityList ) { //go through each element from entityMap, and for each list go through each element and transform it to a BupSuperEntity @@ -268,4 +284,24 @@ public List> wrapLogicalEntity( List test (List entityList) { + ImmutableMap resultMap; + Map tempMap = new HashMap<>(); + tempMap.put( 1, entityList.get( 0 ) ); + String name = entityList.get( 0 ).name; + resultMap = ImmutableMap.copyOf( tempMap ); + return resultMap; + } + + public Map> test2 (Map> entityMap) { + ImmutableMap> resultMap; + Map> tempMap = new HashMap<>(); + tempMap.put( 1, entityMap.get( 0 )); + String name = entityMap.get( 0 ).get( 0 ).name; + resultMap = ImmutableMap.copyOf( tempMap ); + return resultMap; + } + + */ + } diff --git a/dbms/src/main/java/org/polypheny/db/backup/BackupManager.java b/dbms/src/main/java/org/polypheny/db/backup/BackupManager.java index a3e430c158..2f6d08f74c 100644 --- a/dbms/src/main/java/org/polypheny/db/backup/BackupManager.java +++ b/dbms/src/main/java/org/polypheny/db/backup/BackupManager.java @@ -25,6 +25,7 @@ import org.polypheny.db.backup.datagatherer.GatherEntries; import org.polypheny.db.backup.datagatherer.GatherSchema; import org.polypheny.db.backup.datainserter.InsertSchema; +import org.polypheny.db.catalog.entity.logical.LogicalEntity; import org.polypheny.db.catalog.entity.logical.LogicalForeignKey; import org.polypheny.db.catalog.entity.logical.LogicalNamespace; import org.polypheny.db.catalog.entity.logical.LogicalTable; @@ -167,8 +168,20 @@ private void wrapEntities() { backupInformationObject.setWrappedNamespaces( wrappedNamespaces ); // wrap all tables with BackupEntityWrapper - ImmutableMap>> wrappedTables = backupInformationObject.tempWrapLogicalTables( backupInformationObject.getTables(), tableDependencies, namespaceTableDependendencies, true); + ImmutableMap>> wrappedTables = backupInformationObject.wrapLogicalEntities( backupInformationObject.getTables(), tableDependencies, namespaceTableDependendencies, true); backupInformationObject.setWrappedTables( wrappedTables ); + + /* + ArrayList lol = new ArrayList<>(); + lol.add( (LogicalTable) backupInformationObject.getTables().get( 0 )); + + Map> lol2 = backupInformationObject.getTables(); + Map> lol3 = backupInformationObject.test2(lol2); + + + //ImmutableMap ha = backupInformationObject.test( lol ); + + */ } diff --git a/dbms/src/main/java/org/polypheny/db/backup/datagatherer/GatherSchema.java b/dbms/src/main/java/org/polypheny/db/backup/datagatherer/GatherSchema.java index 47f1fe82c1..c9e0887860 100644 --- a/dbms/src/main/java/org/polypheny/db/backup/datagatherer/GatherSchema.java +++ b/dbms/src/main/java/org/polypheny/db/backup/datagatherer/GatherSchema.java @@ -51,7 +51,7 @@ public class GatherSchema { List docNamespaces; //namespace id, list of tables for the namespace - ImmutableMap> tables; + ImmutableMap> tables; //TODO(FF): make views and materialized views not (? deleted?)... views and materialized views can be over several tables -> over several namespaces?? ImmutableMap> views; ImmutableMap> materializedViews; @@ -138,6 +138,7 @@ private void getRelSchema() { // get tables from namespace List tablesFromNamespace = snapshot.rel().getTablesFromNamespace( namespaceId ); + //List tablesFromNamespace = snapshot.rel().getTables( namespaceId, null ).stream().map( v -> v.unwrap( LogicalEntity.class ) ).collect(Collectors.toList( )); tables.put( namespaceId, tablesFromNamespace ); // get other schema information for each table @@ -196,7 +197,9 @@ private void getRelSchema() { } //safes the gathered information in the class variables - this.tables = ImmutableMap.copyOf( tables ); + this.tables = ImmutableMap.copyOf( tables.entrySet().stream().collect(Collectors.toMap(v -> v.getKey(), v -> v.getValue().stream().map( e -> e.unwrap( LogicalEntity.class ) ).collect(Collectors.toList() ) ))); + //this.tables = ImmutableMap.copyOf( (Map>) tables ); + //this.tables = ImmutableMap.copyOf( (Map>) tables ); this.backupInformationObject.setTables( this.tables ); this.views = ImmutableMap.copyOf( views ); this.backupInformationObject.setViews( this.views ); diff --git a/dbms/src/main/java/org/polypheny/db/backup/datainserter/InsertSchema.java b/dbms/src/main/java/org/polypheny/db/backup/datainserter/InsertSchema.java index 1cc51526fc..3c9230fbe2 100644 --- a/dbms/src/main/java/org/polypheny/db/backup/datainserter/InsertSchema.java +++ b/dbms/src/main/java/org/polypheny/db/backup/datainserter/InsertSchema.java @@ -17,6 +17,7 @@ package org.polypheny.db.backup.datainserter; import com.google.common.collect.ImmutableMap; +import com.google.gson.JsonObject; import java.util.ArrayList; import java.util.HashMap; import java.util.stream.Collectors; @@ -100,6 +101,7 @@ public void start( BackupInformationObject backupInformationObject ) { Map>> tempMap = new HashMap<>(); //luege öbs goht eso (a de räschtleche ort, ond söcht met instance of caste) + /* for ( Map.Entry> a : backupInformationObject.getTables().entrySet()) { List> bupEntityList = new ArrayList<>(); //TODO(FF): doesn't work with return value :( @@ -108,11 +110,19 @@ public void start( BackupInformationObject backupInformationObject ) { } //bupInformationObject.setBupTables( ImmutableMap.copyOf( tempMap ) ); + */ + // create table //bupInformationObject.transformLogicalEntitiesToBupSuperEntity( bupInformationObject.getTables() ); //tables = backupInformationObject.tempWrapLogicalTables( backupInformationObject.getTables(), true ); //backupInformationObject.setWrappedTables( tables ); + + + //LogicalTable lol = backupInformationObject.getWrappedTables().get( 0 ).get( 0 ).getEntityObject().unwrap( LogicalTable.class ); + //use LogicalEntity.unwrap(LogicalTable.class) for each of the LogicalEntities in wrappedTables + //ImmutableMap>> tableTables = backupInformationObject.getWrappedTables() + insertCreateTable( backupInformationObject.getWrappedTables() ); // alter table - add unique constraint @@ -187,20 +197,20 @@ private void insertCreateNamespace( ImmutableMap>> tables ) { + private void insertCreateTable( ImmutableMap>> tables ) { String query = new String(); // key: namespace id, value: list of tables for the namespace - for ( Map.Entry>> tablesPerNs : tables.entrySet() ) { + for ( Map.Entry>> tablesPerNs : tables.entrySet() ) { Long nsID = tablesPerNs.getKey(); //String namespaceName = bupInformationObject.getBupRelNamespaces().get( nsID ).getNameForQuery(); //only get rel namespaces from all bup namespaces String namespaceName = backupInformationObject.getWrappedNamespaces().get( nsID ).getNameForQuery(); - List> tablesList = tablesPerNs.getValue(); + List> tablesList = tablesPerNs.getValue(); // go through each table in the list (of tables for one namespace) - for ( BackupEntityWrapper table : tablesList ) { + for ( BackupEntityWrapper table : tablesList ) { // only create tables that don't (exist by default in polypheny) if ( !(table.getEntityObject().entityType.equals( EntityType.SOURCE )) ) { @@ -212,23 +222,23 @@ private void insertCreateTable( ImmutableMap>> tables, ImmutableMap> constraints ) { + private void insertAlterTableUQ( ImmutableMap>> tables, ImmutableMap> constraints ) { String query = new String(); - for ( Map.Entry>> tablesPerNs : tables.entrySet() ) { + for ( Map.Entry>> tablesPerNs : tables.entrySet() ) { Long nsID = tablesPerNs.getKey(); String namespaceName = backupInformationObject.getWrappedNamespaces().get( nsID ).getNameForQuery(); - List> tablesList = tablesPerNs.getValue(); + List> tablesList = tablesPerNs.getValue(); // go through each constraint in the list (of tables for one namespace) - for ( BackupEntityWrapper table : tablesList ) { + for ( BackupEntityWrapper table : tablesList ) { //TODO(FF - cosmetic): exclude source tables (for speed) // compare the table id with the constraint keys, and if they are the same, create the constraint - if ( constraints.containsKey( table.getEntityObject().getId() ) ) { - List constraintsList = constraints.get( table.getEntityObject().getId() ); - List logicalColumns = backupInformationObject.getColumns().get( table.getEntityObject().getId() ); + if ( constraints.containsKey( table.getEntityObject().unwrap( LogicalTable.class ).getId() ) ) { + List constraintsList = constraints.get( table.getEntityObject().unwrap( LogicalTable.class ).getId() ); + List logicalColumns = backupInformationObject.getColumns().get( table.getEntityObject().unwrap( LogicalTable.class ).getId() ); // go through all constraints per table for ( LogicalConstraint constraint : constraintsList ) { @@ -251,18 +261,18 @@ private void insertAlterTableUQ( ImmutableMap>> bupTables, ImmutableMap> foreignKeysPerTable ) { + private void insertAlterTableFK( ImmutableMap>> bupTables, ImmutableMap> foreignKeysPerTable ) { String query = new String(); // go through foreign key constraints and collect the necessary data for ( Map.Entry> fkListPerTable : foreignKeysPerTable.entrySet() ) { for ( LogicalForeignKey foreignKey : fkListPerTable.getValue() ) { String namespaceName = backupInformationObject.getWrappedNamespaces().get( foreignKey.namespaceId ).getNameForQuery(); - String tableName = backupInformationObject.getWrappedTables().get( foreignKey.namespaceId ).stream().filter( e -> e.getEntityObject().getId() == foreignKey.tableId ).findFirst().get().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().getId() == foreignKey.referencedKeyTableId ).findFirst().get().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(); @@ -327,13 +337,13 @@ private String getListOfCol( List colIDs, List logicalColum } - private String createTableQuery( BackupEntityWrapper table, String namespaceName ) { + private String createTableQuery( BackupEntityWrapper table, String namespaceName ) { String query = new String(); String columnDefinitions = new String(); String pkConstraint = new String(); ImmutableMap> columns = backupInformationObject.getColumns(); ImmutableMap> primaryKeys = backupInformationObject.getPrimaryKeysPerTable(); - LogicalTable logicalTable = table.getEntityObject(); + LogicalTable logicalTable = table.getEntityObject().unwrap( LogicalTable.class ); Long tableID = logicalTable.getId(); List colsPerTable = columns.get( tableID ); List pksPerTable = primaryKeys.get( tableID ); @@ -373,27 +383,31 @@ private String createColumnDefinition( LogicalColumn col ) { String colDataType = col.getType().toString(); String colNullable = col.nullableBoolToString(); + //TODO(FF): dröber switche för alli datatypes + String defaultValue = new String(); if ( !(col.defaultValue == null) ) { - PolyValue lool = col.defaultValue.value; - PolyType lol = col.defaultValue.value.type; //decimal - String type = lol.getTypeName(); - //PolyString lal = col.defaultValue.value.asString(); // works with varchar - //@NotNull PolyUserDefinedValue lkjlkj = col.defaultValue.value.asUserDefinedValue(); - //String value = lool.asUserDefinedValue().toString(); - - //String hahaaaa = lool.asString().asCharset( "UTF-8" ); //signsign hallo - //org.polypheny.db.catalog.exceptions.GenericRuntimeException: Cannot parse PolyBigDecimal(value=2) to type PolyString - String hahaa = lool.asString().toTypedString( true ); //'hallo' - String hahaaa = lool.asString().toTypedString( false ); //hallo - - String haha = lool.asString().toString(); + + String value = col.defaultValue.value.toTypedJson( ); //'hallo', {"@class":"org.polypheny.db.type.entity.PolyBigDecimal","value":2} + //for string it is this: {"@class":"org.polypheny.db.type.entity.PolyString","value":"hallo","charset":"UTF-16"}, figure out regex to only get value + //from the string value (in json format), get only the value with regex from string {"@class":"org.polypheny.db.type.entity.PolyString","value":"hallo","charset":"UTF-16"} + String regexString = value.replaceAll( ".*\"value\":", "" ); //is now value":"hallo","charset":"UTF-16"}, dont want ,"charset":"UTF-16"} part + regexString = regexString.replaceAll( ",.*", "" ); //correct for varchar, for int it is still 2}, dont want last } + regexString = regexString.replaceAll( "}", "" ); //TODO(FF): what to do if there is a "}" in the value? + regexString = regexString.replaceAll( "\"", "" ); + //TODO(FF): gives an error (with " there is no error?) but works: Caused by: org.polypheny.db.languages.sql.parser.impl.ParseException: Encountered " "DEFAULT" "DEFAULT "" at line 1, column 172. + + + + PolyValue reverse = PolyValue.fromTypedJson( value, PolyValue.class ); + if (PolyType.CHAR_TYPES.contains( col.defaultValue.type ) ) { - //defaultValue = String.format( " DEFAULT '%s'", col.defaultValue.value.type.toString() ); + defaultValue = String.format( " DEFAULT '%s'", regexString ); } else { - //defaultValue = String.format( " DEFAULT %s", col.defaultValue.value.type.toString() ); + defaultValue = String.format( " DEFAULT %s", regexString ); } //defaultValue = String.format( " DEFAULT %s", col.defaultValue.value ); + log.info( regexString ); } String caseSensitivity = new String(); @@ -448,6 +462,8 @@ private void executeStatementInPolypheny( String query, String queryLanguageType Statement statement = null; PolyImplementation result; + //TODO: use anyquery, rest not necessary + try { // get a transaction and a statement transaction = transactionManager.startTransaction( Catalog.defaultUserId, false, "Backup Inserter" ); diff --git a/dbms/src/test/java/org/polypheny/db/backup/GeneralTest.java b/dbms/src/test/java/org/polypheny/db/backup/GeneralTest.java new file mode 100644 index 0000000000..c68964bbad --- /dev/null +++ b/dbms/src/test/java/org/polypheny/db/backup/GeneralTest.java @@ -0,0 +1,132 @@ +/* + * Copyright 2019-2023 The Polypheny Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.polypheny.db.backup; + +import com.google.common.collect.ImmutableList; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.sql.Statement; +import lombok.extern.slf4j.Slf4j; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.polypheny.db.TestHelper; +import org.polypheny.db.TestHelper.JdbcConnection; + + +@SuppressWarnings({ "SqlDialectInspection", "SqlNoDataSourceInspection" }) +@Slf4j +public class GeneralTest { + + @BeforeClass + public static void start() { + // Ensures that Polypheny-DB is running + //noinspection ResultOfMethodCallIgnored + TestHelper.getInstance(); + //deleteOldData(); + addTestData(); + } + + + @AfterClass + public static void stop() { + deleteOldData(); + } + + + private static void addTestData() { + try ( JdbcConnection jdbcConnection = new JdbcConnection( false ) ) { + Connection connection = jdbcConnection.getConnection(); + try ( Statement statement = connection.createStatement() ) { + statement.executeUpdate( "CREATE NAMESPACE schema1" ); + statement.executeUpdate( "CREATE TABLE schema1.table1( id INTEGER NOT NULL, PRIMARY KEY(id))" ); + statement.executeUpdate( "ALTER TABLE schema1.table1 ADD COLUMN name VARCHAR (255) NULL" ); + statement.executeUpdate( "ALTER TABLE schema1.table1 ADD UNIQUE INDEX index1 ON id ON STORE hsqldb" ); + statement.executeUpdate( "CREATE TABLE schema1.table2( id INTEGER NOT NULL, PRIMARY KEY(id) )" ); + statement.executeUpdate( "ALTER TABLE schema1.table2 ADD CONSTRAINT fk_id FOREIGN KEY (id) REFERENCES schema1.table1(id) ON UPDATE RESTRICT ON DELETE RESTRICT" ); + statement.executeUpdate( "CREATE DOCUMENT SCHEMA private" ); + connection.commit(); + } + } catch ( SQLException e ) { + log.error( "Exception while adding test data", e ); + } + } + + + private static void deleteOldData() { + try ( JdbcConnection jdbcConnection = new JdbcConnection( false ) ) { + Connection connection = jdbcConnection.getConnection(); + try ( Statement statement = connection.createStatement() ) { + try { + statement.executeUpdate( "ALTER TABLE schema1.table2 DROP FOREIGN KEY fk_id" ); + statement.executeUpdate( "ALTER TABLE schema1.table1 DROP INDEX index1" ); + } catch ( SQLException e ) { + log.error( "Exception while deleting old data", e ); + } + try { + statement.executeUpdate( "DROP TABLE schema1.table1" ); + } catch ( SQLException e ) { + log.error( "Exception while deleting old data", e ); + } + try { + statement.executeUpdate( "DROP TABLE schema1.table2" ); + } catch ( SQLException e ) { + log.error( "Exception while deleting old data", e ); + } + statement.executeUpdate( "DROP SCHEMA schema1" ); + statement.executeUpdate( "DROP SCHEMA private" ); + connection.commit(); + } + } catch ( SQLException e ) { + log.error( "Exception while deleting old data", e ); + } + } + + + @Test + public void testGetCatalogs() { + try ( JdbcConnection polyphenyDbConnection = new JdbcConnection( false ) ) { + Connection connection = polyphenyDbConnection.getConnection(); + ResultSet resultSet = connection.getMetaData().getCatalogs(); + ResultSetMetaData rsmd = resultSet.getMetaData(); + + // Check number of columns + int totalColumns = rsmd.getColumnCount(); + Assert.assertEquals( "Wrong number of columns", 3, totalColumns ); + + // Check column names + Assert.assertEquals( "Wrong column name", "TABLE_CAT", rsmd.getColumnName( 1 ) ); + Assert.assertEquals( "Wrong column name", "OWNER", rsmd.getColumnName( 2 ) ); + Assert.assertEquals( "Wrong column name", "DEFAULT_SCHEMA", rsmd.getColumnName( 3 ) ); + + // Check data + final Object[] databaseApp = new Object[]{ "APP", "system", "public" }; + + TestHelper.checkResultSet( + connection.getMetaData().getCatalogs(), + ImmutableList.of( databaseApp ) ); + + } catch ( SQLException e ) { + log.error( "Exception while testing getCatalogs()", e ); + } + } + + +} diff --git a/webui/src/main/java/org/polypheny/db/webui/Crud.java b/webui/src/main/java/org/polypheny/db/webui/Crud.java index 63034b7325..ab18e13b0e 100644 --- a/webui/src/main/java/org/polypheny/db/webui/Crud.java +++ b/webui/src/main/java/org/polypheny/db/webui/Crud.java @@ -2812,7 +2812,8 @@ private void createGraph( Namespace namespace, Context ctx ) { new QueryRequest( "CREATE DATABASE " + namespace.getName() + " ON STORE " + namespace.getStore(), false, - true, "cypher", + true, + "cypher", namespace.getName() ), transactionManager, Catalog.defaultUserId,