Beispiel #1
0
 public Field AsBigInt()
 {
     return(CastTo(PrimitiveTypes.Numeric(SqlTypeCode.BigInt)));
 }
 public static SqlObject Array(SqlArray array)
 {
     return(new SqlObject(PrimitiveTypes.Array(array.Length), array));
 }
Beispiel #3
0
        public static SqlType Deserialize(BinaryReader reader, ISqlTypeResolver typeResolver)
        {
            var typeCode = (SqlTypeCode)reader.ReadByte();

            if (SqlBooleanType.IsBooleanType(typeCode))
            {
                return(PrimitiveTypes.Boolean(typeCode));
            }
            if (SqlDateTimeType.IsDateType(typeCode))
            {
                return(PrimitiveTypes.DateTime(typeCode));
            }

            if (typeCode == SqlTypeCode.DayToSecond)
            {
                return(PrimitiveTypes.DayToSecond());
            }
            if (typeCode == SqlTypeCode.YearToMonth)
            {
                return(PrimitiveTypes.YearToMonth());
            }

            if (SqlCharacterType.IsStringType(typeCode))
            {
                var maxSize = reader.ReadInt32();

                CultureInfo locale    = null;
                var         hasLocale = reader.ReadByte() == 1;
                if (hasLocale)
                {
                    var name = reader.ReadString();
                    locale = new CultureInfo(name);
                }

                return(PrimitiveTypes.String(typeCode, maxSize, locale));
            }

            if (SqlNumericType.IsNumericType(typeCode))
            {
                var size  = reader.ReadInt32();
                var scale = reader.ReadInt32();

                return(PrimitiveTypes.Numeric(typeCode, size, scale));
            }

            if (SqlBinaryType.IsBinaryType(typeCode))
            {
                var size = reader.ReadInt32();
                return(PrimitiveTypes.Binary(typeCode, size));
            }

            if (typeCode == SqlTypeCode.Type)
            {
                if (typeResolver == null)
                {
                    throw new NotSupportedException("User-Defined types require a resolver context.");
                }

                // TODO: support type arguments
                var typeName = reader.ReadString();
                return(typeResolver.Resolve(new SqlTypeResolveInfo(typeName)));
            }

            /*
             * TODO:
             * if (typeCode == SqlTypeCode.QueryPlan)
             *      return new SqlQueryType();
             */

            if (typeCode == SqlTypeCode.Array)
            {
                var size = reader.ReadInt32();
                return(new SqlArrayType(size));
            }

            throw new NotSupportedException($"The type code '{typeCode}' does not support deserialization");
        }
 public static SqlObject Integer(int value)
 {
     return(new SqlObject(PrimitiveTypes.Integer(), (SqlNumber)value));
 }
 public static SqlObject Double(double value)
 {
     return(new SqlObject(PrimitiveTypes.Double(), (SqlNumber)value));
 }
        private static SqlType GetSqlType(ISqlValue value)
        {
            if (value == null ||
                SqlNull.Value == value)
            {
                throw new ArgumentException();
            }

            if (value is SqlNumber)
            {
                var number = (SqlNumber)value;

                if (number.CanBeInt32)
                {
                    return(PrimitiveTypes.Integer());
                }
                if (number.CanBeInt64)
                {
                    return(PrimitiveTypes.BigInt());
                }

                if (number.Precision == SqlNumericType.FloatPrecision)
                {
                    return(new SqlNumericType(SqlTypeCode.Float, number.Precision, number.Scale));
                }
                if (number.Precision == SqlNumericType.DoublePrecision)
                {
                    return(new SqlNumericType(SqlTypeCode.Double, number.Precision, number.Scale));
                }
                if (number.Precision == SqlNumericType.DecimalPrecision)
                {
                    return(new SqlNumericType(SqlTypeCode.Decimal, number.Precision, number.Scale));
                }

                return(PrimitiveTypes.Numeric(number.Precision, number.Scale));
            }

            if (value is ISqlString)
            {
                // TODO: support the long string
                var length = ((ISqlString)value).Length;

                return(PrimitiveTypes.VarChar((int)length));
            }

            if (value is SqlBinary)
            {
                var bin = (SqlBinary)value;

                return(PrimitiveTypes.VarBinary((int)bin.Length));
            }

            if (value is SqlDateTime)
            {
                return(PrimitiveTypes.TimeStamp());
            }

            if (value is SqlBoolean)
            {
                return(PrimitiveTypes.Boolean());
            }

            if (value is SqlYearToMonth)
            {
                return(PrimitiveTypes.YearToMonth());
            }
            if (value is SqlDayToSecond)
            {
                return(PrimitiveTypes.DayToSecond());
            }

            if (value is SqlArray)
            {
                return(PrimitiveTypes.Array(((SqlArray)value).Length));
            }

            throw new NotSupportedException();
        }
 public static SqlObject Bit(SqlBoolean?value)
 => new SqlObject(PrimitiveTypes.Bit(), value);
