Пример #1
0
        public void Parse()
        {
            try {
                SqlInt64.Parse(null);
                Assert.Fail("#O01");
            } catch (ArgumentNullException e) {
                Assert.AreEqual(typeof(ArgumentNullException), e.GetType(), "#O02");
            }

            try {
                SqlInt64.Parse("not-a-number");
                Assert.Fail("#O03");
            } catch (FormatException e) {
                Assert.AreEqual(typeof(FormatException), e.GetType(), "#O04");
            }

            try {
                SqlInt64.Parse("1000000000000000000000000000");
                Assert.Fail("#O05");
            } catch (OverflowException e) {
                Assert.AreEqual(typeof(OverflowException), e.GetType(), "#O06");
            }

            Assert.AreEqual((long)150, SqlInt64.Parse("150").Value, "#O07");
        }
Пример #2
0
        public void Parse()
        {
            try
            {
                SqlInt64.Parse(null);
                Assert.False(true);
            }
            catch (ArgumentNullException e)
            {
                Assert.Equal(typeof(ArgumentNullException), e.GetType());
            }

            try
            {
                SqlInt64.Parse("not-a-number");
                Assert.False(true);
            }
            catch (FormatException e)
            {
                Assert.Equal(typeof(FormatException), e.GetType());
            }

            try
            {
                SqlInt64.Parse("1000000000000000000000000000");
                Assert.False(true);
            }
            catch (OverflowException e)
            {
                Assert.Equal(typeof(OverflowException), e.GetType());
            }

            Assert.Equal(150, SqlInt64.Parse("150").Value);
        }
Пример #3
0
        public static PInt64 Parse(string s, PValueType type)
        {
            var sp = string.IsNullOrEmpty(s)
                ? new PInt64(type)
                : SqlInt64.Parse(s);

            return(sp);
        }
Пример #4
0
        public void Parse()
        {
            Assert.Throws <ArgumentNullException>(() => SqlInt64.Parse(null));

            Assert.Throws <FormatException>(() => SqlInt64.Parse("not-a-number"));

            Assert.Throws <OverflowException>(() => SqlInt64.Parse("1000000000000000000000000000"));

            Assert.Equal(150, SqlInt64.Parse("150").Value);
        }
Пример #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="s"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static PInt64 Parse(string s, PValueType type)
        {
            PInt64 sp;

            if (string.IsNullOrEmpty(s))
            {
                sp = new PInt64(type);
            }
            else
            {
                sp = SqlInt64.Parse(s);
            }

            return(sp);
        }
Пример #6
0
    public static IntDecimal Parse(SqlString s)
    {
        if (s.IsNull)
        {
            return(Null);
        }

        // Parse input string to separate out sum and prod.
        IntDecimal sp = new IntDecimal();

        string[] xy = s.Value.Split(",".ToCharArray());
        //sp.sum = Int64.Parse(xy[0]);
        sp.i = SqlInt64.Parse(xy[0]);
        sp.d = decimal.Parse(xy[1]);

        // Call ValidatePoint to enforce validation
        // for string conversions.
        if (!sp.ValidatePoint())
        {
            throw new ArgumentException("Invalid sum and prod values");
        }
        return(sp);
    }
Пример #7
0
        public static SqlInt64Array Parse(SqlString s)
        {
            if (s.IsNull)
            {
                return(Null);
            }

            return(new SqlInt64Array(SqlFormatting.ParseArray <Int64?>(s.Value,
                                                                       t => !t.Equals(SqlFormatting.NullText, StringComparison.InvariantCultureIgnoreCase) ? SqlInt64.Parse(t).Value : default(Int64?))));
        }
