Exemplo n.º 1
0
        public static MsSqlColumnType Create(string databaseType)
        {
            var dbType = databaseType.ToLowerInvariant();

            if (IntegerMsSqlColumnType.TryCreate(dbType, out var integerType))
            {
                return(integerType);
            }

            if (FloatingPointMsSqlColumnType.TryCreate(dbType, out var floatingType))
            {
                return(floatingType);
            }

            if (DecimalMsSqlColumnType.TryCreate(dbType, out var decimalType))
            {
                return(decimalType);
            }

            if (DateTimeMsSqlColumnType.TryCreate(dbType, out var dateTimeType))
            {
                return(dateTimeType);
            }

            if (VarCharMsSqlColumnType.TryCreate(dbType, out var varcharType))
            {
                return(varcharType);
            }

            if (TextMsSqlColumnType.TryCreate(dbType, out var textType))
            {
                return(textType);
            }

            throw new NotImplementedException($"The database type \'{dbType}\' is not yet supported");
        }
Exemplo n.º 2
0
        public static bool TryCreate(string dbType, out VarCharMsSqlColumnType returnValue)
        {
            bool isVar;
            bool isUnicode;

            var curDbType = dbType;

            if (curDbType.StartsWith("n"))
            {
                isUnicode = true;
                curDbType = curDbType.Substring(1);
            }
            else
            {
                isUnicode = false;
            }

            if (curDbType.StartsWith("var"))
            {
                isVar     = true;
                curDbType = curDbType.Substring(3);
            }
            else
            {
                isVar = false;
            }

            if (curDbType.StartsWith("char"))
            {
                curDbType = curDbType.Substring(4);
                if (curDbType.Length > 0 && curDbType[0] == '(' && curDbType[curDbType.Length - 1] == ')')
                {
                    var lengthString = curDbType.Substring(1, curDbType.Length - 2);

                    int?maxLength;
                    if (int.TryParse(lengthString, out var length))
                    {
                        maxLength = length;
                    }
                    else if (lengthString == "max")
                    {
                        maxLength = null;
                    }
                    else
                    {
                        throw new InvalidOperationException($"Invalid argument for char type: {dbType}");
                    }

                    returnValue = new VarCharMsSqlColumnType(isUnicode, isVar, maxLength);
                    return(true);
                }

                if (curDbType.Length == 0)
                {
                    returnValue = new VarCharMsSqlColumnType(isUnicode, isVar, null);
                    return(true);
                }

                throw new InvalidOperationException($"Invalid char type: {dbType}");
            }
            else
            {
                returnValue = null;
                return(false);
            }
        }