Skip to content

Commit e8f8fcd

Browse files
authored
return unsigned in database type name when necessary (#1238)
* return unsigned in database type name when necessary * Fix test * Update README * Add myself in AUTHORS
1 parent 6a88ab9 commit e8f8fcd

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Linh Tran Tuan <linhduonggnu at gmail.com>
6767
Lion Yang <lion at aosc.xyz>
6868
Luca Looz <luca.looz92 at gmail.com>
6969
Lucas Liu <extrafliu at gmail.com>
70+
Lunny Xiao <xiaolunwen at gmail.com>
7071
Luke Scott <luke at webconnex.com>
7172
Maciej Zimnoch <maciej.zimnoch at codilime.com>
7273
Michael Woolnough <michael.woolnough at gmail.com>

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ user:password@/
454454
The connection pool is managed by Go's database/sql package. For details on how to configure the size of the pool and how long connections stay in the pool see `*DB.SetMaxOpenConns`, `*DB.SetMaxIdleConns`, and `*DB.SetConnMaxLifetime` in the [database/sql documentation](https://golang.org/pkg/database/sql/). The read, write, and dial timeouts for each individual connection are configured with the DSN parameters [`readTimeout`](#readtimeout), [`writeTimeout`](#writetimeout), and [`timeout`](#timeout), respectively.
455455

456456
## `ColumnType` Support
457-
This driver supports the [`ColumnType` interface](https://golang.org/pkg/database/sql/#ColumnType) introduced in Go 1.8, with the exception of [`ColumnType.Length()`](https://golang.org/pkg/database/sql/#ColumnType.Length), which is currently not supported.
457+
This driver supports the [`ColumnType` interface](https://golang.org/pkg/database/sql/#ColumnType) introduced in Go 1.8, with the exception of [`ColumnType.Length()`](https://golang.org/pkg/database/sql/#ColumnType.Length), which is currently not supported. All Unsigned database type names will be returned `UNSIGNED ` with `INT`, `TINYINT`, `SMALLINT`, `BIGINT`.
458458

459459
## `context.Context` Support
460460
Go 1.8 added `database/sql` support for `context.Context`. This driver supports query timeouts and cancellation via contexts.

driver_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2808,10 +2808,10 @@ func TestRowsColumnTypes(t *testing.T) {
28082808
{"mediumintnull", "MEDIUMINT", "MEDIUMINT", scanTypeNullInt, true, 0, 0, [3]string{"0", "42", "NULL"}, [3]interface{}{ni0, ni42, niNULL}},
28092809
{"bigint", "BIGINT NOT NULL", "BIGINT", scanTypeInt64, false, 0, 0, [3]string{"0", "65535", "-42"}, [3]interface{}{int64(0), int64(65535), int64(-42)}},
28102810
{"bigintnull", "BIGINT", "BIGINT", scanTypeNullInt, true, 0, 0, [3]string{"NULL", "1", "42"}, [3]interface{}{niNULL, ni1, ni42}},
2811-
{"tinyuint", "TINYINT UNSIGNED NOT NULL", "TINYINT", scanTypeUint8, false, 0, 0, [3]string{"0", "255", "42"}, [3]interface{}{uint8(0), uint8(255), uint8(42)}},
2812-
{"smalluint", "SMALLINT UNSIGNED NOT NULL", "SMALLINT", scanTypeUint16, false, 0, 0, [3]string{"0", "65535", "42"}, [3]interface{}{uint16(0), uint16(65535), uint16(42)}},
2813-
{"biguint", "BIGINT UNSIGNED NOT NULL", "BIGINT", scanTypeUint64, false, 0, 0, [3]string{"0", "65535", "42"}, [3]interface{}{uint64(0), uint64(65535), uint64(42)}},
2814-
{"uint13", "INT(13) UNSIGNED NOT NULL", "INT", scanTypeUint32, false, 0, 0, [3]string{"0", "1337", "42"}, [3]interface{}{uint32(0), uint32(1337), uint32(42)}},
2811+
{"tinyuint", "TINYINT UNSIGNED NOT NULL", "UNSIGNED TINYINT", scanTypeUint8, false, 0, 0, [3]string{"0", "255", "42"}, [3]interface{}{uint8(0), uint8(255), uint8(42)}},
2812+
{"smalluint", "SMALLINT UNSIGNED NOT NULL", "UNSIGNED SMALLINT", scanTypeUint16, false, 0, 0, [3]string{"0", "65535", "42"}, [3]interface{}{uint16(0), uint16(65535), uint16(42)}},
2813+
{"biguint", "BIGINT UNSIGNED NOT NULL", "UNSIGNED BIGINT", scanTypeUint64, false, 0, 0, [3]string{"0", "65535", "42"}, [3]interface{}{uint64(0), uint64(65535), uint64(42)}},
2814+
{"uint13", "INT(13) UNSIGNED NOT NULL", "UNSIGNED INT", scanTypeUint32, false, 0, 0, [3]string{"0", "1337", "42"}, [3]interface{}{uint32(0), uint32(1337), uint32(42)}},
28152815
{"float", "FLOAT NOT NULL", "FLOAT", scanTypeFloat32, false, math.MaxInt64, math.MaxInt64, [3]string{"0", "42", "13.37"}, [3]interface{}{float32(0), float32(42), float32(13.37)}},
28162816
{"floatnull", "FLOAT", "FLOAT", scanTypeNullFloat, true, math.MaxInt64, math.MaxInt64, [3]string{"0", "NULL", "13.37"}, [3]interface{}{nf0, nfNULL, nf1337}},
28172817
{"float74null", "FLOAT(7,4)", "FLOAT", scanTypeNullFloat, true, math.MaxInt64, 4, [3]string{"0", "NULL", "13.37"}, [3]interface{}{nf0, nfNULL, nf1337}},

fields.go

+12
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,19 @@ func (mf *mysqlField) typeDatabaseName() string {
4141
case fieldTypeJSON:
4242
return "JSON"
4343
case fieldTypeLong:
44+
if mf.flags&flagUnsigned != 0 {
45+
return "UNSIGNED INT"
46+
}
4447
return "INT"
4548
case fieldTypeLongBLOB:
4649
if mf.charSet != collations[binaryCollation] {
4750
return "LONGTEXT"
4851
}
4952
return "LONGBLOB"
5053
case fieldTypeLongLong:
54+
if mf.flags&flagUnsigned != 0 {
55+
return "UNSIGNED BIGINT"
56+
}
5157
return "BIGINT"
5258
case fieldTypeMediumBLOB:
5359
if mf.charSet != collations[binaryCollation] {
@@ -63,6 +69,9 @@ func (mf *mysqlField) typeDatabaseName() string {
6369
case fieldTypeSet:
6470
return "SET"
6571
case fieldTypeShort:
72+
if mf.flags&flagUnsigned != 0 {
73+
return "UNSIGNED SMALLINT"
74+
}
6675
return "SMALLINT"
6776
case fieldTypeString:
6877
if mf.charSet == collations[binaryCollation] {
@@ -74,6 +83,9 @@ func (mf *mysqlField) typeDatabaseName() string {
7483
case fieldTypeTimestamp:
7584
return "TIMESTAMP"
7685
case fieldTypeTiny:
86+
if mf.flags&flagUnsigned != 0 {
87+
return "UNSIGNED TINYINT"
88+
}
7789
return "TINYINT"
7890
case fieldTypeTinyBLOB:
7991
if mf.charSet != collations[binaryCollation] {

0 commit comments

Comments
 (0)