Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 3b31e9a + 6c0e701 commit 55cf856Copy full SHA for 55cf856
2 files changed
dialect_mysql.go
@@ -72,21 +72,21 @@ func (d MySQLDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool)
72
return "datetime"
73
}
74
75
- if maxsize < 1 {
76
- maxsize = 255
77
- }
78
-
79
/* == About varchar(N) ==
80
* N is number of characters.
81
- * A varchar column can store up to 65535 bytes.
82
- * Remember that 1 character is 3 bytes in utf-8 charset.
83
- * Also remember that each row can store up to 65535 bytes,
84
- * and you have some overheads, so it's not possible for a
85
- * varchar column to have 65535/3 characters really.
86
- * So it would be better to use 'text' type in stead of
87
- * large varchar type.
+ * According to the documentation, 0 < N <= 65535.
+ * But one utf-8 character takes 3 bytes and each row can
+ * be only 65535 bytes. So there is no way to know exactly
+ * how many chars a varchar column can actually store.
+ * Text columns contribute only up to 12 bytes to the row
+ * size as their contents are stored off-page.
+ * Hence, we use a conservative maximum of 512 for varchar,
+ * after which we switch to the text type.
88
*/
89
- if maxsize < 256 {
+ if maxsize == 0 {
+ // Closer match for unbounded text
+ return "longtext"
+ } else if maxsize < 256 {
90
return fmt.Sprintf("varchar(%d)", maxsize)
91
} else {
92
return "text"
dialect_mysql_test.go
@@ -61,9 +61,9 @@ var _ = Describe("MySQLDialect", func() {
61
Entry("NullFloat64", sql.NullFloat64{}, 0, false, "double"),
62
Entry("NullBool", sql.NullBool{}, 0, false, "tinyint"),
63
Entry("Time", time.Time{}, 0, false, "datetime"),
64
- Entry("default-size string", "", 0, false, "varchar(255)"),
+ Entry("default-size string", "", 0, false, "longtext"),
65
Entry("sized string", "", 50, false, "varchar(50)"),
66
- Entry("large string", "", 1024, false, "text"),
+ Entry("large string", "", 1024, false, "text(1024)"),
67
)
68
69
Describe("AutoIncrStr", func() {
0 commit comments