Beispiel #1
0
        public override object ConvertTo(ISqlObject obj, Type destType)
        {
            if (!(obj is SqlDateTime))
            {
                throw new NotSupportedException();
            }

            if (obj.IsNull)
            {
                return(null);
            }

            if (destType == typeof(DateTime?) ||
                destType == typeof(DateTimeOffset?))
            {
                destType = Nullable.GetUnderlyingType(destType);
            }

            var dateTime = (SqlDateTime)obj;

            if (destType == typeof(DateTime))
            {
                return(dateTime.ToDateTime());
            }
            if (destType == typeof(DateTimeOffset))
            {
                return(dateTime.ToDateTimeOffset());
            }

            return(base.ConvertTo(obj, destType));
        }
Beispiel #2
0
        public override void SerializeObject(Stream stream, ISqlObject obj)
        {
            var writer = new BinaryWriter(stream);

            if (obj is SqlNull)
            {
                writer.Write((byte)0);
            }
            else
            {
                var date = (SqlDateTime)obj;

                if (date.IsNull)
                {
                    writer.Write((byte)0);
                }
                else
                {
                    var bytes = date.ToByteArray(true);

                    writer.Write((byte)1);
                    writer.Write(bytes.Length);
                    writer.Write(bytes);
                }
            }
        }
Beispiel #3
0
        public override object ConvertTo(ISqlObject obj, Type destType)
        {
            if (obj == null || obj.IsNull)
                return null;

            throw new InvalidCastException();
        }
Beispiel #4
0
        protected virtual async Task <IEnumerable <StoryRunnerMessageWork> > CreateSendObjectsSQL(
            IRepository repository,
            ISqlObject sqlObject,
            string contextType)
        {
            if (sqlObject != null && !string.IsNullOrEmpty(sqlObject.Sql))
            {
                var objects = await repository.GetAllAsync <dynamic>(sqlObject);

                if (objects.Any())
                {
                    return(objects
                           .Select(x => new StoryRunnerMessageWork
                    {
                        Name = contextType,
                        Context = JsonConvert.SerializeObject(x, _settings),
                    }));
                }

                return(Enumerable.Empty <StoryRunnerMessageWork>());
            }
            else
            {
                return(new[]
                {
                    new StoryRunnerMessageWork
                    {
                        Name = contextType,
                        Context = JsonConvert.SerializeObject(string.Empty),
                    },
                });
            }
        }
Beispiel #5
0
        public override ISqlObject Add(ISqlObject a, ISqlObject b)
        {
            if (!(a is SqlNumber))
                throw new ArgumentException();
            if (b is SqlNull || b.IsNull)
                return SqlNumber.Null;

            var num1 = (SqlNumber) a;
            SqlNumber num2;

            if (b is SqlBoolean) {
                if ((SqlBoolean) b) {
                    num2 = SqlNumber.One;
                } else if (!(SqlBoolean) b) {
                    num2 = SqlNumber.Zero;
                } else {
                    num2 = SqlNumber.Null;
                }
            } else if (b is SqlNumber) {
                num2 = (SqlNumber) b;
            } else {
                throw new ArgumentException();
            }

            return num1.Add(num2);
        }
        public QueryParameter(string name, SqlType sqlType, ISqlObject value)
        {
            if (sqlType == null)
            {
                throw new ArgumentNullException("sqlType");
            }

            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            if (!String.Equals(name, Marker, StringComparison.Ordinal) &&
                name[0] == NamePrefix)
            {
                name = name.Substring(1);

                if (String.IsNullOrEmpty(name))
                {
                    throw new ArgumentException("Cannot specify only the variable bind prefix as parameter.");
                }
            }

            Name      = name;
            SqlType   = sqlType;
            Value     = value;
            Direction = QueryParameterDirection.In;
        }
Beispiel #7
0
        public override ISqlObject CastTo(ISqlObject value, SqlType destType)
        {
            var xmlNode = value as SqlXmlNode;

            if (xmlNode == null)
            {
                return(SqlNull.Value);
            }

            var destTypeCode = destType.TypeCode;

            switch (destTypeCode)
            {
            case SqlTypeCode.String:
            case SqlTypeCode.VarChar:
            case SqlTypeCode.LongVarChar:
                // TODO: more advanced casting...
                return(xmlNode.ToSqlString());

            case SqlTypeCode.Binary:
            case SqlTypeCode.LongVarBinary:
            case SqlTypeCode.VarBinary:
                // TODO: more advanced casting...
                return(xmlNode.ToSqlBinary());

            default:
                throw new InvalidCastException(String.Format("Cannot cast XML node to type '{0}'.", destType));
            }
        }
