CengSin / oracle

GORM oracle driver
Other
114 stars 42 forks source link

Do not Fetch Varchar2 #3

Closed bulenttokuzlu closed 3 years ago

bulenttokuzlu commented 3 years ago

Hi,

Thx for the effort.

I cannot fetch Varchar2 fields, dates, integers and booleans are all OK, but varchar2 fields are missing.

How can we fix it?

Thx

bulenttokuzlu commented 3 years ago

Rewrite the NamingStrategy works here as well.

package utils

import ( "crypto/sha1" "fmt" "strings" "unicode/utf8"

"github.com/jinzhu/inflection"
"gorm.io/gorm/schema"

)

// OracleNamingStrategy jj type OracleNamingStrategy struct { schema.NamingStrategy }

// TableName convert string to table name func (ns OracleNamingStrategy) TableName(str string) string { if ns.SingularTable { return ns.TablePrefix + ns.toDBName(str) } return ns.TablePrefix + inflection.Plural(ns.toDBName(str)) }

// ColumnName convert string to column name func (ns OracleNamingStrategy) ColumnName(table, column string) string { return ns.toDBName(column) }

// JoinTableName convert string to join table name func (ns OracleNamingStrategy) JoinTableName(str string) string { if strings.ToLower(str) == str { return ns.TablePrefix + str }

if ns.SingularTable {
    return ns.TablePrefix + ns.toDBName(str)
}
return ns.TablePrefix + inflection.Plural(ns.toDBName(str))

}

// RelationshipFKName generate fk name for relation func (ns OracleNamingStrategy) RelationshipFKName(rel schema.Relationship) string { return strings.Replace(fmt.Sprintf("FK%s%s", rel.Schema.Table, ns.toDBName(rel.Name)), ".", "_", -1) }

// CheckerName generate checker name func (ns OracleNamingStrategy) CheckerName(table, column string) string { return strings.Replace(fmt.Sprintf("CHK%s%s", table, column), ".", "_", -1) }

// IndexName generate index name func (ns OracleNamingStrategy) IndexName(table, column string) string { myColumn := column if strings.HasPrefix(ns.toDBName(column), fmt.Sprintf("IDX%v", table)) { myColumn = column[len(fmt.Sprintf("IDX%v", table))+1:] //log.Printf("myColumn is %s\n", myColumn) }

idxName := fmt.Sprintf("IDX_%v_%v", table, ns.toDBName(myColumn))
//log.Printf("table is %s, column is %s, idxName is %s\n", table, myColumn, idxName)
idxName = strings.Replace(idxName, ".", "_", -1)

if utf8.RuneCountInString(idxName) > 64 {
    h := sha1.New()
    h.Write([]byte(idxName))
    bs := h.Sum(nil)

    idxName = fmt.Sprintf("IDX%v%v", table, column)[0:56] + string(bs)[:8]
}
return idxName

}

func (ns OracleNamingStrategy) toDBName(name string) string { return strings.ToUpper(name) }