Tsql { 字符串ret=“”; 尝试 { ret+=“\n”; string[]grpsExpr=_orderby.ToLower().Split(新字符串[]{,“},StringSplitOptions.RemoveEmptyEntries); foreac
{ 字符串ret=“”; 尝试 { ret+=“\n”; string[]grpsExpr=_orderby.ToLower().Split(新字符串[]{,“},StringSplitOptions.RemoveEmptyEntries); foreach(grpsExpr中的字符串expr) { 字符串val=expr.ToLower(); 字符串ascc=val.ToLower().Contains(“asc”)?“TRUE”:val.ToLower().Contains(“desc”)?“FALSE”:“TRUE”; val=val.Replace(“asc”和“); val=val.Replace(“desc”和“”); val=val.Trim(); ret+=string.Format(“\n”,val,ascc.Trim(); } ret+=“\n”; } 捕获(例外情况除外) { 日志(“CSqlToCAML.sqltocamlSelect()错误:”+ex.Message); } 返回ret; } 私有静态字符串sqltocamlSelect(字符串_select) { 字符串ret=“”; 尝试 { ret+=“\n”; 字符串[]grpsExpr=_select.ToLower().Split(新字符串[]{“,”},StringSplitOptions.RemoveEmptyEntries); foreach(grpsExpr中的字符串expr) { ret+=string.Format(“\n”,expr.Trim(); } ret+=“\n”; } 捕获(例外情况除外) { 日志(“CSqlToCAML.sqltocamlSelect()错误:”+ex.Message); } 返回ret; } 私有静态字符串sqltocamlWhere(字符串_-where) { 字符串ret=“”,retAnd=“”,retOr=“”; 尝试 { /* •Eq=等于 •Neq=不等于 •BeginsWith=以开始 •包含=包含 •Lt=小于 •Leq=小于或等于 •Gt=大于 •Geq=大于或等于 •IsNull=为null •IsNotNull=不为null */ //“(id_-typ=3或id_-typ=4)和(datum_-zal>datum_-odos)” ret+=“\n”; string[]grpsExpr=_where.ToLower().Split(新字符串[]{“(“,”)”},StringSplitOptions.RemoveEmptyEntries); foreach(grpsExpr中的字符串expr) { 如果(表达式包含(“和”)) { retAnd=“”; foreach(expr.Split中的字符串exp(新字符串[]{“和”},StringSplitOptions.RemoveEmptyEntries)) { retAnd+=expStr(exp); } 如果(retAnd.Length>0) { ret+=“\n”; ret+=retAnd; ret+=“\n”; } } 如果(表达式包含(“或”)!=null) { 反驳=”; foreach(expr.Split中的字符串exp(新字符串[]{“或”},StringSplitOptions.RemoveEmptyEntries)) { retOr+=expStr(exp); } 如果(回复长度>0) { ret+=“\n”; ret+=干馏器; ret+=“\n”; } } } ret+=“\n”; } 捕获(例外情况除外) { 日志(“CSqlToCAML.sqltocamlWhere()错误:”+ex.Message); } 返回ret; } 私有静态字符串expStr(字符串exp) { 字符串ret=“”; ret+=propExp(exp,“=”); ret+=propExp(exp,“”); ret+=propExp(exp,“”); ret+=propExp(exp,“=”); ret+=propExp(exp,“为空”); ret+=propExp(exp,“不为空”); ret+=propExp(exp,“in”); ret+=propExp(exp,“like”); ret+=propExp(exp,介于两者之间); 返回ret; } 私有静态字符串propExp(字符串sExp,字符串op) { 字符串ret=“”,_op=“”; 尝试 { 如果(!sExp.Contains(op)) 返回“”; sExp=sExp.Replace(“,”); sExp=sExp.Replace(“,”); sExp=sExp.Replace(“,”); string[]_ops=sExp.Split(新字符串[]{”“},StringSplitOptions.RemoveEmptyEntries); string[]_opx=sExp.Split(新字符串[]{op},StringSplitOptions.RemoveEmptyEntries); 如果(_ops[1]!=op) 返回“”; 字符串名称、值; name=sExp.Split(新字符串[]{op},StringSplitOptions.RemoveEmptyEntries)[0]; value=sExp.Split(新字符串[]{op},StringSplitOptions.RemoveEmptyEntries)[1]; value=value.Trim(); name=name.Trim(); while(true) { if(sExp.Contains(op)&&op==“=”) { _op=“Geq”; 打破 } if(sExp.Contains(op)&&op==“=”) { _op=“Eq”; 打破 } if(sExp.Contains(op)&&op==“”) { _op=“Eq”; 打破 } if(sExp.Contains(op)&&op==“”&&sExp.Contains(“null”)) { _op=“IsNotNull”; 打破 } if(sExp.Contains(op)&&op==“不为空”) { _op=“IsNotNull”; 打破 } if(sExp.Contains(op)&&op==“为空”) { _op=“IsNull”; 打破 } if(sExp.Contains(op)&&op==“”) { _op=“Gt”; 打破 } 打破 } 如果(!string.IsNullOrEmpty(_op)和&!string.IsNullOrEmpty(name)) ret+=string.FormTsql { 字符串ret=“”; 尝试 { ret+=“\n”; string[]grpsExpr=_orderby.ToLower().Split(新字符串[]{,“},StringSplitOptions.RemoveEmptyEntries); foreac,tsql,sharepoint,sharepoint-2007,caml,Tsql,Sharepoint,Sharepoint 2007,Caml,{ 字符串ret=“”; 尝试 { ret+=“\n”; string[]grpsExpr=_orderby.ToLower().Split(新字符串[]{,“},StringSplitOptions.RemoveEmptyEntries); foreach(grpsExpr中的字符串expr) { 字符串val=expr.ToLower(); 字符串ascc=val.ToLower().Contains(“asc”)?“TRUE”:val.ToLower().Contains(“desc”)?
<Query>
<Where>
<Or>
<Eq>
<FieldRef Name="ContentType" />
<Value Type="Text">My Content Type</Value>
</Eq>
<IsNotNull>
<FieldRef Name="Description" />
</IsNotNull>
</Or>
</Where>
<Order>
<FieldRef Name="Author" />
<FieldRef Name="AuthoringDate" />
<FieldRef Name="AssignedTo" Ascending="True" />
</Order>
<Group>
<FieldRef Name="Title" />
</Group>
</Query>
WHERE ContentType="My Content Type"
OR Description<>null
GROUPBY Title DESC
ORDERBY Author, AuthoringDate, AssignedTo ASC
public void SharePointConnectionExample1()
{
using (SharePointConnection connection = new SharePointConnection(@"
Server=mysharepointserver.com;
Database=mysite/subsite
User=spuser;
Password=******;
Authentication=Ntlm;
TimeOut=10;
StrictMode=True;
RecursiveMode=RecursiveAll;
DefaultLimit=1000;
CacheTimeout=5"))
{
connection.Open();
using (SharePointCommand command = new SharePointCommand("UPDATE `mytable` SET `mycolumn` = 'hello world'", connection))
{
command.ExecuteNonQuery();
}
}
}
string query = "SELECT * FROM list";
conn = new SharePointConnection(connectionString);
SharePointDataAdapter adapter = new SharePointDataAdapter(query, conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
string query = "Select * from mylist.viewname";
DataGrid dataGrid = new DataGrid();
dataGrid.DataSource = Camelot.SharePointConnector.Data.Helper.ExecuteDataTable(query, connectionString);
dataGrid.DataBind();
Controls.Add(dataGrid);
CSqlToCAML.TextSqlToCAML(sql);
select id,evid_cislo,nazov,adresa,ulica,vec,datum_zal,datum_odos,ukoncene_dna
from koresp
where ((id_typ <= 3 or id_typ = 4)
and (datum_zal > datum_odos)) or (id > 21)
order by nazov desc ,id asc
<Query>
<ViewFields>
<FieldRef Name=" id" /><FieldRef Name="evid_cislo" /><FieldRef Name="nazov" />
<FieldRef Name="adresa" /><FieldRef Name="ulica" />
<FieldRef Name="vec" /><FieldRef Name="datum_zal" />
<FieldRef Name="datum_odos" /><FieldRef Name="ukoncene_dna " />
</ViewFields>
<Where>
<Or>
<Leq><FieldRef Name="id_typ" /><Value Type="Text">3</Value></Leq>
<Eq><FieldRef Name="id_typ" /><Value Type="Text">4</Value></Eq>
</Or>
<Or>
<Gt><FieldRef Name="datum_zal" /><Value Type="Text">datum_odos</Value></Gt>
</Or>
<Or>
<Gt><FieldRef Name="id" /><Value Type="Text">21</Value></Gt>
</Or>
</Where>
<OrderBy>
<FieldRef Name="nazov" Ascending="FALSE" />
<FieldRef Name="id" Ascending="TRUE" />
</OrderBy>
</Query>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SPASMXServices.ISAPI
{
public static class CSqlToCAML
{
public static string TextSqlToCAML(string query)
{
string ret = "";
try
{
string[] grpsExpr = query.ToLower().Split(new string[] { "select","from","where","order by","having" }, StringSplitOptions.RemoveEmptyEntries);
ret += TextSqlToCAML(getValueStrArr(grpsExpr, 0),
getValueStrArr(grpsExpr, 1),
getValueStrArr(grpsExpr, 2),
getValueStrArr(grpsExpr, 3),
getValueStrArr(grpsExpr, 4)
);
}
catch (Exception ex)
{
Log("CSqlToCAML.TextSqlToCAML() error: " + ex.Message);
}
return ret;
}
public static string TextSqlToCAML(string select, string from, string where, string orderby, string having)
{
string ret = "<Query>";
try
{
ret += sqltocamlSelect(select);
ret += sqltocamlWhere(where);
ret += sqltocamlOrderBy(orderby);
}
catch (Exception ex)
{
Log("CSqlToCAML.TextSqlToCAML() error: " + ex.Message);
}
return ret + "</Query>";
}
private static string getValueStrArr(string[] strs, int index)
{
try
{
return strs[index];
}
catch
{
return "";
}
}
private static string sqltocamlOrderBy(string _orderby)
{
string ret = "";
try
{
ret += "<OrderBy>\n";
string[] grpsExpr = _orderby.ToLower().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
foreach (string expr in grpsExpr)
{
string val = expr.ToLower();
string ascc = val.ToLower().Contains("asc") ? "TRUE" : val.ToLower().Contains("desc") ? "FALSE" : "TRUE";
val = val.Replace("asc", "");
val = val.Replace("desc", "");
val = val.Trim();
ret += string.Format("<FieldRef Name=\"{0}\" Ascending=\"{1}\" />\n", val,ascc).Trim();
}
ret += "</OrderBy>\n";
}
catch (Exception ex)
{
Log("CSqlToCAML.sqltocamlSelect() error: " + ex.Message);
}
return ret;
}
private static string sqltocamlSelect(string _select)
{
string ret = "";
try
{
ret += "<ViewFields>\n";
string[] grpsExpr = _select.ToLower().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
foreach (string expr in grpsExpr)
{
ret += string.Format("<FieldRef Name=\"{0}\" />\n", expr).Trim();
}
ret += "</ViewFields>\n";
}
catch (Exception ex)
{
Log("CSqlToCAML.sqltocamlSelect() error: " + ex.Message);
}
return ret;
}
private static string sqltocamlWhere(string _where)
{
string ret = "", retAnd = "", retOr = "";
try
{
/*
•Eq = equal to
•Neq = not equal to
•BeginsWith = begins with
•Contains = contains
•Lt = less than
•Leq = less than or equal to
•Gt = greater than
•Geq = greater than or equal to
•IsNull = is null
•IsNotNull = is not null
*/
// "(id_typ = 3 or id_typ = 4) and (datum_zal > datum_odos) "
ret += "<Where>\n";
string[] grpsExpr = _where.ToLower().Split(new string[] { "(", ")"}, StringSplitOptions.RemoveEmptyEntries);
foreach (string expr in grpsExpr)
{
if (expr.Contains("and"))
{
retAnd = "";
foreach (string exp in expr.Split(new string[] { "and" }, StringSplitOptions.RemoveEmptyEntries))
{
retAnd += expStr(exp);
}
if (retAnd.Length > 0)
{
ret += "<And>\n";
ret += retAnd;
ret += "</And>\n";
}
}
if (expr.Contains("or") != null)
{
retOr = "";
foreach (string exp in expr.Split(new string[] { "or" }, StringSplitOptions.RemoveEmptyEntries))
{
retOr += expStr(exp);
}
if (retOr.Length > 0)
{
ret += "<Or>\n";
ret += retOr;
ret += "</Or>\n";
}
}
}
ret += "</Where>\n";
}
catch (Exception ex)
{
Log("CSqlToCAML.sqltocamlWhere() error: " + ex.Message);
}
return ret;
}
private static string expStr(string exp)
{
string ret = "";
ret += propExp(exp, "=");
ret += propExp(exp, "<>");
ret += propExp(exp, "<");
ret += propExp(exp, ">");
ret += propExp(exp, "<=");
ret += propExp(exp, ">=");
ret += propExp(exp, "is null");
ret += propExp(exp, "is not null");
ret += propExp(exp, "in");
ret += propExp(exp, "like");
ret += propExp(exp, "between");
return ret;
}
private static string propExp(string sExp, string op)
{
string ret = "", _op = "";
try
{
if (!sExp.Contains(op))
return "";
sExp = sExp.Replace("'", " ");
sExp = sExp.Replace(" "," ");
sExp = sExp.Replace(" ", " ");
string[] _ops = sExp.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
string[] _opx = sExp.Split(new string[] { op }, StringSplitOptions.RemoveEmptyEntries);
if (_ops[1] != op)
return "";
string name, value;
name = sExp.Split(new string[] { op }, StringSplitOptions.RemoveEmptyEntries)[0];
value = sExp.Split(new string[] { op }, StringSplitOptions.RemoveEmptyEntries)[1];
value = value.Trim();
name = name.Trim();
while(true)
{
if (sExp.Contains(op) && op == "<=")
{
_op = "Leq";
break;
}
if (sExp.Contains(op) && op == ">=")
{
_op = "Geq";
break;
}
if (sExp.Contains(op) && op == "=")
{
_op = "Eq";
break;
}
if (sExp.Contains(op) && op == "<>")
{
_op = "Eq";
break;
}
if (sExp.Contains(op) && op == "<>" && sExp.Contains("null"))
{
_op = "IsNotNull";
break;
}
if (sExp.Contains(op) && op == "is not null")
{
_op = "IsNotNull";
break;
}
if (sExp.Contains(op) && op == "is null")
{
_op = "IsNull";
break;
}
if (sExp.Contains(op) && op == "<")
{
_op = "Lt";
break;
}
if (sExp.Contains(op) && op == ">")
{
_op = "Gt";
break;
}
break;
}
if (!string.IsNullOrEmpty(_op) && !string.IsNullOrEmpty(name))
ret += string.Format("<{0}><FieldRef Name=\"{1}\" /><Value Type=\"Text\">{2}</Value></{0}>\n", _op, name, value);
}
catch (Exception ex)
{
Log("CSqlToCAML.propExp(" + sExp + ") error: " + ex.Message);
}
return ret;
}
private static void Log(string text)
{
//MessageBox.Show(text);
LOG += string.Format("[{0} - {1};\n]", DateTime.Now, text);
}
public static string LOG;
}
}
"<Where>
<And>
<Or>
<Geq>
<FieldRef Name=\"Microfilm\" />
<Value Type=\"Text\">10</Value>
</Geq>
<Leq>
<FieldRef Name=\"Microfilm\" />
<Value Type=\"Text\">50</Value>
</Leq>
</Or>
<BeginsWith>
<FieldRef Name=\"Title\" />
<Value Type=\"Text\">Ice</Value>
</BeginsWith>
</And>
</Where>"
"(Microfilm >= 10 OR Microfilm <= 50) AND Title LIKE 'Ice%'"
private static string ViewQueryToSqlWhere(SPView v)
{
string sqlWhere = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
XmlNodeList nodeList;
//Add <Query> around the SPView.Query since a valid XML document requires a single root element.
//and SPView.Query doesn't.
xmlDoc.LoadXml("<Query>" + v.Query + "</Query>");
nodeList = xmlDoc.GetElementsByTagName("Where");
if (nodeList.Count == 1)
{
XmlNode nodeWhere = nodeList[0];
if (nodeWhere.HasChildNodes) //Should Always be the case
{
StringBuilder sb = new StringBuilder();
bool isSuccess = ProcessWhereNode(nodeWhere, ref sb);
}
}
return sqlWhere;
}
private static bool ProcessWhereNode(XmlNode xmlNode, ref StringBuilder sb)
{
bool isSuccess = false;
Stack<string> operatorStack = new Stack<string>();
Queue<string> valueQueue = new Queue<string>();
string previousOp = string.Empty;
string strOperator = string.Empty;
try
{
//Call a method to iterate "recursively" throught the nodes to get the values and operators.
if (ProcessRecursiveWhereNode(xmlNode, "", "", ref operatorStack, ref valueQueue))
{
while (valueQueue.Count > 0)
{
if (operatorStack.Count > 0)
{
strOperator = operatorStack.Pop();
//Open bracket if it's an OR operator except if the previous one was also an OR.
if (strOperator == "OR" && previousOp != "OR")
sb.Append("(");
}
else
{
strOperator = string.Empty;
}
sb.Append(valueQueue.Dequeue());
//Close bracket if previous OP was an OR, and it's not followed by another one
if (previousOp == "OR" && strOperator != "OR")
sb.Append(")");
if (strOperator != string.Empty)
{
sb.Append(" " + strOperator + " ");
}
previousOp = strOperator;
}
}
}
catch (Exception ex)
{ }
return isSuccess;
}
private static bool ProcessRecursiveWhereNode(XmlNode xmlNode, string strOperatorValue, string strOperatorType, ref Stack<string> operatorStack, ref Queue<string> valueQueue)
{
bool isSuccess = false;
string fieldName = string.Empty;
string value = string.Empty;
string thisIterationOperatorType = string.Empty;
string thisIterationOperatorValue = string.Empty;
try
{
XmlNodeList nodeList = xmlNode.ChildNodes;
//Get Child node - Possible tags {<Or>, <And>, <Eq>, <Neq>, <Gt>, <Lt>, <Geq>, <Leq>, </BeginsWith>, <Contains>, <FieldRef>, <Value>}
foreach (XmlNode node in nodeList)
{
thisIterationOperatorType = string.Empty;
thisIterationOperatorValue = string.Empty;
//Check if it's one of these tag: <Or>, <And>, <Eq>, <Neq>, <Gt>, <Lt>, <Geq>, <Leq>, </BeginsWith>, <Contains>
thisIterationOperatorValue = GetOperatorString(node.Name, out thisIterationOperatorType);
if (thisIterationOperatorType == "statement")
operatorStack.Push(thisIterationOperatorValue);
//It's one of these tag: <Or>, <And>, <Eq>, <Neq>, <Gt>, <Lt>, <Geq>, <Leq>, </BeginsWith>, <Contains>
if (thisIterationOperatorValue != string.Empty)
{
ProcessRecursiveWhereNode(node, thisIterationOperatorValue, thisIterationOperatorType, ref operatorStack, ref valueQueue);
}
else //It is probably a <FieldRef> or <Value> tag.
{
if (node.Name == "FieldRef")
fieldName = node.Attributes["Name"].Value.ToString();
else if (node.Name == "Value")
value = node.LastChild.Value.ToString();
}
}
if (strOperatorType == "value" && strOperatorValue != string.Empty && fieldName != string.Empty && value != string.Empty)
{
valueQueue.Enqueue(string.Format(strOperatorValue, fieldName, "'" + value + "'"));
}
isSuccess = true;
}
catch
{
isSuccess = false;
throw;
}
return isSuccess;
}
static private string GetOperatorString(string tagName, out string operatorType)
{
string operatorString = string.Empty;
switch (tagName)
{
case "Or":
operatorString = "OR";
operatorType = "statement";
break;
case "And":
operatorString = "AND";
operatorType = "statement";
break;
case "Eq":
operatorString = "{0} = {1}";
operatorType = "value";
break;
case "Neq":
operatorString = "{0} != {1}";
operatorType = "value";
break;
case "Gt":
operatorString = "{0} > {1}";
operatorType = "value";
break;
case "Lt":
operatorString = "{0} < {1}";
operatorType = "value";
break;
case "Geq":
operatorString = "{0} >= {1}";
operatorType = "value";
break;
case "Leq":
operatorString = "{0} <= {1}";
operatorType = "value";
break;
case "BeginsWith":
operatorString = "{0} LIKE '{1}%";
operatorType = "value";
break;
case "Contains":
operatorString = "{0} LIKE '%{1}%";
operatorType = "value";
break;
default:
operatorString = string.Empty;
operatorType = string.Empty;
break;
}
return operatorString;
}
<Where>
<Or>
<And>
<Neq><FieldRef Name="F1" /><Value Type="Text">Yes</Value></Neq>
<Neq><FieldRef Name="F2" /><Value Type="Text">Yes</Value></Neq>
</And>
<Eq><FieldRef Name="F3" /><Value Type="Text">Yes</Value></Eq>
</Or>
</Where>
private bool ProcessWhereNode(XmlNode xmlNode, ref StringBuilder sb) {
bool isSuccess = false;
Stack<string> operatorStack = new Stack<string>();
Queue<string> valueQueue = new Queue<string>();
string previousOp = string.Empty;
string strOperator = string.Empty;
try {
//Call a method to iterate "recursively" throught the nodes to get the values and operators.
if (ProcessRecursiveWhereNode(xmlNode, "", "", ref operatorStack, ref valueQueue)) {
// For each operator adding parenthesis before starting
StringBuilder sbTmp = new StringBuilder();
operatorStack.ToList().ForEach(x => sbTmp.Append("("));
sb.Append(sbTmp.ToString());
while (valueQueue.Count > 0) {
if (operatorStack.Count > 0) {
strOperator = operatorStack.Pop();
} else {
strOperator = string.Empty;
}
sb.Append(valueQueue.Dequeue());
// After each logical operation closing parenthesis
if (previousOp != string.Empty)
sb.Append(")");
if (strOperator != string.Empty)
sb.Append(" " + strOperator + " ");
previousOp = strOperator;
}
}
isSuccess = true;
} catch (Exception) {
isSuccess = false;
}
return isSuccess;
}
//http://stackoverflow.com/questions/5834700/sharepoint-caml-query-to-t-sql
public static class CAMLtoSQL
{
public static string ViewQueryToSqlWhere(string query)
{
string sqlWhere = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
XmlNodeList nodeList;
//Add <Query> around the SPView.Query since a valid XML document requires a single root element.
//and SPView.Query doesn't.
xmlDoc.LoadXml("<Query>" + query + "</Query>");
nodeList = xmlDoc.GetElementsByTagName("Where");
if (nodeList.Count == 1)
{
XmlNode nodeWhere = nodeList[0];
if (nodeWhere.HasChildNodes) //Should Always be the case
{
StringBuilder sb = new StringBuilder();
bool isSuccess = ProcessWhereNode(nodeWhere, ref sb);
sqlWhere = sb.ToString();
}
}
return sqlWhere;
}
private static bool ProcessWhereNode(XmlNode xmlNode, ref StringBuilder sb)
{
bool isSuccess = false;
Stack<string> operatorStack = new Stack<string>();
Queue<string> valueQueue = new Queue<string>();
string previousOp = string.Empty;
string strOperator = string.Empty;
try
{
//Call a method to iterate "recursively" throught the nodes to get the values and operators.
if (ProcessRecursiveWhereNode(xmlNode, "", "", ref operatorStack, ref valueQueue))
{
// For each operator adding parenthesis before starting
StringBuilder sbTmp = new StringBuilder();
operatorStack.ToList().ForEach(x => sbTmp.Append("("));
sb.Append(sbTmp.ToString());
while (valueQueue.Count > 0)
{
if (operatorStack.Count > 0)
{
strOperator = operatorStack.Pop();
}
else
{
strOperator = string.Empty;
}
sb.Append(valueQueue.Dequeue());
// After each logical operation closing parenthesis
if (previousOp != string.Empty)
sb.Append(")");
if (strOperator != string.Empty)
sb.Append(" " + strOperator + " ");
previousOp = strOperator;
}
}
isSuccess = true;
}
catch (Exception)
{
isSuccess = false;
}
return isSuccess;
}
private static bool ProcessRecursiveWhereNode(XmlNode xmlNode, string strOperatorValue, string strOperatorType, ref Stack<string> operatorStack, ref Queue<string> valueQueue)
{
bool isSuccess = false;
string fieldName = string.Empty;
string value = string.Empty;
string thisIterationOperatorType = string.Empty;
string thisIterationOperatorValue = string.Empty;
try
{
XmlNodeList nodeList = xmlNode.ChildNodes;
//Get Child node - Possible tags {<Or>, <And>, <Eq>, <Neq>, <Gt>, <Lt>, <Geq>, <Leq>, </BeginsWith>, <Contains>, <IsNotNull>, <IsNull>, <FieldRef>, <Value>}
foreach (XmlNode node in nodeList)
{
thisIterationOperatorType = string.Empty;
thisIterationOperatorValue = string.Empty;
//Check if it's one of these tag: <Or>, <And>, <Eq>, <Neq>, <Gt>, <Lt>, <Geq>, <Leq>, </BeginsWith>, <Contains>, <IsNotNull>, <IsNull>
thisIterationOperatorValue = GetOperatorString(node.Name, out thisIterationOperatorType);
if (thisIterationOperatorType == "statement")
operatorStack.Push(thisIterationOperatorValue);
//It's one of these tag: <Or>, <And>, <Eq>, <Neq>, <Gt>, <Lt>, <Geq>, <Leq>, </BeginsWith>, <Contains>, <IsNotNull>, <IsNull>
if (thisIterationOperatorValue != string.Empty)
{
ProcessRecursiveWhereNode(node, thisIterationOperatorValue, thisIterationOperatorType, ref operatorStack, ref valueQueue);
}
else if (strOperatorType != "statement") //It is probably a <FieldRef> or <Value> tag.
{
if (node.Name == "FieldRef")
fieldName = node.Attributes["Name"].Value.ToString();
else if (node.Name == "Value")
value = node.LastChild.Value.ToString();
}
}
if ((strOperatorType == "value" && strOperatorValue != string.Empty && fieldName != string.Empty && value != string.Empty)
||
(strOperatorType == "is" && strOperatorValue != string.Empty && fieldName != string.Empty))
{
// if contains a like we don't add the '
if (strOperatorValue.Contains("LIKE"))
valueQueue.Enqueue(string.Format(strOperatorValue, fieldName, value));
else
valueQueue.Enqueue(string.Format(strOperatorValue, fieldName, "'" + value + "'"));
}
isSuccess = true;
}
catch
{
isSuccess = false;
throw;
}
return isSuccess;
}
private static string GetOperatorString(string tagName, out string operatorType)
{
string operatorString = string.Empty;
switch (tagName)
{
case "Or":
operatorString = "OR";
operatorType = "statement";
break;
case "And":
operatorString = "AND";
operatorType = "statement";
break;
case "Eq":
operatorString = "{0} = {1}";
operatorType = "value";
break;
case "Neq":
operatorString = "{0} != {1}";
operatorType = "value";
break;
case "Gt":
operatorString = "{0} > {1}";
operatorType = "value";
break;
case "Lt":
operatorString = "{0} < {1}";
operatorType = "value";
break;
case "Geq":
operatorString = "{0} >= {1}";
operatorType = "value";
break;
case "Leq":
operatorString = "{0} <= {1}";
operatorType = "value";
break;
case "BeginsWith":
operatorString = "{0} LIKE '{1}%'";
operatorType = "value";
break;
case "Contains":
operatorString = "{0} LIKE '%{1}%'";
operatorType = "value";
break;
case "IsNotNull":
operatorString = "{0} IS NOT NULL";
operatorType = "is";
break;
case "IsNull":
operatorString = "{0} IS NULL";
operatorType = "is";
break;
default:
operatorString = string.Empty;
operatorType = string.Empty;
break;
}
return operatorString;
}
}