Skip to content

Commit

Permalink
💡 更新完善代码注释
Browse files Browse the repository at this point in the history
  • Loading branch information
iTanken committed Sep 13, 2024
1 parent 2069995 commit 6a18ba8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
23 changes: 22 additions & 1 deletion migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"gorm.io/gorm/schema"
)

// Migrator implement gorm migrator interface
type Migrator struct {
migrator.Migrator
}
Expand Down Expand Up @@ -77,6 +78,7 @@ func (m Migrator) FullDataTypeOf(field *schema.Field) (expr clause.Expr) {
return
}

// CurrentDatabase returns current database name
func (m Migrator) CurrentDatabase() (name string) {
_ = m.DB.Raw(
fmt.Sprintf(`SELECT ORA_DATABASE_NAME as "Current Database" FROM %s`, m.Dialector.(Dialector).DummyTableName()),
Expand Down Expand Up @@ -109,6 +111,7 @@ func (m Migrator) GetTypeAliases(databaseTypeName string) (types []string) {
return
}

// CreateTable create table in database for values
func (m Migrator) CreateTable(values ...interface{}) (err error) {
ignoreCase := !m.Dialector.(Dialector).NamingCaseSensitive
for _, value := range values {
Expand Down Expand Up @@ -150,6 +153,8 @@ func (m Migrator) setCommentForColumn(field *schema.Field, stmt *gorm.Statement)
return
}

// DropTable drop table for values
//
//goland:noinspection SqlNoDataSourceInspection
func (m Migrator) DropTable(values ...interface{}) error {
values = m.ReorderModels(values, false)
Expand All @@ -167,6 +172,7 @@ func (m Migrator) DropTable(values ...interface{}) error {
return nil
}

// HasTable returns table exists or not for value, value could be a struct or string
func (m Migrator) HasTable(value interface{}) bool {
var count int64

Expand Down Expand Up @@ -235,6 +241,7 @@ func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {
return columnTypes, execErr
}

// RenameTable rename table from oldName to newName
func (m Migrator) RenameTable(oldName, newName interface{}) (err error) {
resolveTable := func(name interface{}) (result string, err error) {
if v, ok := name.(string); ok {
Expand Down Expand Up @@ -268,6 +275,7 @@ func (m Migrator) RenameTable(oldName, newName interface{}) (err error) {
).Error
}

// GetTables returns tables under the current user database
func (m Migrator) GetTables() (tableList []string, err error) {
err = m.DB.Raw(`SELECT TABLE_NAME FROM USER_TABLES
WHERE TABLESPACE_NAME IS NOT NULL AND TABLESPACE_NAME <> 'SYSAUX'
Expand All @@ -294,10 +302,13 @@ func (m Migrator) AddColumn(value interface{}, name string) (err error) {
return
}

// DropColumn drop value's "name" column
func (m Migrator) DropColumn(value interface{}, name string) error {
return m.Migrator.DropColumn(value, name)
}

// AlterColumn alter value's "field" column's type based on schema definition
//
//goland:noinspection SqlNoDataSourceInspection
func (m Migrator) AlterColumn(value interface{}, field string) error {
if !m.HasColumn(value, field) {
Expand All @@ -318,6 +329,7 @@ func (m Migrator) AlterColumn(value interface{}, field string) error {
})
}

// HasColumn check has column "field" for value or not
func (m Migrator) HasColumn(value interface{}, field string) bool {
var count int64
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
Expand Down Expand Up @@ -387,11 +399,14 @@ func (m Migrator) AlterDataTypeOf(stmt *gorm.Statement, field *schema.Field) (ex
return
}

// CreateConstraint create constraint
func (m Migrator) CreateConstraint(value interface{}, name string) error {
_ = m.TryRemoveOnUpdate(value)
return m.Migrator.CreateConstraint(value, name)
}

// DropConstraint drop constraint
//
//goland:noinspection SqlNoDataSourceInspection
func (m Migrator) DropConstraint(value interface{}, name string) error {
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
Expand All @@ -412,6 +427,7 @@ func (m Migrator) DropConstraint(value interface{}, name string) error {
})
}

// HasConstraint check has constraint or not
func (m Migrator) HasConstraint(value interface{}, name string) bool {
var count int64
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
Expand All @@ -421,6 +437,7 @@ func (m Migrator) HasConstraint(value interface{}, name string) bool {
}) == nil && count > 0
}

// DropIndex drop index "name"
func (m Migrator) DropIndex(value interface{}, name string) error {
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
if idx := stmt.Schema.LookIndex(name); idx != nil {
Expand All @@ -432,6 +449,7 @@ func (m Migrator) DropIndex(value interface{}, name string) error {
})
}

// HasIndex check has index "name" or not
func (m Migrator) HasIndex(value interface{}, name string) bool {
var count int64
_ = m.RunWithValue(value, func(stmt *gorm.Statement) error {
Expand All @@ -449,7 +467,10 @@ func (m Migrator) HasIndex(value interface{}, name string) bool {
return count > 0
}

// RenameIndex https://docs.oracle.com/database/121/SPATL/alter-index-rename.htm
// RenameIndex rename index from oldName to newName
//
// see also:
// https://docs.oracle.com/database/121/SPATL/alter-index-rename.htm
func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error {
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
return m.DB.Exec(
Expand Down
23 changes: 17 additions & 6 deletions namer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"gorm.io/gorm/schema"
)

// Namer implement gorm schema namer interface
type Namer struct {
// NamingStrategy use custom naming strategy in gorm.Config on initialize
NamingStrategy schema.Namer

CaseSensitive bool // whether naming is case-sensitive
// CaseSensitive determines whether naming is case-sensitive
CaseSensitive bool
}

// Deprecated: As of v1.5.0, use the Namer.ConvertNameToFormat instead.
Expand All @@ -19,41 +21,50 @@ func ConvertNameToFormat(x string) string {
return (Namer{}).ConvertNameToFormat(x)
}

// ConvertNameToFormat return appropriate capitalization name based on CaseSensitive
func (n Namer) ConvertNameToFormat(x string) string {
if n.CaseSensitive {
return x
}
return strings.ToUpper(x)
}

// TableName convert string to table name
func (n Namer) TableName(table string) (name string) {
return n.ConvertNameToFormat(n.NamingStrategy.TableName(table))
}

// SchemaName generate schema name from table name, don't guarantee it is the reverse value of TableName
func (n Namer) SchemaName(table string) string {
return n.ConvertNameToFormat(n.NamingStrategy.SchemaName(table))
}

// ColumnName convert string to column name
func (n Namer) ColumnName(table, column string) (name string) {
return n.ConvertNameToFormat(n.NamingStrategy.ColumnName(table, column))
}

// JoinTableName convert string to join table name
func (n Namer) JoinTableName(table string) (name string) {
return n.ConvertNameToFormat(n.NamingStrategy.JoinTableName(table))
}

// RelationshipFKName generate fk name for relation
func (n Namer) RelationshipFKName(relationship schema.Relationship) (name string) {
return n.ConvertNameToFormat(n.NamingStrategy.RelationshipFKName(relationship))
}

// CheckerName generate checker name
func (n Namer) CheckerName(table, column string) (name string) {
return n.ConvertNameToFormat(n.NamingStrategy.CheckerName(table, column))
}

// IndexName generate index name
func (n Namer) IndexName(table, column string) (name string) {
return n.ConvertNameToFormat(n.NamingStrategy.IndexName(table, column))
}

func (n Namer) SchemaName(table string) string {
return n.ConvertNameToFormat(n.NamingStrategy.SchemaName(table))
}

// UniqueName generate unique constraint name
func (n Namer) UniqueName(table, column string) string {
return n.ConvertNameToFormat(n.NamingStrategy.UniqueName(table, column))
}
1 change: 1 addition & 0 deletions oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Config struct {
RowNumberAliasForOracle11 string
}

// Dialector implement GORM database dialector
type Dialector struct {
*Config
}
Expand Down

0 comments on commit 6a18ba8

Please sign in to comment.