Handles zero, one, or more phrases, returning a
standard SQL and clause.
Namespace: CleanCode.DataAssembly: CleanCode (in CleanCode.dll) Version: 1.2.3.0 (1.2.03)
public string GetConjunction(
params string[] phrase
)
Public Function GetConjunction ( _
ParamArray phrase As String() _
) As String
Public Function GetConjunction ( _
ParamArray phrase As String() _
) As String
public:
String^ GetConjunction(
... array<String^>^ phrase
)
public:
String^ GetConjunction(
... array<String^>^ phrase
)
Parameters
- phrase
- Type: System String
List of zero or more
phrases to combine.
Return Value
SQL predicate with 'AND' operators as necessary.
With an argument list of, e.g., "phrase-x", "phrase-y", "phrase-z",
the result will be
(phrase-x AND phrase-y AND phrase-Z).
Use
GetPhrase(String, String ) to generate the individual phrases
passed as arguments to this method.
This method is well-suited to automatic clause generation
because any null or zero-length arguments will be skipped.
The value of this is best illustrated by example.
Say you want to create a filter on two TextBox controls, tb1 and tb2
so that a DataGridView is filtered by tb1 on field1 and by tb2 on field2.
The initial attempt at this might be:
BindingSource.Filter =
string.Format(
"field1 = '{0}' AND field2 = '{1}'", tb1.Text, tb2.Text);
BindingSource.Filter =
string.Format(
"field1 = '{0}' AND field2 = '{1}'", tb1.Text, tb2.Text);
field1 = 'foo' AND field2 = 'bar'
field1 = 'foo' AND field2 = 'bar'
field1 = '' AND field2 = 'bar'
field1 = '' AND field2 = 'bar'
filter = (tb1.Text.Length > 0 && tb2.Text.Length > 0) ?
GetConjunction("field1 = "+tb1.Text, "field2 = "+tb2.Text)
: (tb1.Text.Length > 0) ? "field2 = "+tb2.Text
: (tb2.Text.Length > 0) ? "field1 = "+tb1.Text
: "";
filter = (tb1.Text.Length > 0 && tb2.Text.Length > 0) ?
GetConjunction("field1 = "+tb1.Text, "field2 = "+tb2.Text)
: (tb1.Text.Length > 0) ? "field2 = "+tb2.Text
: (tb2.Text.Length > 0) ? "field1 = "+tb1.Text
: "";
filter = builder.GetConjunction(
builder.GetPhrase("field1",tb1.Text),
builder.GetPhrase("field2",tb2.Text));
filter = builder.GetConjunction(
builder.GetPhrase("field1",tb1.Text),
builder.GetPhrase("field2",tb2.Text));
So if tb1 contains "foo" and tb2 contains "bar", the filter string will be
But what if tb1 is empty?
To the user, that means that tb1 should be ignored.
However, the simplistic construct above will yield:
wherein field1 must be empty! So if the user leaves the field blank,
we want to ignore it rather than match an empty string.
Which means testing for tb1 being empty and if so, omitting it
from the filter expression. Then we must treat tb2 similarly,
quickly yielding messy conditionalized code.
This class obviates the need for all that.
Thus, instead of code like:
you may simply use: