public static SQLPart create(SQLPartType type, string partValue)
 {
     SQLPart i = new SQLPart();
     i.value = partValue;
     i.partType = type;
     return i;
 }
        public string getPart(SQLPartType part)
        {
            foreach (SQLPart tpart in sParts)
            {
                if (tpart.partType == part)
                {
                    return tpart.value;
                }

            }
            return "";
        }
 public void deletePart(SQLPartType deletePartType)
 {
     SQLPart sp = this.sParts.Find(delegate(SQLPart opart)
     {
         return (opart.partType == deletePartType);
     });
     if (sp == null) { return; }
     sp.value = "";
 }
        public void addPart(string addItem, SQLPartType addPartType)
        {
            SQLPart sp = this.sParts.Find(delegate(SQLPart opart)
            {
                return (opart.partType == addPartType);
            });
            if (sp == null)
            {
                sp = new SQLPart();
                sp.partType = addPartType;
                sParts.Add(sp);
            }
            if (sp.value == null || sp.value == "")
            {
                sp.value += SQLPartKeyword(addPartType).startkeyword;

            }
            else if (addPartType == SQLPartType.where)
            {
                if (sp.value != null && sp.value.Trim() != "" && sp.value.Trim().ToUpper() != "WHERE")
                {
                    sp.value += " and "; // +addItem;
                }
            }
            else if (addPartType == SQLPartType.fields)
            {
                sp.value += ", ";
            }
            if (addPartType == SQLPartType.where && !(String.IsNullOrEmpty(addItem)))
            {
                sp.value += "(" + addItem + ")";
            }
            else
            {
                sp.value += addItem;
            }
        }
        public static parseInstruction SQLPartKeyword(SQLPartType part)
        {
            parseInstruction res = new parseInstruction();

            res.startkeyword = "";
            res.endkeyword = "";
            switch (part)
            {
                case SQLPartType.fields:
                    res.startkeyword = "Select ";
                    res.endkeyword = " From ";
                    break;
                case SQLPartType.from:
                    res.startkeyword = " From ";
                    res.endkeyword = " Where ";
                    break;
                case SQLPartType.where:
                    res.startkeyword = " Where ";
                    res.endkeyword = " Group By ";
                    break;
                case SQLPartType.groupby:
                    res.startkeyword = " Group By ";
                    res.endkeyword = " Having ";
                    break;
                case SQLPartType.having:
                    res.startkeyword = " Having ";
                    res.endkeyword = " Order By ";
                    break;
                case SQLPartType.orderby:
                    res.startkeyword = " Order By ";
                    res.endkeyword = "";
                    break;
            }
            return res;
        }
        public static string partFromSQL(string sql, SQLPartType part)
        {
            parseInstruction pi = SQLPartKeyword(part);
            int startpos = sql.IndexOf(pi.startkeyword, 0, StringComparison.InvariantCultureIgnoreCase);
            int endpos = -1;
            if (startpos == -1)
            {
                return "";
            }

            //Find the ending spot -- loop from the current sql part to the end looking for the next end keyword match

            for (byte i = (byte)part; i <= Enum.GetNames(typeof(SQLPartType)).Length; i++)
            {
                SQLPartType tpart = (SQLPartType)i;
                pi = SQLPartKeyword(tpart);
                if (pi.endkeyword == "")
                {
                    return sql.Substring(startpos, sql.Length - startpos); //return from the startpos to the end

                }
                else
                {
                    endpos = sql.IndexOf(pi.endkeyword, startpos + 1, StringComparison.InvariantCultureIgnoreCase);
                    if (endpos > -1)
                    {
                        return sql.Substring(startpos, endpos - startpos);
                    }
                }
            }
            return "";
        }