Skip to content

Commit

Permalink
🩹 修复 GORM 模型字段标签包含默认值时创建数据报 ORA-03146 错误的问题
Browse files Browse the repository at this point in the history
ORA-03146: TTC 字段的缓冲区长度无效

> [!Warning]
> 注意:创建数据返回参数类型由 `sql.Out` 改为了 `go_ora.Out`!

Ref: [#13](#13 (comment))
  • Loading branch information
iTanken committed Aug 28, 2024
1 parent cce56ee commit 8b0053f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
12 changes: 8 additions & 4 deletions create.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package oracle

import (
"database/sql"
"reflect"

"github.com/sijms/go-ora/v2"
Expand Down Expand Up @@ -76,9 +75,14 @@ func Create(db *gorm.DB) {
if idx > 0 {
_ = stmt.WriteByte(',')
}
stmt.AddVar(stmt, sql.Out{Dest: reflect.New(field.FieldType).Interface()})

outVar := go_ora.Out{Dest: reflect.New(field.FieldType).Interface()}
if field.Size > 0 {
outVar.Size = field.Size
}
stmt.AddVar(stmt, outVar)
}
_, _ = stmt.WriteString(" /*-sql.Out{}-*/")
_, _ = stmt.WriteString(" /*-go_ora.Out{}-*/")
}
}

Expand Down Expand Up @@ -229,7 +233,7 @@ func getDefaultValues(db *gorm.DB, idx int) {

for _, val := range db.Statement.Vars {
switch v := val.(type) {
case sql.Out:
case go_ora.Out:
switch insertTo.Kind() {
case reflect.Slice, reflect.Array:
for i := insertTo.Len() - 1; i >= 0; i-- {
Expand Down
53 changes: 53 additions & 0 deletions create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,56 @@ func TestMergeCreateUnique(t *testing.T) {
t.Logf("result: %s", dataJsonBytes)
})
}

type testModelOra03146TTC struct {
Id int64 `gorm:"primaryKey;autoIncrement:false;column:SL_ID;type:uint;size:20;default:0;comment:id" json:"SL_ID"`
ApiName string `gorm:"column:SL_API_NAME;type:VARCHAR2;size:100;default:null;comment:接口名称" json:"SL_API_NAME"`
RawReceive string `gorm:"column:SL_RAW_RECEIVE_JSON;type:VARCHAR2;size:4000;default:null;comment:原始请求参数" json:"SL_RAW_RECEIVE_JSON"`
RawSend string `gorm:"column:SL_RAW_SEND_JSON;type:VARCHAR2;size:4000;default:null;comment:原始响应参数" json:"SL_RAW_SEND_JSON"`
DealReceive string `gorm:"column:SL_DEAL_RECEIVE_JSON;type:VARCHAR2;size:4000;default:null;comment:处理请求参数" json:"SL_DEAL_RECEIVE_JSON"`
DealSend string `gorm:"column:SL_DEAL_SEND_JSON;type:VARCHAR2;size:4000;default:null;comment:处理响应参数" json:"SL_DEAL_SEND_JSON"`
Code string `gorm:"column:SL_CODE;type:VARCHAR2;size:16;default:null;comment:http状态" json:"SL_CODE"`
CreatedTime time.Time `gorm:"column:SL_CREATED_TIME;type:date;default:null;comment:创建时间" json:"SL_CREATED_TIME"`
}

func TestOra03146TTC(t *testing.T) {
db, err := dbNamingCase, dbErrors[0]
if err != nil {
t.Fatal(err)
}
if db == nil {
t.Log("db is nil!")
return
}

model := testModelOra03146TTC{}
migrator := db.Set("gorm:table_comments", "TTC 字段的缓冲区长度无效问题测试表").Migrator()
if migrator.HasTable(model) {
if err = migrator.DropTable(model); err != nil {
t.Fatalf("DropTable() error = %v", err)
}
}
if err = migrator.AutoMigrate(model); err != nil {
t.Fatalf("AutoMigrate() error = %v", err)
} else {
t.Log("AutoMigrate() success!")
}

// INSERT INTO "T100_SCPTOAPI_LOG" ("SL_ID","SL_API_NAME","SL_RAW_RECEIVE_JSON","SL_RAW_SEND_JSON","SL_DEAL_RECEIVE_JSON","SL_DEAL_SEND_JSON","SL_CODE","SL_CREATED_TIME")
// VALUES (9578529926701056,'/v1/t100/packingNum','11111','11111','11111','11111','111','2024-08-27 18:21:39.495')
data := testModelOra03146TTC{
Id: 9578529926701056,
ApiName: "/v1/t100/packingNum",
RawReceive: "11111",
RawSend: "11111",
DealReceive: "11111",
DealSend: "11111",
Code: "111",
CreatedTime: time.Now(),
}
result := db.Create(&data)
if err = result.Error; err != nil {
t.Fatalf("执行失败:%v", err)
}
t.Log("执行成功,影响行数:", result.RowsAffected)
}

0 comments on commit 8b0053f

Please sign in to comment.