docs: add some comment
This commit is contained in:
		
							parent
							
								
									c6511a0dc6
								
							
						
					
					
						commit
						4d291f68b9
					
				
							
								
								
									
										24
									
								
								model.go
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								model.go
									
									
									
									
									
								
							@ -14,9 +14,27 @@ type Model struct {
 | 
			
		||||
	DeletedAt DeletedAt `gorm:"index"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UnDelete clear model's deleted info, you need save this model manually
 | 
			
		||||
func (m *Model) UnDelete() {
 | 
			
		||||
	m.DeletedAt.Valid = false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ModelSupportUnique model that support soft delete and unique index
 | 
			
		||||
//
 | 
			
		||||
// For example
 | 
			
		||||
//
 | 
			
		||||
// when you annouce an unique index for column A,
 | 
			
		||||
// gorm will automate create a composite index for A & deleted_flag.
 | 
			
		||||
//
 | 
			
		||||
// create: deleted_flag default to 0
 | 
			
		||||
// delete: set deleted_flag's value to primary id to avoid unique conflict
 | 
			
		||||
type ModelSupportUnique struct {
 | 
			
		||||
	ID          uint `gorm:"primarykey"`
 | 
			
		||||
	CreatedAt   time.Time
 | 
			
		||||
	UpdatedAt   time.Time
 | 
			
		||||
	Model
 | 
			
		||||
	DeletedFlag DeletedFlag `gorm:"type:BIGINT UNSIGNED NOT NULL DEFAULT 0" json:"deleted_flag"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UnDelete clear model's deleted info, you need save this model manually
 | 
			
		||||
func (m *ModelSupportUnique) UnDelete() {
 | 
			
		||||
	m.Model.UnDelete()
 | 
			
		||||
	m.DeletedFlag = 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -65,7 +65,8 @@ func (schema *Schema) ParseIndexes() map[string]Index {
 | 
			
		||||
 | 
			
		||||
						if !exists {
 | 
			
		||||
							idx.Fields = append(idx.Fields, IndexOption{
 | 
			
		||||
								Field: df,
 | 
			
		||||
								Field:    df,
 | 
			
		||||
								priority: 100,
 | 
			
		||||
							})
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
@ -65,10 +65,11 @@ func (sd SoftDeleteUniqueDeleteClause) Build(clause.Builder) {
 | 
			
		||||
func (sd SoftDeleteUniqueDeleteClause) MergeClause(*clause.Clause) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var regReplaceUpdate = regexp.MustCompile(`UPDATE (.+?) WHERE `)
 | 
			
		||||
 | 
			
		||||
func (sd SoftDeleteUniqueDeleteClause) ModifyStatement(stmt *Statement) {
 | 
			
		||||
	re := regexp.MustCompile(`UPDATE (.*) WHERE `)
 | 
			
		||||
	if sql := stmt.SQL.String(); sql != "" {
 | 
			
		||||
		setClause := re.FindStringSubmatch(sql)[1]
 | 
			
		||||
		setClause := regReplaceUpdate.FindStringSubmatch(sql)[1]
 | 
			
		||||
		if setClause == "" {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user