/// <summary>
        /// Sets the current value of the sequence, overriding the increment
        /// mechanism in place.
        /// </summary>
        /// <param name="sequenceName">The name of the sequence whose current state
        /// to be set.</param>
        /// <param name="value">The numeric value to set.</param>
        /// <exception cref="ObjectNotFoundException">
        /// If none sequence was found for the given <paramref name="sequenceName"/>.
        /// </exception>
        public static void SetCurrentValue(this IQueryContext context, ObjectName sequenceName, SqlNumber value)
        {
            var sequence = context.GetSequence(sequenceName);
            if (sequence == null)
                throw new InvalidOperationException(String.Format("Sequence {0} was not found.", sequenceName));

            sequence.SetValue(value);
        }
        public static SqlNumber SetValue(this ISequenceManager sequenceManager, ObjectName sequenceName, SqlNumber value)
        {
            var sequence = sequenceManager.GetObject(sequenceName) as ISequence;
            if (sequence == null)
                throw new ObjectNotFoundException(sequenceName);

            return sequence.SetValue(value);
        }
Example #3
0
        public static void EqualsToNotNull()
        {
            var value1 = SqlNull.Value;
            var value2 = new SqlNumber(340.09);

            var result = value1 == value2;

            Assert.IsFalse(result);
        }
Example #4
0
        public static void Byte_Convert(byte value)
        {
            var number = new SqlNumber(value);

            var result = Convert.ChangeType(number, typeof(byte));

            Assert.IsInstanceOf<byte>(result);
            Assert.AreEqual(value, (byte)result);
        }
Example #5
0
        public static void Double_Convert(double value)
        {
            var number = new SqlNumber(value);

            var result = Convert.ChangeType(number, typeof(double));

            Assert.IsInstanceOf<double>(result);
            Assert.AreEqual(value, (double)result);
        }
Example #6
0
 /// <summary>
 /// Constructs a new object with the information given
 /// </summary>
 /// <param name="sequenceName"></param>
 /// <param name="startValue">The start value of the sequence</param>
 /// <param name="increment">The incremental value of the sequence, that is the
 /// value added to the current value of the sequence, each time it advances.</param>
 /// <param name="minValue">The minimum value of the sequence.</param>
 /// <param name="maxValue">The maximum value of the sequence.</param>
 /// <param name="cache">The number of items to cache.</param>
 /// <param name="cycle">Indicates if the sequence must be cycled when it reaches
 /// the minimum or maximum value.</param>
 public SequenceInfo(ObjectName sequenceName, SqlNumber startValue, SqlNumber increment, SqlNumber minValue, SqlNumber maxValue, long cache, bool cycle)
     : this(sequenceName, SequenceType.Normal)
 {
     StartValue = startValue;
     Increment = increment;
     MinValue = minValue;
     MaxValue = maxValue;
     Cache = cache;
     Cycle = cycle;
 }
Example #7
0
 public void Create_FromBigInt()
 {
     var value = new SqlNumber(4599356655L);
     Assert.IsFalse(value.IsNull);
     Assert.IsFalse(value.CanBeInt32);
     Assert.IsTrue(value.CanBeInt64);
     Assert.AreEqual(0, value.Scale);
     Assert.AreEqual(10, value.Precision);
     Assert.AreEqual(NumericState.None, value.State);
     Assert.AreEqual(1, value.Sign);
 }
Example #8
0
 public void Create_FromInteger()
 {
     var value = new SqlNumber((int) 45993);
     Assert.IsFalse(value.IsNull);
     Assert.IsTrue(value.CanBeInt32);
     Assert.IsTrue(value.CanBeInt64);
     Assert.AreEqual(0, value.Scale);
     Assert.AreEqual(5, value.Precision);
     Assert.AreEqual(NumericState.None, value.State);
     Assert.AreEqual(1, value.Sign);
 }
Example #9
0
 public void Create_FromDouble()
 {
     var value = new SqlNumber(459935.9803d);
     Assert.IsFalse(value.IsNull);
     Assert.IsFalse(value.CanBeInt32);
     Assert.IsFalse(value.CanBeInt64);
     Assert.AreEqual(28, value.Scale);
     Assert.AreEqual(34, value.Precision);
     Assert.AreEqual(NumericState.None, value.State);
     Assert.AreEqual(1, value.Sign);
 }