Beispiel #8
0
		/// <summary>
		/// Constructs a new database data object with a specific <see cref="SqlType"/> 
		/// and handling the specified <see cref="ISqlObject"/> value.
		/// </summary>
		/// <param name="type">The specific <see cref="SqlType"/> that is used by this object
		/// to shape the data and compute operations.</param>
		/// <param name="value">The innermost value of the object to be handled.</param>
		/// <exception cref="ArgumentNullException">
		/// If the specified <paramref name="type"/> is <c>null</c>.
		/// </exception>
		public DataObject(SqlType type, ISqlObject value) {
			if (type == null)
				throw new ArgumentNullException("type");

			Type = type;
			Value = value;
		}
Beispiel #9
0
        private static string GetWhereExpression(this ISqlObject obj)
        {
            string name      = null;
            object value     = null;
            bool   isencrypt = false;

            foreach (var property in obj.GetType().GetProperties())
            {
                if (property.CanWrite && property.CanRead && property.GetCustomAttribute <SqlSearchKeyAttribute>() != null && property.TryGetSqlElementName(out string name1))
                {
                    name      = name1;
                    value     = property.GetValue(obj);
                    isencrypt = property.IsSqlEncrypt();
                    break;
                }
            }

            if (name != null)
            {
                return(GetWhereExpression(name, value, isencrypt));
            }
            else
            {
                throw new KeyNotFoundException();
            }
        }
        public override void SerializeObject(Stream stream, ISqlObject obj)
        {
            var writer = new BinaryWriter(stream);

            if (obj is SqlNull)
            {
                writer.Write((byte)0);
            }
            else if (obj is SqlBoolean)
            {
                var b = (SqlBoolean)obj;

                if (b.IsNull)
                {
                    writer.Write((byte)0);
                }
                else
                {
                    var value = (bool)b;
                    writer.Write((byte)1);
                    writer.Write((byte)(value ? 1 : 0));
                }
            }
            else
            {
                throw new ArgumentException("Cannot serialize an object that is not a BOOLEAN using a boolean type.");
            }
        }
Beispiel #11
0
        internal override int ColumnSizeOf(ISqlObject obj)
        {
            if (obj is SqlNull)
            {
                return(1);
            }

            if (!(obj is SqlNumber))
            {
                throw new ArgumentException(String.Format("Cannot determine the size of an object of type '{0}'.", obj.GetType()));
            }

            var number = (SqlNumber)obj;

            if (number.IsNull)
            {
                return(1);
            }

            if (number.CanBeInt32)
            {
                return(1 + 4);
            }
            if (number.CanBeInt64)
            {
                return(1 + 8);
            }

            // Type + Scale + Precision + Byte Count
            var length = number.ToByteArray().Length;

            return(1 + 4 + 4 + 4 + length);
        }
Beispiel #12
0
        public override void SerializeObject(Stream stream, ISqlObject obj)
        {
            var writer = new BinaryWriter(stream);

            if (obj is SqlBinary)
            {
                var bin = (SqlBinary)obj;
                writer.Write((byte)1);
                writer.Write((int)bin.Length);
                writer.Write(bin.ToByteArray());
            }
            else if (obj is SqlLongBinary)
            {
                var lob = (SqlLongBinary)obj;

                writer.Write((byte)2);

                // TODO:

                throw new NotImplementedException();
            }
            else
            {
                base.SerializeObject(stream, obj);
            }
        }
        public override int Compare(ISqlObject x, ISqlObject y)
        {
            if (!(x is SqlBoolean))
            {
                throw new ArgumentException();
            }

            var        a = (SqlBoolean)x;
            SqlBoolean b;

            if (y is SqlNumber)
            {
                b = ((SqlNumber)y) == SqlNumber.One ? SqlBoolean.True : ((SqlNumber)y) == SqlNumber.Zero ? SqlBoolean.False : SqlBoolean.Null;
            }
            else if (y is SqlBoolean)
            {
                b = (SqlBoolean)y;
            }
            else
            {
                throw new ArgumentException();
            }

            return(a.CompareTo(b));
        }
