diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractConstraint.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractConstraint.java new file mode 100644 index 0000000000..74140a6ff3 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractConstraint.java @@ -0,0 +1,64 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +import java.util.List; +import java.util.ArrayList; + +/** + * Support for writing {@link Constraint} implementations + * + * @todo do we need to support defining these on particular schemas/catalogs? + * + * @author Steve Ebersole + */ +public abstract class AbstractConstraint implements Constraint { + private final TableSpecification table; + private final String name; + private List columns = new ArrayList(); + + protected AbstractConstraint(TableSpecification table, String name) { + this.table = table; + this.name = name; + } + + public TableSpecification getTable() { + return table; + } + + public String getName() { + return name; + } + + public List getColumns() { + return columns; + } + + public void addColumn(Column column) { + if ( column.getValueContainer() != getTable() ) { + throw new IllegalArgumentException( "Unable to add column to constraint; tables did not match" ); + } + columns.add( column ); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractSimpleValue.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractSimpleValue.java new file mode 100644 index 0000000000..f3405edfb0 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractSimpleValue.java @@ -0,0 +1,68 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Basic support for {@link SimpleValue} implementations. + * + * @author Steve Ebersole + */ +public abstract class AbstractSimpleValue implements SimpleValue { + private static final Logger log = LoggerFactory.getLogger( AbstractSimpleValue.class ); + + private final ValueContainer container; + private Datatype datatype; + + protected AbstractSimpleValue(ValueContainer container) { + this.container = container; + } + + /** + * {@inheritDoc} + */ + public ValueContainer getValueContainer() { + return container; + } + + /** + * {@inheritDoc} + */ + public Datatype getDatatype() { + return datatype; + } + + /** + * {@inheritDoc} + */ + public void setDatatype(Datatype datatype) { + log.debug( "setting datatype for column {} : {}", toLoggableString(), datatype ); + if ( this.datatype != null && ! this.datatype.equals( datatype ) ) { + log.debug( "overriding previous datatype : {}", this.datatype ); + } + this.datatype = datatype; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractTableSpecification.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractTableSpecification.java new file mode 100644 index 0000000000..08a13acfca --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractTableSpecification.java @@ -0,0 +1,41 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +/** + * Convenience base class for implementing the {@link ValueContainer} contract centralizing commonality + * between modelling tables views and inline views. + * + * @author Steve Ebersole + */ +public abstract class AbstractTableSpecification extends AbstractValueContainer implements TableSpecification { + private PrimaryKey primaryKey = new PrimaryKey( this ); + + /** + * {@inheritDoc} + */ + public PrimaryKey getPrimaryKey() { + return primaryKey; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractValueContainer.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractValueContainer.java new file mode 100644 index 0000000000..c679185518 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractValueContainer.java @@ -0,0 +1,61 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +import java.util.LinkedHashSet; + +/** + * Convenience base class for implementing the {@link ValueContainer} contract + * + * @author Steve Ebersole + */ +public abstract class AbstractValueContainer implements ValueContainer { + private final LinkedHashSet values = new LinkedHashSet(); + + @Override + public Iterable iterateValues() { + return values; + } + + @Override + public Column createColumn(String name) { + final Column column = new Column( this, name ); + values.add( column ); + return column; + } + + @Override + public DerivedValue createDerivedValue(String fragment) { + final DerivedValue value = new DerivedValue( this, fragment ); + values.add( value ); + return value; + } + + @Override + public Tuple createTuple(String name) { + final Tuple tuple = new Tuple( this, name ); + values.add( tuple ); + return tuple; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Column.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Column.java new file mode 100644 index 0000000000..e9f30ed78a --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Column.java @@ -0,0 +1,104 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +/** + * Models a physical column + * + * @author Gavin King + * @author Steve Ebersole + */ +public class Column extends AbstractSimpleValue implements SimpleValue { + private final String name; + private Size size; + + protected Column(ValueContainer table, String name) { + super( table ); + this.name = name; + } + + public String getName() { + return name; + } + + public Size getSize() { + return size; + } + + public void setSize(Size size) { + this.size = size; + } + + @Override + public String toLoggableString() { + return getValueContainer().getLoggableValueQualifier() + '.' + getName(); + } + + /** + * Models size restrictions/requirements on a column's datatype. + *

+ * IMPL NOTE: since we do not necessarily know the datatype up front, and therefore do not necessarily know + * whether length or precision/scale sizing is needed, we simply account for both here. Additionally LOB + * definitions, by standard, are allowed a "multiplier" consisting of 'K' (Kb), 'M' (Mb) or 'G' (Gb). + */ + public static class Size { + private static enum LobMultiplier { K, M, G } + + private final int precision; + private final int scale; + private final long length; + private final LobMultiplier lobMultiplier; + + /** + * Complete constructor. + * + * @param precision numeric precision + * @param scale numeric scale + * @param length type length + * @param lobMultiplier LOB length multiplier + */ + public Size(int precision, int scale, long length, LobMultiplier lobMultiplier) { + this.precision = precision; + this.scale = scale; + this.length = length; + this.lobMultiplier = lobMultiplier; + } + + public Size(int precision) { + this( precision, -1, -1, null ); + } + + public Size(int precision, int scale) { + this( precision, scale, -1, null ); + } + + public Size(long length) { + this( -1, -1, length, null ); + } + + public Size(long length, LobMultiplier lobMultiplier) { + this( -1, -1, length, lobMultiplier ); + } + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Constraint.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Constraint.java new file mode 100644 index 0000000000..3dec833664 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Constraint.java @@ -0,0 +1,58 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +import java.util.List; + +/** + * Basic contract for the types of constraints we fully support as metadata constructs:

    + *
  • primary key
  • + *
  • foreign key
  • + *
  • unique constraint
  • + *
+ * + * @author Steve Ebersole + */ +public interface Constraint { + /** + * Obtain the table to which this constraint applies. + * + * @return The constrained table. + */ + public TableSpecification getTable(); + + /** + * Obtain the constraint name. + * + * @return the name. + */ + public String getName(); + + /** + * Obtain the columns that are part of this constraint. + * + * @return The constrained columns. + */ + public List getColumns(); +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Datatype.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Datatype.java new file mode 100644 index 0000000000..35ef2d0a59 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Datatype.java @@ -0,0 +1,91 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +/** + * Models a JDBC {@link java.sql.Types DATATYPE} + * + * @todo Do we somehow link this in with {@link org.hibernate.internal.util.jdbc.TypeInfo} ? + * + * @author Steve Ebersole + */ +public class Datatype { + private final int typeCode; + private final String typeName; + private final Class javaType; + private final int hashCode; + + public Datatype(int typeCode, String typeName, Class javaType) { + this.typeCode = typeCode; + this.typeName = typeName; + this.javaType = javaType; + this.hashCode = generateHashCode(); + } + + private int generateHashCode() { + int result = typeCode; + result = 31 * result + typeName.hashCode(); + result = 31 * result + javaType.hashCode(); + return result; + } + + public int getTypeCode() { + return typeCode; + } + + public String getTypeName() { + return typeName; + } + + public Class getJavaType() { + return javaType; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + + Datatype datatype = (Datatype) o; + + return typeCode == datatype.typeCode + && javaType.equals( datatype.javaType ) + && typeName.equals( datatype.typeName ); + + } + + @Override + public int hashCode() { + return hashCode; + } + + @Override + public String toString() { + return super.toString() + "[code=" + typeCode + ", name=" + typeName + ", javaClass=" + javaType.getName() + "]"; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/DerivedValue.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/DerivedValue.java new file mode 100644 index 0000000000..33c1e65992 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/DerivedValue.java @@ -0,0 +1,45 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +/** + * Models a value expression. It is the result of a formula mapping. + * + * @author Steve Ebersole + */ +public class DerivedValue extends AbstractSimpleValue implements SimpleValue { + private final String expression; + + public DerivedValue(ValueContainer table, String expression) { + super( table ); + this.expression = expression; + } + + /** + * {@inheritDoc} + */ + public String toLoggableString() { + return getValueContainer().toLoggableString() + ".{derived-column}"; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Exportable.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Exportable.java new file mode 100644 index 0000000000..e6689ccb5a --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Exportable.java @@ -0,0 +1,38 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +/** + * Contract for entities (in the ERD sense) which can be exported via {@code CREATE}, {@code ALTER}, etc + * + * @author Steve Ebersole + */ +public interface Exportable { + /** + * Get a unique identifier to make sure we are not exporting the same database structure multiple times. + * + * @return The exporting identifier. + */ + public String getExportIdentifier(); +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ForeignKey.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ForeignKey.java new file mode 100644 index 0000000000..d8f9ac97ca --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ForeignKey.java @@ -0,0 +1,106 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Models the notion of a foreign key. + *

+ * Note that this need not mean a physical foreign key; we just mean a relationship between 2 table + * specifications. + * + * @author Gavin King + * @author Steve Ebersole + */ +public class ForeignKey extends AbstractConstraint implements Constraint, Exportable { + private static final Logger log = LoggerFactory.getLogger( ForeignKey.class ); + + private final TableSpecification targetTable; + private List targetColumns; + + public ForeignKey(TableSpecification sourceTable, TableSpecification targetTable, String name) { + super( sourceTable, name ); + this.targetTable = targetTable; + } + + public ForeignKey(TableSpecification sourceTable, TableSpecification targetTable) { + this( sourceTable, targetTable, null ); + } + + public TableSpecification getSourceTable() { + return getTable(); + } + + public TableSpecification getTargetTable() { + return targetTable; + } + + public List getSourceColumns() { + return getColumns(); + } + + public List getTargetColumns() { + return targetColumns == null + ? getTargetTable().getPrimaryKey().getColumns() + : targetColumns; + } + + public void addColumnMapping(Column sourceColumn, Column targetColumn) { + if ( targetColumn == null ) { + if ( targetColumns != null ) { + if ( log.isWarnEnabled() ) { + log.warn( + "Attempt to map column [" + sourceColumn.toLoggableString() + + "] to no target column after explicit target column(s) named for FK [name=" + + getName() + "]" + ); + } + } + } + else { + if ( targetColumns == null ) { + if ( !getSourceColumns().isEmpty() ) { + log.warn( + "Value mapping mismatch as part of FK [table=" + getTable().toLoggableString() + + ", name=" + getName() + "] while adding source column [" + + sourceColumn.toLoggableString() + "]" + ); + } + targetColumns = new ArrayList(); + } + targetColumns.add( targetColumn ); + } + getSourceColumns().add( sourceColumn ); + } + + @Override + public String getExportIdentifier() { + return getSourceTable().getLoggableValueQualifier() + ".FK-" + getName(); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Identifier.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Identifier.java new file mode 100644 index 0000000000..d39ec56577 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Identifier.java @@ -0,0 +1,122 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +import org.hibernate.util.StringHelper; + +/** + * Models an identifier (name). + * + * @author Steve Ebersole + */ +public class Identifier { + private final String name; + private final boolean isQuoted; + + /** + * Means to generate an {@link Identifier} instance from its simple name + * + * @param name The name + * @return + */ + public static Identifier toIdentifier(String name) { + if ( name == null ) { + return null; + } + final String trimmedName = name.trim(); + if ( isQuoted( trimmedName ) ) { + final String bareName = trimmedName.substring( 1, trimmedName.length() - 1 ); + return new Identifier( bareName, true ); + } + else { + return new Identifier( trimmedName, false ); + } + } + + private static boolean isQuoted(String name) { + return name.startsWith( "`" ) && name.endsWith( "`" ); + } + + /** + * Constructs an identifier instance. + * + * @param name The identifier text. + * @param quoted Is this a quoted identifier? + */ + public Identifier(String name, boolean quoted) { + if ( StringHelper.isEmpty( name ) ) { + throw new IllegalIdentifierException( "Identifier text cannot be null" ); + } + if ( isQuoted( name ) ) { + throw new IllegalIdentifierException( "Identifier text should not contain quote markers (`)" ); + } + this.name = name; + this.isQuoted = quoted; + } + + /** + * Get the identifiers name (text) + * + * @return The name + */ + public String getName() { + return name; + } + + /** + * Is this a quoted identifier> + * + * @return True if this is a quote identifier; false otherwise. + */ + public boolean isQuoted() { + return isQuoted; + } + + @Override + public String toString() { + return isQuoted + ? '`' + getName() + '`' + : getName(); + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + + Identifier that = (Identifier) o; + + return isQuoted == that.isQuoted + && name.equals( that.name ); + } + + @Override + public int hashCode() { + return name.hashCode(); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/IllegalIdentifierException.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/IllegalIdentifierException.java new file mode 100644 index 0000000000..80ccba8ecc --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/IllegalIdentifierException.java @@ -0,0 +1,37 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +import org.hibernate.HibernateException; + +/** + * Indicates an attempted use of a name that was deemed illegal + * + * @author Steve Ebersole + */ +public class IllegalIdentifierException extends HibernateException { + public IllegalIdentifierException(String s) { + super( s ); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/InLineView.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/InLineView.java new file mode 100644 index 0000000000..dc21bb5e19 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/InLineView.java @@ -0,0 +1,75 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +import java.util.Set; +import java.util.HashSet; + +/** + * A data container defined by a SELECT statement. This translates into an inline view in the + * SQL statements: select ... from (select ... from logical_table_table ...) ... + * + * @author Gavin King + * @author Steve Ebersole + */ +public class InLineView extends AbstractTableSpecification implements ValueContainer { + private final String select; + private final String uniqueValueQualifier; + private Set synchronizedTableSpaces = java.util.Collections.emptySet(); + + public InLineView(String select, String uniqueValueQualifier) { + this.select = select; + this.uniqueValueQualifier = uniqueValueQualifier; + } + + public String getSelect() { + return select; + } + + @Override + public String getLoggableValueQualifier() { + return uniqueValueQualifier; + } + + public void addSynchronizedTable(String tableName) { + addSynchronizedTable( new ObjectName( null, null, tableName ) ); + } + + public void addSynchronizedTable(ObjectName tableName) { + if ( synchronizedTableSpaces.isEmpty() ) { + synchronizedTableSpaces = new HashSet(); + } + synchronizedTableSpaces.add( tableName ); + } + + @Override + public Set getSpaces() { + return synchronizedTableSpaces; + } + + @Override + public String toLoggableString() { + return "{inline-view}"; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Index.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Index.java new file mode 100644 index 0000000000..74694bdf66 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Index.java @@ -0,0 +1,36 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +/** + * Models a SQL INDEX + * + * @author Gavin King + * @author Steve Ebersole + */ +public class Index extends AbstractConstraint implements Constraint { + protected Index(TableSpecification table, String name) { + super( table, name ); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ObjectName.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ObjectName.java new file mode 100644 index 0000000000..ee0dac6a3d --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ObjectName.java @@ -0,0 +1,139 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +/** + * Models the qualified name of a database object. + *

+ * Some things to keep in mind wrt catalog/schema: + * 1) {@link java.sql.DatabaseMetaData#isCatalogAtStart} + * 2) {@link java.sql.DatabaseMetaData#getCatalogSeparator()} + * + * @author Steve Ebersole + */ +public class ObjectName { + private final Identifier schema; + private final Identifier catalog; + private final Identifier name; + + private final String identifier; + private final int hashCode; + + public ObjectName(Identifier name) { + this( null, null, name ); + } + + public ObjectName(String schemaName, String catalogName, String name) { + this( + Identifier.toIdentifier( schemaName ), + Identifier.toIdentifier( catalogName ), + Identifier.toIdentifier( name ) + ); + } + + /** + * Creates a qualified name reference. + * + * @param schema The in which the object is defined (optional) + * @param catalog The catalog in which the object is defined (optional) + * @param name The name (required) + */ + public ObjectName(Identifier schema, Identifier catalog, Identifier name) { + if ( name == null ) { + // Identifier cannot be constructed with an 'empty' name + throw new IllegalIdentifierException( "Object name must be specified" ); + } + this.name = name; + this.schema = schema; + this.catalog = catalog; + + StringBuilder buff = new StringBuilder( name.toString() ); + if ( catalog != null ) { + buff.insert( 0, catalog.toString() + '.' ); + } + if ( schema != null ) { + buff.insert( 0, schema.toString() + '.' ); + } + this.identifier = buff.toString(); + + int tmpHashCode = schema != null ? schema.hashCode() : 0; + tmpHashCode = 31 * tmpHashCode + (catalog != null ? catalog.hashCode() : 0); + tmpHashCode = 31 * tmpHashCode + name.hashCode(); + this.hashCode = tmpHashCode; + } + + public Identifier getSchema() { + return schema; + } + + public Identifier getCatalog() { + return catalog; + } + + public Identifier getName() { + return name; + } + + public String getIdentifier() { + return identifier; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + ObjectName that = (ObjectName) o; + + return name.equals( that.name ) + && areEqual( catalog, that.catalog ) + && areEqual( schema, that.schema ); + } + + private boolean areEqual(Identifier one, Identifier other) { + return one == null + ? other == null + : one.equals( other ); + } + + @Override + public int hashCode() { + return hashCode; + } + + + @Override + public String toString() { + return "ObjectName{" + + "name='" + name + '\'' + + ", schema='" + schema + '\'' + + ", catalog='" + catalog + '\'' + + '}'; + } +} + diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/PrimaryKey.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/PrimaryKey.java new file mode 100644 index 0000000000..f92b121bb4 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/PrimaryKey.java @@ -0,0 +1,59 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +/** + * Models a table's primary key. + *

+ * NOTE : This need not be a physical primary key; we just mean a column or columns which uniquely identify rows in + * the table. Of course it is recommended to define proper integrity constraints, including primary keys. + * + * @author Gavin King + * @author Steve Ebersole + */ +public class PrimaryKey extends AbstractConstraint implements Constraint, Exportable { + // IMPL NOTE : I override the name behavior here because: + // (1) primary keys are not required to be named. + // (2) because a primary key is required for each table, it is easier to allow setting the constraint name + // later in terms of building the metamodel + private String name; + + public PrimaryKey(TableSpecification table) { + super( table, null ); + } + + @Override + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String getExportIdentifier() { + return getTable().getLoggableValueQualifier() + ".PK"; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Sequence.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Sequence.java new file mode 100644 index 0000000000..e45dac3705 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Sequence.java @@ -0,0 +1,57 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +/** + * Models a database {@code SEQUENCE}. + * + * @author Steve Ebersole + */ +public class Sequence implements Exportable { + private final ObjectName name; + private final int initialValue; + private final int incrementSize; + + public Sequence(ObjectName name, int initialValue, int incrementSize) { + this.name = name; + this.initialValue = initialValue; + this.incrementSize = incrementSize; + } + + public String getExportIdentifier() { + return name.getIdentifier(); + } + + public ObjectName getName() { + return name; + } + + public int getInitialValue() { + return initialValue; + } + + public int getIncrementSize() { + return incrementSize; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/SimpleValue.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/SimpleValue.java new file mode 100644 index 0000000000..d13985c309 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/SimpleValue.java @@ -0,0 +1,45 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +/** + * Models a simple, non-compound value. + * + * @author Steve Ebersole + */ +public interface SimpleValue extends Value { + /** + * Retrieve the datatype of this value. + * + * @return The value's datatype + */ + public Datatype getDatatype(); + + /** + * Set the datatype of this value. + * + * @param datatype The value's datatype + */ + public void setDatatype(Datatype datatype); +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Table.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Table.java new file mode 100644 index 0000000000..f68608482e --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Table.java @@ -0,0 +1,73 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +import java.util.Set; + +/** + * Models the concept of a relational TABLE (or VIEW). + * + * @author Gavin King + * @author Steve Ebersole + */ +public class Table extends AbstractTableSpecification implements ValueContainer, Exportable { + private final ObjectName name; + private final Set spaces; + + public Table(ObjectName name) { + this.name = name; + this.spaces = java.util.Collections.singleton( name ); + } + + public ObjectName getObjectName() { + return name; + } + + @Override + public String getLoggableValueQualifier() { + return getObjectName().getIdentifier(); + } + + @Override + public String getExportIdentifier() { + return getObjectName().getIdentifier(); + } + + @Override + public Set getSpaces() { + return spaces; + } + + @Override + public String toLoggableString() { + return getObjectName().getIdentifier(); + } + + @Override + public String toString() { + return "Table{" + + "name=" + getObjectName().getIdentifier() + + '}'; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/TableSpecification.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/TableSpecification.java new file mode 100644 index 0000000000..f63702ec3b --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/TableSpecification.java @@ -0,0 +1,47 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +import java.util.Set; + +/** + * Models what ANSI SQL terms a table specification which is a table or a view or an inline view. + * + * @author Steve Ebersole + */ +public interface TableSpecification extends ValueContainer { + /** + * Get the primary key definition for this table spec. + * + * @return The PK definition. + */ + public PrimaryKey getPrimaryKey(); + + /** + * Get the physical table names modelled here. This is especially important in the case of an inline view. + * + * @return The spaces. + */ + public Set getSpaces(); +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Tuple.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Tuple.java new file mode 100644 index 0000000000..e36c532b5f --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Tuple.java @@ -0,0 +1,65 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +/** + * Models a compound value (a tuple or row-value-constructor is SQL terms). It is both a {@link Value} and + * a {@link ValueContainer} simultaneously. + *

+ * IMPL NOTE : in terms of the tables themselves, SQL has no notion of a tuple/compound-value. We simply model + * it this way because: + *

    + *
  • it is a cleaner mapping to the logical model
  • + *
  • it allows more meaningful traversals from simple values back up to table through any intermediate tuples + * because it gives us a better understanding of the model.
  • + *
  • it better conveys intent
  • + *
  • it adds richness to the model
  • + *
+ * + * @author Steve Ebersole + */ +public class Tuple extends AbstractValueContainer implements Value { + private final ValueContainer valueContainer; + private final String name; + + public Tuple(ValueContainer valueContainer, String name) { + this.name = name; + this.valueContainer = valueContainer; + } + + @Override + public ValueContainer getValueContainer() { + return valueContainer; + } + + @Override + public String getLoggableValueQualifier() { + return getValueContainer().getLoggableValueQualifier() + '.' + name; + } + + @Override + public String toLoggableString() { + return getLoggableValueQualifier() + "{tuple}"; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/UniqueKey.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/UniqueKey.java new file mode 100644 index 0000000000..5ed1a35386 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/UniqueKey.java @@ -0,0 +1,36 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +/** + * Models a SQL INDEX defined as UNIQUE + * + * @author Gavin King + * @author Steve Ebersole + */ +public class UniqueKey extends AbstractConstraint implements Constraint { + protected UniqueKey(TableSpecification table, String name) { + super( table, name ); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Value.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Value.java new file mode 100644 index 0000000000..dac3c6a67c --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Value.java @@ -0,0 +1,46 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +/** + * Models a value within a {@link ValueContainer}. This will generally be either a {@link Column column} or a + * {@link DerivedValue derived value}, but we also allow the notion of {@link Tuple} at this level + * + * @author Steve Ebersole + */ +public interface Value { + /** + * Retrieve the table that owns this value. + * + * @return The owning table. + */ + public ValueContainer getValueContainer(); + + /** + * Obtain the string representation of this value usable in log statements. + * + * @return The loggable representation + */ + public String toLoggableString(); +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ValueContainer.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ValueContainer.java new file mode 100644 index 0000000000..c0387bc728 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ValueContainer.java @@ -0,0 +1,82 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.relational; + +/** + * Contract for data containers (what the ANSI SQL spec calls "table specifications") to which we can map + * entity state. The two flavors here are {@link Table physical table} and {@link InLineView inline view}, but a + * {@link Tuple} is a conceptual value container as well. + * + * @author Steve Ebersole + */ +public interface ValueContainer { + /** + * Obtain an iterator over this containers current set of value definitions. + * + * @return Iterator over value definitions. + */ + public Iterable iterateValues(); + + /** + * Get a qualifier which can be used to qualify {@link Value values} belonging to this container in + * their logging. + * + * @return The qualifier + */ + public String getLoggableValueQualifier(); + + /** + * Obtain the string representation of this value usable in log statements. + * + * @return The loggable representation + */ + public String toLoggableString(); + + /** + * Factory method for creating a {@link Column} associated with this container. + * + * @param name The column name + * + * @return The generated column + */ + public Column createColumn(String name); + + /** + * Factory method for creating a {@link DerivedValue} associated with this container. + * + * @param fragment The value expression + * + * @return The generated value. + */ + public DerivedValue createDerivedValue(String fragment); + + /** + * Factory method for creating a {@link Tuple} associated with this container. + * + * @param name The (logical) tuple name + * + * @return The generated tuple. + */ + public Tuple createTuple(String name); +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/package.html b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/package.html new file mode 100644 index 0000000000..4dd12a5ff3 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/package.html @@ -0,0 +1,31 @@ + + + + +

+ This package defines the metamodel of a relational database schema. +

+ +