Example #10
0
        public void String_Convert_BigNumber()
        {
            const string s = "7689994.0000033992988477226661525553666370058812345883288477383";
            var sqlString = new SqlString(s);

            var number = new SqlNumber();
            Assert.DoesNotThrow(() => number = (SqlNumber)Convert.ChangeType(sqlString, typeof(SqlNumber)));
            Assert.IsFalse(number.IsNull);
            Assert.IsFalse(number.CanBeInt32);
            Assert.IsFalse(number.CanBeInt64);
            Assert.AreEqual(NumericState.None, number.State);
        }
Example #11
0
        public static void Function_Abs(double value, double expected)
        {
            var number = new SqlNumber(value);
            var result = number.Abs();

            Assert.IsNotNull(result);
            Assert.IsFalse(result.IsNull);

            var doubleResult = result.ToDouble();

            Assert.AreEqual(expected, doubleResult);
        }
        public void NumericNegate(double a, double expected)
        {
            var exp1 = SqlExpression.Constant(Field.Number(new SqlNumber(a)));
            var negExp = SqlExpression.Negate(exp1);

            SqlExpression resultExp = null;
            Assert.DoesNotThrow(() => resultExp = negExp.Evaluate());
            Assert.IsNotNull(resultExp);
            Assert.IsInstanceOf<SqlConstantExpression>(resultExp);

            var constExp = (SqlConstantExpression)resultExp;
            Assert.IsNotNull(constExp.Value.Value);
            Assert.IsInstanceOf<NumericType>(constExp.Value.Type);
            Assert.IsInstanceOf<SqlNumber>(constExp.Value.Value);

            var actual = ((SqlNumber)constExp.Value.Value);
            var expectedResult = new SqlNumber(expected);
            Assert.AreEqual(expectedResult, actual);
        }
Example #13
0
        /// <inheritdoc/>
        public override ISqlObject CastTo(ISqlObject value, SqlType destType)
        {
            if (value is SqlLongString)
            {
                var clob = (SqlLongString)value;
                switch (destType.TypeCode)
                {
                case SqlTypeCode.LongVarChar:
                case SqlTypeCode.Clob: {
                    if (clob.IsNull)
                    {
                        return(SqlLongString.Null);
                    }

                    var destStringType = (StringType)destType;
                    if (MaxSize > destStringType.MaxSize)
                    {
                        throw new InvalidCastException(String.Format("The destination type '{0}' is not large enough.", destStringType));
                    }
                    if (clob.Length > destStringType.MaxSize)
                    {
                        throw new InvalidCastException(String.Format("The source object is too large ({0} bytes) for the destination type '{1}'", clob.Length, destStringType));
                    }

                    return(clob);
                }

                case SqlTypeCode.LongVarBinary:
                case SqlTypeCode.Blob: {
                    if (clob.IsNull)
                    {
                        return(SqlLongBinary.Null);
                    }

                    var destBinaryType = (BinaryType)destType;
                    if (clob.Length > destBinaryType.MaxSize)
                    {
                        throw new InvalidCastException(
                                  String.Format("The source object is too large ({0} bytes) for the destination type '{1}'", clob.Length,
                                                destBinaryType));
                    }

                    return(new SqlLongBinary(clob.LargeObject));
                }

                default:
                    throw new InvalidCastException(String.Format("Cannot cast a CLOB of type '{0}' to '{1}'.", this, destType));
                }
            }

            string     str     = value.ToString();
            var        sqlType = destType.TypeCode;
            ISqlObject castedValue;

            if (value.IsNull)
            {
                return(SqlNull.Value);
            }

            switch (sqlType)
            {
            case (SqlTypeCode.Bit):
            case (SqlTypeCode.Boolean):
                castedValue = ToBoolean(str);
                break;

            case (SqlTypeCode.TinyInt):
            case (SqlTypeCode.SmallInt):
            case (SqlTypeCode.Integer): {
                var num = ToNumber(str);
                if (num.IsNull)
                {
                    castedValue = num;
                }
                else
                {
                    castedValue = new SqlNumber(num.ToInt32());
                }

                break;
            }

            case (SqlTypeCode.BigInt): {
                var num = ToNumber(str);
                if (num.IsNull)
                {
                    castedValue = num;
                }
                else
                {
                    castedValue = new SqlNumber(num.ToInt64());
                }

                break;
            }

            case (SqlTypeCode.Float):
            case (SqlTypeCode.Double): {
                var num = ToNumber(str);
                if (num.IsNull)
                {
                    castedValue = num;
                }
                else
                {
                    castedValue = new SqlNumber(num.ToDouble());
                }

                break;
            }

            case (SqlTypeCode.Real):
            case (SqlTypeCode.Numeric):
            case (SqlTypeCode.Decimal):
                castedValue = ToNumber(str);
                break;

            case (SqlTypeCode.Char):
                castedValue = new SqlString(str.PadRight(((StringType)destType).MaxSize));
                break;

            case (SqlTypeCode.VarChar):
            case (SqlTypeCode.String):
                castedValue = new SqlString(str);
                break;

            case (SqlTypeCode.Date):
                castedValue = ToDate(str);
                break;

            case (SqlTypeCode.Time):
                castedValue = ToTime(str);
                break;

            case (SqlTypeCode.TimeStamp):
                castedValue = ToTimeStamp(str);
                break;

            case (SqlTypeCode.DateTime):
                castedValue = ToDateTime(str);
                break;

            case (SqlTypeCode.Binary):
            case (SqlTypeCode.VarBinary):
            case (SqlTypeCode.LongVarBinary):
                castedValue = new SqlBinary(Encoding.Unicode.GetBytes(str));
                break;

            case (SqlTypeCode.Null):
                castedValue = SqlNull.Value;
                break;

            default:
                throw new InvalidCastException();
            }

            return(castedValue);
        }
