-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue 2751 make sure drop schema drops everything (#2932)
* issue 2751 make sure drop schema drops everything Signed-off-by: Robin Arnold <[email protected]> * issue 2751 fixed drop checks for Postgres, Db2 and Derby Signed-off-by: Robin Arnold <[email protected]> * issue 2751 idempotent schema drop for Db2, PostgreSQL and Derby Signed-off-by: Robin Arnold <[email protected]> * issue 2751 updates per review comments Signed-off-by: Robin Arnold <[email protected]> * copyright update but really just to force a rebuild... Signed-off-by: Lee Surprenant <[email protected]> Co-authored-by: Lee Surprenant <[email protected]>
- Loading branch information
1 parent
7aaac5a
commit b19bda2
Showing
25 changed files
with
1,136 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
fhir-database-utils/src/main/java/com/ibm/fhir/database/utils/common/SchemaInfoObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* (C) Copyright IBM Corp. 2021 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.ibm.fhir.database.utils.common; | ||
|
||
|
||
/** | ||
* A database object (table, index, view etc) existing within a schema | ||
*/ | ||
public class SchemaInfoObject { | ||
public enum Type { | ||
FUNCTION, | ||
INDEX, | ||
PROCEDURE, | ||
SEQUENCE, | ||
TABLE, | ||
VIEW | ||
} | ||
|
||
// The object type | ||
private final Type type; | ||
|
||
// The object name | ||
private final String name; | ||
|
||
public SchemaInfoObject(Type type, String name) { | ||
this.type = type; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return type.name() + ":" + name; | ||
} | ||
/** | ||
* @return the type | ||
*/ | ||
public Type getType() { | ||
return type; | ||
} | ||
|
||
/** | ||
* @return the name | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...tabase-utils/src/main/java/com/ibm/fhir/database/utils/db2/Db2ListSequencesForSchema.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* (C) Copyright IBM Corp. 2021 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.ibm.fhir.database.utils.db2; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.ibm.fhir.database.utils.api.IDatabaseSupplier; | ||
import com.ibm.fhir.database.utils.api.IDatabaseTranslator; | ||
import com.ibm.fhir.database.utils.common.DataDefinitionUtil; | ||
import com.ibm.fhir.database.utils.common.SchemaInfoObject; | ||
import com.ibm.fhir.database.utils.common.SchemaInfoObject.Type; | ||
|
||
/** | ||
* DAO to fetch the names of sequences in the given schema | ||
*/ | ||
public class Db2ListSequencesForSchema implements IDatabaseSupplier<List<SchemaInfoObject>> { | ||
|
||
// The schema of the table | ||
private final String schemaName; | ||
|
||
/** | ||
* Public constructor | ||
* @param schemaName | ||
*/ | ||
public Db2ListSequencesForSchema(String schemaName) { | ||
this.schemaName = DataDefinitionUtil.assertValidName(schemaName); | ||
} | ||
|
||
@Override | ||
public List<SchemaInfoObject> run(IDatabaseTranslator translator, Connection c) { | ||
List<SchemaInfoObject> result = new ArrayList<>(); | ||
// Grab the list of sequences for the configured schema from the DB2 catalog | ||
final String sql = "SELECT seqname FROM SYSCAT.SEQUENCES WHERE seqschema = ?"; | ||
|
||
try (PreparedStatement ps = c.prepareStatement(sql)) { | ||
ps.setString(1, schemaName); | ||
ResultSet rs = ps.executeQuery(); | ||
while (rs.next()) { | ||
result.add(new SchemaInfoObject(Type.SEQUENCE, rs.getString(1))); | ||
} | ||
} | ||
catch (SQLException x) { | ||
throw translator.translate(x); | ||
} | ||
|
||
return result; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...-database-utils/src/main/java/com/ibm/fhir/database/utils/db2/Db2ListTablesForSchema.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* (C) Copyright IBM Corp. 2021 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.ibm.fhir.database.utils.db2; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.ibm.fhir.database.utils.api.IDatabaseSupplier; | ||
import com.ibm.fhir.database.utils.api.IDatabaseTranslator; | ||
import com.ibm.fhir.database.utils.common.DataDefinitionUtil; | ||
import com.ibm.fhir.database.utils.common.SchemaInfoObject; | ||
import com.ibm.fhir.database.utils.common.SchemaInfoObject.Type; | ||
|
||
/** | ||
* DAO to fetch the names of tables in the given schema | ||
*/ | ||
public class Db2ListTablesForSchema implements IDatabaseSupplier<List<SchemaInfoObject>> { | ||
|
||
// The schema of the table | ||
private final String schemaName; | ||
|
||
/** | ||
* Public constructor | ||
* @param schemaName | ||
*/ | ||
public Db2ListTablesForSchema(String schemaName) { | ||
this.schemaName = DataDefinitionUtil.assertValidName(schemaName); | ||
} | ||
|
||
@Override | ||
public List<SchemaInfoObject> run(IDatabaseTranslator translator, Connection c) { | ||
List<SchemaInfoObject> result = new ArrayList<>(); | ||
// Grab the list of tables for the configured schema from the DB2 catalog | ||
final String sql = "SELECT tabname FROM SYSCAT.TABLES WHERE tabschema = ?"; | ||
|
||
try (PreparedStatement ps = c.prepareStatement(sql)) { | ||
ps.setString(1, schemaName); | ||
ResultSet rs = ps.executeQuery(); | ||
while (rs.next()) { | ||
result.add(new SchemaInfoObject(Type.TABLE, rs.getString(1))); | ||
} | ||
} | ||
catch (SQLException x) { | ||
throw translator.translate(x); | ||
} | ||
|
||
return result; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
fhir-database-utils/src/main/java/com/ibm/fhir/database/utils/db2/Db2ListViewsForSchema.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* (C) Copyright IBM Corp. 2021 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.ibm.fhir.database.utils.db2; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.ibm.fhir.database.utils.api.IDatabaseSupplier; | ||
import com.ibm.fhir.database.utils.api.IDatabaseTranslator; | ||
import com.ibm.fhir.database.utils.common.DataDefinitionUtil; | ||
import com.ibm.fhir.database.utils.common.SchemaInfoObject; | ||
import com.ibm.fhir.database.utils.common.SchemaInfoObject.Type; | ||
|
||
/** | ||
* DAO to fetch the names of views in the given schema | ||
*/ | ||
public class Db2ListViewsForSchema implements IDatabaseSupplier<List<SchemaInfoObject>> { | ||
|
||
// The schema of the table | ||
private final String schemaName; | ||
|
||
/** | ||
* Public constructor | ||
* @param schemaName | ||
*/ | ||
public Db2ListViewsForSchema(String schemaName) { | ||
this.schemaName = DataDefinitionUtil.assertValidName(schemaName); | ||
} | ||
|
||
@Override | ||
public List<SchemaInfoObject> run(IDatabaseTranslator translator, Connection c) { | ||
List<SchemaInfoObject> result = new ArrayList<>(); | ||
// Grab the list of views for the configured schema from the DB2 catalog | ||
final String sql = "SELECT viewname FROM SYSCAT.VIEWS WHERE viewschema = ?"; | ||
|
||
try (PreparedStatement ps = c.prepareStatement(sql)) { | ||
ps.setString(1, schemaName); | ||
ResultSet rs = ps.executeQuery(); | ||
while (rs.next()) { | ||
result.add(new SchemaInfoObject(Type.VIEW, rs.getString(1))); | ||
} | ||
} | ||
catch (SQLException x) { | ||
throw translator.translate(x); | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.