Skip to content

Commit

Permalink
Understand new (eclipselink-)orm xsd
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Jungmann <[email protected]>
  • Loading branch information
lukasj committed Apr 11, 2024
1 parent 3d1e6b4 commit 2dd631c
Show file tree
Hide file tree
Showing 38 changed files with 1,452 additions and 321 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
/**
* Options for how Entity instances should be shared within an EclipseLink Persistence Unit / ServerSession
* @see org.eclipse.persistence.descriptors.ClassDescriptor#setCacheIsolation(CacheIsolationType)
* @see org.eclipse.persistence.annotations.Cache
* @see Cache
* @author Gordon Yorke
* @since EclipseLink 2.2
*/

public enum CacheIsolationType {
// These enums are ordered with ascending protective requirements
// This is intentional and any additions/alterations should take that into account
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ public class DatabaseField implements Cloneable, Serializable, CoreField {
protected int scale;
protected int length;
protected int precision;
protected int secondPrecision;
protected boolean isUnique;
protected boolean isNullable;
protected boolean isUpdatable;
protected boolean isInsertable;
protected boolean isCreatable;
protected boolean isPrimaryKey;
protected String columnDefinition;
protected String comment;
protected String optionalSuffix;

/** Column name of the field. */
protected String name;
Expand Down Expand Up @@ -131,7 +134,7 @@ public DatabaseField(String qualifiedName, String startDelimiter, String endDeli
setName(qualifiedName, startDelimiter, endDelimiter);
this.table = new DatabaseTable();
} else {
setName(qualifiedName.substring(index + 1, qualifiedName.length()), startDelimiter, endDelimiter);
setName(qualifiedName.substring(index + 1), startDelimiter, endDelimiter);
this.table = new DatabaseTable(qualifiedName.substring(0, index), startDelimiter, endDelimiter);
}
initDDLFields();
Expand Down Expand Up @@ -164,13 +167,16 @@ public void initDDLFields() {
scale = 0;
length = 0;
precision = 0;
secondPrecision = -1;
isUnique = false;
isNullable = true;
isUpdatable = true;
isInsertable = true;
isCreatable = true;
isPrimaryKey = false;
columnDefinition = "";
comment = "";
optionalSuffix = "";
}

/**
Expand Down Expand Up @@ -274,6 +280,13 @@ public String getColumnDefinition() {
return this.columnDefinition;
}

/**
* Get the SQL fragment that is used when generating the DDL for the column.
*/
public String getComment() {
return this.comment;
}

/**
* Return the expected index that this field will occur in the result set
* row. This is used to optimize performance of database row field lookups.
Expand Down Expand Up @@ -308,6 +321,13 @@ public String getNameDelimited(DatasourcePlatform platform) {
return this.name;
}

/**
* Get the SQL fragment appended to the generated DDL.
*/
public String getOptionalSuffix() {
return this.optionalSuffix;
}

/**
* Returns the precision for a decimal column when generating DDL.
*/
Expand Down Expand Up @@ -337,13 +357,22 @@ public String getQualifiedNameDelimited(DatasourcePlatform platform) {
return getNameDelimited(platform);
}
}

/**
* Returns the scale for a decimal column when generating DDL.
*/
public int getScale() {
return this.scale;
}

/**
* Get the number of decimal digits to use for storing fractional
* seconds in a SQL time or timestamp column when generating DDL.
*/
public int getSecondPrecision() {
return this.secondPrecision;
}

public DatabaseTable getTable() {
return table;
}
Expand Down Expand Up @@ -490,6 +519,13 @@ public void setColumnDefinition(String columnDefinition) {
this.columnDefinition = columnDefinition;
}

/**
* Set the SQL fragment that is used when generating the DDL for the column.
*/
public void setComment(String comment) {
this.comment = comment;
}

/**
* Set the expected index that this field will occur in the result set row.
* This is used to optimize performance of database row field lookups.
Expand Down Expand Up @@ -567,13 +603,30 @@ public void setNullable(boolean isNullable) {
this.isNullable = isNullable;
}

/**
* Used to specify the SQL fragment appended to the generated DDL.
*/
public void setOptionalSuffix(String optionalSuffix) {
this.optionalSuffix = optionalSuffix;
}

/**
* Used to specify the precision for a decimal column when generating DDL.
*/
public void setPrecision(int precision) {
this.precision = precision;
}

/**
* Used to specify the number of decimal digits to use for storing fractional
* seconds in a SQL time or timestamp column when generating DDL.
* <p>
* Applies only to columns of time or timestamp type.
*/
public void setSecondPrecision(int secondPrecision) {
this.secondPrecision = secondPrecision;
}

/**
* Used to specify whether the column should be included in primary key
* on the database table.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.persistence.internal.databaseaccess.DatabasePlatform;
import org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform;
import org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter;
import org.eclipse.persistence.tools.schemaframework.CheckConstraint;
import org.eclipse.persistence.tools.schemaframework.ForeignKeyConstraint;
import org.eclipse.persistence.tools.schemaframework.IndexDefinition;

Expand Down Expand Up @@ -63,6 +64,8 @@ public class DatabaseTable implements CoreTable, Cloneable, Serializable {
*/
protected Map<String, List<List<String>>> uniqueConstraints;

protected Map<String, CheckConstraint> checkConstraints;

/**
* Store the set of indexes defined through meta-data for the table.
*/
Expand All @@ -72,6 +75,8 @@ public class DatabaseTable implements CoreTable, Cloneable, Serializable {

protected String creationSuffix;

protected String comment;

/**
* Initialize the newly allocated instance of this class.
* By default their is no qualifier.
Expand Down Expand Up @@ -99,6 +104,13 @@ public DatabaseTable(String tableName, String qualifier, boolean useDelimiters,
this.useDelimiters = useDelimiters;
}

public void addCheckConstraint(CheckConstraint checkConstraint) {
if (checkConstraints == null) {
checkConstraints = new HashMap<>();
}
checkConstraints.put(checkConstraint.getName(), checkConstraint);
}

public void addForeignKeyConstraint(ForeignKeyConstraint foreignKeyConstraint) {
if (foreignKeyConstraints == null) {
foreignKeyConstraints = new HashMap<>();
Expand Down Expand Up @@ -183,6 +195,14 @@ public boolean equals(DatabaseTable table) {
return false;
}

public CheckConstraint getCheckConstraint(String name) {
return checkConstraints.get(name);
}

public Map<String, CheckConstraint> getCheckConstraints() {
return checkConstraints;
}

/**
* returns the suffix applied to the CREATE table statement on this field for DDL generation.
*/
Expand Down Expand Up @@ -379,7 +399,7 @@ public void setPossiblyQualifiedName(String possiblyQualifiedName, String startD
setName(possiblyQualifiedName, startDelimiter, endDelimiter);
this.tableQualifier = "";
} else {
setName(possiblyQualifiedName.substring(index + 1, possiblyQualifiedName.length()), startDelimiter, endDelimiter);
setName(possiblyQualifiedName.substring(index + 1), startDelimiter, endDelimiter);
setTableQualifier(possiblyQualifiedName.substring(0, index), startDelimiter, endDelimiter);

if((startDelimiter != null) && possiblyQualifiedName.startsWith(startDelimiter) && (endDelimiter != null) && possiblyQualifiedName.endsWith(endDelimiter)) {
Expand Down Expand Up @@ -419,4 +439,13 @@ public void setUseDelimiters(boolean useDelimiters) {
public boolean shouldUseDelimiters() {
return useDelimiters;
}

public void setComment(String comment) {
this.comment = comment;
}

public String getComment() {
return comment;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public PrefixMapperNamespaceResolver(NamespacePrefixMapper mapper, org.eclipse.p
}
String defaultUri= null;
if(nestedResolver != null){
for(Object next:nestedResolver.getNamespaces()) {
Namespace ns = (Namespace)next;
for (Namespace ns: nestedResolver.getNamespaces()) {
String uri = ns.getNamespaceURI();
String existingPrefix = null;
if(contextualNamespaces != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public Object buildAggregateFromRow(AbstractRecord databaseRow, Object targetObj
}

// Build a new aggregate if the target object does not reference an existing aggregate.
// EL Bug 474956 - build a new aggregate if the the target object references an existing aggregate, and
// EL Bug 474956 - build a new aggregate if the the target object references an existing aggregate, and
// the passed cacheKey is null from the invalidation of the target object in the IdentityMap.
if (aggregate == null || (aggregate != null && cacheKey == null)) {
aggregate = descriptor.getObjectBuilder().buildNewInstance();
Expand Down Expand Up @@ -1926,7 +1926,7 @@ public void setAggregateToSourceFields(Map<String, DatabaseField> aggregateToSou

/**
* INTERNAL:
* Set the hashtable that stores a field in the source table
* Set the hashtable that stores a field in the source table
* to a field name in a nested aggregate descriptor.
*/
public void setNestedFieldTranslations(Map<String, Object[]> fieldTranslations) {
Expand Down Expand Up @@ -1978,7 +1978,10 @@ protected void translateField(DatabaseField sourceField, DatabaseField mappingFi
mappingField.setScale(sourceField.getScale());
mappingField.setLength(sourceField.getLength());
mappingField.setPrecision(sourceField.getPrecision());
mappingField.setSecondPrecision(sourceField.getSecondPrecision());
mappingField.setOptionalSuffix(sourceField.getOptionalSuffix());
mappingField.setColumnDefinition(sourceField.getColumnDefinition());
mappingField.setComment(sourceField.getComment());

// Check if the translated field specified a table qualifier.
if (sourceField.hasTableName()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.persistence.tools.schemaframework;

import org.eclipse.persistence.exceptions.ValidationException;
import org.eclipse.persistence.internal.sessions.AbstractSession;

import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

/**
* <b>Purpose</b>: Define a check constraint.
*/
public class CheckConstraint implements Serializable {
protected String name;
protected String constraint;
protected String options;

public CheckConstraint() {
this.name = "";
this.constraint = "";
}

public CheckConstraint(String name, String constraint) {
this();
this.name = name;
this.constraint = constraint;
}

/**
* INTERNAL:
* Append the database field definition string to the table creation statement.
*/
public void appendDBString(Writer writer, AbstractSession session) {
try {
writer.write("CONSTRAINT " + getName() + " CHECK (");
writer.write(getConstraint());
if (getOptions() != null && !getOptions().isEmpty()) {
writer.write(" ");
writer.write(getOptions());
writer.write(" ");
}
writer.write(")");
} catch (IOException ioException) {
throw ValidationException.fileError(ioException);
}
}

/**
* PUBLIC:
*/
public String getName() {
return name;
}

/**
* PUBLIC:
*/
public String getConstraint() {
return constraint;
}

public String getOptions() {
return options;
}

/**
* PUBLIC:
*/
public void setName(String name) {
this.name = name;
}

/**
* PUBLIC:
*/
public void setConstraint(String constraint) {
this.constraint = constraint;
}

public void setOptions(String options) {
this.options = options;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,10 @@ protected TableDefinition getTableDefFromDBTable(DatabaseTable databaseTable) {
tableDefinition.setCreationSuffix(databaseTable.getCreationSuffix());
}

if (databaseTable.getComment() !=null){
tableDefinition.setComment(databaseTable.getComment());
}

// Add the foreign key constraints that were set on the table.
if (databaseTable.hasForeignKeyConstraints()) {
tableDefinition.setUserDefinedForeignKeyConstraints(databaseTable.getForeignKeyConstraints());
Expand Down Expand Up @@ -842,6 +846,12 @@ protected FieldDefinition getFieldDefFromDBField(DatabaseField dbField) {

fieldDef.setShouldAllowNull(dbField.isNullable());
fieldDef.setUnique(dbField.isUnique());
if (dbField.getOptionalSuffix() != null) {
fieldDef.setAdditional(dbField.getOptionalSuffix());
}
}
if (dbField.getComment() != null) {
fieldDef.setComment(dbField.getComment());
}
this.fieldMap.put(dbField, fieldDef);
this.databaseFields.put(dbField, dbField);
Expand Down
Loading

0 comments on commit 2dd631c

Please sign in to comment.