Example #14
0
 public void IncrementCurrentValue()
 {
     CurrentValue = IncrementValue(CurrentValue);
 }
            protected override void ExecuteStatement(ExecutionContext context)
            {
                if (!context.Request.Query.UserCanCreateObject(DbObjectType.Sequence, SequenceName))
                    throw new MissingPrivilegesException(context.Request.Query.UserName(), SequenceName, Privileges.Create);

                if (context.Request.Query.ObjectExists(DbObjectType.Sequence, SequenceName))
                    throw new InvalidOperationException(String.Format("The sequence '{0}' already exists.", SequenceName));

                var startValue = SqlNumber.Zero;
                var incrementBy = SqlNumber.One;
                var minValue = SqlNumber.Zero;
                var maxValue = new SqlNumber(Int64.MaxValue);
                var cache = 16;
                var cycle = Cycle;

                if (StartWith != null)
                    startValue = (SqlNumber) StartWith.EvaluateToConstant(context.Request, null).AsBigInt().Value;
                if (IncrementBy != null)
                    incrementBy = (SqlNumber) IncrementBy.EvaluateToConstant(context.Request, null).AsBigInt().Value;
                if (MinValue != null)
                    minValue = (SqlNumber) MinValue.EvaluateToConstant(context.Request, null).AsBigInt().Value;
                if (MaxValue != null)
                    maxValue = (SqlNumber) MaxValue.EvaluateToConstant(context.Request, null).AsBigInt().Value;

                if (minValue >= maxValue)
                    throw new InvalidOperationException("The minimum value cannot be more than the maximum.");
                if (startValue < minValue ||
                    startValue >= maxValue)
                    throw new InvalidOperationException("The start value cannot be out of the mim/max range.");

                var seqInfo = new SequenceInfo(SequenceName, startValue, incrementBy, minValue, maxValue, cache, cycle);
                context.Request.Query.CreateObject(seqInfo);
            }
