Skip to content

Commit

Permalink
refactor: optimize the mapping relationship for integer data types
Browse files Browse the repository at this point in the history
  • Loading branch information
iTanken committed Mar 18, 2024
1 parent 4b596bc commit e01b9ff
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (dialector Dialector) ClauseBuilders() map[string]clause.ClauseBuilder {
stmt.WriteString("OUTPUT ")

if len(returning.Columns) > 0 {
columns := []clause.Column{}
var columns []clause.Column
for _, column := range returning.Columns {
column.Table = outputTable
columns = append(columns, column)
Expand Down Expand Up @@ -187,13 +187,24 @@ func (dialector Dialector) DataTypeOf(field *schema.Field) string {
return "bit"
case schema.Int, schema.Uint:
var sqlType string
switch {
case field.Size < 16:
sqlType = "smallint"
case field.Size < 31:
sqlType = "int"
default:
sqlType = "bigint"
if field.DataType == schema.Int {
switch {
case field.Size <= 16:
sqlType = "smallint"
case field.Size <= 32:
sqlType = "int"
default:
sqlType = "bigint"
}
} else {
switch {
case field.Size <= 8:
sqlType = "tinyint"
case field.Size <= 16:
sqlType = "int"
default:
sqlType = "bigint"
}
}

if field.AutoIncrement {
Expand Down Expand Up @@ -234,12 +245,12 @@ func (dialector Dialector) DataTypeOf(field *schema.Field) string {
return string(field.DataType)
}

func (dialectopr Dialector) SavePoint(tx *gorm.DB, name string) error {
func (Dialector) SavePoint(tx *gorm.DB, name string) error {
tx.Exec("SAVE TRANSACTION " + name)
return nil
}

func (dialectopr Dialector) RollbackTo(tx *gorm.DB, name string) error {
func (Dialector) RollbackTo(tx *gorm.DB, name string) error {
tx.Exec("ROLLBACK TRANSACTION " + name)
return nil
}

0 comments on commit e01b9ff

Please sign in to comment.