SMO#3 : Analyse récursive des dépendances

Publié le par Renaud Harduin

Sur la base de la proposition, l'ajout de récursivité va permettre d'analyser dynamiquement l'arbre :
____

private

List<TablePath> lookForChilds( TablePath root , TablePath parent)

{

 

List<TablePath> ltp = new List<TablePath>();

 

 

DataTable dt = _database.Tables[parent.TableName,parent.Schema].EnumForeignKeys();

 

foreach (DataRow dr in dt.Rows)

{

 

string tableName = dr[1].ToString();

 

string schemaName = dr[0].ToString();

 

string fkname = dr[2].ToString();

 

if (!(tableName.Equals(parent.TableName) &&

schemaName.Equals(parent.Schema))

)

{


____

Je teste le parent afin de ne pas partir en récursion infinie liée à un éventuel Parent-Chlid ou association :

____

log.Info(

"scanning now " + tableName);

 

TablePath tp = new TablePath();

tp._database = parent._database;

tp._server = parent._server;

tp.TableName = dr[1].ToString();

tp.Schema = schemaName;

tp.ForeignKey = fkname;

tp._childs =

this.lookForChilds(root, tp);

ltp.Add(tp);

}

 

else

{

log.Info(

"preventing infiniteRecursion on " + tableName);

}

 

}

 

return ltp;

}


____


En passant, nous remplissons la ForeignKey impliquée. L'ensemble se restitue toujours avec la méthode AdaptTablePath() :

____

 

private void AdaptTablePath(ref TreeNode node, TablePath tablePath)

{

node.Text = tablePath.TableName +

" via < " +tablePath.ForeignKey +" >";

 

if (tablePath.Childs != null)

{

 

foreach (TablePath tp in tablePath.Childs)

{

 

TreeNode childNode = new TreeNode(tp.TableName );

AdaptTablePath(

ref childNode, tp);

node.Nodes.Add(childNode);

}

}

}


____

En mode restitué :
____

____
Publicité

Publié dans SMO

Pour être informé des derniers articles, inscrivez vous :
Commenter cet article