sqlserver icon indicating copy to clipboard operation
sqlserver copied to clipboard

Mapping of int8 and int16 to SQL Server types is not optimal

Open Yunaido opened this issue 2 years ago • 0 comments

Hello,

I've noticed an issue with the way GORM maps Go's int8 and int16 types to SQL Server types. In the current implementation, int8 is mapped to smallint and int16 is mapped to int.

https://github.com/go-gorm/sqlserver/blob/b8d91cbd0b9b9d9a3538cf0d9f7f01ac2fcf006d/sqlserver.go#L188-L202

This mapping seems odd because int8 in Go is an 8-bit integer, and SQL Server has a matching tinyint type which is also 8 bits. Mapping int8 to smallint (which is 16 bits) seems unnecessary.

Similarly, int16 in Go is a 16-bit integer, but it's being mapped to int in SQL Server, which is a 32-bit integer. SQL Server's smallint would be a better match for int16 because it's also 16 bits.

I propose that the mapping should be changed as follows:

int8 in Go should map to tinyint in SQL Server int16 in Go should map to smallint in SQL Server

switch {
case field.Size <= 8:
  sqlType = "tinyint"
case field.Size <= 16:
  sqlType = "smallint"
case field.Size <= 32:
  sqlType = "int"
default:
  sqlType = "bigint"
}

This would make the type mapping more intuitive and efficient, and it would prevent unnecessary widening of the integer types.

Please let me know if you need any additional information about this issue.

Thank you for your consideration.

Yunaido avatar Nov 10 '23 17:11 Yunaido