Skip to content

Commit

Permalink
Revert "rename dbNames to columnMap"
Browse files Browse the repository at this point in the history
This reverts commit 5bdd7a3.
  • Loading branch information
calebx committed Dec 26, 2020
1 parent b2426c9 commit a72e045
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
54 changes: 27 additions & 27 deletions nested_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type nodeItem struct {
Lft int
ChildrenCount int
TableName string
ColumnMap map[string]string
DbNames map[string]string
}

// parseNode parse a gorm structure into an internal source structure
Expand All @@ -53,7 +53,7 @@ func parseNode(db *gorm.DB, source interface{}) (tx *gorm.DB, item nodeItem, err
return
}

item = nodeItem{TableName: stmt.Table, ColumnMap: map[string]string{}}
item = nodeItem{TableName: stmt.Table, DbNames: map[string]string{}}
sourceValue := reflect.Indirect(reflect.ValueOf(source))
sourceType := sourceValue.Type()
for i := 0; i < sourceType.NumField(); i++ {
Expand All @@ -66,27 +66,27 @@ func parseNode(db *gorm.DB, source interface{}) (tx *gorm.DB, item nodeItem, err
switch t.Tag.Get("nestedset") {
case "id":
item.ID = v.Int()
item.ColumnMap["id"] = dbName
item.DbNames["id"] = dbName
break
case "parent_id":
item.ParentID = v.Interface().(sql.NullInt64)
item.ColumnMap["parent_id"] = dbName
item.DbNames["parent_id"] = dbName
break
case "depth":
item.Depth = int(v.Int())
item.ColumnMap["depth"] = dbName
item.DbNames["depth"] = dbName
break
case "rgt":
item.Rgt = int(v.Int())
item.ColumnMap["rgt"] = dbName
item.DbNames["rgt"] = dbName
break
case "lft":
item.Lft = int(v.Int())
item.ColumnMap["lft"] = dbName
item.DbNames["lft"] = dbName
break
case "children_count":
item.ChildrenCount = int(v.Int())
item.ColumnMap["children_count"] = dbName
item.DbNames["children_count"] = dbName
break
case "scope":
rawVal, _ := schemaField.ValueOf(sourceValue)
Expand All @@ -110,15 +110,15 @@ func Create(db *gorm.DB, source, parent interface{}) error {

// for totally blank table / scope default init root would be [1 - 2]
setDepth, setToLft, setToRgt := 0, 1, 2
tableName, ColumnMap := target.TableName, target.ColumnMap
tableName, dbNames := target.TableName, target.DbNames

// put node into root level when parent is nil
if parent == nil {
lastNode := make(map[string]interface{})
orderSQL := formatSQL(":rgt desc", target)
rst := tx.Model(source).Select(ColumnMap["rgt"]).Order(orderSQL).First(&lastNode)
rst := tx.Model(source).Select(dbNames["rgt"]).Order(orderSQL).First(&lastNode)
if rst.Error == nil {
setToLft = lastNode[ColumnMap["rgt"]].(int) + 1
setToLft = lastNode[dbNames["rgt"]].(int) + 1
setToRgt = setToLft + 1
}
} else {
Expand All @@ -134,7 +134,7 @@ func Create(db *gorm.DB, source, parent interface{}) error {
// UPDATE tree SET rgt = rgt + 2 WHERE rgt >= new_lft;
err = tx.Table(tableName).
Where(formatSQL(":rgt >= ?", target), setToLft).
UpdateColumn(ColumnMap["rgt"], gorm.Expr(formatSQL(":rgt + 2", target))).
UpdateColumn(dbNames["rgt"], gorm.Expr(formatSQL(":rgt + 2", target))).
Error
if err != nil {
return err
Expand All @@ -144,15 +144,15 @@ func Create(db *gorm.DB, source, parent interface{}) error {
// UPDATE tree SET lft = lft + 2 WHERE lft > new_lft;
err = tx.Table(tableName).
Where(formatSQL(":lft > ?", target), setToLft).
UpdateColumn(ColumnMap["lft"], gorm.Expr(formatSQL(":lft + 2", target))).
UpdateColumn(dbNames["lft"], gorm.Expr(formatSQL(":lft + 2", target))).
Error
if err != nil {
return err
}

// UPDATE tree SET children_count = children_count + 1 WHERE id = parent.id;
err = db.Model(parent).Update(
ColumnMap["children_count"], gorm.Expr(formatSQL(":children_count + 1", target)),
dbNames["children_count"], gorm.Expr(formatSQL(":children_count + 1", target)),
).Error
if err != nil {
return err
Expand Down Expand Up @@ -202,7 +202,7 @@ func Delete(db *gorm.DB, source interface{}) error {

// Batch Delete Method in GORM requires an instance of current source type without ID
// to avoid GORM style Delete interface, we hacked here by set source ID to 0
tableName, ColumeMap := target.TableName, target.ColumnMap
tableName, dbNames := target.TableName, target.DbNames
v := reflect.Indirect(reflect.ValueOf(source))
t := v.Type()
for i := 0; i < t.NumField(); i++ {
Expand All @@ -228,7 +228,7 @@ func Delete(db *gorm.DB, source interface{}) error {
tx, _, _ = parseNode(db, source)
err = tx.Table(tableName).
Where(formatSQL(":"+d+" > ?", target), target.Rgt).
Update(ColumeMap[d], gorm.Expr(formatSQL(":"+d+" - ?", target), width)).
Update(dbNames[d], gorm.Expr(formatSQL(":"+d+" - ?", target), width)).
Error
if err != nil {
return err
Expand Down Expand Up @@ -324,7 +324,7 @@ func syncChildrenCount(tx *gorm.DB, targetNode nodeItem, oldParentID, newParentI
if err != nil {
return
}
err = tx.Where(formatSQL(":id = ?", targetNode), oldParentID).Update(targetNode.ColumnMap["children_count"], oldParentCount).Error
err = tx.Where(formatSQL(":id = ?", targetNode), oldParentID).Update(targetNode.DbNames["children_count"], oldParentCount).Error
if err != nil {
return
}
Expand All @@ -335,7 +335,7 @@ func syncChildrenCount(tx *gorm.DB, targetNode nodeItem, oldParentID, newParentI
if err != nil {
return
}
err = tx.Where(formatSQL(":id = ?", targetNode), newParentID).Update(targetNode.ColumnMap["children_count"], newParentCount).Error
err = tx.Where(formatSQL(":id = ?", targetNode), newParentID).Update(targetNode.DbNames["children_count"], newParentCount).Error
if err != nil {
return
}
Expand All @@ -345,38 +345,38 @@ func syncChildrenCount(tx *gorm.DB, targetNode nodeItem, oldParentID, newParentI
}

func moveTarget(tx *gorm.DB, targetNode nodeItem, targetID int64, targetIds []int64, step, depthChange int, newParentID sql.NullInt64) (err error) {
cm := targetNode.ColumnMap
dbNames := targetNode.DbNames

if len(targetIds) > 0 {
err = tx.Where(formatSQL(":id IN (?)", targetNode), targetIds).
Updates(map[string]interface{}{
cm["lft"]: gorm.Expr(formatSQL(":lft + ?", targetNode), step),
cm["rgt"]: gorm.Expr(formatSQL(":rgt + ?", targetNode), step),
cm["depth"]: gorm.Expr(formatSQL(":depth + ?", targetNode), depthChange),
dbNames["lft"]: gorm.Expr(formatSQL(":lft + ?", targetNode), step),
dbNames["rgt"]: gorm.Expr(formatSQL(":rgt + ?", targetNode), step),
dbNames["depth"]: gorm.Expr(formatSQL(":depth + ?", targetNode), depthChange),
}).Error
if err != nil {
return
}
}

return tx.Where(formatSQL(":id = ?", targetNode), targetID).Update(cm["parent_id"], newParentID).Error
return tx.Where(formatSQL(":id = ?", targetNode), targetID).Update(dbNames["parent_id"], newParentID).Error
}

func moveAffected(tx *gorm.DB, targetNode nodeItem, gte, lte, step int) (err error) {
cm := targetNode.ColumnMap
dbNames := targetNode.DbNames

return tx.Where(formatSQL("(:lft BETWEEN ? AND ?) OR (:rgt BETWEEN ? AND ?)", targetNode), gte, lte, gte, lte).
Updates(map[string]interface{}{
cm["lft"]: gorm.Expr(formatSQL("(CASE WHEN :lft >= ? THEN :lft + ? ELSE :lft END)", targetNode), gte, step),
cm["rgt"]: gorm.Expr(formatSQL("(CASE WHEN :rgt <= ? THEN :rgt + ? ELSE :rgt END)", targetNode), lte, step),
dbNames["lft"]: gorm.Expr(formatSQL("(CASE WHEN :lft >= ? THEN :lft + ? ELSE :lft END)", targetNode), gte, step),
dbNames["rgt"]: gorm.Expr(formatSQL("(CASE WHEN :rgt <= ? THEN :rgt + ? ELSE :rgt END)", targetNode), lte, step),
}).Error
}

func formatSQL(placeHolderSQL string, node nodeItem) (out string) {
out = placeHolderSQL

out = strings.ReplaceAll(out, ":table_name", node.TableName)
for k, v := range node.ColumnMap {
for k, v := range node.DbNames {
out = strings.Replace(out, ":"+k, v, -1)
}

Expand Down
28 changes: 14 additions & 14 deletions nested_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func TestNewNodeItem(t *testing.T) {
stmt.Build(clause.Where{}.Name())
assert.Equal(t, "WHERE user_id = $1 AND user_type = $2", stmt.SQL.String())

columnMap := node.ColumnMap
assert.Equal(t, "id", columnMap["id"])
assert.Equal(t, "parent_id", columnMap["parent_id"])
assert.Equal(t, "depth", columnMap["depth"])
assert.Equal(t, "rgt", columnMap["rgt"])
assert.Equal(t, "lft", columnMap["lft"])
assert.Equal(t, "children_count", columnMap["children_count"])
dbNames := node.DbNames
assert.Equal(t, "id", dbNames["id"])
assert.Equal(t, "parent_id", dbNames["parent_id"])
assert.Equal(t, "depth", dbNames["depth"])
assert.Equal(t, "rgt", dbNames["rgt"])
assert.Equal(t, "lft", dbNames["lft"])
assert.Equal(t, "children_count", dbNames["children_count"])

// Test for difference column names
specialItem := SpecialItem{
Expand All @@ -80,13 +80,13 @@ func TestNewNodeItem(t *testing.T) {
stmt.Build(clause.Where{}.Name())
assert.Equal(t, "", stmt.SQL.String())

columnMap = node.ColumnMap
assert.Equal(t, "item_id", columnMap["id"])
assert.Equal(t, "pid", columnMap["parent_id"])
assert.Equal(t, "depth1", columnMap["depth"])
assert.Equal(t, "right", columnMap["rgt"])
assert.Equal(t, "left", columnMap["lft"])
assert.Equal(t, "nodes_count", columnMap["children_count"])
dbNames = node.DbNames
assert.Equal(t, "item_id", dbNames["id"])
assert.Equal(t, "pid", dbNames["parent_id"])
assert.Equal(t, "depth1", dbNames["depth"])
assert.Equal(t, "right", dbNames["rgt"])
assert.Equal(t, "left", dbNames["lft"])
assert.Equal(t, "nodes_count", dbNames["children_count"])

// formatSQL test
assert.Equal(t, "item_id = ? AND left > right AND pid = ?, nodes_count = 1, depth1 = 0", formatSQL(":id = ? AND :lft > :rgt AND :parent_id = ?, :children_count = 1, :depth = 0", node))
Expand Down

0 comments on commit a72e045

Please sign in to comment.