SqlScripter.Core/src/Data/DependencySort.cs
/// <summary>
/// A heleper mthod that sorts named dependecies.
/// </summary>
/// <remarks>
/// 
/// </remarks>
/// <param name="table"></param>
/// <returns></returns>
A heleper mthod that sorts named dependecies.
A heleper mthod that sorts named dependecies .
public static StringCollection Sort(DataTable table){
            if (!table.Columns.Contains("Priority")) {
                throw new ArgumentException("Source table must have an int column named Priority");
            }
            if (!table.Columns.Contains("depends")) {
                throw new ArgumentException("Source table must have a string column named depends, containing dependency of name");
            }
            if (!table.Columns.Contains("name")) {
                throw new ArgumentException("Source table must have a string column named name, containing name of object that can depend on others");
            }
            foreach (DataRow row in table.Rows) {
                if (Convert.IsDBNull(row["depends"])) {
                    Console.Out.WriteLine("Undependent: " + row["name"]);
                    row["Priority"] = 1;
                }
            }
            int priority = 1;
            DataRow[] left = table.Select("Priority = 0");
            DataRow[] possibleDepnedencies = table.Select("Priority = 0");
            do {
                priority++;
                foreach (DataRow row in left) {
                    string dependency = row["depends"].ToString();
                    bool found = IsTableInCollection(dependency, possibleDepnedencies);
                    if (!found) {
                        row["Priority"] = priority;
                        CheckOtherDependencies(row, table);
                    }
                }
                left = table.Select("Priority = 0");
                possibleDepnedencies = table.Select("Priority = 0");
            } while (left.Length > 0);
            DataRow[] rows = table.Select("", "Priority ASC");
            StringCollection names = new StringCollection();
            foreach (DataRow row in rows) {
                string name = row["name"].ToString();
                if (!names.Contains(name)) {
                    names.Add(name);
                }
            }
            return names;
        }
public static StringCollection Sort ( DataTable table ) { if ( ! table . Columns . Contains ( " " ) ) { throw new ArgumentException ( " " ) ; } if ( ! table . Columns . Contains ( " " ) ) { throw new ArgumentException ( " " ) ; } if ( ! table . Columns . Contains ( " " ) ) { throw new ArgumentException ( " " ) ; } foreach ( DataRow row in table . Rows ) { if ( Convert . IsDBNull ( row [ " " ] ) ) { Console . Out . WriteLine ( " " + row [ " " ] ) ; row [ " " ] = 1 ; } } int priority = 1 ; DataRow [ ] left = table . Select ( " " ) ; DataRow [ ] possibleDepnedencies = table . Select ( " " ) ; do { priority ++ ; foreach ( DataRow row in left ) { string dependency = row [ " " ] . ToString ( ) ; bool found = IsTableInCollection ( dependency , possibleDepnedencies ) ; if ( ! found ) { row [ " " ] = priority ; CheckOtherDependencies ( row , table ) ; } } left = table . Select ( " " ) ; possibleDepnedencies = table . Select ( " " ) ; } while ( left . Length > 0 ) ; DataRow [ ] rows = table . Select ( " " , " " ) ; StringCollection names = new StringCollection ( ) ; foreach ( DataRow row in rows ) { string name = row [ " " ] . ToString ( ) ; if ( ! names . Contains ( name ) ) { names . Add ( name ) ; } } return names ; }
A heleper mthod that sorts named dependecies.
A heleper mthod that sorts named dependecies .
//System.Console.Out.WriteLine("# " + row["depends"] );
None
table
DataTable
None
remarks
None