private SqlKey getEnterKey4Key(string key) { SqlKey res = null; foreach (SqlKey sqlkey in EnterKeys) { if (sqlkey.Key == key) { res = sqlkey; break; } } return(res); }
/* Set offsets for blocks LEFT JOIN, INNER JOIN, EXISTS */ private void parseIdentBlocks() { identBlocks.Clear(); List <string> identBlocksAfter = new List <string>(); List <string> identGroups = new List <string>(); foreach (SqlKey sqlkey in EnterKeys) { if (sqlkey.IdentAfter > 0) { identBlocksAfter.Add(sqlkey.Key); } if (sqlkey.IsGroup) { identGroups.Add(sqlkey.Key); } } string patternBegin = String.Format(@"(^|\W)(?i)({0})(?-i)\W", string.Join("|", identBlocksAfter.ToArray())); Regex reBeg = new Regex(patternBegin, RegexOptions.Singleline); string patternEnd = String.Format(@"\W(?i)({0}|$)(?-i)\W", string.Join("|", identGroups.ToArray())); Regex reEnd = new Regex(patternEnd, RegexOptions.Singleline); int partBegPos = -1; int prevBegPos = 0; int lastPos = this.sql.Length - 1; int partEndPos = lastPos; int id = 1; int lvl; do { Match mcBeg = reBeg.Match(this.sql, prevBegPos); if (mcBeg.Success) { int keyStartPos = mcBeg.Index == 0 ? 0 : 1; int matchLen = mcBeg.Length - keyStartPos - 1; partBegPos = mcBeg.Index + matchLen + 1; partEndPos = lastPos; string key = mcBeg.Value.Substring(keyStartPos, matchLen); SqlKey sqlkey = getEnterKey4Key(key); bool endIsInPart = sqlkey != null && sqlkey.endIsInPart; lvl = 0; SqlPart partBeg = getSqlPart4Pos(partBegPos); if (partBeg != null) { if (endIsInPart) { int blockBegPos = this.sql.IndexOf(PartBegin, partBegPos); if (blockBegPos > 0) { SqlPart blockPart = getSqlPartEnd4Pos(blockBegPos + 1); if (blockPart != null && blockPart.End < this.sql.Length - 1) { partEndPos = blockPart.End; } } lvl = sqlkey != null ? sqlkey.IdentAfter : 1; } else { MatchCollection mcEnds = reEnd.Matches(this.sql, partBegPos); for (int i = 0; i < mcEnds.Count; i++) { Match mcItem = mcEnds[i]; int endPos = mcItem.Index + 1; SqlPart partEnd = getSqlPart4Pos(endPos); if (partEnd != null && partEnd.id == partBeg.id) { partEndPos = mcItem.Index; lvl = 1; break; } } } SqlPart part = new SqlPart(); part.id = id++; part.parent_id = part.id; part.Begin = partBegPos; part.End = partEndPos; part.Level = lvl; identBlocks.Add(part); } else { partBegPos = -1; break; } prevBegPos = partBegPos; } else { partBegPos = -1; break; } } while (partBegPos > -1); }