Skip to content

Commit

Permalink
jakartaee/persistence#431 - add SchemaManager
Browse files Browse the repository at this point in the history
Initial implementation.

Signed-off-by: Tomáš Kraus <[email protected]>
  • Loading branch information
Tomas-Kraus committed Dec 5, 2023
1 parent 05a15a9 commit 47b3686
Show file tree
Hide file tree
Showing 26 changed files with 1,736 additions and 308 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3805,6 +3805,33 @@ public void writeAddColumnClause(Writer writer, AbstractSession session, TableDe
field.appendDBString(writer, session, table);
}

/**
* INTERNAL:
* May need to override this method if the platform supports ALTER TABLE DROP COLUMN &lt;column&gt;
* and the generated sql doesn't work.
* Write the string that follows ALTER TABLE to create a sql statement for
* the platform in order to drop existing column from an existing table.
*/
public void writeDropColumnClause(Writer writer, AbstractSession session, TableDefinition table, String fieldName) throws IOException {
writer.write("DROP COLUMN ");
writer.write(fieldName);
}

/**
* INTERNAL:
* May need to override this method if the platform supports TRUNCATE TABLE &lt;table&gt;
* and the generated sql doesn't work.
* Write the string that creates TRUNCATE TABLE sql statement for the platform in order
* to truncate an existing table.
*/
public void writeTruncateTable(Writer writer, AbstractSession session, TableDefinition table) throws IOException {
String tableName = table.getTable() == null
? table.getName()
: table.getTable().getName();
writer.write("TRUNCATE TABLE ");
writer.write(tableName);
}

/**
* INTERNAL:
* Override this method if the platform supports storing JDBC connection user name during
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,11 @@ public class ExceptionLocalizationResource extends ListResourceBundle {
{ "json_pgsql_pgobject_conversion", "Database PGobject conversion failed."},
{ "json_pgsql_unknown_type", "Unknown JSON type returned from database."},
{ "json_ora21c_jsonvalue_to_oraclevalue", "Could not convert JsonValue to OracleJsonValue."},
{ "json_ora21c_resultset_to_jsonvalue", "Could not convert JDBC ResultSet type to JsonValue."}
{ "json_ora21c_resultset_to_jsonvalue", "Could not convert JDBC ResultSet type to JsonValue."},
{ "schema_validation_failed", "Schema validation failed"},
{ "schema_validation_missing_table", "The {0} table vas not found in the schema"},
{ "schema_validation_table_surplus_columns", "The {0} table has surplus columns in the schema"},
{ "schema_validation_table_missing_columns", "The {0} table has missing columns in the schema"}
};
/**
* Return the lookup table.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,41 @@ public FieldDefinition(String name, String typeName) {
this.name = name;
this.typeName = typeName;
}

/**
* INTERNAL:
* Append the database field definition string to the table creation statement.
*
* @param writer Target writer where to write field definition string.
* @param session Current session context.
* @param table Database table being processed.
* @throws ValidationException When invalid or inconsistent data were found.
*/
public void appendDBString(final Writer writer, final AbstractSession session,
final TableDefinition table) throws ValidationException {
final TableDefinition table) {
appendDBString(writer, session, table, null);
}

/**
* INTERNAL:
* Append the database field definition string to the table creation/modification statement.
*
* @param writer Target writer where to write field definition string.
* @param session Current session context.
* @param table Database table being processed.
* @param alterSeparator Field definition is part of ALTER/MODIFY COUMN statement when not {@code null}
* and {@code alterSeparator} is appended after column name
* @throws ValidationException When invalid or inconsistent data were found.
*/
public void appendDBString(final Writer writer, final AbstractSession session,
final TableDefinition table, String alterSeparator) throws ValidationException {
try {
writer.write(name);
writer.write(" ");
writer.write(name);
writer.write(" ");

if (alterSeparator != null) {
writer.write(alterSeparator);
writer.write(" ");
}

if (getTypeDefinition() != null) { //apply user-defined complete type definition
writer.write(typeDefinition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.util.function.Consumer;

/**
* <p>
Expand Down Expand Up @@ -1126,6 +1128,25 @@ public void replaceDefaultTables(boolean createSequenceTables, boolean createSeq
}
}

public void truncateDefaultTables(boolean generateFKConstraints) {
boolean shouldLogExceptionStackTrace = getSession().getSessionLog().shouldLogExceptionStackTrace();
session.getSessionLog().setShouldLogExceptionStackTrace(false);

try {
TableCreator tableCreator = getDefaultTableCreator(generateFKConstraints);
tableCreator.truncateTables(session, this, generateFKConstraints);
} catch (DatabaseException exception) {
// Ignore error
} finally {
session.getSessionLog().setShouldLogExceptionStackTrace(shouldLogExceptionStackTrace);
}
}

public boolean validateDefaultTables(Consumer<List<TableValidationException>> onFailed, boolean generateFKConstraints) {
TableCreator tableCreator = getDefaultTableCreator(generateFKConstraints);
return tableCreator.validateTables(session, this, onFailed);
}

public void setSession(DatabaseSessionImpl session) {
this.session = session;
}
Expand Down
Loading

0 comments on commit 47b3686

Please sign in to comment.