Beispiel #8
0
 public static Field TinyInt(byte value)
 {
     return(Number(PrimitiveTypes.Numeric(SqlTypeCode.TinyInt), value));
 }
Beispiel #9
0
 public static Field SmallInt(short value)
 {
     return(Number(PrimitiveTypes.Numeric(SqlTypeCode.SmallInt), value));
 }
Beispiel #10
0
 public static Field Boolean(SqlBoolean value)
 {
     return(new Field(PrimitiveTypes.Boolean(), value));
 }
Beispiel #11
0
 public static Field Number(SqlNumber value)
 {
     return(Number(PrimitiveTypes.Numeric(), value));
 }
Beispiel #12
0
 public Field AsTimeStamp()
 {
     return(CastTo(PrimitiveTypes.TimeStamp()));
 }
Beispiel #13
0
 public Field AsDate()
 {
     return(CastTo(PrimitiveTypes.Date()));
 }
Beispiel #14
0
 public Field AsVarChar()
 {
     return(CastTo(PrimitiveTypes.String(SqlTypeCode.VarChar)));
 }
Beispiel #15
0
 public static Field VarChar(SqlString s)
 {
     return(new Field(PrimitiveTypes.String(SqlTypeCode.VarChar), s));
 }
Beispiel #16
0
 public static Field Integer(int value)
 {
     return(Number(PrimitiveTypes.Numeric(SqlTypeCode.Integer), value));
 }
Beispiel #17
0
        public static Field Create(object value)
        {
            // Numeric values ...
            if (value is bool)
            {
                return(Boolean((bool)value));
            }
            if (value is byte)
            {
                return(TinyInt((byte)value));
            }
            if (value is short)
            {
                return(SmallInt((short)value));
            }
            if (value is int)
            {
                return(Integer((int)value));
            }
            if (value is long)
            {
                return(BigInt((long)value));
            }
            if (value is float)
            {
                return(Float((float)value));
            }
            if (value is double)
            {
                return(Double((double)value));
            }

            if (value is SqlNumber)
            {
                var num = (SqlNumber)value;
                if (num.IsNull)
                {
                    return(Null(PrimitiveTypes.Numeric()));
                }

                if (num.CanBeInt32)
                {
                    return(Integer(num.ToInt32()));
                }
                if (num.CanBeInt64)
                {
                    return(BigInt(num.ToInt64()));
                }

                return(Number(num));
            }

            if (value is SqlDateTime)
            {
                return(Date((SqlDateTime)value));
            }
            if (value is DateTime)
            {
                return(Date((DateTime)value));
            }
            if (value is DateTimeOffset)
            {
                return(Date((DateTimeOffset)value));
            }

            // String values ...
            if (value is string)
            {
                return(String((string)value));
            }
            if (value is SqlString)
            {
                var s = (SqlString)value;
                if (s.IsNull)
                {
                    return(Null(PrimitiveTypes.String()));
                }

                return(String(s));
            }
            if (value is SqlLongString)
            {
                var clob = (SqlLongString)value;
                if (clob.IsNull)
                {
                    return(Null(PrimitiveTypes.Clob()));
                }

                return(Clob(clob));
            }


            if (value == null)
            {
                return(Null());
            }

            // Trivial case
            if (value is Field)
            {
                return((Field)value);
            }

            throw new NotSupportedException("Cannot build an object from the given value.");
        }
Beispiel #18
0
 public static Field BigInt(long value)
 {
     return(Number(PrimitiveTypes.Numeric(SqlTypeCode.BigInt), value));
 }
Beispiel #19
0
 public static SqlObject Boolean(SqlBoolean?value)
 {
     return(new SqlObject(PrimitiveTypes.Boolean(), value));
 }
