Skip to content

Commit

Permalink
⬆️ 更新达梦官方驱动版本 v20240326
Browse files Browse the repository at this point in the history
支持在连接串上直接配置动态服务名,使用示例:
dm://user:password@GroupName?GroupName=(host1:port1,host2:port2,...)

Signed-off-by: liutianqi <[email protected]>
  • Loading branch information
iTanken committed Apr 29, 2024
1 parent 5683375 commit 697dab3
Show file tree
Hide file tree
Showing 40 changed files with 4,587 additions and 3,974 deletions.
6 changes: 4 additions & 2 deletions create.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ func Create(db *gorm.DB) {
_, isZero := field.ValueOf(db.Statement.Context, db.Statement.ReflectValue)
setIdentityInsert = !isZero
case reflect.Slice, reflect.Array:
for i := 0; i < db.Statement.ReflectValue.Len(); i++ {
for i := 0; i < db.Statement.ReflectValue.Len(); {
obj := db.Statement.ReflectValue.Index(i)
if reflect.Indirect(obj).Kind() == reflect.Struct {
_, isZero := field.ValueOf(db.Statement.Context, db.Statement.ReflectValue.Index(i))
setIdentityInsert = !isZero
}
break
}
default:
}

if setIdentityInsert && !db.DryRun && db.Error == nil {
Expand Down Expand Up @@ -202,6 +203,7 @@ func Create(db *gorm.DB) {
if isZero {
_ = db.AddError(db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.Context, db.Statement.ReflectValue, insertID))
}
default:
}
}
}
Expand All @@ -221,7 +223,7 @@ func MergeCreate(db *gorm.DB, onConflict clause.OnConflict, values clause.Values
_, _ = db.Statement.WriteString(" FROM DUAL")
}

_, _ = db.Statement.WriteString(`) AS"excluded" (`)
_, _ = db.Statement.WriteString(`) AS "excluded" (`)
for idx, column := range values.Columns {
if idx > 0 {
_ = db.Statement.WriteByte(',')
Expand Down
33 changes: 24 additions & 9 deletions dm.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// 本方言包基于gorm v1.24.2开发,需要配合达梦数据库驱动使用

package dameng

import (
"database/sql"
"fmt"
"strings"

_ "github.com/godoes/gorm-dameng/dm8" // 引入dm数据库驱动包
"gorm.io/gorm" // 引入gorm v2包
Expand Down Expand Up @@ -35,6 +38,8 @@ var (
UpdateClauses = []string{"UPDATE", "SET", "WHERE", "ORDER BY", "LIMIT"}
// DeleteClauses delete clauses
DeleteClauses = []string{"DELETE", "FROM", "WHERE", "ORDER BY", "LIMIT"}

defaultDatetimePrecision = 3
)

func Open(dsn string) gorm.Dialector {
Expand Down Expand Up @@ -167,7 +172,7 @@ func (d Dialector) DataTypeOf(field *schema.Field) string {
case schema.Bytes:
return d.getSchemaBytesType(field)
default:
return string(field.DataType)
return d.getSchemaCustomType(field)
// what oracle do:
//notNull, _ := field.TagSettings["NOT NULL"]
//unique, _ := field.TagSettings["UNIQUE"]
Expand Down Expand Up @@ -230,19 +235,19 @@ func (d Dialector) getSchemaStringType(field *schema.Field) string {
}
}

if size > 0 && size < 32768 {
// VARCHAR 可以指定一个不超过 32767 的正整数作为字节或字符长度
if d.VarcharSizeIsCharLength {
return fmt.Sprintf("VARCHAR(%d CHAR)", size) // 字符长度(size * 4)
}
return fmt.Sprintf("VARCHAR(%d)", size) // 字节长度
} else if size == 0 {
if size == 0 {
if d.VarcharSizeIsCharLength {
return "VARCHAR(8188 CHAR)" // 字符长度(8188 * 4)
}
return "VARCHAR" // 如果未指定长度,缺省为 8188 字节
} else {
} else if size < 0 || size >= 32768 {
return "CLOB" // 长度超过 32767,使用 CLOB(TEXT)
} else {
// VARCHAR 可以指定一个不超过 32767 的正整数作为字节或字符长度
if d.VarcharSizeIsCharLength {
return fmt.Sprintf("VARCHAR(%d CHAR)", size) // 字符长度(size * 4)
}
return fmt.Sprintf("VARCHAR(%d)", size) // 字节长度
}
}

Expand All @@ -262,6 +267,16 @@ func (d Dialector) getSchemaBytesType(field *schema.Field) string {
return "BLOB"
}

func (d Dialector) getSchemaCustomType(field *schema.Field) string {
sqlType := string(field.DataType)

if field.AutoIncrement && !strings.Contains(strings.ToLower(sqlType), " auto_increment") && !strings.Contains(strings.ToLower(sqlType), " identity") {
sqlType += " IDENTITY(1,1)"
}

return sqlType
}

func (d Dialector) SavePoint(tx *gorm.DB, name string) error {
return tx.Exec("SAVEPOINT " + name).Error
}
Expand Down
4 changes: 4 additions & 0 deletions dm8/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

*你可以在文件p.go中的发版标记里找到当前驱动的svn号

## svn 16752
支持在连接串上直接配置动态服务名,使用示例:
dm://user:password@GroupName?GroupName=(host1:port1,host2:port2,...)

## svn 16505
新增连接串属性driverReconnect,配合doSwitch=1或2使用,表示连接重连是否使用驱动自身的重连机制,否则在连接失效时返回sql标准错误driver.ErrBadConn,由go来处理重连

Expand Down
6 changes: 3 additions & 3 deletions dm8/VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#8.1.3.12
#2023.04.17
#16532
#8.1.3.140
#2024.03.20
#22766
Loading

0 comments on commit 697dab3

Please sign in to comment.