Elijah
4 years ago
6 changed files with 143 additions and 9 deletions
@ -0,0 +1,63 @@
|
||||
package com.fr.third.org.hibernate.tool.schema.internal; |
||||
|
||||
import com.fr.third.org.hibernate.boot.Metadata; |
||||
import com.fr.third.org.hibernate.dialect.Dialect; |
||||
import com.fr.third.org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; |
||||
import com.fr.third.org.hibernate.mapping.Column; |
||||
import com.fr.third.org.hibernate.mapping.PrimaryKey; |
||||
import com.fr.third.org.hibernate.tool.schema.spi.Exporter; |
||||
|
||||
import java.util.Iterator; |
||||
|
||||
/** |
||||
* @author Elijah |
||||
* @version 10.5 |
||||
* Created by Elijah on 2020/9/30 |
||||
*/ |
||||
public class StandardPrimaryKeyExporter implements Exporter<PrimaryKey> { |
||||
private Dialect dialect; |
||||
|
||||
public StandardPrimaryKeyExporter(Dialect dialect) { |
||||
this.dialect = dialect; |
||||
} |
||||
|
||||
@Override |
||||
public String[] getSqlCreateStrings(PrimaryKey primaryKey, Metadata metadata) { |
||||
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment(); |
||||
final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format( |
||||
primaryKey.getTable().getQualifiedTableName(), |
||||
dialect |
||||
); |
||||
|
||||
final String constraintName = dialect.quote( primaryKey.getName() ); |
||||
return new String[]{ "alter table " + tableName + dialect.getAddPrimaryKeyConstraintString(constraintName) + " " + columnsToString( primaryKey ) }; |
||||
} |
||||
|
||||
protected String columnsToString(PrimaryKey primaryKey) { |
||||
final StringBuilder sb = new StringBuilder(); |
||||
sb.append( " (" ); |
||||
final Iterator<Column> columnIterator = primaryKey.columnIterator(); |
||||
while ( columnIterator.hasNext() ) { |
||||
final Column column = columnIterator.next(); |
||||
sb.append( column.getQuotedName( dialect ) ); |
||||
if ( columnIterator.hasNext() ) { |
||||
sb.append( ", " ); |
||||
} |
||||
} |
||||
|
||||
return sb.append( ')' ).toString(); |
||||
} |
||||
|
||||
@Override |
||||
public String[] getSqlDropStrings(PrimaryKey primaryKey, Metadata metadata) { |
||||
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment(); |
||||
final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format( |
||||
primaryKey.getTable().getQualifiedTableName(), |
||||
dialect |
||||
); |
||||
final StringBuilder buf = new StringBuilder( "alter table " ); |
||||
buf.append( tableName ); |
||||
buf.append(dialect.getDropPrimaryKeyConstraintString(dialect.quote( primaryKey.getName()))); |
||||
return new String[] { buf.toString() }; |
||||
} |
||||
} |
Loading…
Reference in new issue