private void FindTag(string sql, int position) { if (position >= sql.Length) { return; } Match match = find.Match(sql, position); if (match.Success) { string value = match.Value.Trim(); if (value.EndsWith(")") || value.EndsWith("(")) { string field = value.Substring(0, value.Length - 1); if (field.Length > 0) { TagTreeNode node1 = TagFactory.Create(field); tags.Append(node1); } TagTreeNode node2 = TagFactory.Create(value.Substring(value.Length - 1)); tags.Append(node2); } else { TagTreeNode node = TagFactory.Create(value); tags.Append(node); } position = match.Index + match.Value.Length; FindTag(sql, position); } }
public static TagTreeNode Create(string value) { if (string.IsNullOrWhiteSpace(value)) { return(null); } string key = value.Trim().ToLower(); TagTreeNode node = new TagTreeNode(); node.NodeKey = defaultTag; if (key == "(") { node.NodeKey = "l_" + child; } else if (key == ")") { node.NodeKey = "r_" + child; } else { foreach (string k in allTag) { if (k == key) { node.NodeKey = k; break; } } } if (node.NodeKey == defaultTag) { node.NodeData = new List <string>(key.Split(',')); } return(node); }
public void Append(TagTreeNode node) { if (head == null) { head = node; tail = node; return; } // tail.NextNode = node; tail = node; }
public List <Tag.Tag> GetTags() { List <Tag.Tag> tags = new List <Tag.Tag>(); TagTreeNode next = head.NextNode; TagTreeNode last = null; TagTreeNode cur = null; TagTreeNode lastPre = null; Tag.Tag tag = new Tag.Tag(); try { tag.TagType = (TagType)Enum.Parse(typeof(TagType), head.NodeKey); } catch { throw new Exception("SQL异常"); } while (next != null) { if (TagFactory.defaultTag == next.NodeKey) { //说明是数据 if (null == last) { cur = head; } else { cur = last; } // switch (cur.NodeKey) { case "select": tag.Fields.AddRange(next.NodeData); break; case "from": case "table": case "into": case "join": tag.Table.AddRange(next.NodeData); break; case "l_clause": { if (lastPre != null && lastPre.NodeKey == "into") { tag.Fields.AddRange(next.NodeData); } } break; case "as": { if (lastPre != null && lastPre.NodeData.Count > 0) { tag.Alias[lastPre.NodeData[lastPre.NodeData.Count - 1]] = next.NodeData[0]; } } break; case "field": { if (cur.NodeData.Count > 0) { tag.Alias[cur.NodeData[cur.NodeData.Count - 1]] = next.NodeData[0]; } } break; } // } else { TagType tagType; if (Enum.TryParse <TagType>(next.NodeKey, out tagType)) { //说明是操作类关键词 tags.Add(tag); tag = new Tag.Tag(); tag.TagType = tagType; } } lastPre = last; last = next; next = next.NextNode; } // tags.Add(tag); return(tags); }