Beispiel #14
0
        public override int Compare(ISqlObject x, ISqlObject y)
        {
            if (x == null)
            {
                throw new ArgumentNullException("x");
            }

            if (!(x is ISqlString) ||
                !(y is ISqlString))
            {
                throw new ArgumentException("Cannot compare objects that are not strings.");
            }

            if (x.IsNull && y.IsNull)
            {
                return(0);
            }
            if (x.IsNull && !y.IsNull)
            {
                return(1);
            }
            if (!x.IsNull && y.IsNull)
            {
                return(-1);
            }

            // If lexicographical ordering,
            if (Locale == null)
            {
                return(LexicographicalOrder((ISqlString)x, (ISqlString)y));
            }

            return(Collator.Compare(x.ToString(), y.ToString()));
        }
Beispiel #15
0
        public static bool Exists(this ISqlObject obj)
        {
            string cmd = $"select * from {obj.Table} {obj.GetWhereExpression()}";

            var table = obj.SqlProvider.Query(cmd);

            return(table.Rows != null && table.Rows.Count != 0);
        }
Beispiel #16
0
        public void FromVariableTest()
        {
            string     variable = "@@error";
            ISqlObject obj      = SqlObject.FromVariable(variable);

            Assert.AreEqual <string>(variable, obj.Fullname);
            Assert.AreEqual <string>(variable, obj.SqlString);
        }
Beispiel #17
0
 /// <summary>
 /// Initialize a new instance of MySqlField class from a field name and alias.
 /// </summary>
 /// <param name="source">A SQL Server source.</param>
 /// <param name="name">Field name.</param>
 /// <param name="alias">Field alias.</param>
 /// <exception cref="System.ArgumentException"><paramref name="source"/> is null.</exception>
 /// <exception cref="System.ArgumentException"><paramref name="name"/> is null or empty.</exception>
 public MySqlField(ISqlObject source, string name, string alias)
     : base(new SqlObject(source, new SqlStringExpression(name), name, new MySqlObjectFullnameProvider(), new SqlObjectFullnameSqlStringProvider()), alias, new SqlAliasObjectFullnameProvider(), new MySqlAliasObjectSqlStringProvider())
 {
     if (source == null)
     {
         throw new ArgumentException("SQL source is null.", "source");
     }
 }
Beispiel #18
0
        bool ISqlObject.IsComparableTo(ISqlObject other)
        {
            if (!(other is SqlGeometry))
            {
                return(false);
            }

            return(IsComparableTo((SqlGeometry)other));
        }
Beispiel #19
0
        public void SqlStringTest_Expression()
        {
            ISqlObject      source     = new SqlServerSource("Table");
            ISqlExpression  expression = new SqlFunction("MAX").AddArgument(new SqlServerField(source, "ID", null));
            ISqlObject      field      = SqlObject.FromVariable("ROWNUM");
            ISqlGroupClause target     = new SqlGroupClause().AddExpressions(expression, field);

            Assert.AreEqual <string>(string.Format("{0} {1}, {2}", target.Keyword, expression.SqlString, field.Fullname), target.SqlString);
        }
Beispiel #20
0
        public void SqlStringTest_More()
        {
            ISqlObject      source = new SqlServerSource("Table");
            ISqlObject      field0 = new SqlServerField(source, "Field0", null);
            ISqlObject      field1 = SqlObject.FromVariable("ROWNUM");
            ISqlGroupClause target = new SqlGroupClause().AddExpressions(field0, field1);

            Assert.AreEqual <string>(string.Format("{0} {1}, {2}", target.Keyword, field0.Fullname, field1.Fullname), target.SqlString);
        }
        /// <inheritdoc />
        public string GetFullname(ISqlObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentException("SQL object is null.", "obj");
            }

            return(obj.Owner != null?string.Format("{0}.`{1}`", obj.Owner.Fullname, obj.Name) : string.Format("`{0}`", obj.Name));
        }
