Example #1
0
        public bool AddColumn(string name, string type, string length)
        {
            Column col = new Column();
            bool   b   = TypeSetter.FillType(type, col, true);

            if (b == false)
            {
                if (col.tableType)
                {
                    HasTableParam = true;
                }
                else
                {
                    throw new InvalidDataException("Invalid type of \"" + type + "\"?");
                }
            }

            if (name.Length > 0 && name[0] == '@')
            {
                name = name.Substring(1);
            }

            col.name         = name;
            col.canBeNull    = false;
            col.nullableType = false;

            if (col.hasStrLength)
            {
                try
                {
                    uint len = uint.Parse(length);
                    col.length = len;
                }
                catch (System.Exception)
                {
                    col.length = 0;
                }
            }
            else
            {
                col.length = 0;
            }

            for (int i = 0; i < Columns.Count; ++i)
            {
                if (Columns[i].name.ToLower() == name.ToLower())
                {
                    throw new InvalidDataException(string.Format("Parameter '{0}' is already exists", name));
                }
            }

            Columns.Add(col);

            return(true);
        }
Example #2
0
        public bool Parse(string signature, bool noNullableTypes)
        {
            Columns.Clear();
            string signLower = signature.ToLower();

            int pos = signLower.IndexOf("proc");

            if (pos == -1)
            {
                return(false);
            }

            bool   PosInSpaceBefProcName = false;
            bool   PosProcName           = false;
            string spname = string.Empty;

            for (int i = pos; i < signature.Length; ++i)
            {
                char c = signature[i];

                if (PosInSpaceBefProcName == false)
                {
                    if (IsWhitespace(c) == false)
                    {
                        continue;
                    }
                    else
                    {
                        PosInSpaceBefProcName = true;
                    }
                }
                else if (PosInSpaceBefProcName && PosProcName == false)
                {
                    if (IsWhitespace(c))
                    {
                        continue;
                    }
                    else
                    {
                        spname     += c;
                        PosProcName = true;
                    }
                }
                else if (PosProcName)
                {
                    if (IsWhitespace(c) == false && c != '(')
                    {
                        spname += c;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            Name = spname;
            if (string.IsNullOrEmpty(Name))
            {
                throw new InvalidDataException("Empty stored procedure name");
            }

            string sigLow = signature.ToLower();

            pos = sigLow.IndexOf("inout ");

            if (pos == -1)
            {
                pos = sigLow.IndexOf("in ");
                if (pos == -1)
                {
                    pos = sigLow.IndexOf("out ");
                    if (pos == -1)
                    {
                        pos = sigLow.IndexOf("input ");
                        if (pos == -1)
                        {
                            pos = sigLow.IndexOf("output ");
                            if (pos == -1)
                            {
                                return(true);
                            }
                        }
                    }
                }
            }

            pos -= 1;
            // Parsing parameters
            while ((pos = FindInOut(sigLow, pos + 1)) != -1)
            {
                bool ParsingInOut      = true;
                bool ParsingInOutEnded = false;
                bool ParsingName       = false;
                bool ParsingNameEnded  = false;
                bool ParsingType       = false;
                bool ParsingTypeEnded  = false;
                bool ParsingLength     = false;

                Column col     = new Column();
                string name    = string.Empty;
                string type    = string.Empty;
                string output  = string.Empty;
                string slength = string.Empty;
                string inout   = string.Empty;
                for (int i = pos; i < signature.Length; ++i)
                {
                    char c = signature[i];
                    if (ParsingInOut && ParsingInOutEnded == false)
                    {
                        if (c == ',')
                        {
                            continue;
                        }

                        if (IsWhitespace(c) == false)
                        {
                            inout += c;
                        }
                        else
                        {
                            ParsingInOutEnded = true;
                            bool b = TypeSetter.FillMySQLOutputType(inout, col);
                            if (b == false)
                            {
                                throw new InvalidDataException("Invalid output type of \"" + inout + "\"?");
                            }
                        }
                    }
                    else if (ParsingInOutEnded && ParsingName == false)
                    {
                        if (IsWhitespace(c) == false)
                        {
                            ParsingName = true;
                            name       += c;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else if (ParsingName && ParsingNameEnded == false)
                    {
                        if (c == ',')
                        {
                            continue;
                        }

                        if (IsWhitespace(c) == false)
                        {
                            name += c;
                        }
                        else
                        {
                            ParsingNameEnded = true;
                        }
                    }
                    else if (ParsingNameEnded && ParsingType == false)
                    {
                        if (IsWhitespace(c) == false)
                        {
                            ParsingType = true;
                            type       += c;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else if (ParsingType && ParsingTypeEnded == false)
                    {
                        if (IsWhitespace(c) == false && c != ',' && c != '(' && c != ')')
                        {
                            type += c;
                        }
                        else
                        {
                            ParsingTypeEnded = true;
                            if (c == ')' || i == signature.Length - 1) // signature ended
                            {
                                bool b = TypeSetter.FillType(type, col, noNullableTypes);
                                if (b == false)
                                {
                                    if (col.tableType)
                                    {
                                        HasTableParam = true;
                                    }
                                    else
                                    {
                                        throw new InvalidDataException("Invalid type of \"" + type + "\"?");
                                    }
                                }
                                col.name = name;
                                Columns.Add(col);

                                pos = i;
                                break;
                            }
                        }

                        if (c == ',' || c == ')' || i == signature.Length - 1) // ) means last parameter
                        {
                            bool b = TypeSetter.FillType(type, col, noNullableTypes);
                            if (b == false)
                            {
                                if (col.tableType)
                                {
                                    HasTableParam = true;
                                }
                                else
                                {
                                    throw new InvalidDataException("Invalid type of \"" + type + "\"?");
                                }
                            }
                            col.name = name;
                            Columns.Add(col);

                            pos = i;
                            break;
                        }

                        if (c == '(')
                        {
                            ParsingLength = true;
                        }
                    }
                    else if (ParsingTypeEnded)
                    {
                        if (ParsingLength == false)
                        {
                            if (c == '(')
                            {
                                ParsingLength = true;
                            }
                            else if (c == ',' || c == ')' || i == signature.Length - 1) // no output type specified, so is input only.
                            {
                                bool b = TypeSetter.FillType(type, col, noNullableTypes);
                                if (b == false)
                                {
                                    if (col.tableType)
                                    {
                                        HasTableParam = true;
                                    }
                                    else
                                    {
                                        throw new InvalidDataException("Invalid type of \"" + type + "\"?");
                                    }
                                }
                                col.name = name;
                                Columns.Add(col);
                                pos = i;
                                break;
                            }
                            else if (IsWhitespace(c) == false)
                            {
                                output += signature[i];
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else // ParsingLength == true
                        {
                            if (c != ')')
                            {
                                slength += c;
                            }
                            else
                            {
                                bool b = TypeSetter.FillType(type, col, noNullableTypes);
                                if (b == false)
                                {
                                    if (col.tableType)
                                    {
                                        HasTableParam = true;
                                    }
                                    else
                                    {
                                        throw new InvalidDataException("Invalid type of \"" + type + "\"?");
                                    }
                                }
                                try
                                {
                                    col.length = Convert.ToUInt32(slength);
                                }
                                catch (System.Exception)
                                {
                                    throw new InvalidDataException("Invalid length :" + slength + "!");
                                }
                                col.name = name;
                                Columns.Add(col);
                                pos = i;
                                break;
                            }
                        }
                    }
                }
            }

            TableParamNum = 0;
            if (HasTableParam)
            {
                foreach (Column col in Columns)
                {
                    if (col.tableType)
                    {
                        ++TableParamNum;
                    }
                }
            }
            return(false);
        }
Example #3
0
        public bool Parse(string signature, bool noNullableTypes)
        {
            Columns.Clear();
            string signLower = signature.ToLower();

            int pos = signLower.IndexOf("type");

            if (pos == -1)
            {
                return(false);
            }

            bool   PosInSpaceBefProcName = false;
            bool   PosProcName           = false;
            string spname = string.Empty;

            for (int i = pos; i < signature.Length; ++i)
            {
                char c = signature[i];

                if (PosInSpaceBefProcName == false)
                {
                    if (IsWhitespace(c) == false)
                    {
                        continue;
                    }
                    else
                    {
                        PosInSpaceBefProcName = true;
                    }
                }
                else if (PosInSpaceBefProcName && PosProcName == false)
                {
                    if (IsWhitespace(c))
                    {
                        continue;
                    }
                    else
                    {
                        spname     += c;
                        PosProcName = true;
                    }
                }
                else if (PosProcName)
                {
                    if (IsWhitespace(c) == false)
                    {
                        spname += c;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            Name = spname;
            if (string.IsNullOrEmpty(Name))
            {
                throw new InvalidDataException("Empty table type name");
            }

            pos = signature.IndexOf("(");

            if (pos == -1)
            {
                return(true); // no parameters for this stored procedure
            }
            // Parsing parameters
            do
            {
                bool BeforeParsingName = true;
                bool ParsingName       = false;
                bool ParsingNameEnded  = false;
                bool ParsingType       = false;
                bool ParsingTypeEnded  = false;
                bool ParsingOutputType = false;
                bool ParsingLength     = false;

                Column col     = new Column();
                string name    = string.Empty;
                string type    = string.Empty;
                string output  = string.Empty;
                string slength = string.Empty;
                for (int i = pos + 1; i < signature.Length; ++i)
                {
                    char c = signature[i];
                    if (BeforeParsingName && ParsingName == false)
                    {
                        if (IsWhitespace(c))
                        {
                            continue;
                        }
                        else
                        {
                            ParsingName = true;
                            name       += c;
                        }
                    }
                    else if (ParsingName && ParsingNameEnded == false)
                    {
                        if (c == ',')
                        {
                            continue;
                        }

                        if (IsWhitespace(c) == false)
                        {
                            name += c;
                        }
                        else
                        {
                            ParsingNameEnded = true;
                        }
                    }
                    else if (ParsingNameEnded && ParsingType == false)
                    {
                        string lowerName = name.ToLower();
                        if (lowerName == "primary" || lowerName == "constraint")
                        {
                            break;
                        }

                        name = name.Trim('[', ']');

                        if (IsWhitespace(c) == false)
                        {
                            ParsingType = true;
                            type       += c;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else if (ParsingType && ParsingTypeEnded == false)
                    {
                        if (IsWhitespace(c) == false && c != ',' && c != '(' && c != ')')
                        {
                            type += c;
                        }
                        else
                        {
                            ParsingTypeEnded = true;
                            if (c == ')') // signature ended
                            {
                                bool b = TypeSetter.FillType(type, col, noNullableTypes);
                                if (b == false)
                                {
                                    throw new InvalidDataException("Invalid type of \"" + type + "\"?");
                                }
                                col.name = name;
                                Columns.Add(col);

                                pos = i;
                                break;
                            }
                        }

                        if (c == ',' || c == ')') // ) means last parameter
                        {
                            bool b = TypeSetter.FillType(type, col, noNullableTypes);
                            if (b == false)
                            {
                                throw new InvalidDataException("Invalid type of \"" + type + "\"?");
                            }
                            col.name = name;
                            Columns.Add(col);

                            pos = i;
                            break;
                        }

                        if (c == '(')
                        {
                            ParsingLength = true;
                        }
                    }
                    else if (ParsingTypeEnded && ParsingOutputType == false)
                    {
                        if (ParsingLength == false)
                        {
                            if (c == '(')
                            {
                                ParsingLength = true;
                            }
                            else if (c == ',' || c == ')') // no output type specified, so is input only.
                            {
                                bool b = TypeSetter.FillType(type, col, noNullableTypes);
                                if (b == false)
                                {
                                    throw new InvalidDataException("Invalid type of \"" + type + "\"?");
                                }
                                col.name = name;
                                Columns.Add(col);
                                pos = i;
                                break;
                            }
                            else if (IsWhitespace(c) == false)
                            {
                                ParsingOutputType = true;
                                output           += signature[i];
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else // ParsingLength == true
                        {
                            if (c != ')')
                            {
                                slength += c;
                            }
                            else
                            {
                                bool b = TypeSetter.FillType(type, col, noNullableTypes);
                                if (b == false)
                                {
                                    throw new InvalidDataException("Invalid type of \"" + type + "\"?");
                                }
                                TypeSetter.FillNullType(output, col);
                                try
                                {
                                    col.length = Convert.ToUInt32(slength);
                                }
                                catch (System.Exception)
                                {
                                    throw new InvalidDataException("Invalid length :" + slength + "!");
                                }
                                col.name = name;
                                Columns.Add(col);
                                pos = i;
                                break;
                            }
                        }
                    }
                    else if (ParsingOutputType)
                    {
                        if (IsWhitespace(c) == false && c != ')' && c != ',')
                        {
                            output += signature[i];
                        }
                        else
                        {
                            bool b = TypeSetter.FillType(type, col, noNullableTypes);
                            if (b == false)
                            {
                                throw new InvalidDataException("Invalid type of \"" + type + "\"?");
                            }
                            TypeSetter.FillNullType(output, col);
                            col.name = name;
                            Columns.Add(col);
                            pos = i;
                            break;
                        }
                    }
                }
            }while ((pos = signature.IndexOf(",", pos + 1)) != -1);

            return(false);
        }