From e90c1be9157dacafbbcf0525dae0c55aed189916 Mon Sep 17 00:00:00 2001
From: lidongy <1879087903@qq.com>
Date: Thu, 17 Sep 2020 17:53:55 +0800
Subject: [PATCH 1/5] =?UTF-8?q?KERNEL-5345=20refactor:=20hibernate?=
=?UTF-8?q?=E7=89=B9=E6=AE=8A=E5=A4=84=E7=90=86=EF=BC=8C=E4=BD=BF=E5=85=B6?=
=?UTF-8?q?=E5=8F=AF=E4=BB=A5=E6=89=A9=E5=B1=95=E5=BB=BA=E8=A1=A8=E6=97=B6?=
=?UTF-8?q?=E7=9A=84=E4=B8=BB=E9=94=AE=E8=AE=BE=E7=BD=AE=E8=AF=AD=E5=8F=A5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../com/fr/third/org/hibernate/dialect/Dialect.java | 12 ++++++++++--
.../fr/third/org/hibernate/mapping/PrimaryKey.java | 4 +++-
2 files changed, 13 insertions(+), 3 deletions(-)
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..7331c1219 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:
*
* - a "static" delegate based on the JDBC 4 defined SQLException hierarchy;
@@ -2783,4 +2783,12 @@ public abstract class Dialect implements ConversionContext {
public boolean supportsNationalizedTypes() {
return true;
}
+
+ /**
+ * 获取主键sql字符串
+ *
+ * @param columnList 主键列集合
+ * @return int
+ */
+ public String getPrimaryKeyString(List columnList) { return "primary key";}
}
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..4059b1474 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
@@ -6,6 +6,7 @@
*/
package com.fr.third.org.hibernate.mapping;
import java.util.Iterator;
+import java.util.List;
import com.fr.third.org.hibernate.dialect.Dialect;
import com.fr.third.org.hibernate.internal.util.StringHelper;
@@ -58,7 +59,8 @@ public class PrimaryKey extends Constraint {
}
public String sqlConstraintString(Dialect dialect) {
- StringBuilder buf = new StringBuilder("primary key (");
+ List columnList = getColumns();
+ StringBuilder buf = new StringBuilder(dialect.getPrimaryKeyString(columnList)).append(" (");
Iterator iter = getColumnIterator();
while ( iter.hasNext() ) {
buf.append( ( (Column) iter.next() ).getQuotedName(dialect) );
From 9039b3a65492087c79cd0cd2f8d9619146234046 Mon Sep 17 00:00:00 2001
From: lidongy <1879087903@qq.com>
Date: Fri, 18 Sep 2020 09:22:12 +0800
Subject: [PATCH 2/5] =?UTF-8?q?KERNEL-5345=20refactor:=20=E5=AF=B9mysql5?=
=?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E7=89=B9=E6=AE=8A=E5=A4=84=E7=90=86?=
=?UTF-8?q?=20=E6=94=B9=E5=9C=A8third?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../org/hibernate/dialect/MySQL5Dialect.java | 19 ++++++++++++++++++-
.../org/hibernate/dialect/MySQL8Dialect.java | 17 +++++++++++++++++
2 files changed, 35 insertions(+), 1 deletion(-)
create mode 100644 fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL8Dialect.java
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..0b8ee5e86 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,19 @@ public class MySQL5Dialect extends MySQLDialect {
public boolean supportsColumnCheck() {
return false;
}
-
+
+ //utf8编码下,mysql5.7以下版本主键长度不能超过255,故特殊处理,去掉它的primary。
+ // 它默认会生成一个长度为255的前缀索引
+ @Override
+ public String getPrimaryKeyString(List columnList) {
+ for (Column column : columnList) {
+ if (column.getLength() > INDEX_LENGTH_LIMIT) {
+ return "key";
+ }
+ }
+ return "primary key";
+ }
+
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..4e10003c2
--- /dev/null
+++ b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL8Dialect.java
@@ -0,0 +1,17 @@
+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 getPrimaryKeyString(List columnList) {
+ return "primary key";
+ }
+}
From 986e74992426bc4a5f315c5875cafbcfda352eaa Mon Sep 17 00:00:00 2001
From: lidongy <1879087903@qq.com>
Date: Fri, 18 Sep 2020 13:39:44 +0800
Subject: [PATCH 3/5] =?UTF-8?q?DEC-15106=20fix:=20mysql=E6=9C=89=E4=BA=9B?=
=?UTF-8?q?=E5=B0=8F=E7=89=88=E6=9C=AC(=E5=A6=825.5.46)=E4=B8=8D=E6=94=AF?=
=?UTF-8?q?=E6=8C=81=E5=88=9B=E5=BB=BAkey=E7=9A=84=E5=90=8C=E6=97=B6?=
=?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=88=9B=E5=BB=BA=E5=89=8D=E7=BC=80=E7=B4=A2?=
=?UTF-8?q?=E5=BC=95=EF=BC=8C=E6=AD=A4=E5=A4=84=E6=89=8B=E5=8A=A8=E6=8C=87?=
=?UTF-8?q?=E5=AE=9A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../java/com/fr/third/org/hibernate/dialect/Dialect.java | 9 ++++++++-
.../fr/third/org/hibernate/dialect/MySQL5Dialect.java | 7 +++++++
.../fr/third/org/hibernate/dialect/MySQL8Dialect.java | 4 ++++
.../com/fr/third/org/hibernate/mapping/PrimaryKey.java | 4 +++-
4 files changed, 22 insertions(+), 2 deletions(-)
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 7331c1219..402cfc459 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
@@ -2788,7 +2788,14 @@ public abstract class Dialect implements ConversionContext {
* 获取主键sql字符串
*
* @param columnList 主键列集合
- * @return int
+ * @return String
*/
public String getPrimaryKeyString(List columnList) { return "primary key";}
+
+ /**
+ * 获取主键sql后缀
+ *
+ * @return String
+ */
+ public String getPrimaryKeyPostfix(Column column) { return "";}
}
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 0b8ee5e86..cb3f39a10 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
@@ -49,6 +49,13 @@ public class MySQL5Dialect extends MySQLDialect {
return "primary key";
}
+ // 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() {
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
index 4e10003c2..c72256e0d 100644
--- 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
@@ -14,4 +14,8 @@ public class MySQL8Dialect extends MySQL5Dialect {
public String getPrimaryKeyString(List columnList) {
return "primary key";
}
+
+
+ @Override
+ public String getPrimaryKeyPostfix(Column column) { return "";}
}
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 4059b1474..d9674ebeb 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
@@ -63,7 +63,9 @@ public class PrimaryKey extends Constraint {
StringBuilder buf = new StringBuilder(dialect.getPrimaryKeyString(columnList)).append(" (");
Iterator iter = getColumnIterator();
while ( iter.hasNext() ) {
- buf.append( ( (Column) iter.next() ).getQuotedName(dialect) );
+ Column column = (Column) iter.next();
+ buf.append( column.getQuotedName(dialect) );
+ buf.append( dialect.getPrimaryKeyPostfix(column) );
if ( iter.hasNext() ) {
buf.append(", ");
}
From 3fbdf933fda87d842b7dd52b4eea66fbf236a955 Mon Sep 17 00:00:00 2001
From: lidongy <1879087903@qq.com>
Date: Fri, 18 Sep 2020 14:59:36 +0800
Subject: [PATCH 4/5] =?UTF-8?q?DEC-15106=20fix:=20=E6=94=B9=E6=88=90?=
=?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=9C=A8=E6=96=B9=E8=A8=80=E4=B8=AD=E5=AE=9E?=
=?UTF-8?q?=E7=8E=B0=EF=BC=8C=E6=96=B9=E4=BE=BF=E7=BB=B4=E6=8A=A4=E4=B8=80?=
=?UTF-8?q?=E7=82=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../third/org/hibernate/dialect/Dialect.java | 19 ++++++-----
.../org/hibernate/dialect/MySQL5Dialect.java | 34 +++++++++++--------
.../org/hibernate/dialect/MySQL8Dialect.java | 17 ++++++----
.../org/hibernate/mapping/PrimaryKey.java | 14 +-------
4 files changed, 41 insertions(+), 43 deletions(-)
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 402cfc459..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
@@ -2785,17 +2785,20 @@ public abstract class Dialect implements ConversionContext {
}
/**
- * 获取主键sql字符串
+ * 获取建表时创建约束字符串
*
* @param columnList 主键列集合
* @return String
*/
- public String getPrimaryKeyString(List columnList) { return "primary key";}
+ 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();
+ }
- /**
- * 获取主键sql后缀
- *
- * @return String
- */
- public String getPrimaryKeyPostfix(Column column) { return "";}
}
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 cb3f39a10..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
@@ -37,24 +37,28 @@ public class MySQL5Dialect extends MySQLDialect {
return false;
}
- //utf8编码下,mysql5.7以下版本主键长度不能超过255,故特殊处理,去掉它的primary。
- // 它默认会生成一个长度为255的前缀索引
- @Override
- public String getPrimaryKeyString(List columnList) {
- for (Column column : columnList) {
- if (column.getLength() > INDEX_LENGTH_LIMIT) {
- return "key";
+
+ @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 "primary key";
- }
+ 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() {
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
index c72256e0d..b334ad368 100644
--- 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
@@ -11,11 +11,14 @@ import java.util.List;
*/
public class MySQL8Dialect extends MySQL5Dialect {
@Override
- public String getPrimaryKeyString(List columnList) {
- return "primary key";
+ 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();
}
-
-
- @Override
- public String getPrimaryKeyPostfix(Column column) { return "";}
-}
+}
\ 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 d9674ebeb..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
@@ -6,7 +6,6 @@
*/
package com.fr.third.org.hibernate.mapping;
import java.util.Iterator;
-import java.util.List;
import com.fr.third.org.hibernate.dialect.Dialect;
import com.fr.third.org.hibernate.internal.util.StringHelper;
@@ -59,18 +58,7 @@ public class PrimaryKey extends Constraint {
}
public String sqlConstraintString(Dialect dialect) {
- List columnList = 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();
+ return dialect.sqlConstraintString(getColumns());
}
public String sqlConstraintString(Dialect dialect, String constraintName, String defaultCatalog, String defaultSchema) {
From cfef014fdfdac568da43738a51ce1d3a721de5bb Mon Sep 17 00:00:00 2001
From: lidongy <1879087903@qq.com>
Date: Fri, 18 Sep 2020 16:35:37 +0800
Subject: [PATCH 5/5] =?UTF-8?q?DEC-15106=20test:=20=E8=A1=A5=E5=85=85?=
=?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
fine-hibernate/pom.xml | 7 ++++
.../hibernate/dialect/MySQLDialectTest.java | 38 +++++++++++++++++++
2 files changed, 45 insertions(+)
create mode 100644 fine-hibernate/src/test/java/com/fr/third/org/hibernate/dialect/MySQLDialectTest.java
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/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)");
+
+ }
+}