Beispiel #22
0
        public int CompareTo(ISqlObject other)
        {
            if (other is SqlYearToMonth)
            {
                return(CompareTo((SqlYearToMonth)other));
            }

            throw new NotSupportedException();
        }
        /// <inheritdoc />
        public string GetFullname(ISqlObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentException("SQL object is null.", "obj");
            }

            return(obj.Owner != null?string.Format("{0}.\"{1}\"", obj.Owner.Fullname, obj.Name.ToUpper()) : string.Format("\"{0}\"", obj.Name.ToUpper()));
        }
        public void SqlStringTest_More()
        {
            ISqlObject      source = new SqlServerSource("Table");
            ISqlObject      field0 = new SqlServerField(source, "AddTime", null);
            ISqlObject      field1 = SqlObject.FromVariable("ROWNUM");
            ISqlOrderClause target = new SqlOrderClause().AddExpression(field0, SqlOrder.Desc).AddExpression(field1, SqlOrder.Asc);

            Assert.AreEqual <string>(string.Format("{0} ({1}) DESC, ({2}) ASC", target.Keyword, field0.Fullname, field1.Fullname), target.SqlString);
        }
Beispiel #25
0
        int IComparable <ISqlObject> .CompareTo(ISqlObject other)
        {
            if (!(other is SqlNumber))
            {
                throw new ArgumentException();
            }

            return(CompareTo((SqlNumber)other));
        }
Beispiel #26
0
        public override object ConvertTo(ISqlObject obj, Type destType)
        {
            if (obj == null || obj.IsNull)
            {
                return(null);
            }

            throw new InvalidCastException();
        }
Beispiel #27
0
        public override ISqlObject And(ISqlObject a, ISqlObject b)
        {
            if (a.IsNull || b.IsNull)
                return SqlBoolean.Null;

            var b1 = (SqlBoolean) a;
            var b2 = (SqlBoolean) b;

            return b1.And(b2);
        }
Beispiel #28
0
        public SqlXmlNode Update(string xpath, ISqlObject value, string xmlNs)
        {
            byte[] updated;
            if (!Update(xpath, value, xmlNs, out updated))
            {
                return(Null);
            }

            return(new SqlXmlNode(updated));
        }
Beispiel #29
0
        /// <summary>
        /// Constructs a new database data object with a specific <see cref="SqlType"/>
        /// and handling the specified <see cref="ISqlObject"/> value.
        /// </summary>
        /// <param name="type">The specific <see cref="SqlType"/> that is used by this object
        /// to shape the data and compute operations.</param>
        /// <param name="value">The innermost value of the object to be handled.</param>
        /// <exception cref="ArgumentNullException">
        /// If the specified <paramref name="type"/> is <c>null</c>.
        /// </exception>
        public Field(SqlType type, ISqlObject value)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            Type  = type;
            Value = value;
        }
        public override ISqlObject CastTo(ISqlObject value, SqlType destType)
        {
            var bValue = ((SqlBoolean)value);

            if (destType is StringType)
            {
                if (bValue.IsNull)
                {
                    return(SqlString.Null);
                }

                string s;
                if (TypeCode == SqlTypeCode.Bit)
                {
                    s = bValue ? "1" : "0";
                }
                else
                {
                    s = bValue ? "true" : "false";
                }

                return(new SqlString(s));
            }
            if (destType is NumericType)
            {
                SqlNumber num;
                if (bValue == SqlBoolean.Null)
                {
                    num = SqlNumber.Null;
                }
                else if (bValue)
                {
                    num = SqlNumber.One;
                }
                else
                {
                    num = SqlNumber.Zero;
                }

                return(num);
            }
            if (destType is BinaryType)
            {
                var bytes = new[] { bValue ? (byte)1 : (byte)0 };
                return(new SqlBinary(bytes));
            }

            if (destType is BooleanType)
            {
                return(value);
            }

            return(base.CastTo(value, destType));
        }
