/// <summary> /// 获取解析后的Sql语句 /// </summary> /// <returns></returns> public string GetSql() { if (this.map == null) { this.map = this.parentNode.map; } string sql = ""; if (this.SentenceType == SentenceType.Segment) { foreach (SyntaxNode node in nodes) { sql += node.GetSql(); } } else if (this.SentenceType == SentenceType.InBracket) { foreach (SyntaxNode node in nodes) { sql += node.GetSql(); } sql = "(" + sql + ")"; } else if (this.SentenceType == SentenceType.Property) { sql += this.sentence.Replace(this.sentence.Trim(), "[" + this.map[this.sentence.Trim()] + "]"); } else if (this.SentenceType == SentenceType.Entity) { sql += this.sentence.Replace(this.sentence.Trim(), "[" + EntityMap.GetEntityInfo(this.sentence.Trim()).TableName + "]"); } else if (this.SentenceType == SentenceType.String) { sql = this.sentence + " "; } else { sql = this.sentence; } return(sql); }
/// <summary> /// 解析整条语句,生成树型结构 /// </summary> public void Parse() { string s = string.Empty; Char[] charArray = sentence.ToCharArray(); for (int i = 0; i < charArray.Length; i++) { char c = charArray[i]; if (c == '\'') //如果碰见一个“'”,调用ParseInQuotation记录从该处开始其后的字符串 { if (s != string.Empty) { throw new Exception("“'”附近有语法错误"); } ParseInQuotation(ref charArray, ref i); } else if (c == '(') //如果碰见一个“(”,调用ParseInBracket记录从该处开始到与之对应的“)”之间的内容 { string funName = string.Empty; if (s != string.Empty) { SyntaxNode node = new SyntaxNode(); node.ParentNode = this; node.sentence = s; node.SentenceType = SentenceType.Function; this.nodes.Add(node); funName = s.ToLower(); s = string.Empty; } ParseInBracket(ref charArray, ref i); if (funKeywords.Contains(funName) && this.nodes[this.nodes.Count - 1].nodes.Count > 0) { this.nodes[this.nodes.Count - 1].nodes[0].SentenceType = SentenceType.Keyword; } } else if (c == ' ') //如果碰见一个空格,记录空格前的内容 { if (s != string.Empty) { SyntaxNode node = new SyntaxNode(); node.ParentNode = this; if (nodes.Count > 0 && nodes[nodes.Count - 1].sentenceType == SentenceType.From) { node.sentenceType = SentenceType.Entity; EntityInfo entityInfo = EntityMap.GetEntityInfo(GetName(s)); if (entityInfo == null) { throw new Exception("from后有语法错误,使用了一个不存在的实体名"); } this.map = entityInfo.PropertyMap; } else { node.SentenceType = GetSentenceType(s); } node.sentence = GetName(s) + " "; this.nodes.Add(node); s = string.Empty; } } else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '=' || c == '!' || c == '<' || c == '>' || c == ',')//如果碰见一个符号,记录符号前的内容及符号本身 { if (s != string.Empty) { SyntaxNode node = new SyntaxNode(); node.ParentNode = this; node.SentenceType = GetSentenceType(s); node.sentence = s; this.nodes.Add(node); } SyntaxNode operatorNode = new SyntaxNode(); operatorNode.ParentNode = this; operatorNode.SentenceType = SentenceType.Symbol; operatorNode.sentence = c.ToString(); this.nodes.Add(operatorNode); s = string.Empty; } else { s += c.ToString(); } } //记录最后的内容 if (s != string.Empty) { SyntaxNode node = new SyntaxNode(); node.parentNode = this; if (nodes.Count > 0 && nodes[nodes.Count - 1].sentenceType == SentenceType.From) { node.sentenceType = SentenceType.Entity; EntityInfo entityInfo = EntityMap.GetEntityInfo(GetName(s)); if (entityInfo == null) { throw new Exception("from后有语法错误,使用了一个不存在的实体名"); } this.map = entityInfo.PropertyMap; } else { node.SentenceType = GetSentenceType(s); } node.sentence = GetName(s); this.nodes.Add(node); } }