Question

Good day pals! Currently I'm studying this database model design called Modified Pre-order Tree Traversal (MPTT). After seeing the drawbacks of using Common Table Expressions (CTE) because of its poor quality performance, I found recommendations of using MPTT. But before I can use the benefit of MPTT, I need to redesign my database table by adding 'right' and 'left' node values. To do that I need to make a program that will automate and update the values of the each data in the table. My problem is I can't make a program that will automate the node values. I'm trying to convert a php language to a C# code, but I couldn't make it. One of my weakness in programming is creating 'recursive' methods.

I'm using this link as my reference. Hierarchical database model

And here's the code I'm trying to convert to C#

<?php 
function rebuild_tree($parent, $left) {   
    // the right value of this node is the left value + 1   
    $right = $left+1;   

    // get all children of this node   
    $result = mysql_query('SELECT title FROM tree '.   
                           'WHERE parent="'.$parent.'";');  
    while ($row = mysql_fetch_array($result)) {   
        // recursive execution of this function for each   
        // child of this node   
        // $right is the current right value, which is   
        // incremented by the rebuild_tree function   
        $right = rebuild_tree($row['title'], $right);   
    }   

    // we've got the left value, and now that we've processed
    // the children of this node we also know the right value   
    mysql_query('UPDATE tree SET lft='.$left.', rgt='.   
                 $right.' WHERE title="'.$parent.'";');   

    // return the right value of this node + 1   
    return $right+1;   
}   
?>
Was it helpful?

Solution

Here's my own code, please let me know if there is a much better solution

    int right = 0;

    public int rebuild_tree(int parent, int left)
    {
        // the right value of this node is the left value + 1
        right = left + 1;

        // get all children of this node
        command = conn.CreateCommand();
        command.CommandText = "SELECT cat_id FROM tbl_RefDataCategory_mst WHERE parent_id=@parent";
        command.Parameters.Add("parent", SqlDbType.Int).Value = parent;

        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                int temp = Convert.ToInt32(reader["cat_id"]);
                right = rebuild_tree(temp, right);
            }

            command = conn.CreateCommand();
            command.CommandText = "UPDATE tbl_RefDataCategory_mst SET lft=@l, rgt=@r WHERE cat_id=@p";
            command.Parameters.Add("l", SqlDbType.Int).Value = left;
            command.Parameters.Add("r", SqlDbType.Int).Value = right;
            command.Parameters.Add("p", SqlDbType.Int).Value = parent;

            command.ExecuteNonQuery();
        }

        return right + 1;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top