CengSin / oracle

GORM oracle driver
Other
114 stars 42 forks source link

hasTable, hasIndex etc.. #2

Closed bulenttokuzlu closed 3 years ago

bulenttokuzlu commented 3 years ago

Thx for the effort.

You need to add Toupper to hasTable, etc... In Oracle table names are always upper case.

Thx.

CengSin commented 3 years ago

GORM allows users to change the naming conventions by overriding the default NamingStrategy which need to implements interface Namer

CengSin commented 3 years ago

or you can implement Tabler.TableName() method ,So far I have no better solution. It is not a good way to modify HasTable directly.

bulenttokuzlu commented 3 years ago

I have found my way out. Thx for the help

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) }