77using System . Collections . Generic ;
88using System . ComponentModel ;
99using System . Drawing ;
10+ using System . Linq ;
1011using System . Windows . Forms ;
1112
1213namespace BIDSHelper . SSIS
1314{
1415 internal class FindVariables
1516 {
16- private string [ ] expressionMatches = null ;
17- private string [ ] properytMatches = null ;
17+ private string [ ] expressionCandidateMatches = null ;
18+ private string [ ] properytCandidateMatches = null ;
1819 private TreeView treeView ;
1920
2021 public const string IconKeyFolder = "Folder" ;
@@ -76,8 +77,8 @@ public void FindReferences(Package package, Variable[] variables, TreeView treeV
7677 }
7778
7879 // Convert interim lists to arrays
79- this . expressionMatches = expressions . ToArray ( ) ;
80- this . properytMatches = properties . ToArray ( ) ;
80+ expressionCandidateMatches = expressions . ToArray ( ) ;
81+ properytCandidateMatches = properties . ToArray ( ) ;
8182
8283 this . treeView = treeView ;
8384
@@ -529,25 +530,12 @@ private void ScanCustomPropertiesCollection(IDTSCustomPropertyCollection100 prop
529530 if ( string . IsNullOrEmpty ( value ) )
530531 continue ;
531532
532- string match ;
533533 if ( property . ExpressionType == DTSCustomPropertyExpressionType . CPET_NOTIFY )
534534 {
535535 // Check the expression string for our matching variable name
536536 // We ignore the Task level properties derived from these expressions, because here we have much more context.
537- // Could have expression properties (CPET_NOTIFY) entirely, call it Darren's OCD in action.
538- if ( ExpressionMatch ( value , out match ) )
539- {
540- // For the "FriendlyExpression" property, rename to be Expression
541- if ( friendlyExpressionValid && property . Name == "FriendlyExpression" )
542- {
543- propertyName = "Expression" ;
544- }
545-
546- VariableFoundEventArgs info = new VariableFoundEventArgs ( ) ;
547- info . Match = match ;
548- OnRaiseVariableFound ( info ) ;
549- AddNode ( parent , propertyName , GetImageIndex ( IconKeyPropertyExpression ) , new PropertyExpression ( propertyName , value , property . Value . GetType ( ) ) , true ) ;
550- }
537+ // Could have expression properties (CPET_NOTIFY) entirely, call it Darren's OCD in action.
538+ ExpressionMatchCustomProperty ( parent , friendlyExpressionValid , property , propertyName , value ) ;
551539 }
552540 else
553541 {
@@ -561,6 +549,23 @@ private void ScanCustomPropertiesCollection(IDTSCustomPropertyCollection100 prop
561549 }
562550 }
563551
552+ private void ExpressionMatchCustomProperty ( TreeNode parent , bool friendlyExpressionValid , IDTSCustomProperty100 property , string propertyName , string value )
553+ {
554+ foreach ( string match in expressionCandidateMatches . Where ( matchCandidate => value . Contains ( matchCandidate ) ) . ToList ( ) )
555+ {
556+ // For the "FriendlyExpression" property, rename to be Expression
557+ if ( friendlyExpressionValid && property . Name == "FriendlyExpression" )
558+ {
559+ propertyName = "Expression" ;
560+ }
561+
562+ VariableFoundEventArgs info = new VariableFoundEventArgs ( ) ;
563+ info . Match = match ;
564+ OnRaiseVariableFound ( info ) ;
565+ AddNode ( parent , propertyName , GetImageIndex ( IconKeyPropertyExpression ) , new PropertyExpression ( propertyName , value , property . Value . GetType ( ) ) , true ) ;
566+ }
567+ }
568+
564569 private void ScanPrecedenceConstraints ( string containerID , PrecedenceConstraints constraints , TreeNode parent )
565570 {
566571 if ( this . CancellationPending )
@@ -585,14 +590,18 @@ private void ScanPrecedenceConstraints(string containerID, PrecedenceConstraints
585590 continue ;
586591 }
587592
588- string match ;
589- if ( ExpressionMatch ( constraint . Expression , out match ) )
590- {
591- VariableFoundEventArgs info = new VariableFoundEventArgs ( ) ;
592- info . Match = match ;
593- OnRaiseVariableFound ( info ) ;
594- AddNode ( constraintsNode , "Expression" , GetImageIndex ( IconKeyPrecedenceConstraint ) , constraint , true ) ;
595- }
593+ ExpressionMatchConstraint ( constraintsNode , constraint ) ;
594+ }
595+ }
596+
597+ private void ExpressionMatchConstraint ( TreeNode constraintsNode , PrecedenceConstraint constraint )
598+ {
599+ foreach ( string match in expressionCandidateMatches . Where ( matchCandidate => constraint . Expression . Contains ( matchCandidate ) ) . ToList ( ) )
600+ {
601+ VariableFoundEventArgs info = new VariableFoundEventArgs ( ) ;
602+ info . Match = match ;
603+ OnRaiseVariableFound ( info ) ;
604+ AddNode ( constraintsNode , "Expression" , GetImageIndex ( IconKeyPrecedenceConstraint ) , constraint , true ) ;
596605 }
597606 }
598607
@@ -620,14 +629,18 @@ private void ScanVariables(Variables variables, TreeNode parent, string currentP
620629 continue ;
621630 }
622631
623- string match ;
624- if ( ExpressionMatch ( variable . Expression , out match ) )
625- {
626- VariableFoundEventArgs info = new VariableFoundEventArgs ( ) ;
627- info . Match = match ;
628- OnRaiseVariableFound ( info ) ;
629- AddNode ( variablesFolder , variable . QualifiedName , imageIndex , variable , true ) ;
630- }
632+ ExpressionMatchVariable ( variablesFolder , imageIndex , variable ) ;
633+ }
634+ }
635+
636+ private void ExpressionMatchVariable ( TreeNode variablesFolder , int imageIndex , Variable variable )
637+ {
638+ foreach ( string match in expressionCandidateMatches . Where ( matchCandidate => variable . Expression . Contains ( matchCandidate ) ) . ToList ( ) )
639+ {
640+ VariableFoundEventArgs info = new VariableFoundEventArgs ( ) ;
641+ info . Match = match ;
642+ OnRaiseVariableFound ( info ) ;
643+ AddNode ( variablesFolder , variable . QualifiedName , imageIndex , variable , true ) ;
631644 }
632645 }
633646
@@ -657,7 +670,6 @@ private void ScanProperties(IDTSPropertiesProvider provider, TreeNode parent)
657670 string propertyName = property . Name ;
658671
659672 #region Check property value
660- string match ;
661673 if ( property . Type == TypeCode . String && property . Get )
662674 {
663675 string value = property . GetValue ( provider ) as string ;
@@ -678,34 +690,23 @@ private void ScanProperties(IDTSPropertiesProvider provider, TreeNode parent)
678690 // Check this for a while, before we trust it, simce it is undocumented.
679691 System . Diagnostics . Debug . Assert ( hasExpressions , "HasExpressions was false, but we have an expression." ) ;
680692
681- if ( ExpressionMatch ( expression , out match ) )
682- {
683- VariableFoundEventArgs foundArgument = new VariableFoundEventArgs ( ) ;
684- foundArgument . Match = match ;
685- OnRaiseVariableFound ( foundArgument ) ;
686- AddNode ( expressions , propertyName , GetImageIndex ( IconKeyVariableExpression ) , new PropertyExpression ( propertyName , expression , PackageHelper . GetTypeFromTypeCode ( property . Type ) ) , true ) ;
687- }
693+ ExpressionMatchProperty ( expressions , property , propertyName , expression ) ;
688694 #endregion
689695
690696 }
691697 }
692698
693- private bool ExpressionMatch ( string expression , out string match )
699+ private void ExpressionMatchProperty ( TreeNode expressions , DtsProperty property , string propertyName , string expression )
694700 {
695- foreach ( string test in this . expressionMatches )
701+ foreach ( string match in expressionCandidateMatches . Where ( matchCandidate => expression . Contains ( matchCandidate ) ) . ToList ( ) )
696702 {
697- if ( expression . Contains ( test ) )
698- {
699- match = test ;
700- return true ;
701- }
703+ VariableFoundEventArgs foundArgument = new VariableFoundEventArgs ( ) ;
704+ foundArgument . Match = match ;
705+ OnRaiseVariableFound ( foundArgument ) ;
706+ AddNode ( expressions , propertyName , GetImageIndex ( IconKeyVariableExpression ) , new PropertyExpression ( propertyName , expression , PackageHelper . GetTypeFromTypeCode ( property . Type ) ) , true ) ;
702707 }
703-
704- match = null ;
705- return false ;
706708 }
707709
708-
709710 private void PropertyMatch ( TreeNode parent , object property , string propertyName , string value )
710711 {
711712 IEnumerable valueList ;
@@ -736,7 +737,7 @@ private void PropertyMatch(TreeNode parent, object property, string propertyName
736737
737738 private bool PropertyMatchEval ( string value , out string match )
738739 {
739- foreach ( string test in this . properytMatches )
740+ foreach ( string test in this . properytCandidateMatches )
740741 {
741742 if ( test == value )
742743 {
@@ -902,8 +903,7 @@ private void EnumerateCollection(object task, TreeNode parameterBindings, string
902903
903904 private void PropertyAsExpressionMatch ( DtsProperty property , string expression , TreeNode parent )
904905 {
905- string match ;
906- if ( ExpressionMatch ( expression , out match ) )
906+ foreach ( string match in expressionCandidateMatches . Where ( matchCandidate => expression . Contains ( matchCandidate ) ) . ToList ( ) )
907907 {
908908 VariableFoundEventArgs info = new VariableFoundEventArgs ( ) ;
909909 info . Match = match ;
0 commit comments