Example #16
0
        /// <inheritdoc/>
        public override DataObject CastTo(DataObject value, SqlType destType)
        {
            string str = value.Value.ToString();
            var sqlType = destType.TypeCode;
            ISqlObject castedValue;

            if (value.IsNull)
                return DataObject.Null(destType);

            switch (sqlType) {
                case (SqlTypeCode.Bit):
                case (SqlTypeCode.Boolean):
                    castedValue = ToBoolean(str);
                    break;
                case (SqlTypeCode.TinyInt):
                case (SqlTypeCode.SmallInt):
                case (SqlTypeCode.Integer): {
                    var num = ToNumber(str);
                    if (num.IsNull) {
                        castedValue = num;
                    } else {
                        castedValue = new SqlNumber(num.ToInt32());
                    }

                    break;
                }
                case (SqlTypeCode.BigInt): {
                    var num = ToNumber(str);
                    if (num.IsNull) {
                        castedValue = num;
                    } else {
                        castedValue = new SqlNumber(num.ToInt64());
                    }

                    break;
                }
                case (SqlTypeCode.Float):
                case (SqlTypeCode.Double): {
                    var num = ToNumber(str);
                    if (num.IsNull) {
                        castedValue = num;
                    } else {
                        castedValue = new SqlNumber(num.ToDouble());
                    }

                    break;
                }
                case (SqlTypeCode.Real):
                case (SqlTypeCode.Numeric):
                case (SqlTypeCode.Decimal):
                    castedValue = ToNumber(str);
                    break;
                case (SqlTypeCode.Char):
                    castedValue = new SqlString(str.PadRight(((StringType)destType).MaxSize));
                    break;
                case (SqlTypeCode.VarChar):
                case (SqlTypeCode.LongVarChar):
                case (SqlTypeCode.String):
                    castedValue = new SqlString(str);
                    break;
                case (SqlTypeCode.Date):
                    castedValue = ToDate(str);
                    break;
                case (SqlTypeCode.Time):
                    castedValue = ToTime(str);
                    break;
                case (SqlTypeCode.TimeStamp):
                    castedValue = ToTimeStamp(str);
                    break;
                case (SqlTypeCode.DateTime):
                    castedValue = ToDateTime(str);
                    break;
                case (SqlTypeCode.Blob):
                case (SqlTypeCode.Binary):
                case (SqlTypeCode.VarBinary):
                case (SqlTypeCode.LongVarBinary):
                    castedValue = new SqlBinary(Encoding.Unicode.GetBytes(str));
                    break;
                case (SqlTypeCode.Null):
                    castedValue = SqlNull.Value;
                    break;
                case (SqlTypeCode.Clob):
                    // TODO: have a context where to get a new CLOB
                    castedValue = new SqlString(str);
                    break;
                default:
                    throw new InvalidCastException();
            }

            return new DataObject(destType, castedValue);
        }
Example #17
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);
        }
Example #18
0
        protected ICondition ConvertToConditions(Expression expression)
        {
            var _expression = expression as BinaryExpression;

            if (expression.NodeType == ExpressionType.AndAlso)
            {
                return(new Conditions(ConvertToConditions(_expression.Left))
                       .And(ConvertToConditions(((BinaryExpression)expression).Right)));
            }

            if (expression.NodeType == ExpressionType.OrElse)
            {
                return(new Conditions(ConvertToConditions(_expression.Left))
                       .Or(ConvertToConditions(((BinaryExpression)expression).Right)));
            }

            ISqlExpression left  = null;
            ISqlExpression right = null;

            #region Left Evaluation
            if (_expression.Left.NodeType == ExpressionType.MemberAccess)
            {
                var leftExp = _expression.Left as MemberExpression;
                if (leftExp.Expression != null)
                {
                    var column  = _metadata.Columns.FirstOrDefault(x => x.Name == leftExp.Member.Name);
                    var colName = _metadata.TableAlias + "." + column.ColumnName;
                    left = new SqlColumnName(colName, column.ColumnName);
                }
                else if (leftExp.Member.DeclaringType == typeof(DateTime))
                {
                    left = new SqlDateTime(Convert.ToDateTime(ReflectionUtils.GetMemberValue(leftExp.Member, null)));
                }
                else if (leftExp.Member.DeclaringType == typeof(bool))
                {
                    left = new SqlNumber(Convert.ToBoolean(ReflectionUtils.GetMemberValue(leftExp.Member, null)) ? 1 : 0);
                }
                else
                {
                    left = new SqlString(ReflectionUtils.GetMemberValue(leftExp.Member, null).ToString());
                }
            }
            else if (_expression.Left.NodeType == ExpressionType.Constant)
            {
                var constant = _expression.Left as ConstantExpression;

                if (constant.Type == typeof(bool))
                {
                    left = new SqlBoolean((bool)constant.Value);
                }
                else
                {
                    left = new SqlString(constant.Value.ToString());
                }
            }
            #endregion

            #region Right Evaluation
            if (_expression.Right.NodeType == ExpressionType.MemberAccess)
            {
                var rightExp = _expression.Right as MemberExpression;
                if (rightExp.Expression != null)
                {
                    var column  = _metadata.Columns.FirstOrDefault(x => x.Name == rightExp.Member.Name);
                    var colName = _metadata.TableAlias + "." + column.ColumnName;
                    right = new SqlColumnName(colName, column.ColumnName);
                }
                else if (rightExp.Member.DeclaringType == typeof(DateTime))
                {
                    right = new SqlDateTime(Convert.ToDateTime(ReflectionUtils.GetMemberValue(rightExp.Member, null)));
                }
                else if (rightExp.Member.DeclaringType == typeof(bool))
                {
                    right = new SqlNumber(Convert.ToBoolean(ReflectionUtils.GetMemberValue(rightExp.Member, null)) ? 1 : 0);
                }
                else
                {
                    right = new SqlString(ReflectionUtils.GetMemberValue(rightExp.Member, null).ToString());
                }
            }
            else if (_expression.Right.NodeType == ExpressionType.Constant)
            {
                var constant = _expression.Right as ConstantExpression;

                if (constant.Type == typeof(bool))
                {
                    right = new SqlBoolean((bool)constant.Value);
                }
                else
                {
                    right = new SqlString(constant.Value.ToString());
                }
            }
            #endregion

            return(new Condition(left, GetComparisonOperator(_expression), right));
        }
 private ISqlValue ToNumeric(SqlNumber number, SqlNumericType destType)
 {
     // TODO: should we make some checks here?
     return(destType.NormalizeValue(number));
 }
