diff --git a/fine-hibernate/pom.xml b/fine-hibernate/pom.xml index 85a8d335c..a8d68fb1b 100644 --- a/fine-hibernate/pom.xml +++ b/fine-hibernate/pom.xml @@ -107,5 +107,12 @@ system ${basedir}/lib/validation-api-1.1.0.Final.jar + + com.fr.third + junit + local + system + ${basedir}/lib/junit-4.12.jar + \ No newline at end of file diff --git a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/Dialect.java b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/Dialect.java index 2f4d45d07..4aa69b349 100644 --- a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/Dialect.java +++ b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/Dialect.java @@ -1601,11 +1601,11 @@ public abstract class Dialect implements ConversionContext { * Build an instance of a {@link SQLExceptionConversionDelegate} for * interpreting dialect-specific error or SQLState codes. *

- * When {@link #buildSQLExceptionConverter} returns null, the default + * When {@link #buildSQLExceptionConverter} returns null, the default * {@link SQLExceptionConverter} is used to interpret SQLState and * error codes. If this method is overridden to return a non-null value, * the default {@link SQLExceptionConverter} will use the returned - * {@link SQLExceptionConversionDelegate} in addition to the following + * {@link SQLExceptionConversionDelegate} in addition to the following * standard delegates: *

    *
  1. a "static" delegate based on the JDBC 4 defined SQLException hierarchy;
  2. @@ -2783,4 +2783,22 @@ public abstract class Dialect implements ConversionContext { public boolean supportsNationalizedTypes() { return true; } + + /** + * 获取建表时创建约束字符串 + * + * @param columnList 主键列集合 + * @return String + */ + public String sqlConstraintString(List 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(); + } + } diff --git a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL5Dialect.java b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL5Dialect.java index beeec4c7b..f1814368f 100644 --- a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL5Dialect.java +++ b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL5Dialect.java @@ -9,9 +9,11 @@ package com.fr.third.org.hibernate.dialect; import com.fr.third.org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtracter; import com.fr.third.org.hibernate.exception.spi.ViolatedConstraintNameExtracter; import com.fr.third.org.hibernate.internal.util.JdbcExceptionHelper; +import com.fr.third.org.hibernate.mapping.Column; import java.sql.SQLException; import java.sql.Types; +import java.util.List; /** * An SQL dialect for MySQL 5.x specific features. @@ -19,6 +21,9 @@ import java.sql.Types; * @author Steve Ebersole */ public class MySQL5Dialect extends MySQLDialect { + + private static final int INDEX_LENGTH_LIMIT = 255; + @Override protected void registerVarcharTypes() { registerColumnType( Types.VARCHAR, "longtext" ); @@ -31,7 +36,30 @@ public class MySQL5Dialect extends MySQLDialect { public boolean supportsColumnCheck() { return false; } - + + + @Override + public String sqlConstraintString(List 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) { + buf.append("(" + INDEX_LENGTH_LIMIT + ")"); + overLimit = true; + } + } + if (!overLimit) { + buf.insert(0, "primary "); + } + return buf.append(')').toString(); + } + + public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() { return EXTRACTER; } diff --git a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL8Dialect.java b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL8Dialect.java new file mode 100644 index 000000000..b334ad368 --- /dev/null +++ b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL8Dialect.java @@ -0,0 +1,24 @@ +package com.fr.third.org.hibernate.dialect; + +import com.fr.third.org.hibernate.mapping.Column; + +import java.util.List; + +/** + * @author lidongy + * @version 10.0 + * Created by lidongy on 2020/9/18 + */ +public class MySQL8Dialect extends MySQL5Dialect { + @Override + public String sqlConstraintString(List 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(); + } +} \ No newline at end of file diff --git a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/mapping/PrimaryKey.java b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/mapping/PrimaryKey.java index 13a9693f2..48f13c807 100644 --- a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/mapping/PrimaryKey.java +++ b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/mapping/PrimaryKey.java @@ -58,15 +58,7 @@ public class PrimaryKey extends Constraint { } public String sqlConstraintString(Dialect dialect) { - StringBuilder buf = new StringBuilder("primary key ("); - Iterator iter = getColumnIterator(); - while ( iter.hasNext() ) { - buf.append( ( (Column) iter.next() ).getQuotedName(dialect) ); - if ( iter.hasNext() ) { - buf.append(", "); - } - } - return buf.append(')').toString(); + return dialect.sqlConstraintString(getColumns()); } public String sqlConstraintString(Dialect dialect, String constraintName, String defaultCatalog, String defaultSchema) { diff --git a/fine-hibernate/src/test/java/com/fr/third/org/hibernate/dialect/MySQLDialectTest.java b/fine-hibernate/src/test/java/com/fr/third/org/hibernate/dialect/MySQLDialectTest.java new file mode 100644 index 000000000..329de6788 --- /dev/null +++ b/fine-hibernate/src/test/java/com/fr/third/org/hibernate/dialect/MySQLDialectTest.java @@ -0,0 +1,38 @@ +package com.fr.third.org.hibernate.dialect; + +import com.fr.third.org.hibernate.mapping.Column; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author lidongy + * @version 10.0 + * Created by lidongy on 2020/9/18 + */ +public class MySQLDialectTest { + @Test + public void testSqlConstraintString() { + List columnList = new ArrayList<>(); + Column column1 = new Column(); + column1.setName("id1"); + column1.setLength(1000); + Column column2 = new Column(); + column2.setName("id2"); + column2.setLength(200); + Column column3 = new Column(); + column3.setName("id3"); + column3.setLength(255); + + columnList.add(column1); + columnList.add(column2); + columnList.add(column3); + + Assert.assertEquals(new MySQLDialect().sqlConstraintString(columnList), "primary key (id1, id2, id3)"); + Assert.assertEquals(new MySQL5Dialect().sqlConstraintString(columnList), "key (id1(255), id2, id3)"); + Assert.assertEquals(new MySQL8Dialect().sqlConstraintString(columnList), "primary key (id1, id2, id3)"); + + } +}