diff --git a/nested_set.go b/nested_set.go index 311f3f9..0537371 100644 --- a/nested_set.go +++ b/nested_set.go @@ -33,7 +33,7 @@ type nodeItem struct { Lft int ChildrenCount int TableName string - DbNames map[string]string + ColumnMap map[string]string } // parseNode parse a gorm structure into an internal source structure @@ -53,7 +53,7 @@ func parseNode(db *gorm.DB, source interface{}) (tx *gorm.DB, item nodeItem, err return } - item = nodeItem{TableName: stmt.Table, DbNames: map[string]string{}} + item = nodeItem{TableName: stmt.Table, ColumnMap: map[string]string{}} sourceValue := reflect.Indirect(reflect.ValueOf(source)) sourceType := sourceValue.Type() for i := 0; i < sourceType.NumField(); i++ { @@ -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.DbNames["id"] = dbName + item.ColumnMap["id"] = dbName break case "parent_id": item.ParentID = v.Interface().(sql.NullInt64) - item.DbNames["parent_id"] = dbName + item.ColumnMap["parent_id"] = dbName break case "depth": item.Depth = int(v.Int()) - item.DbNames["depth"] = dbName + item.ColumnMap["depth"] = dbName break case "rgt": item.Rgt = int(v.Int()) - item.DbNames["rgt"] = dbName + item.ColumnMap["rgt"] = dbName break case "lft": item.Lft = int(v.Int()) - item.DbNames["lft"] = dbName + item.ColumnMap["lft"] = dbName break case "children_count": item.ChildrenCount = int(v.Int()) - item.DbNames["children_count"] = dbName + item.ColumnMap["children_count"] = dbName break case "scope": rawVal, _ := schemaField.ValueOf(sourceValue) @@ -117,15 +117,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, dbNames := target.TableName, target.DbNames + tableName, ColumnMap := target.TableName, target.ColumnMap // 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(dbNames["rgt"]).Order(orderSQL).First(&lastNode) + rst := tx.Model(source).Select(ColumnMap["rgt"]).Order(orderSQL).First(&lastNode) if rst.Error == nil { - setToLft = lastNode[dbNames["rgt"]].(int) + 1 + setToLft = lastNode[ColumnMap["rgt"]].(int) + 1 setToRgt = setToLft + 1 } } else { @@ -141,7 +141,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(dbNames["rgt"], gorm.Expr(formatSQL(":rgt + 2", target))). + UpdateColumn(ColumnMap["rgt"], gorm.Expr(formatSQL(":rgt + 2", target))). Error if err != nil { return err @@ -151,7 +151,7 @@ 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(dbNames["lft"], gorm.Expr(formatSQL(":lft + 2", target))). + UpdateColumn(ColumnMap["lft"], gorm.Expr(formatSQL(":lft + 2", target))). Error if err != nil { return err @@ -159,7 +159,7 @@ func Create(db *gorm.DB, source, parent interface{}) error { // UPDATE tree SET children_count = children_count + 1 WHERE id = parent.id; err = db.Model(parent).Update( - dbNames["children_count"], gorm.Expr(formatSQL(":children_count + 1", target)), + ColumnMap["children_count"], gorm.Expr(formatSQL(":children_count + 1", target)), ).Error if err != nil { return err @@ -215,7 +215,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, dbNames := target.TableName, target.DbNames + tableName, ColumeMap := target.TableName, target.ColumnMap v := reflect.Indirect(reflect.ValueOf(source)) t := v.Type() for i := 0; i < t.NumField(); i++ { @@ -241,7 +241,7 @@ func Delete(db *gorm.DB, source interface{}) error { tx, _, _ = parseNode(db, source) err = tx.Table(tableName). Where(formatSQL(":"+d+" > ?", target), target.Rgt). - Update(dbNames[d], gorm.Expr(formatSQL(":"+d+" - ?", target), width)). + Update(ColumeMap[d], gorm.Expr(formatSQL(":"+d+" - ?", target), width)). Error if err != nil { return err @@ -337,7 +337,7 @@ func syncChildrenCount(tx *gorm.DB, targetNode nodeItem, oldParentID, newParentI if err != nil { return } - err = tx.Where(formatSQL(":id = ?", targetNode), oldParentID).Update(targetNode.DbNames["children_count"], oldParentCount).Error + err = tx.Where(formatSQL(":id = ?", targetNode), oldParentID).Update(targetNode.ColumnMap["children_count"], oldParentCount).Error if err != nil { return } @@ -348,7 +348,7 @@ func syncChildrenCount(tx *gorm.DB, targetNode nodeItem, oldParentID, newParentI if err != nil { return } - err = tx.Where(formatSQL(":id = ?", targetNode), newParentID).Update(targetNode.DbNames["children_count"], newParentCount).Error + err = tx.Where(formatSQL(":id = ?", targetNode), newParentID).Update(targetNode.ColumnMap["children_count"], newParentCount).Error if err != nil { return } @@ -358,30 +358,30 @@ 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) { - dbNames := targetNode.DbNames + cm := targetNode.ColumnMap if len(targetIds) > 0 { err = tx.Where(formatSQL(":id IN (?)", targetNode), targetIds). Updates(map[string]interface{}{ - dbNames["lft"]: gorm.Expr(formatSQL(":lft + ?", targetNode), step), - dbNames["rgt"]: gorm.Expr(formatSQL(":rgt + ?", targetNode), step), - dbNames["depth"]: gorm.Expr(formatSQL(":depth + ?", targetNode), depthChange), + cm["lft"]: gorm.Expr(formatSQL(":lft + ?", targetNode), step), + cm["rgt"]: gorm.Expr(formatSQL(":rgt + ?", targetNode), step), + cm["depth"]: gorm.Expr(formatSQL(":depth + ?", targetNode), depthChange), }).Error if err != nil { return } } - return tx.Where(formatSQL(":id = ?", targetNode), targetID).Update(dbNames["parent_id"], newParentID).Error + return tx.Where(formatSQL(":id = ?", targetNode), targetID).Update(cm["parent_id"], newParentID).Error } func moveAffected(tx *gorm.DB, targetNode nodeItem, gte, lte, step int) (err error) { - dbNames := targetNode.DbNames + cm := targetNode.ColumnMap return tx.Where(formatSQL("(:lft BETWEEN ? AND ?) OR (:rgt BETWEEN ? AND ?)", targetNode), gte, lte, gte, lte). Updates(map[string]interface{}{ - 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), + 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), }).Error } @@ -389,7 +389,7 @@ func formatSQL(placeHolderSQL string, node nodeItem) (out string) { out = placeHolderSQL out = strings.ReplaceAll(out, ":table_name", node.TableName) - for k, v := range node.DbNames { + for k, v := range node.ColumnMap { out = strings.Replace(out, ":"+k, v, -1) } diff --git a/nested_set_test.go b/nested_set_test.go index 3116266..485f286 100644 --- a/nested_set_test.go +++ b/nested_set_test.go @@ -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()) - 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"]) + 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"]) // Test for difference column names specialItem := SpecialItem{ @@ -80,13 +80,13 @@ func TestNewNodeItem(t *testing.T) { stmt.Build(clause.Where{}.Name()) assert.Equal(t, "", stmt.SQL.String()) - 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"]) + 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"]) // 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))