Пример #8
0
        private bool CheckColumnValue(ref StringBuilder value)
        {
            long curline = lines + 1;

            if (value.Length == 0 || value.ToString().ToUpper() == globals.nullstr)
            {
                if (globals.Columns[curcol].Nullable == true)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            switch (globals.Columns[curcol].Type)
            {
            case "char":
            case "nchar":
            case "varchar":
            case "nvarchar":
                if (globals.Columns[curcol].Length != -1 && value.Length > globals.Columns[curcol].Length)
                {
                    return(OutputValueErrMsg(ref value, "String truncation", "\0x1A"));
                }
                break;

            case "int":
                try
                {
                    SqlInt32 i = SqlInt32.Parse(value.ToString());
                }
                catch (Exception e)
                {
                    return(OutputValueErrMsg(ref value, e.Message, "-28"));
                }
                break;

            case "bigint":
                try
                {
                    SqlInt64 i = SqlInt64.Parse(value.ToString());
                }
                catch (Exception e)
                {
                    return(OutputValueErrMsg(ref value, e.Message, "-28"));
                }
                break;

            case "tinyint":
                try
                {
                    SqlByte i = SqlByte.Parse(value.ToString());
                }
                catch (Exception e)
                {
                    return(OutputValueErrMsg(ref value, e.Message, "28"));
                }
                break;

            case "smallint":
                try
                {
                    SqlInt16 i = SqlInt16.Parse(value.ToString());
                }
                catch (Exception e)
                {
                    return(OutputValueErrMsg(ref value, e.Message, "-28"));
                }
                break;

            case "numeric":
            case "decimal":
                try
                {
                    // SqlDecimal d = SqlDecimal.Parse(value.ToString());       // for very large values this may work, while .Net decimal overflows
                    decimal d = decimal.Parse(value.ToString());                    // the DataTable uses .Net types, so overflows will cause exceptions in outer loop
                }
                catch (Exception e)
                {
                    return(OutputValueErrMsg(ref value, e.Message, "-28.28"));
                }
                break;

            case "float":
            case "real":
                try
                {
                    SqlSingle s = SqlSingle.Parse(value.ToString());
                }
                catch (Exception e)
                {
                    return(OutputValueErrMsg(ref value, e.Message, "-28.28"));
                }
                break;

            case "datetime":
            case "datetime2":
            case "smalldatetime":
                try
                {
                    if (Convert.ToInt32(value.ToString().Substring(0, 4)) < (globals.Columns[curcol].Type == "datetime" ? 1753 : (globals.Columns[curcol].Type == "datetime2" ? 1 : 1900)))
                    {
                        return(OutputValueErrMsg(ref value, "Year value is too small", null));
                    }
                    int dot = value.ToString().IndexOf(".");
                    if (dot > 0)                                                                        // fractional seconds given?
                    {
                        if (globals.Columns[curcol].Type == "smalldatetime")                            // strip fractional seconds off smalldatetime
                        {
                            string tmp = value.ToString();
                            value.Clear();
                            value.Append(tmp.Substring(0, tmp.IndexOf('.')));
                        }
                        else
                        {
                            if (value.Length - dot > 3)
                            {
                                string tmp = value.ToString();
                                value.Clear();
                                value.Append(tmp.Substring(0, dot + 3));                                // limitation here is .NET datetime - in theory this could be a datetime2(1), which would fail...
                            }
                        }
                    }
                    SqlDateTime dt = SqlDateTime.Parse(value.ToString());
                }
                catch (Exception e)
                {
                    return(OutputValueErrMsg(ref value, e.Message, null));
                }
                break;

            case "date":
            case "time":
                try
                {
                    DateTime d = DateTime.Parse(value.ToString());
                }
                catch (Exception e)
                {
                    return(OutputValueErrMsg(ref value, e.Message, null));
                }
                break;

            case "uniqueidentifier":
                try
                {
                    SqlGuid u = SqlGuid.Parse(value.ToString());
                }
                catch (Exception e)
                {
                    return(OutputValueErrMsg(ref value, e.Message, "00000000-0000-0000-0000-000000000000"));
                }
                break;

            case "bit":
                try
                {
                    SqlBoolean u = SqlBoolean.Parse(value.ToString());
                }
                catch (Exception e)
                {
                    return(OutputValueErrMsg(ref value, e.Message, "0"));
                }
                break;
            }
            return(true);
        }
Пример #9
0
    public static SqlBoolean InitMethod([SqlFacet(MaxSize = -1)] SqlString sqlDataValue, SqlString sqlDataType)
    {
        String          dataValue     = sqlDataValue.ToString().Trim();
        String          dataType      = sqlDataType.ToString().Trim();
        GroupCollection typeGroups    = splitType.Match(dataType).Groups;
        String          typeText      = typeGroups["type"].Value;
        String          typePrecision = typeGroups["precision"].Value;
        String          typeScale     = typeGroups["scale"].Value;

        try {
            switch (typeText.ToLower())
            {
            case "bit":
                SqlBoolean.Parse(dataValue);
                break;

            case "tinyint":
                SqlByte.Parse(dataValue);
                break;

            case "smallint":
                SqlInt16.Parse(dataValue);
                break;

            case "int":
                SqlInt32.Parse(dataValue);
                break;

            case "bigint":
                SqlInt64.Parse(dataValue);
                break;

            case "smallmoney":
                if (NumberOfDecimals(dataValue) > 4)
                {
                    throw new OverflowException();
                }
                SqlMoney smallmoneyValue = SqlMoney.Parse(dataValue);
                if (SqlMoney.LessThan(smallmoneyValue, smallmoneyMinValue) || SqlMoney.GreaterThan(smallmoneyValue, smallmoneyMaxValue))
                {
                    throw new OverflowException();
                }
                break;

            case "money":
                if (NumberOfDecimals(dataValue) > 4)
                {
                    throw new OverflowException();
                }
                SqlMoney.Parse(dataValue);
                break;

            case "decimal":
            case "numeric":
                if (NumberOfDecimals(dataValue) > Convert.ToInt32(typeScale))
                {
                    throw new OverflowException();
                }
                SqlDecimal.Parse(dataValue);
                break;

            case "real":
                SqlSingle singleValue = SqlSingle.Parse(dataValue);
                if (singleValue.ToString().Length != dataValue.Length + (NumberOfWholes(dataValue) == 0 ? 1 : 0))
                {
                    throw new OverflowException();
                }
                break;

            case "float":
                SqlDouble doubleValue = SqlDouble.Parse(dataValue);
                if (doubleValue.ToString().Length != dataValue.Length + (NumberOfWholes(dataValue) == 0 ? 1 : 0))
                {
                    throw new OverflowException();
                }
                break;

            case "date":
            case "datetime":
            case "datetime2":
                SqlDateTime.Parse(dataValue);
                break;

            case "char":
            case "varchar":
            case "nchar":
            case "nvarchar":
                if (typePrecision != "max" && dataValue.Length > Convert.ToInt32(typePrecision))
                {
                    throw new OverflowException();
                }
                break;

            case "uniqueidentifier":
                SqlGuid.Parse(dataValue);
                break;

            case "geometry":
                SqlGeometry.Parse(dataValue);
                break;

            case "geography":
                SqlGeography.Parse(dataValue);
                break;

            // we do not handle these at this time
            case "xml":
            case "time":
            case "datetimeoffset":
            case "hierarchyid":
            case "image":
            case "text":
            case "ntext":
            case "rowversion":
            case "sql_variant":
            case "table":
            case "timestamp":
            case "varbinary":
            default:
                break;
            }
            return(SqlBoolean.True);
        }
        catch {
            return(SqlBoolean.False);
        }
    }
Пример #10
0
 public override object FromStringValue(string xml)
 {
     return(SqlInt64.Parse(xml));
 }