Skip to content

Commit

Permalink
uses anyquery now to execute queries
Browse files Browse the repository at this point in the history
  • Loading branch information
flurfis committed Nov 29, 2023
1 parent 74a188a commit 5911939
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,33 @@ private void insertCreateNamespace( ImmutableMap<Long, BackupEntityWrapper<Logic

//TODO(FF): execute query in polypheny, alter owner, set case sensitivity (how?)
if ( !ns.getValue().getEntityObject().name.equals( "public" ) ) {
executeStatementInPolypheny( query, "sql", ns.getValue().getEntityObject().dataModel );

switch ( ns.getValue().getEntityObject().dataModel ) {
case RELATIONAL:
query = String.format( "CREATE %s NAMESPACE %s11", ns.getValue().getEntityObject().dataModel.toString(), ns.getValue().getEntityObject().name );
executeStatementInPolypheny( query, ns.getKey(), ns.getValue().getEntityObject().dataModel );
break;

case DOCUMENT:
query = String.format( "CREATE %s NAMESPACE %s11", ns.getValue().getEntityObject().dataModel.toString(), ns.getValue().getEntityObject().name );
executeStatementInPolypheny( query, ns.getKey(), DataModel.RELATIONAL );
break;

case GRAPH:
query = String.format( "CREATE DATABASE %s11", ns.getValue().getEntityObject().name );
executeStatementInPolypheny( query, ns.getKey(), ns.getValue().getEntityObject().dataModel );
break;
default:
throw new GenericRuntimeException( "During backup schema insertions not supported data model detected" + ns.getValue().getEntityObject().dataModel );
}



//executeStatementInPolypheny( "CREATE DATABASE product2 IF NOT EXISTS", Catalog.defaultNamespaceId, DataModel.GRAPH );
//executeStatementInPolypheny( query, ns.getKey(), ns.getValue().getEntityObject().dataModel );

//executeStatementInPolypheny( query, "sql", ns.getValue().getEntityObject().dataModel );

}
}
}
Expand Down Expand Up @@ -224,7 +250,7 @@ private void insertCreateTable( ImmutableMap<Long, List<BackupEntityWrapper<Logi
// only create tables that don't (exist by default in polypheny)
if ( !(table.getEntityObject().entityType.equals( EntityType.SOURCE )) ) {
query = createTableQuery( table, namespaceName );
executeStatementInPolypheny( query, "sql", DataModel.RELATIONAL );
executeStatementInPolypheny( query, nsID, DataModel.RELATIONAL );
}
}
}
Expand Down Expand Up @@ -263,7 +289,7 @@ private void insertAlterTableUQ( ImmutableMap<Long, List<BackupEntityWrapper<Log

query = String.format( "ALTER TABLE %s11.%s11 ADD CONSTRAINT %s UNIQUE (%s)", namespaceName, tableName, constraintName, listOfCols );
log.info( query );
executeStatementInPolypheny( query, "sql", DataModel.RELATIONAL );
executeStatementInPolypheny( query, nsID, DataModel.RELATIONAL );
}
}
}
Expand Down Expand Up @@ -298,7 +324,7 @@ private void insertAlterTableFK( ImmutableMap<Long, List<BackupEntityWrapper<Log

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, "sql", DataModel.RELATIONAL );
executeStatementInPolypheny( query, nsId, DataModel.RELATIONAL );
}
}
}
Expand All @@ -322,7 +348,7 @@ private void insertCreateCollection() {
*/
//TODO(FF): how to set namespace??? (trhourgh settings? see below for try, but would require dependency)

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


Expand Down Expand Up @@ -412,29 +438,38 @@ private String createColumnDefinition( LogicalColumn col ) {
String defaultValue = new String();
if ( !(col.defaultValue == null) ) {

/*
String value = col.defaultValue.value.toTypedJson( ); //'hallo', {"@class":"org.polypheny.db.type.entity.PolyBigDecimal","value":2}
String value2 = col.defaultValue.value.toJson( ); //'hallo', {"@class":"org.polypheny.db.type.entity.PolyBigDecimal","value":2}
String value2 = col.defaultValue.value.toJson( );
String value3 = value2.replaceAll( "\"", "''" );
value2 = value2.replaceAll( "'", "''" );
//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( "}", "" );
regexString = regexString.replaceAll( "\"", "" );
PolyValue reverse = PolyValue.fromTypedJson( value, PolyValue.class );
Boolean testing = PolyType.DATETIME_TYPES.contains( col.defaultValue.type );
*/

//todo: replace ' to '', wenn " in default denne esch
//replace ' to '', in case there is a " in the default value
String value = col.defaultValue.value.toJson( );
value = value.replaceAll( "'", "''" );

if (PolyType.CHAR_TYPES.contains( col.defaultValue.type ) || PolyType.DATETIME_TYPES.contains( col.defaultValue.type ) ) {
//to quotedJson
defaultValue = String.format( " DEFAULT '%s'", regexString );
String test = " DEFAULT '" + regexString + "'";
//defaultValue = String.format( " DEFAULT '%s'", regexString );
defaultValue = String.format( " DEFAULT '%s'", value );
//String test = " DEFAULT '" + regexString + "'";
String test = " DEFAULT '" + value + "'";
} else {
defaultValue = String.format( " DEFAULT %s", regexString );
defaultValue = String.format( " DEFAULT %s", value );
}
//defaultValue = String.format( " DEFAULT %s", col.defaultValue.value );
//log.info( "default for " + colDataType + ": " + defaultValue );
//log.info( "default for " + colDataType + ": " + value2 + " || " + value3);
//log.info( "default for " + colDataType + ": " + value);
}

String caseSensitivity = new String();
Expand Down Expand Up @@ -552,28 +587,76 @@ private String nullableBoolToString (boolean nullable) {
*/


private void executeStatementInPolypheny( String query, String queryLanguageType, DataModel dataModel ) {
private void executeStatementInPolypheny( String query, Long namespaceId, DataModel dataModel ) {
Transaction transaction;
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" );
statement = transaction.createStatement();
ExecutedContext executedQuery = LanguageManager.getINSTANCE().anyQuery( QueryContext.builder().language( QueryLanguage.from( "sql" ) ).query( query ).origin( "Backup Manager" ).transactionManager( transactionManager ).namespaceId( Catalog.defaultNamespaceId ).build(), statement ).get( 0 );
ResultIterator iter = executedQuery.getIterator();
while ( iter.hasMoreRows() ) {
// liste mit tuples
iter.getNextBatch();
}
switch ( dataModel ) {
case RELATIONAL:
try {
// get a transaction and a statement
transaction = transactionManager.startTransaction( Catalog.defaultUserId, false, "Backup Inserter" );
statement = transaction.createStatement();
ExecutedContext executedQuery = LanguageManager.getINSTANCE().anyQuery( QueryContext.builder().language( QueryLanguage.from( "sql" ) ).query( query ).origin( "Backup Manager" ).transactionManager( transactionManager ).namespaceId( namespaceId ).build(), statement ).get( 0 );

} catch ( Exception e ) {
throw new RuntimeException( "Error while starting transaction", e );
} catch ( Exception e ) {
throw new RuntimeException( "Error while starting transaction", e );
}
break;

case DOCUMENT:
try {
// get a transaction and a statement
transaction = transactionManager.startTransaction( Catalog.defaultUserId, false, "Backup Inserter" );
statement = transaction.createStatement();
ExecutedContext executedQuery = LanguageManager.getINSTANCE().anyQuery( QueryContext.builder().language( QueryLanguage.from( "mql" ) ).query( query ).origin( "Backup Manager" ).transactionManager( transactionManager ).namespaceId( namespaceId ).build(), statement ).get( 0 );

} catch ( Exception e ) {
throw new RuntimeException( "Error while starting transaction", e );
}
break;

case GRAPH:
try {
// get a transaction and a statement
transaction = transactionManager.startTransaction( Catalog.defaultUserId, false, "Backup Inserter" );
statement = transaction.createStatement();
ExecutedContext executedQuery = LanguageManager.getINSTANCE().anyQuery( QueryContext.builder().language( QueryLanguage.from( "cypher" ) ).query( query ).origin( "Backup Manager" ).transactionManager( transactionManager ).namespaceId( namespaceId ).build(), statement ).get( 0 );

} catch ( Exception e ) {
throw new RuntimeException( "Error while starting transaction", e );
}
break;

default:
throw new RuntimeException( "Backup - InsertSchema: DataModel not supported" );
}

//just to keep it
int i = 1;
if(i == 0) {
try {
// get a transaction and a statement
transaction = transactionManager.startTransaction( Catalog.defaultUserId, false, "Backup Inserter" );
statement = transaction.createStatement();
ExecutedContext executedQuery = LanguageManager.getINSTANCE().anyQuery( QueryContext.builder().language( QueryLanguage.from( "sql" ) ).query( query ).origin( "Backup Manager" ).transactionManager( transactionManager ).namespaceId( Catalog.defaultNamespaceId ).build(), statement ).get( 0 );
// in case of results
ResultIterator iter = executedQuery.getIterator();
while ( iter.hasMoreRows() ) {
// liste mit tuples
iter.getNextBatch();
}

} catch ( Exception e ) {
throw new RuntimeException( "Error while starting transaction", e );
}
}

/*
try {
// get algRoot
Processor queryProcessor = statement.getTransaction().getProcessor( QueryLanguage.from( queryLanguageType ) );
Expand All @@ -590,8 +673,7 @@ private void executeStatementInPolypheny( String query, String queryLanguageType
//result = queryProcessor.prepareDdl( statement, parsed, parameters );

/*
//from here on again comment
AlgRoot algRoot = queryProcessor.translate(
statement,
queryProcessor.validate( statement.getTransaction(), sqlNode, RuntimeConfig.ADD_DEFAULT_VALUES_IN_INSERTS.getBoolean()).left,
Expand All @@ -600,13 +682,14 @@ private void executeStatementInPolypheny( String query, String queryLanguageType
//get PolyResult from AlgRoot
final QueryProcessor processor = statement.getQueryProcessor();
result = processor.prepareQuery( algRoot, true );
*/
} catch ( Exception e ) {
log.info( e.getMessage() );
log.info( "exception while executing query: "+ query );
throw new RuntimeException( e );
}
*/

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
@SuppressWarnings({ "SqlDialectInspection", "SqlNoDataSourceInspection" })
@Slf4j
@Category({ AdapterTestSuite.class })
public class GeneralTest {
public class DependencyCircleTest {
static TestHelper testHelper;
BackupManager backupManager;

Expand All @@ -49,14 +49,15 @@ public static void start() {
testHelper = TestHelper.getInstance();
//deleteOldData();
//this.backupManager = new BackupManager( testHelper.getTransactionManager() );
addTestData();
//addTestData();
addDependenyTestData();

}


@AfterClass
public static void stop() {
deleteOldData();
deleteDependencyTestData();
}


Expand Down Expand Up @@ -122,6 +123,66 @@ public void testGatherData() {




private static void addDependenyTestData() {
try ( JdbcConnection jdbcConnection = new JdbcConnection( false ) ) {
Connection connection = jdbcConnection.getConnection();
try ( Statement statement = connection.createStatement() ) {
statement.executeUpdate( "CREATE NAMESPACE reli" );
statement.executeUpdate( "CREATE NAMESPACE temp" );
statement.executeUpdate( "CREATE NAMESPACE lol" );
statement.executeUpdate( "create table reli.t1 (t1pk integer not null, t1fk integer not null)" );
statement.executeUpdate( "create table reli.t2 (t2pk integer not null, t2fk integer not null)" );
statement.executeUpdate( "create table reli.t3 (t2pk integer not null)" );
statement.executeUpdate( "create table temp.t4 (t4pk integer not null,t4fk integer not null)" );
statement.executeUpdate( "create table temp.t5 (t5pk integer not null,t5fk integer not null)" );
statement.executeUpdate( "create table temp.t6 (t6pk integer not null, t6fk integer not null)" );
statement.executeUpdate( "alter table reli.t1 add constraint test foreign key (t1fk) references temp.t6 (t6pk) ON UPDATE RESTRICT ON DELETE RESTRICT" );
statement.executeUpdate( "alter table reli.t2 add constraint test foreign key (t2fk) references reli.t1 (t1pk) ON UPDATE RESTRICT ON DELETE RESTRICT" );
statement.executeUpdate( "alter table temp.t4 add constraint test foreign key (t4fk) references reli.t1 (t1pk) ON UPDATE RESTRICT ON DELETE RESTRICT" );
statement.executeUpdate( "alter table temp.t5 add constraint test foreign key (t5fk) references temp.t4 (t4pk) ON UPDATE RESTRICT ON DELETE RESTRICT" );
statement.executeUpdate( "alter table temp.t6 add constraint test foreign key (t6fk) references temp.t5 (t5pk) ON UPDATE RESTRICT ON DELETE RESTRICT" );
connection.commit();
}
} catch ( SQLException e ) {
log.error( "Exception while adding test data", e );
}
}

private static void deleteDependencyTestData() {
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 reli" );
statement.executeUpdate( "DROP SCHEMA temp" );
statement.executeUpdate( "DROP SCHEMA lol" );
connection.commit();
}
} catch ( SQLException e ) {
log.error( "Exception while deleting old data", e );
}
}


@Test
public void testGetCatalogs() {
try ( JdbcConnection polyphenyDbConnection = new JdbcConnection( false ) ) {
Expand Down

0 comments on commit 5911939

Please sign in to comment.