Example #20
0
 private SqlNumber IncrementValue(SqlNumber val)
 {
     val += SequenceInfo.Increment;
     if (val > SequenceInfo.MaxValue) {
         if (SequenceInfo.Cycle) {
             val = SequenceInfo.MinValue;
         } else {
             throw new InvalidOperationException("Sequence out of bounds.");
         }
     }
     if (val < SequenceInfo.MinValue) {
         if (SequenceInfo.Cycle) {
             val = SequenceInfo.MaxValue;
         } else {
             throw new InvalidOperationException("Sequence out of bounds.");
         }
     }
     return val;
 }
Example #21
0
 /// <summary>
 /// Constructs a new object with the information given
 /// </summary>
 /// <param name="sequenceName"></param>
 /// <param name="startValue">The start value of the sequence</param>
 /// <param name="increment">The incremental value of the sequence, that is the
 /// value added to the current value of the sequence, each time it advances.</param>
 /// <param name="minValue">The minimum value of the sequence.</param>
 /// <param name="maxValue">The maximum value of the sequence.</param>
 /// <param name="cache">The number of items to cache.</param>
 public SequenceInfo(ObjectName sequenceName, SqlNumber startValue, SqlNumber increment, SqlNumber minValue, SqlNumber maxValue, long cache)
     : this(sequenceName, startValue, increment, minValue, maxValue, cache, true)
 {
 }
Example #22
0
 private static string MakeUniqueConstraintName(string constraintName, SqlNumber uniqueId)
 {
     return(String.IsNullOrEmpty(constraintName) ? ("_ANONYMOUS_CONSTRAINT_" + uniqueId) : constraintName);
 }
Example #23
0
 public void Parse_BigDecimal()
 {
     var value = new SqlNumber();
     Assert.DoesNotThrow(() => value = SqlNumber.Parse("98356278.911288837773848500069994933229238e45789"));
     Assert.IsFalse(value.IsNull);
     Assert.IsFalse(value.CanBeInt32);
     Assert.IsFalse(value.CanBeInt64);
     Assert.Greater(value.Precision, 40);
 }
