package org.hibernate.mapping;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.internal.util.StringHelper;
public class Index implements RelationalModel, Serializable {
private Table table;
private List<Column> columns = new ArrayList<Column>();
private java.util.Map<Column, String> columnOrderMap = new HashMap<Column, String>( );
private String name;
public String sqlCreateString(Dialect dialect, Mapping mapping, String defaultCatalog, String defaultSchema)
throws HibernateException {
return buildSqlCreateIndexString(
dialect,
getName(),
getTable(),
getColumnIterator(),
columnOrderMap,
false,
defaultCatalog,
defaultSchema
);
}
public static String buildSqlDropIndexString(
Dialect dialect,
Table table,
String name,
String defaultCatalog,
String defaultSchema
) {
return "drop index " +
StringHelper.qualify(
table.getQualifiedName( dialect, defaultCatalog, defaultSchema ),
name
);
}
public static String buildSqlCreateIndexString(
Dialect dialect,
String name,
Table table,
Iterator<Column> columns,
java.util.Map<Column, String> columnOrderMap,
boolean unique,
String defaultCatalog,
String defaultSchema
) {
StringBuilder buf = new StringBuilder( "create" )
.append( unique ?
" unique" :
"" )
.append( " index " )
.append( dialect.qualifyIndexName() ?
name :
StringHelper.unqualify( name ) )
.append( " on " )
.append( table.getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
.append( " (" );
while ( columns.hasNext() ) {
Column column = columns.next();
buf.append( column.getQuotedName( dialect ) );
if ( columnOrderMap.containsKey( column ) ) {
buf.append( " " ).append( columnOrderMap.get( column ) );
}
if ( columns.hasNext() ) buf.append( ", " );
}
buf.append( ")" );
return buf.toString();
}
public static String buildSqlCreateIndexString(
Dialect dialect,
String name,
Table table,
Iterator<Column> columns,
boolean unique,
String defaultCatalog,
String defaultSchema
) {
return buildSqlCreateIndexString( dialect, name, table, columns, Collections.EMPTY_MAP, unique, defaultCatalog, defaultSchema );
}
public String sqlConstraintString(Dialect dialect) {
StringBuilder buf = new StringBuilder( " index (" );
Iterator iter = getColumnIterator();
while ( iter.hasNext() ) {
buf.append( ( (Column) iter.next() ).getQuotedName( dialect ) );
if ( iter.hasNext() ) buf.append( ", " );
}
return buf.append( ')' ).toString();
}
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) {
return "drop index " +
StringHelper.qualify(
table.getQualifiedName( dialect, defaultCatalog, defaultSchema ),
name
);
}
public Table getTable() {
return table;
}
public void setTable(Table table) {
this.table = table;
}
public int getColumnSpan() {
return columns.size();
}
public Iterator<Column> getColumnIterator() {
return columns.iterator();
}
public void addColumn(Column column) {
if ( !columns.contains( column ) ) columns.add( column );
}
public void addColumn(Column column, String order) {
addColumn( column );
if ( StringHelper.isNotEmpty( order ) ) {
columnOrderMap.put( column, order );
}
}
public void addColumns(Iterator extraColumns) {
while ( extraColumns.hasNext() ) addColumn( (Column) extraColumns.next() );
}
public boolean containsColumn(Column column) {
return columns.contains( column );
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return getClass().getName() + "(" + getName() + ")";
}
}