Hellow, I have problem like this. I have SQL query where with button press need to add brackets.
Sample of SQL: SELECT something FROM sometable WHERE item1 OR item2 XOR item3 AND item4
Whan I press the botton it's adds brackets that SQL is right.
Like this: SELECT something FROM sometable WHERE (((item1 OR item2) XOR item3) AND item4)
Please help me.
Loading

FroglegRank #1,020 · 1.9K reputationPosted Feb 17, 2011, 12:04 AM EST
public void addBrkts()
{
string str = richTextBox1.Text;
richTextBox1.Text = ("");
string[] strsp = str.Split(' ');
int strLen = strsp.Length ;
if (strLen != 12)
{
return;
}
string strAdd = strsp[5].Insert(0, "(((");
strsp[5] = strAdd;
string strAdd2 = strsp[7].Insert(strsp[7].Length , ")");
strsp[7] = strAdd2;
string strAdd3 = strsp[9].Insert(strsp[9].Length, ")");
strsp[9] = strAdd3;
string strAdd4 = strsp[11].Insert(strsp[11].Length, ")");
strsp[11] = strAdd4;
foreach (string s in strsp)
{
richTextBox1.AppendText(s);
richTextBox1.AppendText(" ");
}
}
Janis Peksa145 reputationPosted Feb 16, 2011, 2:51 AM EST
Suthish NairRank #92 · 22.8K reputationPosted Feb 16, 2011, 7:40 AM EST
Janis Peksa145 reputationPosted Feb 16, 2011, 10:18 AM EST
So I need this that my SQL query is right. Thats why I need this brackets to be added.
Janis Peksa145 reputationPosted Feb 16, 2011, 9:26 PM EST
Janis Peksa145 reputationPosted Feb 16, 2011, 11:31 PM EST
public void button1_Click(object sender, EventArgs e)
{
string abc = richTextBox1.Text.Trim();
richTextBox1.Text = abc.Replace(" and ", ") and ((");
string abc2 = richTextBox1.Text.Trim();
if (abc2.Contains(" where ") && (abc2.Contains(" and ") || abc2.Contains(" or ") || abc2.Contains(" xor ")))
{
richTextBox1.Text = abc2.Replace(" where ", " where ((");
}
string abc3 = richTextBox1.Text.Trim();
richTextBox1.Text = abc3.Replace(" xor ", ") xor ((");
int len = richTextBox1.Text.Length;
string bca = richTextBox1.Text;
string word1 = "and";
string word2 = "xor";
Regex r = new Regex(@"\b" + word1 + @"\b", RegexOptions.IgnoreCase);
MatchCollection mc = r.Matches(richTextBox1.Text);
Regex r2 = new Regex(@"\b" + word2 + @"\b", RegexOptions.IgnoreCase);
MatchCollection mc2 = r2.Matches(richTextBox1.Text);
int mcSum = mc.Count + mc2.Count;
if (bca.Contains(" where ") && (bca.Contains(" and ") || bca.Contains(" xor ")))
{
for (int i = 0; i < mcSum; i++)
{
richTextBox1.Text = richTextBox1.Text.Insert(len, ")");
}
if (mcSum == 1)
{
richTextBox1.Text = richTextBox1.Text.Insert(len, ")");
}
}
}
Janis Peksa145 reputationPosted Feb 16, 2011, 11:42 PM EST
if (mcSum == 1)
{
richTextBox1.Text = richTextBox1.Text.Insert(len, ")");
richTextBox1.Text = richTextBox1.Text.Insert(len, ")");
}
Janis Peksa145 reputationPosted Feb 16, 2011, 11:44 PM EST
Janis Peksa145 reputationPosted Feb 17, 2011, 12:24 AM EST
But I have SELECT a FROM b WHERE ... etc ....
This spaces don't need brackets.
So your code is not usefull.
FroglegRank #1,020 · 1.9K reputationPosted Feb 17, 2011, 12:27 AM EST
Janis Peksa145 reputationPosted Feb 17, 2011, 12:35 AM EST
FroglegRank #1,020 · 1.9K reputationPosted Feb 17, 2011, 12:43 AM EST
string abc = richTextBox1.Text;
richTextBox1.Text = abc.Replace(" WHERE ", " WHERE (((");
string abc2 = richTextBox1.Text;
richTextBox1.Text = abc2.Replace(" XOR ", ") XOR ");
string abc3 = richTextBox1.Text;
richTextBox1.Text = abc3.Replace(" AND ", ") AND ");
richTextBox1.AppendText(")");
Janis Peksa145 reputationPosted Feb 17, 2011, 12:50 AM EST
I just wanna know is my code is correct.
For any SQL that have been entered in richtextbox.
FroglegRank #1,020 · 1.9K reputationPosted Feb 17, 2011, 1:03 AM EST
So you are OK now ?
Janis Peksa145 reputationPosted Feb 17, 2011, 1:07 AM EST
I'm not sure that my code is correct.
Janis Peksa145 reputationPosted Feb 17, 2011, 2:09 AM EST
public void button1_Click(object sender, EventArgs e)
{
string abc = richTextBox1.Text.Trim();
richTextBox1.Text = abc.Replace(" and ", ") and ((");
string abc2 = richTextBox1.Text.Trim();
if (abc2.Contains(" where ") && (abc2.Contains(" and ") || abc2.Contains(" or ")))
{
richTextBox1.Text = abc2.Replace(" where ", " where ((");
}
int len = richTextBox1.Text.Length;
string bca = richTextBox1.Text;
string word1 = "and";
Regex r = new Regex(@"\b" + word1 + @"\b", RegexOptions.IgnoreCase);
MatchCollection mc = r.Matches(richTextBox1.Text);
int mcSum = mc.Count;
if (bca.Contains(" where ") && (bca.Contains(" and ")))
{
for (int i = 0; i < mcSum + 1; i++)
{
richTextBox1.Text = richTextBox1.Text.Insert(len, ")");
}
if (mcSum == 1)
{
richTextBox1.Text = richTextBox1.Text.Insert(len, ")");
richTextBox1.Text = richTextBox1.Text.Insert(len, ")");
}
}
}