Example #24
0
        /// <inheritdoc/>
        public override DataObject CastTo(DataObject value, SqlType destType)
        {
            string     str     = value.Value.ToString();
            var        sqlType = destType.TypeCode;
            ISqlObject castedValue;

            if (value.IsNull)
            {
                return(DataObject.Null(destType));
            }

            switch (sqlType)
            {
            case (SqlTypeCode.Bit):
            case (SqlTypeCode.Boolean):
                castedValue = ToBoolean(str);
                break;

            case (SqlTypeCode.TinyInt):
            case (SqlTypeCode.SmallInt):
            case (SqlTypeCode.Integer): {
                var num = ToNumber(str);
                if (num.IsNull)
                {
                    castedValue = num;
                }
                else
                {
                    castedValue = new SqlNumber(num.ToInt32());
                }

                break;
            }

            case (SqlTypeCode.BigInt): {
                var num = ToNumber(str);
                if (num.IsNull)
                {
                    castedValue = num;
                }
                else
                {
                    castedValue = new SqlNumber(num.ToInt64());
                }

                break;
            }

            case (SqlTypeCode.Float):
            case (SqlTypeCode.Double): {
                var num = ToNumber(str);
                if (num.IsNull)
                {
                    castedValue = num;
                }
                else
                {
                    castedValue = new SqlNumber(num.ToDouble());
                }

                break;
            }

            case (SqlTypeCode.Real):
            case (SqlTypeCode.Numeric):
            case (SqlTypeCode.Decimal):
                castedValue = ToNumber(str);
                break;

            case (SqlTypeCode.Char):
                castedValue = new SqlString(str.PadRight(((StringType)destType).MaxSize));
                break;

            case (SqlTypeCode.VarChar):
            case (SqlTypeCode.LongVarChar):
            case (SqlTypeCode.String):
                castedValue = new SqlString(str);
                break;

            case (SqlTypeCode.Date):
                castedValue = ToDate(str);
                break;

            case (SqlTypeCode.Time):
                castedValue = ToTime(str);
                break;

            case (SqlTypeCode.TimeStamp):
                castedValue = ToTimeStamp(str);
                break;

            case (SqlTypeCode.DateTime):
                castedValue = ToDateTime(str);
                break;

            case (SqlTypeCode.Blob):
            case (SqlTypeCode.Binary):
            case (SqlTypeCode.VarBinary):
            case (SqlTypeCode.LongVarBinary):
                castedValue = new SqlBinary(Encoding.Unicode.GetBytes(str));
                break;

            case (SqlTypeCode.Null):
                castedValue = SqlNull.Value;
                break;

            case (SqlTypeCode.Clob):
                // TODO: have a context where to get a new CLOB
                castedValue = new SqlString(str);
                break;

            default:
                throw new InvalidCastException();
            }

            return(new DataObject(destType, castedValue));
        }
Example #25
0
 public SqlNumber SetValue(SqlNumber value)
 {
     return(manager.SetValue(FullName, value));
 }
Example #26
0
 public Sequence(SequenceManager manager, SqlNumber id, SequenceInfo sequenceInfo)
     : this(manager, id, SqlNumber.Null, sequenceInfo)
 {
 }
Example #27
0
 public void SetValue(int columnIndex, SqlNumber value)
 {
     SetValue(columnIndex, DataObject.Number(value));
 }
Example #28
0
        public void Integer_Greater_True()
        {
            var value1 = new SqlNumber(76);
            var value2 = new SqlNumber(54);

            Assert.IsTrue(value1 > value2);
        }
        /// <summary>
        /// Sets the current value of a table native sequence.
        /// </summary>
        /// <param name="transaction"></param>
        /// <param name="tableName">The table name.</param>
        /// <param name="value">The current value of the native sequence.</param>
        /// <seealso cref="ISequence"/>
        /// <seealso cref="SequenceManager"/>
        /// <returns>
        /// Returns the current table sequence value after the set.
        /// </returns>
        /// <exception cref="ObjectNotFoundException">
        /// If it was not possible to find any table having the given
        /// <paramref name="tableName">name</paramref>.
        /// </exception>
        public static SqlNumber SetTableId(this ITransaction transaction, ObjectName tableName, SqlNumber value)
        {
            transaction.AssertNotReadOnly();

            var tableManager = transaction.GetTableManager();

            if (tableManager == null)
            {
                throw new InvalidOperationException();
            }

            return(tableManager.SetUniqueId(tableName, value));
        }
Example #30
0
        public void Compare_ToNumber_OutOfRange()
        {
            var value1 = SqlBoolean.True;
            var value2 = new SqlNumber(21);

            Assert.IsFalse(value1.IsNull);
            Assert.IsFalse(value2.IsNull);

            Assert.IsFalse(value1.IsComparableTo(value2));

            int i = -2;
            Assert.Throws<ArgumentOutOfRangeException>(() => i = value1.CompareTo(value2));
            Assert.AreEqual(-2, i);
        }
Example #31
0
        private SqlNumber SetValue(ObjectName name, SqlNumber value)
        {
            lock (this) {
                var sequence = (Sequence) GetSequence(name);

                if (sequence.SequenceInfo.Type == SequenceType.Native)
                    return Transaction.SetTableId(name, value);

                // Custom sequence generator
                sequence.CurrentValue = value;
                sequence.LastValue = value;

                // Update the state
                UpdateSequenceState(sequence);

                return value;
            }
        }
