Browse Source

DEC-15106 fix: 改成统一在方言中实现,方便维护一点

release/10.0
lidongy 4 years ago
parent
commit
3fbdf933fd
  1. 19
      fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/Dialect.java
  2. 28
      fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL5Dialect.java
  3. 15
      fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL8Dialect.java
  4. 14
      fine-hibernate/src/main/java/com/fr/third/org/hibernate/mapping/PrimaryKey.java

19
fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/Dialect.java

@ -2785,17 +2785,20 @@ public abstract class Dialect implements ConversionContext {
} }
/** /**
* 获取主键sql字符串 * 获取建表时创建约束字符串
* *
* @param columnList 主键列集合 * @param columnList 主键列集合
* @return String * @return String
*/ */
public String getPrimaryKeyString(List<Column> columnList) { return "primary key";} public String sqlConstraintString(List<Column> columnList) {
StringBuilder buf = new StringBuilder("primary key (");
for (int i = 0; i < columnList.size(); i ++) {
if (i != 0) {
buf.append(", ");
}
buf.append(columnList.get(i).getQuotedName(this));
}
return buf.append(')').toString();
}
/**
* 获取主键sql后缀
*
* @return String
*/
public String getPrimaryKeyPostfix(Column column) { return "";}
} }

28
fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL5Dialect.java

@ -37,25 +37,29 @@ public class MySQL5Dialect extends MySQLDialect {
return false; return false;
} }
//utf8编码下,mysql5.7以下版本主键长度不能超过255,故特殊处理,去掉它的primary。
// 它默认会生成一个长度为255的前缀索引
@Override @Override
public String getPrimaryKeyString(List<Column> columnList) { public String sqlConstraintString(List<Column> columnList) {
for (Column column : columnList) { StringBuilder buf = new StringBuilder("key (");
boolean overLimit = false;
for (int i = 0; i < columnList.size(); i ++) {
if (i != 0) {
buf.append(", ");
}
Column column = columnList.get(i);
buf.append(column.getQuotedName(this));
if (column.getLength() > INDEX_LENGTH_LIMIT) { if (column.getLength() > INDEX_LENGTH_LIMIT) {
return "key"; buf.append("(" + INDEX_LENGTH_LIMIT + ")");
overLimit = true;
} }
} }
return "primary key"; if (!overLimit) {
buf.insert(0, "primary ");
} }
return buf.append(')').toString();
// mysql有些小版本(如5.5.46)不支持创建key的同时自动创建前缀索引,此处手动指定
@Override
public String getPrimaryKeyPostfix(Column column) {
int columnLength = column.getLength();
return columnLength > INDEX_LENGTH_LIMIT ? "(" + INDEX_LENGTH_LIMIT + ")" : "";
} }
public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() { public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
return EXTRACTER; return EXTRACTER;
} }

15
fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL8Dialect.java

@ -11,11 +11,14 @@ import java.util.List;
*/ */
public class MySQL8Dialect extends MySQL5Dialect { public class MySQL8Dialect extends MySQL5Dialect {
@Override @Override
public String getPrimaryKeyString(List<Column> columnList) { public String sqlConstraintString(List<Column> columnList) {
return "primary key"; StringBuilder buf = new StringBuilder("primary key (");
for (int i = 0; i < columnList.size(); i++) {
if (i != 0) {
buf.append(", ");
}
buf.append(columnList.get(i).getQuotedName(this));
}
return buf.append(')').toString();
} }
@Override
public String getPrimaryKeyPostfix(Column column) { return "";}
} }

14
fine-hibernate/src/main/java/com/fr/third/org/hibernate/mapping/PrimaryKey.java

@ -6,7 +6,6 @@
*/ */
package com.fr.third.org.hibernate.mapping; package com.fr.third.org.hibernate.mapping;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import com.fr.third.org.hibernate.dialect.Dialect; import com.fr.third.org.hibernate.dialect.Dialect;
import com.fr.third.org.hibernate.internal.util.StringHelper; import com.fr.third.org.hibernate.internal.util.StringHelper;
@ -59,18 +58,7 @@ public class PrimaryKey extends Constraint {
} }
public String sqlConstraintString(Dialect dialect) { public String sqlConstraintString(Dialect dialect) {
List<Column> columnList = getColumns(); return dialect.sqlConstraintString(getColumns());
StringBuilder buf = new StringBuilder(dialect.getPrimaryKeyString(columnList)).append(" (");
Iterator iter = getColumnIterator();
while ( iter.hasNext() ) {
Column column = (Column) iter.next();
buf.append( column.getQuotedName(dialect) );
buf.append( dialect.getPrimaryKeyPostfix(column) );
if ( iter.hasNext() ) {
buf.append(", ");
}
}
return buf.append(')').toString();
} }
public String sqlConstraintString(Dialect dialect, String constraintName, String defaultCatalog, String defaultSchema) { public String sqlConstraintString(Dialect dialect, String constraintName, String defaultCatalog, String defaultSchema) {

Loading…
Cancel
Save