Beispiel #20
0
 public static Field Float(float value)
 {
     return(Number(PrimitiveTypes.Numeric(SqlTypeCode.Float), new SqlNumber(value)));
 }
Beispiel #21
0
 public static SqlObject String(SqlString value)
 {
     return(new SqlObject(PrimitiveTypes.String(), value));
 }
Beispiel #22
0
 public static Field Double(double value)
 {
     return(Number(PrimitiveTypes.Numeric(SqlTypeCode.Double), new SqlNumber(value)));
 }
Beispiel #23
0
 public static SqlObject BigInt(long value)
 {
     return(new SqlObject(PrimitiveTypes.BigInt(), (SqlNumber)value));
 }
Beispiel #24
0
 public static Field String(SqlString s)
 {
     return(new Field(PrimitiveTypes.String(SqlTypeCode.String), s));
 }
Beispiel #25
0
 public static SqlObject Numeric(SqlNumber value)
 {
     return(new SqlObject(PrimitiveTypes.Numeric(value.Precision, value.Scale), value));
 }
Beispiel #26
0
 public static Field Clob(SqlLongString s)
 {
     return(new Field(PrimitiveTypes.Clob(s.Encoding, (int)s.Length), s));
 }
Beispiel #27
0
        ///// <summary>
        ///// Gets the SQL type code corresponding to the given type
        ///// </summary>
        ///// <param name="type">The type to get the corresponding type code</param>
        ///// <returns>
        ///// Returns a <see cref="SqlTypeCode"/> that represents a code corresponding
        ///// the SQL type.
        ///// </returns>
        ///// <exception cref="NotSupportedException">If the given <paramref name="type"/>
        ///// is not supported by the system</exception>
        //public static SqlTypeCode GetTypeCode(Type type) {
        //	if (type == null)
        //		return SqlTypeCode.Unknown;

        //	if (type == typeof(bool))
        //		return SqlTypeCode.Boolean;
        //	if (type == typeof(byte))
        //		return SqlTypeCode.TinyInt;
        //	if (type == typeof(short))
        //		return SqlTypeCode.SmallInt;
        //	if (type == typeof(int))
        //		return SqlTypeCode.Integer;
        //	if (type == typeof(long))
        //		return SqlTypeCode.BigInt;
        //	if (type == typeof(float))
        //		return SqlTypeCode.Real;
        //	if (type == typeof(double))
        //		return SqlTypeCode.Double;
        //	if (type == typeof(DateTime) ||
        //	    type == typeof(DateTimeOffset))
        //		return SqlTypeCode.TimeStamp;
        //	if (type == typeof(TimeSpan))
        //		return SqlTypeCode.DayToSecond;
        //	if (type == typeof(string))
        //		return SqlTypeCode.String;
        //	if (type == typeof(byte[]))
        //		return SqlTypeCode.Binary;

        //	if (type == typeof(SqlBoolean))
        //		return SqlTypeCode.Boolean;
        //	if (type == typeof(SqlNumber))
        //		return SqlTypeCode.Numeric;
        //	if (type == typeof(SqlDateTime))
        //		return SqlTypeCode.TimeStamp;
        //	if (type == typeof(SqlString))
        //		return SqlTypeCode.String;
        //	if (type == typeof(SqlDayToSecond))
        //		return SqlTypeCode.DayToSecond;

        //	throw new NotSupportedException(String.Format("The type '{0}' is not supported.", type));
        //}

        public static bool IsPrimitiveType(SqlTypeCode typeCode)
        {
            return(PrimitiveTypes.IsPrimitive(typeCode));
        }
Beispiel #28
0
 public static Field Date(SqlTypeCode typeCode, SqlDateTime value)
 {
     return(new Field(PrimitiveTypes.DateTime(typeCode), value));
 }
Beispiel #29
0
        public static void AddInvalidStyledQueryParameter(SqlParameterNaming naming, string paramName)
        {
            var query = new SqlCommand($"INSERT INTO a (col1) VALUES ({paramName})", naming);

            Assert.Throws <ArgumentException>(() => query.Parameters.Add(new SqlParameter(paramName, PrimitiveTypes.Boolean(), SqlBoolean.True)));
        }
Beispiel #30
0
 public Field AsInteger()
 {
     return(CastTo(PrimitiveTypes.Numeric(SqlTypeCode.Integer)));
 }