Beispiel #31
0
        public void FromFunctionTest()
        {
            ISqlFunction function = new SqlFunction("SUM");

            function.AddArgument(new SqlStringExpression("1"));
            function.AddArgument(new SqlStringExpression("2"));
            ISqlObject obj = SqlObject.FromFunction(function);

            Assert.AreEqual <string>(function.SqlString, obj.Fullname);
            Assert.AreEqual <string>(function.SqlString, obj.SqlString);
        }
        /// <summary>
        /// Initialize a new instance of SqlServerObjectValue.
        /// </summary>
        /// <param name="obj">A SQL object.</param>
        /// <param name="type">The SqlDbType of <paramref name="obj"/>.</param>
        /// <param name="value">The value of <paramref name="obj"/>.</param>
        /// <exception cref="System.ArgumentException"><paramref name="obj"/> is null.</exception>
        public SqlServerObjectValue(ISqlObject obj, SqlDbType type, object value)
        {
            if (obj == null)
            {
                throw new ArgumentException("SQL object is null.", "obj");
            }

            this.Object = obj;
            this.Type   = type;
            this.Value  = value;
        }
        public override SqlBoolean IsEqualTo(ISqlObject a, ISqlObject b)
        {
            if (a.IsNull && b.IsNull)
            {
                return(true);
            }

            var b1 = (SqlBoolean)a;
            var b2 = (SqlBoolean)b;

            return(b1.Equals(b2));
        }
Beispiel #34
0
        public override void SerializeObject(Stream stream, ISqlObject obj)
        {
            var writer = new BinaryWriter(stream);

            if (obj is SqlNull) {
                writer.Write((byte)1);
            } else if (obj == null || obj.IsNull) {
                writer.Write((byte) 2);
            } else {
                throw new FormatException();
            }
        }
        public string BuildName(ISqlObject sqlObject)
        {
            var sb = new StringBuilder();
            var schemaMember = sqlObject as ISchemaMember;
            if (schemaMember != null)
            {
                sb.Append(GetSchemaNameSql(schemaMember))
                    .Append(".");
            }
            sb.Append(keywordEncoder.Encode(sqlObject.Name));

            return sb.ToString();
        }
Beispiel #36
0
        public QueryResultRow(ISqlObject[] values, int[] valueSizes)
        {
            if (values == null)
                throw new ArgumentNullException("values");
            if (valueSizes == null)
                throw new ArgumentNullException("valueSizes");

            if (values.Length != valueSizes.Length)
                throw new ArgumentException();

            Values = values;
            ValueSizes = valueSizes;
        }
Beispiel #37
0
        public override object ConvertTo(ISqlObject obj, Type destType)
        {
            var xmlNode = obj as SqlXmlNode;
            if (xmlNode == null || xmlNode.IsNull)
                return null;

            if (destType == typeof (string))
                return xmlNode.ToString();
            if (destType == typeof (XmlNode))
                return xmlNode.ToXmlNode();
            if (destType == typeof (byte[]))
                return xmlNode.ToBytes();

            return base.ConvertTo(obj, destType);
        }
Beispiel #38
0
        public override void SerializeObject(Stream stream, ISqlObject obj)
        {
            var writer = new BinaryWriter(stream, Encoding.Unicode);

            var queryPlanObj = (SqlQueryObject) obj;
            if (queryPlanObj.IsNull) {
                writer.Write((byte) 0);
            } else {
                writer.Write((byte)1);
                var nodeTypeString = queryPlanObj.QueryPlan.GetType().AssemblyQualifiedName;
                if (String.IsNullOrEmpty(nodeTypeString))
                    throw new InvalidOperationException();

                writer.Write(nodeTypeString);
                SerializePlan(queryPlanObj.QueryPlan, writer);
            }
        }
