private Dictionary <string, string> FormatQuote(ref string sql) { if (string.IsNullOrEmpty(sql)) { return(null); } Dictionary <string, string> result = new Dictionary <string, string>(); MatchInfos mis = QueryHelper.GetMinMatchData(sql, "'", "'"); if (mis.Count <= 0) { return(null); } int i = 0; StringBuilder sb = new StringBuilder(sql); foreach (MatchInfo mi in mis) { string replace = "?QUOTE" + i.ToString(); //sql = sql.Replace(mi.MatchContext, replace); sb = sb.Replace(mi.MatchContext, replace); result.Add(mi.MatchContext, replace); i = i + 1; } sql = sb.ToString(); return(result); }
/// <summary> /// 载入sql格式内容 /// </summary> /// <param name="sqlFormatContext"></param> public void LoadFromString(string sqlFormatContext) { Clear(); if (string.IsNullOrEmpty(sqlFormatContext)) { return; } _sourceSqlFmt = sqlFormatContext; MatchInfos whereItems = QueryHelper.GetMinMatchData(sqlFormatContext, "<wi=", "/wi>"); if (whereItems.Count <= 0) { return; } foreach (MatchInfo whereitem in whereItems) { WhereItem wi = new WhereItem(); wi.LoadWhereFromString("<wi=" + whereitem.MatchContext + "/wi>"); _whereItems.Add(wi.Name, wi, whereitem.StartIndex); } }
public bool HasInputWhere(string sqlFormat) { MatchInfos items = QueryHelper.GetMinMatchData(sqlFormat, "[", "]"); foreach (MatchInfo input in items) { if (input.MatchContext.Contains(QueryConstDefine.SystemTag) == false) { return(true); } } return(false); }
/// <summary> /// 预览测试SQL /// </summary> /// <returns></returns> public string TestSql() { string result = _sourceSqlFmt; foreach (WhereItem wi in _whereItems.Values) { string curWhere = wi.Condition; MatchInfos inputs = QueryHelper.GetMinMatchData(curWhere, "[", "]"); foreach (MatchInfo input in inputs) { curWhere = curWhere.Replace("[" + input.MatchContext + "]", "null"); } result = result.Replace(wi.SourceFmt, " " + wi.LinkType + " " + curWhere); } return(result); }
private void richTextBox1_TextChanged(object sender, EventArgs e) { try { //tabItems.TabPages.Clear(); if (_isTabChanging) { return; } _isCondationChange = true; string source = rtbWhereContext.Text; MatchInfos inputs = QueryHelper.GetMinMatchData(source, "[", "]"); for (int i = _wi.InputItems.Values.Count - 1; i >= 0; i--) { InputItem iiDel = _wi.InputItems.Values.ElementAt(i); if (source.IndexOf("[" + iiDel.Name + "]") < 0) { _wi.InputItems.Remove(iiDel.Name); } } //删除不存在的tab for (int i = tabItems.TabPages.Count - 1; i >= 0; i--) { if (inputs.Contains(tabItems.TabPages[i].Name) == false) { tabItems.TabPages.RemoveAt(i); } } InputControlEnable(true); foreach (MatchInfo input in inputs) { if (string.IsNullOrEmpty(input.MatchContext)) { continue; } if (tabItems.TabPages.IndexOfKey(input.MatchContext) >= 0) { continue; } if (input.MatchContext.IndexOf("系统_") >= 0) { continue; } tabItems.TabPages.Add(input.MatchContext, input.MatchContext); if (_wi.InputItems.ContainsKey(input.MatchContext) == false) { InputItem curInput = new InputItem(); curInput.Name = input.MatchContext; _wi.AddInputItem(curInput, input.StartIndex); } } if (tabItems.TabPages.Count > 0) { tabItems.SelectedIndex = 0; } else { //清除数据 cbxControlType.SelectedIndex = 0; rtbDataFrom.Text = ""; txtExtPros.Text = ""; txtDefaultValue.Text = ""; chkReplace.Checked = false; } InputControlEnable((tabItems.TabPages.Count <= 0) ? false : true); rtbWhereContext.Focus(); } catch (Exception ex) { MsgBox.ShowException(ex, this); } finally { _isCondationChange = false; } }
static public MatchInfos GetMinMatchData(string source, string startMatch, string endMatch, char exceptSChr, char exceptEChr) { MatchInfos result = new MatchInfos(); string rStart = "<"; string rEnd = ">"; if (rStart == startMatch) { rStart = "("; } if (rEnd == endMatch) { rEnd = ")"; } MatchCollection mc = null; if ((exceptSChr != '\0') && (exceptEChr != '\0')) { mc = Regex.Matches(source, "[\\" + exceptSChr + "](.*?)[\\" + exceptEChr + "]"); for (int i = 0; i <= mc.Count - 1; i++) { source = source.Replace(mc[i].Value, rStart + "@" + i + "/" + rEnd); } } string tmp = source; int startMatchLen = startMatch.Length; int endMatchLen = endMatch.Length; int indexStart = tmp.IndexOf(startMatch); if (indexStart < 0) { return(result); //没有匹配项,则直接退出 } int indexEnd = tmp.IndexOf(endMatch); int indexNext = tmp.IndexOf(startMatch, indexStart + startMatchLen); if (indexNext <= 0) { indexNext = tmp.Length; } while (indexStart >= 0) { if (indexStart >= 0 && indexEnd > 0 && indexEnd > indexStart && indexEnd < indexNext)//满足[xxx]这种形式,过滤[xxx[bbb]这种形式 { string context = tmp.Substring(indexStart + startMatchLen, indexEnd - indexStart - startMatchLen); if (mc != null && context.IndexOf("<@") >= 0) { for (int i = 0; i <= mc.Count - 1; i++) { context = context.Replace(rStart + "@" + i + "/" + rEnd, mc[i].Value); } } result.Add(new MatchInfo(context, indexStart)); tmp = tmp.Substring(indexEnd + endMatchLen); } else { tmp = tmp.Substring(indexStart + startMatchLen); } if (tmp.Length <= 0) { break; } indexStart = tmp.IndexOf(startMatch); indexEnd = tmp.IndexOf(endMatch); indexNext = tmp.IndexOf(startMatch, (indexStart < 0) ? 0 : indexStart + startMatchLen); if (indexNext <= 0) { indexNext = tmp.Length; } } return(result); }
public void LoadWhereFromString(string whereitem) { if (string.IsNullOrEmpty(whereitem)) { return; } SourceFmt = whereitem; string tmp = whereitem; int indexStart = tmp.IndexOf("<wi="); if (indexStart < 0) { return; } indexStart = indexStart + 4; int indexEnd = tmp.IndexOf(":", indexStart + 1); Name = tmp.Substring(indexStart, indexEnd - indexStart); tmp = tmp.Substring(indexEnd + 1); indexStart = tmp.IndexOf(@"连接类型="""); if (indexStart < 0) { LinkType = ""; indexEnd = -1; } else { indexStart = indexStart + 5; indexEnd = tmp.IndexOf(",", indexStart + 1); LinkType = tmp.Substring(indexStart, indexEnd - indexStart).Replace(@"""", ""); } tmp = tmp.Substring(indexEnd + 1).Replace("/wi>", ""); string curCondation = tmp; //配置录入项 MatchInfos inputs = QueryHelper.GetMinMatchData(tmp, "[", "]", '"', '"'); foreach (MatchInfo input in inputs) { string[] pros = input.MatchContext.Split(','); InputItem ii = new InputItem(); ii.Name = pros[0]; curCondation = curCondation.Replace(input.MatchContext, ii.Name); ii.ControlType = FindPro(pros, "类型", "文本框"); ii.DBAlias = FindPro(pros, "数据源"); ii.DataFrom = FindPro(pros, "数据来源"); ii.ExtPro = FindPro(pros, "扩展属性"); ii.DefaultValue = FindPro(pros, "默认值"); ii.IsWhereReplace = (FindPro(pros, "条件替换") == "1" ? true : false); //ii.StartIndex = input.StartIndex; ii.Parent = this; InputItems.Add(ii.Name, ii, input.StartIndex); } Condition = curCondation; }