Example #32
0
 /// <summary>
 /// Constructs a new object with the information given
 /// </summary>
 /// <param name="sequenceName"></param>
 /// <param name="startValue">The start value of the sequence</param>
 /// <param name="increment">The incremental value of the sequence, that is the
 /// value added to the current value of the sequence, each time it advances.</param>
 /// <param name="minValue">The minimum value of the sequence.</param>
 /// <param name="maxValue">The maximum value of the sequence.</param>
 /// <param name="cycle">Indicates if the sequence must be cycled when it reaches
 /// the minimum or maximum value.</param>
 public SequenceInfo(ObjectName sequenceName, SqlNumber startValue, SqlNumber increment, SqlNumber minValue, SqlNumber maxValue, bool cycle)
     : this(sequenceName, startValue, increment, minValue, maxValue, 256, cycle)
 {
 }
Example #33
0
 public Sequence(SequenceManager manager, SqlNumber id, SqlNumber lastValue, SequenceInfo sequenceInfo)
 {
     this.manager = manager;
     Id = id;
     FullName = sequenceInfo.SequenceName;
     SequenceInfo = sequenceInfo;
     LastValue = lastValue;
     CurrentValue = lastValue;
 }
 private static string MakeUniqueConstraintName(string constraintName, SqlNumber uniqueId)
 {
     return String.IsNullOrEmpty(constraintName) ? ("_ANONYMOUS_CONSTRAINT_" + uniqueId) : constraintName;
 }
Example #35
0
        /// <summary>
        /// Sets the current value of the sequence, overriding the increment
        /// mechanism in place.
        /// </summary>
        /// <param name="sequenceName">The name of the sequence whose current state
        /// to be set.</param>
        /// <param name="value">The numeric value to set.</param>
        /// <exception cref="ObjectNotFoundException">
        /// If none sequence was found for the given <paramref name="sequenceName"/>.
        /// </exception>
        public static void SetCurrentValue(this IQuery context, ObjectName sequenceName, SqlNumber value)
        {
            var sequence = context.GetSequence(sequenceName);

            if (sequence == null)
            {
                throw new InvalidOperationException(String.Format("Sequence {0} was not found.", sequenceName));
            }

            sequence.SetValue(value);
        }
        /// <summary>
        /// Sets the current value of a table native sequence.
        /// </summary>
        /// <param name="transaction"></param>
        /// <param name="tableName">The table name.</param>
        /// <param name="value">The current value of the native sequence.</param>
        /// <seealso cref="ISequence"/>
        /// <seealso cref="ISequenceManager"/>
        /// <returns>
        /// Returns the current table sequence value after the set.
        /// </returns>
        /// <exception cref="ObjectNotFoundException">
        /// If it was not possible to find any table having the given
        /// <paramref name="tableName">name</paramref>.
        /// </exception>
        public static SqlNumber SetTableId(this ITransaction transaction, ObjectName tableName, SqlNumber value)
        {
            transaction.AssertNotReadOnly();

            var tableManager = transaction.GetTableManager();
            if (tableManager == null)
                throw new InvalidOperationException();

            return tableManager.SetUniqueId(tableName, value);
        }
        public void NumericMultiply()
        {
            var exp1 = SqlExpression.Constant(DataObject.Number(new SqlNumber(56894.09)));
            var exp2 = SqlExpression.Constant(DataObject.Number(new SqlNumber(456)));
            var mulExp = SqlExpression.Multiply(exp1, exp2);

            SqlExpression resultExp = null;
            Assert.DoesNotThrow(() => resultExp = mulExp.Evaluate());
            Assert.IsNotNull(resultExp);
            Assert.IsInstanceOf<SqlConstantExpression>(resultExp);

            var constExp = (SqlConstantExpression)resultExp;
            Assert.IsNotNull(constExp.Value.Value);
            Assert.IsInstanceOf<NumericType>(constExp.Value.Type);
            Assert.IsInstanceOf<SqlNumber>(constExp.Value.Value);

            var actual = ((SqlNumber) constExp.Value.Value).Round(2);
            var expected = new SqlNumber(25943705.04, 2);
            Assert.AreEqual(expected, actual);
        }
