Пример #1
0
        /* Set offsets for blocks with brackets (...) */
        private void parseParts()
        {
            parts.Clear();
            if (sqlLen <= LineWidth)
            {
                return;
            }

            int     prevPos;
            int     nextPos  = -1;
            int     id       = 1;
            string  prevEdge = "";
            SqlPart prevPart = new SqlPart();

            prevPart.id    = id;
            prevPart.Level = -1;
            Regex rePart = new Regex(PartEdge, RegexOptions.Singleline);

            do
            {
                prevPos = nextPos + 1;
                SqlPart part = new SqlPart();

                if (prevPos >= sqlLen)
                {
                    break;
                }
                Match mcPart = rePart.Match(this.sql, prevPos);
                if (mcPart.Success)
                {
                    nextPos = mcPart.Index;
                    string partEdge = mcPart.Groups[0].Value;
                    if (prevEdge != PartEnd)
                    {
                        part.id        = id++;
                        part.parent_id = prevPart.id;
                        part.Level     = prevPart.Level + 1;
                        prevPart       = part;
                    }
                    else
                    {
                        prevPart       = getSqlPartById(prevPart.parent_id);
                        part.id        = prevPart.id;
                        part.parent_id = prevPart.parent_id;
                        part.Level     = prevPart.Level;
                    }
                    prevEdge = partEdge;
                }
                else
                {
                    break;
                }
                if (nextPos > -1)
                {
                    part.Begin = prevPos;
                    part.End   = nextPos;
                    parts.Add(part);
                }
            } while (nextPos > -1);
        }
Пример #2
0
        private SqlPart getSqlPartById(int id)
        {
            SqlPart res = null;

            foreach (SqlPart part in parts)
            {
                if (part.id == id)
                {
                    res = part;
                    break;
                }
            }
            return(res);
        }
Пример #3
0
 private void setBlockIdentOffset(int pos, int offset)
 {
     for (int i = 0; i < identBlocks.Count; i++)
     {
         SqlPart part = identBlocks[i];
         if (part.Begin >= pos)
         {
             part.BeginOffset += offset;
         }
         if (part.End >= pos)
         {
             part.EndOffset += offset;
         }
     }
 }
Пример #4
0
 private void setSqlPartOffset(int pos, int offset)
 {
     for (int i = 0; i < parts.Count; i++)
     {
         SqlPart part = parts[i];
         if (part.Begin >= pos)
         {
             part.BeginOffset += offset;
         }
         if (part.End >= pos)
         {
             part.EndOffset += offset;
         }
     }
 }
Пример #5
0
        private SqlPart getSqlPart4Pos(int pos, bool withOffset)
        {
            SqlPart res = null;

            foreach (SqlPart part in parts)
            {
                int beg = withOffset ? part.Begin + part.BeginOffset : part.Begin;
                int end = withOffset ? part.End + part.EndOffset : part.End;
                if (pos >= beg && pos <= end)
                {
                    res = part;
                    break;
                }
            }
            return(res);
        }
Пример #6
0
        private SqlPart getSqlPartEnd4Pos(int pos)
        {
            SqlPart res = null;
            SqlPart beg = getSqlPart4Pos(pos);

            if (beg != null)
            {
                int endPos = 0;
                foreach (SqlPart part in parts)
                {
                    if (part.id == beg.id)
                    {
                        if (part.End > endPos)
                        {
                            res    = part;
                            endPos = part.End;
                        }
                    }
                }
            }
            return(res);
        }
Пример #7
0
        private void parse()
        {
            parseParts();
            parseIdentBlocks();

            /* Set offset depend on level */
            SortedDictionary <int, string> insStr = new SortedDictionary <int, string>();

            foreach (SqlKey sqlkey in EnterKeys)
            {
                string          prefix       = new String(CTAB, sqlkey.IdentBefore);
                string          regexPattern = String.Format(@"(^|\W)(?i){0}(?-i)\W", sqlkey.Key);
                Regex           rgx          = new Regex(regexPattern);
                MatchCollection matches      = rgx.Matches(this.sql);
                foreach (Match match in matches)
                {
                    if (match.Index > 0)
                    {
                        int    pos         = match.Index == 0 ? match.Index : match.Index + 1;
                        int    lvl         = getLevel4Pos(pos);
                        int    blockIdents = getBlockIdent4Pos(pos);
                        string insertStr   = sqlkey.breakLineBefore ? BR + prefix + new String(CTAB, lvl) + new String(CTAB, blockIdents) : "";
                        if (this.sql[pos - 1].ToString() == PartBegin)
                        {
                            /* to break line "(SELECT" with the bracket */
                            pos--;
                        }
                        insStr.Add(pos, insertStr);
                        if (sqlkey.breakLineAfter)
                        {
                            /* For "EXISTS" and "NOT EXISTS" blocks: find block end and do break line */
                            int blockBegPos = this.sql.IndexOf(PartBegin, pos + match.Value.Length - 1);
                            if (blockBegPos > 0)
                            {
                                SqlPart blockPart = getSqlPartEnd4Pos(blockBegPos);
                                if (blockPart != null && blockPart.End < this.sql.Length - 1)
                                {
                                    pos = blockPart.End + 1;
                                    int commaIdx = this.sql.IndexOf(",", pos);
                                    if (commaIdx > 0)
                                    {
                                        string blockTail = this.sql.Substring(pos, commaIdx - pos + 1).Trim();
                                        if (blockTail.Length == 0)
                                        {
                                            pos = commaIdx + 1;
                                        }
                                    }
                                    lvl         = getLevel4Pos(pos);
                                    blockIdents = getBlockIdent4Pos(pos);
                                    insStr.Add(pos, BR + new String(CTAB, lvl) + new String(CTAB, blockIdents));
                                }
                            }
                        }
                    }
                }
            }
            int offset = 0;

            foreach (KeyValuePair <int, string> pair in insStr)
            {
                this.sql = this.sql.Insert(pair.Key + offset, pair.Value);
                offset  += pair.Value.Length;
                setSqlPartOffset(pair.Key, pair.Value.Length);
                setBlockIdentOffset(pair.Key, pair.Value.Length);
            }

            checkLineWidth();
        }
Пример #8
0
        /* 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);
        }