Git Product home page Git Product logo

gorm-oracle's Issues

使用 gorm.io/gen 创建数据时无法获取自增主键返回值(insert 语句主键 id 获取不到)

INSERT INTO doc_compare_user (name,account,password,status,ctm,utm,type,admin_id,note,expire_time,deleted,source_id,sso_user_unique_id) VALUES ('test','test','d9d7392be643cc709bbcb403e441b163',1,'2024-01-17 18:08:00.598','2024-01-17 18:08:00.598',0,20230831,'','2121-01-01 00:00:00',-1,-1,'3f752d69-bf9b-4f2f-8f4f-d34d9a201bfc') /*--*/RETURNING id INTO '{{} 0xc001688a90 false}' /*-sql.Out{}-*/

我的主键是int32 , insertTo.Kind() = 22, 是指针类型

func getDefaultValues(db *gorm.DB, idx int) {
	insertTo := db.Statement.ReflectValue
	switch insertTo.Kind() {
	case reflect.Slice, reflect.Array:
		insertTo = insertTo.Index(idx)
	default:
	}

	for _, val := range db.Statement.Vars {
		switch v := val.(type) {
		case sql.Out:
			switch insertTo.Kind() {
			case reflect.Slice, reflect.Array:
				for i := insertTo.Len() - 1; i >= 0; i-- {
					rv := insertTo.Index(i)
					if reflect.Indirect(rv).Kind() != reflect.Struct {
						break
					}

					_, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(db.Statement.Context, rv)
					if isZero {
						_ = db.AddError(db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.Context, rv, v.Dest))
					}
				}
			case reflect.Struct:
				_, isZero := db.Statement.Schema.PrioritizedPrimaryField.ValueOf(db.Statement.Context, insertTo)
				if isZero {
					_ = db.AddError(db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.Context, insertTo, v.Dest))
				}
			default:
                               
                                 // 会跳到这里 insertTo.Kind()  = 22, 是指针类型
				}
			}
		default:
		}
	}
}

企业微信截图_17054870027306

分页问题

table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&items).Offset(-1).Limit(-1).Count(&countTmp),这种写法在mysql,sqlserver都可以 ,oracle只返回前10条,不管pageIndex是多少,请问有其他写法嘛针对oracle

在gorm的自定义类型`JSONMap` 时,在已生成表的情况下,再次运行数据库迁移报`ORA-22859: 无效的列修改`

Your Question 你的问题

在gorm的自定义类型JSONMap 时,数据库迁移报ORA-22859: 无效的列修改,
https://github.com/go-gorm/datatypes/blob/master/json_map.go 中加入 oracle 的类型:

// GormDBDataType gorm db data type
func (JSONMap) GormDBDataType(db *gorm.DB, field *schema.Field) string {
	switch db.Dialector.Name() {
	case "sqlite":
		return "JSON"
	case "mysql":
		return "JSON"
	case "postgres":
		return "JSONB"
	case "sqlserver":
		return "NVARCHAR(MAX)"
	case "oracle": // 添加 oracle 类型
		return "BLOB"
	}
	return ""
}

使用时:

type User struct {
	types.BaseModel
	Extras       datatypes.JSONMap `gorm:"check:extras IS JSON"`
}

第一次运行成功生成表,再次运行会 alter table user modify EXTRAS BLOB, 报ORA-22859: 无效的列修改

Expected answer 预期答案

在第一次自动生成表后,再次运行数据库迁移成功

列名大小写问题

查询在结构体中column需要指定为全大写才能查询到数据,全小写则不可以

结构体中如果存在保留字段,默认会多加一对双引号,导致数据库自动创建表失败。

Description 问题说明

结构体中如果存在保留字段,默认会多加一对双引号,导致数据库自动创建表失败。

Code 示例代码

type P struct {
Sort int 
}

CreateTable() 时,会被多加一对双引号,变为 “”Sort“”,导致表创建失败

func (m Migrator) CreateTable(values ...interface{}) (err error) {
	for _, value := range values {
		// _ = m.TryQuotifyReservedWords(value) // 注释掉这段,能够成功创建表
		_ = m.TryRemoveOnUpdate(value)
	}
   ...
}

Oracle11 以上版本数据库查询数据使用 Limit(-1) 时也会设置默认排序字段

问题描述

Oracle11 以上版本数据库查询数据使用 Limit(-1) 时也会设置默认排序字段

问题原因

oracle.go#L273 没有判断 Limit 的值是否大于 0

gorm-oracle/oracle.go

Lines 270 to 298 in d105a1d

func (d Dialector) RewriteLimit(c clause.Clause, builder clause.Builder) {
if limit, ok := c.Expression.(clause.Limit); ok {
if stmt, ok := builder.(*gorm.Statement); ok {
if _, ok := stmt.Clauses["ORDER BY"]; !ok {
s := stmt.Schema
_, _ = builder.WriteString("ORDER BY ")
if s != nil && s.PrioritizedPrimaryField != nil {
builder.WriteQuoted(s.PrioritizedPrimaryField.DBName)
_ = builder.WriteByte(' ')
} else {
_, _ = builder.WriteString("(SELECT NULL FROM ")
_, _ = builder.WriteString(d.DummyTableName())
_, _ = builder.WriteString(")")
}
}
}
if offset := limit.Offset; offset > 0 {
_, _ = builder.WriteString(" OFFSET ")
_, _ = builder.WriteString(strconv.Itoa(offset))
_, _ = builder.WriteString(" ROWS")
}
if limit := limit.Limit; limit != nil && *limit >= 0 {
_, _ = builder.WriteString(" FETCH NEXT ")
_, _ = builder.WriteString(strconv.Itoa(*limit))
_, _ = builder.WriteString(" ROWS ONLY")
}
}
}

Oracle stored procedure OUT Variables

Hi,
I am getting error numeric or value error: character string buffer too small when trying to map output variables of oracle stored procedure to go variables, kindly assist.

result := database.DB.Exec(`BEGIN
    ProcedureName(
        @amount,
        '010',
        @sourceUser,
        @UPN, @IssueDate, @Errno, @Errtxt);
    END;`,
		sql.Named("amount", amount),
		sql.Named("sourceUser", sourceUser),
		sql.Named("UPN", sql.Out{Dest: &outputs.UPN}),
		sql.Named("IssueDate", sql.Out{Dest: &outputs.UPN}),
		sql.Named("Errno", sql.Out{Dest: &outputs.UPN}),
		sql.Named("Errtxt", sql.Out{Dest: &outputs.UPN}))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.