Example #38
0
 public Sequence(SequenceManager manager, SqlNumber id, SequenceInfo sequenceInfo)
     : this(manager, id, SqlNumber.Null, sequenceInfo)
 {
 }
        public void NumericAnd(long a, long b, long expected)
        {
            var exp1 = SqlExpression.Constant(DataObject.Number(new SqlNumber(a)));
            var exp2 = SqlExpression.Constant(DataObject.Number(new SqlNumber(b)));
            var orExp = SqlExpression.And(exp1, exp2);

            SqlExpression resultExp = null;
            Assert.DoesNotThrow(() => resultExp = orExp.Evaluate());
            Assert.IsNotNull(resultExp);
            Assert.IsInstanceOf<SqlConstantExpression>(resultExp);

            var constExp = (SqlConstantExpression)resultExp;
            Assert.IsNotNull(constExp.Value.Value);
            Assert.IsInstanceOf<NumericType>(constExp.Value.Type);
            Assert.IsInstanceOf<SqlNumber>(constExp.Value.Value);

            var actual = ((SqlNumber)constExp.Value.Value);
            var expectedResult = new SqlNumber(expected);
            Assert.AreEqual(expectedResult, actual);
        }
Example #40
0
 public SqlNumber SetValue(SqlNumber value)
 {
     return manager.SetValue(FullName, value);
 }
Example #41
0
        public void CreateRoutine(RoutineInfo routineInfo)
        {
            try {
                string routineType = null;

                if (routineInfo is FunctionInfo) {
                    if (routineInfo is ExternalFunctionInfo) {
                        routineType = ExtrernalFunctionType;
                    } else if (routineInfo is PlSqlFunctionInfo) {
                        routineType = FunctionType;
                    }
                } else if (routineInfo is ProcedureInfo) {
                    if (routineInfo is PlSqlProcedureInfo) {
                        routineType = ProcedureType;
                    } else if (routineInfo is ExternalProcedureInfo) {
                        routineType = ExternalProcedureType;
                    }
                } else {
                    throw new ArgumentException();
                }

                if (String.IsNullOrEmpty(routineType))
                    throw new InvalidOperationException("Could not determine the kind of routine.");

                var id = transaction.NextTableId(RoutineTableName);

                var routine = transaction.GetMutableTable(RoutineTableName);
                var routineParams = transaction.GetMutableTable(RoutineParameterTableName);

                var row = routine.NewRow();
                row.SetValue(0, id);
                row.SetValue(1, routineInfo.RoutineName.ParentName);
                row.SetValue(2, routineInfo.RoutineName.Name);
                row.SetValue(3, routineType);

                if (routineType == ExternalProcedureType) {
                    var extProcedure = (ExternalProcedureInfo)routineInfo;
                    var location = extProcedure.ExternalRef.ToString();
                    row.SetValue(4, location);
                } else if (routineType == ExtrernalFunctionType) {
                    var extFunction = (ExternalFunctionInfo)routineInfo;
                    var location = extFunction.ExternalRef.ToString();
                    row.SetValue(4, location);
                } else if (routineType == ProcedureType) {
                    var plsqlProcedure = (PlSqlProcedureInfo)routineInfo;
                    var bin = SqlBinary.ToBinary(plsqlProcedure.Body);
                    row.SetValue(5, bin);
                } else if (routineType == FunctionType) {
                    var plsqlFunction = (PlSqlFunctionInfo)routineInfo;
                    var bin = SqlBinary.ToBinary(plsqlFunction.Body);
                    row.SetValue(5, bin);
                }

                if (routineInfo is FunctionInfo) {
                    var returnType = ((FunctionInfo)routineInfo).ReturnType.ToString();
                    row.SetValue(6, returnType);
                }

                row.SetValue(7, routineInfo.Owner);
                routine.AddRow(row);

                if (routineInfo.Parameters != null) {
                    foreach (var parameter in routineInfo.Parameters) {
                        var prow = routineParams.NewRow();
                        prow.SetValue(0, id);
                        prow.SetValue(1, parameter.Name);

                        var argType = parameter.Type.ToString();
                        prow.SetValue(2, argType);

                        var attrs = new SqlNumber((int)parameter.Attributes);
                        prow.SetValue(3, attrs);

                        var dir = new SqlNumber((int)parameter.Direction);
                        prow.SetValue(4, dir);

                        prow.SetValue(5, parameter.Offset);

                        routineParams.AddRow(prow);
                    }
                }

                transaction.OnObjectCreated(DbObjectType.Routine, routineInfo.RoutineName);
            } finally {
                routinesCache.Clear();
            }
        }
Example #42
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;
        }
Example #43
0
 public void SetValue(int columnIndex, SqlNumber value)
 {
     SetValue(columnIndex, Field.Number(value));
 }