Skip to content

Commit

Permalink
feat: add support for setting nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
vabshere committed Jul 15, 2020
1 parent 5672191 commit a2832f1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Dialect interface {
HasColumn(tableName string, columnName string) bool
// ModifyColumn modify column's type
ModifyColumn(tableName string, columnName string, typ string) error
// Nullable sets column's null constraint
Nullable(tableName string, columnName string, colType string, isNull bool) error

// LimitAndOffsetSQL return generated SQL with Limit and Offset, as mssql has special case
LimitAndOffsetSQL(limit, offset interface{}) string
Expand Down
10 changes: 10 additions & 0 deletions dialect_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ func (s commonDialect) ModifyColumn(tableName string, columnName string, typ str
return err
}

func (s commonDialect) Nullable(tableName string, columnName string, colType string, isNull bool) error {
var err error
if isNull {
_, err = s.db.Exec(fmt.Sprintf("ALTER TABLE %v MODIFY %v %v NULL", tableName, columnName, colType))
} else {
_, err = s.db.Exec(fmt.Sprintf("ALTER TABLE %v MODIFY %v %v NOT NULL", tableName, columnName, colType))
}
return err
}

func (s commonDialect) CurrentDatabase() (name string) {
s.db.QueryRow("SELECT DATABASE()").Scan(&name)
return
Expand Down
10 changes: 10 additions & 0 deletions dialect_oci8.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ func (o *oci8) HasColumn(tableName string, columnName string) bool {
return count > 0
}

func (s *oci8) Nullable(tableName string, columnName string, colType string, isNull bool) error {
var err error
if isNull {
_, err = s.db.Exec(fmt.Sprintf("ALTER TABLE %v MODIFY %v NULL", tableName, columnName))
} else {
_, err = s.db.Exec(fmt.Sprintf("ALTER TABLE %v MODIFY %v NOT NULL", tableName, columnName))
}
return err
}


func (*oci8) buildSha(str string) string {
if utf8.RuneCountInString(str) <= 30 {
Expand Down
10 changes: 10 additions & 0 deletions dialect_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ func (s postgres) HasColumn(tableName string, columnName string) bool {
return count > 0
}

func (s postgres) Nullable(tableName string, columnName string, colType string, isNull bool) error {
var err error
if isNull {
_, err = s.db.Exec(fmt.Sprintf("ALTER TABLE %v ALTER COLUMN %v DROP NOT NULL", tableName, columnName))
} else {
_, err = s.db.Exec(fmt.Sprintf("ALTER TABLE %v ALTER COLUMN %v SET NOT NULL", tableName, columnName))
}
return err
}

func (s postgres) CurrentDatabase() (name string) {
s.db.QueryRow("SELECT CURRENT_DATABASE()").Scan(&name)
return
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,13 @@ func (s *DB) ModifyColumn(column string, typ string) *DB {
return scope.db
}

// Nullable sets column's null constraint
func (s *DB) Nullable(column string, isNull bool) *DB {
scope := s.NewScope(s.Value)
scope.nullable(column, isNull)
return scope.db
}

// DropColumn drop a column
func (s *DB) DropColumn(column string) *DB {
scope := s.NewScope(s.Value)
Expand Down
9 changes: 9 additions & 0 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,15 @@ func (scope *Scope) modifyColumn(column string, typ string) {
scope.db.AddError(scope.Dialect().ModifyColumn(scope.QuotedTableName(), scope.Quote(column), typ))
}

func (scope *Scope) nullable(column string, isNull bool) {
colField, ok := scope.FieldByName(column)
if !ok {
scope.db.AddError(errors.New("No such column found"))
}
colType := scope.Dialect().DataTypeOf(colField.StructField)
scope.db.AddError(scope.Dialect().Nullable(scope.QuotedTableName(), scope.Quote(column), colType, isNull))
}

func (scope *Scope) dropColumn(column string) {
scope.Raw(fmt.Sprintf("ALTER TABLE %v DROP COLUMN %v", scope.QuotedTableName(), scope.Quote(column))).Exec()
}
Expand Down

0 comments on commit a2832f1

Please sign in to comment.