Beispiel #39
0
        public QueryParameter(string name, SqlType sqlType, ISqlObject value)
        {
            if (sqlType == null)
                throw new ArgumentNullException("sqlType");

            if (String.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");

            if (!String.Equals(name, Marker, StringComparison.Ordinal) &&
                name[0] != NamePrefix)
                throw new ArgumentException(String.Format("The parameter name '{0}' is invalid: must be '{1}' or starting with '{2}'", name, Marker, NamePrefix));

            Name = name;
            SqlType = sqlType;
            Value = value;
            Direction = QueryParameterDirection.In;
        }
Beispiel #40
0
        public override ISqlObject CastTo(ISqlObject value, SqlType destType)
        {
            var sqlType = destType.TypeCode;
            var binary = ((ISqlBinary) value);

            ISqlObject casted;

            switch (sqlType) {
                case SqlTypeCode.Bit:
                case SqlTypeCode.Boolean:
                    casted = ToBoolean(binary);
                    break;
                    // TODO: All other casts
                default:
                    throw new InvalidCastException();
            }

            return casted;
        }
Beispiel #41
0
        public QueryParameter(string name, SqlType sqlType, ISqlObject value)
        {
            if (sqlType == null)
                throw new ArgumentNullException("sqlType");

            if (String.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");

            if (!String.Equals(name, Marker, StringComparison.Ordinal) &&
                name[0] == NamePrefix) {
                name = name.Substring(1);

                if (String.IsNullOrEmpty(name))
                    throw new ArgumentException("Cannot specify only the variable bind prefix as parameter.");
            }

            Name = name;
            SqlType = sqlType;
            Value = value;
            Direction = QueryParameterDirection.In;
        }
Beispiel #42
0
        public override ISqlObject CastTo(ISqlObject value, SqlType destType)
        {
            var bValue = ((SqlBoolean) value);
            if (destType is StringType) {
                if (bValue.IsNull)
                    return SqlString.Null;

                string s;
                if (TypeCode == SqlTypeCode.Bit) {
                    s = bValue ? "1" : "0";
                } else {
                    s = bValue ? "true" : "false";
                }

                return new SqlString(s);
            }
            if (destType is NumericType) {
                SqlNumber num;
                if (bValue == SqlBoolean.Null) {
                    num = SqlNumber.Null;
                } else if (bValue) {
                    num = SqlNumber.One;
                } else {
                    num = SqlNumber.Zero;
                }

                return num;
            }
            if (destType is BinaryType) {
                var bytes = new[] {bValue ? (byte)1 : (byte)0};
                return new SqlBinary(bytes);
            }

            if (destType is BooleanType)
                return value;

            return base.CastTo(value, destType);
        }
        /// <inheritdoc/>
        protected override ISqlNode OnChildNode(ISqlNode node)
        {
            if (node is SqlKeyNode) {
                var keyNode = (SqlKeyNode) node;
                if (String.Equals(keyNode.Text, "true", StringComparison.OrdinalIgnoreCase)) {
                    Value = SqlBoolean.True;
                } else if (String.Equals(keyNode.Text, "false", StringComparison.OrdinalIgnoreCase)) {
                    Value = SqlBoolean.False;
                } else if (String.Equals(keyNode.Text, "null", StringComparison.OrdinalIgnoreCase)) {
                    Value = SqlNull.Value;
                } else {
                    Value = new SqlString(((SqlKeyNode) node).Text.ToCharArray());
                }
            } else if (node is IntegerLiteralNode) {
                Value = new SqlNumber(((IntegerLiteralNode) node).BigValue);
            } else if (node is NumberLiteralNode) {
                Value = new SqlNumber(((NumberLiteralNode) node).BigValue);
            } else if (node is StringLiteralNode) {
                Value = new SqlString(((StringLiteralNode) node).Value.ToCharArray());
            }

            return base.OnChildNode(node);
        }
Beispiel #44
0
        public override ISqlObject UnaryPlus(ISqlObject value)
        {
            if (!(value is SqlNumber))
                throw new ArgumentException();

            var num = (SqlNumber)value;

            if (num.IsNull)
                return num;

            return num.Plus();
        }
Beispiel #45
0
        public override void SerializeObject(Stream stream, ISqlObject obj)
        {
            var writer = new BinaryWriter(stream);

            if (obj is SqlNull || obj == null) {
                writer.Write((byte)0);
            } else {
                var number = (SqlNumber) obj;
                if (obj.IsNull) {
                    writer.Write((byte) 0);
                } else if (number.CanBeInt32) {
                    writer.Write((byte) 1);
                    writer.Write(number.ToInt32());
                } else if (number.CanBeInt64) {
                    writer.Write((byte) 2);
                    writer.Write(number.ToInt64());
                } else {
                    var bytes = number.ToByteArray();
                    writer.Write((byte) 3);
                    writer.Write(number.Precision);
                    writer.Write(number.Scale);
                    writer.Write(bytes.Length);
                    writer.Write(bytes);
                }
            }
        }
Beispiel #46
0
        public override SqlBoolean IsSmallerThan(ISqlObject a, ISqlObject b)
        {
            if (!(a is SqlNumber) ||
                !(b is SqlNumber))
                throw new ArgumentException();

            if (b.IsNull)
                return SqlBoolean.Null;

            if (a.IsNull)
                return SqlBoolean.Null;

            var num1 = (SqlNumber) a;
            var num2 = (SqlNumber) b;

            return num1 < num2;
        }
Beispiel #47
0
 public override SqlBoolean IsNotEqualTo(ISqlObject a, ISqlObject b)
 {
     return !IsEqualTo(a, b);
 }
Beispiel #48
0
        public override void SerializeObject(Stream stream, ISqlObject obj)
        {
            var writer = new BinaryWriter(stream);

            if (obj is SqlNull) {
                writer.Write((byte) 0);
            } else {
                var date = (SqlDateTime) obj;

                if (date.IsNull) {
                    writer.Write((byte)0);
                } else {
                    var bytes = date.ToByteArray(true);

                    writer.Write((byte)1);
                    writer.Write(bytes.Length);
                    writer.Write(bytes);
                }
            }
        }
Beispiel #49
0
        public override ISqlObject CastTo(ISqlObject value, SqlType destType)
        {
            var n = (SqlNumber) value;
            var sqlType = destType.TypeCode;
            ISqlObject casted;

            switch (sqlType) {
                case (SqlTypeCode.Bit):
                case (SqlTypeCode.Boolean):
                    casted = new SqlBoolean(n.ToBoolean());
                    break;
                case (SqlTypeCode.TinyInt):
                case (SqlTypeCode.SmallInt):
                case (SqlTypeCode.Integer):
                    casted = new SqlNumber(n.ToInt32());
                    break;
                case (SqlTypeCode.BigInt):
                    casted = new SqlNumber(n.ToInt64());
                    break;
                case (SqlTypeCode.Float):
                case (SqlTypeCode.Real):
                case (SqlTypeCode.Double):
                    double d;
                    if (n.State == NumericState.NotANumber) {
                        casted = new SqlNumber(Double.NaN);
                    } else if (n.State == NumericState.PositiveInfinity) {
                        casted = new SqlNumber(Double.PositiveInfinity);
                    } else if (n.State == NumericState.NegativeInfinity) {
                        casted = new SqlNumber(Double.NegativeInfinity);
                    } else {
                        casted = new SqlNumber(n.ToDouble());
                    }

                    break;
                case (SqlTypeCode.Numeric):
                case (SqlTypeCode.Decimal):
                    casted = n;
                    break;
                case (SqlTypeCode.Char):
                    casted = new SqlString(n.ToString().PadRight(((StringType) destType).MaxSize));
                    break;
                case (SqlTypeCode.VarChar):
                case (SqlTypeCode.LongVarChar):
                case (SqlTypeCode.String):
                    casted = new SqlString(n.ToString());
                    break;
                case (SqlTypeCode.Date):
                case (SqlTypeCode.Time):
                case (SqlTypeCode.TimeStamp):
                    casted = ToDate(n.ToInt64());
                    break;
                case (SqlTypeCode.Blob):
                case (SqlTypeCode.Binary):
                case (SqlTypeCode.VarBinary):
                case (SqlTypeCode.LongVarBinary):
                    casted = new SqlBinary(n.ToByteArray());
                    break;
                case (SqlTypeCode.Null):
                    casted = SqlNull.Value;
                    break;
                default:
                    throw new InvalidCastException();
            }

            return casted;
        }
Beispiel #50
0
        internal override int ColumnSizeOf(ISqlObject obj)
        {
            if (obj is SqlBinary) {
                var binary = (SqlBinary) obj;
                return 1 + 4 + (int) binary.Length;
            } else if (obj is SqlLongBinary) {
                throw new NotImplementedException();
            }

            throw new NotSupportedException();
        }
Beispiel #51
0
        public override void SerializeObject(Stream stream, ISqlObject obj)
        {
            var writer = new BinaryWriter(stream);

            if (obj is SqlBinary) {
                var bin = (SqlBinary) obj;
                writer.Write((byte)1);
                writer.Write((int)bin.Length);
                writer.Write(bin.ToByteArray());
            } else if (obj is SqlLongBinary) {
                var lob = (SqlLongBinary) obj;

                writer.Write((byte) 2);

                // TODO:

                throw new NotImplementedException();
            } else {
                base.SerializeObject(stream, obj);
            }
        }
Beispiel #52
0
 public override bool IsCacheable(ISqlObject value)
 {
     return value is SqlBinary || value is SqlNull;
 }
Beispiel #53
0
        public override ISqlObject Add(ISqlObject a, ISqlObject b)
        {
            var date = (SqlDateTime) a;

            if (b is SqlDayToSecond) {
                var interval = (SqlDayToSecond) b;
                return date.Add(interval);
            }
            if (b is SqlYearToMonth) {
                var interval = (SqlYearToMonth) b;
                return date.Add(interval);
            }

            throw new InvalidOperationException("Only YEAR TO MONTH and DAY TO SECOND can be added to a date");
        }
Beispiel #54
0
        public override ISqlObject XOr(ISqlObject a, ISqlObject b)
        {
            if (!(a is SqlNumber) ||
                !(b is SqlNumber))
                throw new ArgumentException();

            if (b.IsNull)
                return SqlNumber.Null;

            if (a.IsNull)
                return a;

            var num1 = (SqlNumber) a;
            var num2 = (SqlNumber) b;

            return num1.XOr(num2);
        }
Beispiel #55
0
        internal override int ColumnSizeOf(ISqlObject obj)
        {
            if (obj is SqlNull || obj == null)
                return 1;

            if (!(obj is SqlNumber))
                throw new ArgumentException(String.Format("Cannot determine the size of an object of type '{0}'.", obj.GetType()));

            var number = (SqlNumber) obj;

            if (number.IsNull)
                return 1;

            if (number.CanBeInt32)
                return 1 + 4;
            if (number.CanBeInt64)
                return 1+ 8;

            // Type + Scale + Precision + Byte Count
            var length = number.ToByteArray().Length;
            return 1 + 4 + 4 + 4 + length;
        }
Beispiel #56
0
        public override int Compare(ISqlObject x, ISqlObject y)
        {
            var n1 = (SqlNumber)x;
            SqlNumber n2;

            if (y is SqlNumber) {
                n2 = (SqlNumber)y;
            } else if (y is SqlBoolean) {
                n2 = (SqlBoolean) y ? SqlNumber.One : SqlNumber.Zero;
            } else {
                throw new NotSupportedException();
            }

            return n1.CompareTo(n2);
        }
Beispiel #57
0
 public override bool IsCacheable(ISqlObject value)
 {
     return value is SqlDateTime || value is SqlNull;
 }
Beispiel #58
0
        public override object ConvertTo(ISqlObject obj, Type destType)
        {
            if (!(obj is SqlNumber))
                throw new ArgumentException();

            var number = (SqlNumber)obj;
            if (number.IsNull)
                return null;

            if (destType == typeof(byte))
                return number.ToByte();
            if (destType == typeof(short))
                return number.ToInt16();
            if (destType == typeof(int))
                return number.ToInt32();
            if (destType == typeof(long))
                return number.ToInt64();
            if (destType == typeof(float))
                return number.ToSingle();
            if (destType == typeof(double))
                return number.ToDouble();

            if (destType == typeof(bool))
                return number.ToBoolean();

            if (destType == typeof(string))
                return number.ToString();

            return base.ConvertTo(obj, destType);
        }
Beispiel #59
0
        internal override int ColumnSizeOf(ISqlObject obj)
        {
            if (obj is SqlNull)
                return 1;

            if (!(obj is SqlDateTime))
                throw new ArgumentException(String.Format("Cannot determine the size of an object of type '{0}'", obj.GetType()));

            if (obj.IsNull)
                return 1;

            // Type + Length + Bytes
            return 1 + 4 + 13;
        }
Beispiel #60
0
        public override SqlBoolean IsEqualTo(ISqlObject a, ISqlObject b)
        {
            if (!(a is SqlNumber) ||
                !(b is SqlNumber))
                throw new ArgumentException();

            if (b.IsNull)
                return a.IsNull;

            if (a.IsNull)
                return b.IsNull;

            var num1 = (SqlNumber) a;
            var num2 = (SqlNumber) b;

            return num1.Equals(num2);
        }