Пример #1
0
        private object EvalFunction(FunctionId id, object[] argumentValues, DataRow row, DataRowVersion version)
        {
            StorageType type;

            switch (id)
            {
            case FunctionId.Charindex:
                if (!DataStorage.IsObjectNull(argumentValues[0]) && !DataStorage.IsObjectNull(argumentValues[1]))
                {
                    if (argumentValues[0] is SqlString)
                    {
                        SqlString str6 = (SqlString)argumentValues[0];
                        argumentValues[0] = str6.Value;
                    }
                    if (argumentValues[1] is SqlString)
                    {
                        SqlString str5 = (SqlString)argumentValues[1];
                        argumentValues[1] = str5.Value;
                    }
                    return(((string)argumentValues[1]).IndexOf((string)argumentValues[0], StringComparison.Ordinal));
                }
                return(DBNull.Value);

            case FunctionId.Len:
            {
                if (!(argumentValues[0] is SqlString))
                {
                    goto Label_02D4;
                }
                SqlString str4 = (SqlString)argumentValues[0];
                if (!str4.IsNull)
                {
                    SqlString str3 = (SqlString)argumentValues[0];
                    argumentValues[0] = str3.Value;
                    goto Label_02D4;
                }
                return(DBNull.Value);
            }

            case FunctionId.Substring:
            {
                int startIndex = ((int)argumentValues[1]) - 1;
                int length     = (int)argumentValues[2];
                if (startIndex < 0)
                {
                    throw ExprException.FunctionArgumentOutOfRange("index", "Substring");
                }
                if (length < 0)
                {
                    throw ExprException.FunctionArgumentOutOfRange("length", "Substring");
                }
                if (length == 0)
                {
                    return("");
                }
                if (argumentValues[0] is SqlString)
                {
                    SqlString str2 = (SqlString)argumentValues[0];
                    argumentValues[0] = str2.Value;
                }
                int num3 = ((string)argumentValues[0]).Length;
                if (startIndex > num3)
                {
                    return(DBNull.Value);
                }
                if ((startIndex + length) > num3)
                {
                    length = num3 - startIndex;
                }
                return(((string)argumentValues[0]).Substring(startIndex, length));
            }

            case FunctionId.IsNull:
                if (!DataStorage.IsObjectNull(argumentValues[0]))
                {
                    return(argumentValues[0]);
                }
                return(argumentValues[1]);

            case FunctionId.Iif:
                if (!DataExpression.ToBoolean(this.arguments[0].Eval(row, version)))
                {
                    return(this.arguments[2].Eval(row, version));
                }
                return(this.arguments[1].Eval(row, version));

            case FunctionId.Convert:
            {
                if (this.argumentCount != 2)
                {
                    throw ExprException.FunctionArgumentCount(this.name);
                }
                if (argumentValues[0] == DBNull.Value)
                {
                    return(DBNull.Value);
                }
                Type        dataType    = (Type)argumentValues[1];
                StorageType storageType = DataStorage.GetStorageType(dataType);
                type = DataStorage.GetStorageType(argumentValues[0].GetType());
                if ((storageType == StorageType.DateTimeOffset) && (type == StorageType.String))
                {
                    return(SqlConvert.ConvertStringToDateTimeOffset((string)argumentValues[0], base.FormatProvider));
                }
                if (StorageType.Object == storageType)
                {
                    return(argumentValues[0]);
                }
                if ((storageType == StorageType.Guid) && (type == StorageType.String))
                {
                    return(new Guid((string)argumentValues[0]));
                }
                if (ExpressionNode.IsFloatSql(type) && ExpressionNode.IsIntegerSql(storageType))
                {
                    if (StorageType.Single == type)
                    {
                        return(SqlConvert.ChangeType2((float)SqlConvert.ChangeType2(argumentValues[0], StorageType.Single, typeof(float), base.FormatProvider), storageType, dataType, base.FormatProvider));
                    }
                    if (StorageType.Double == type)
                    {
                        return(SqlConvert.ChangeType2((double)SqlConvert.ChangeType2(argumentValues[0], StorageType.Double, typeof(double), base.FormatProvider), storageType, dataType, base.FormatProvider));
                    }
                    if (StorageType.Decimal == type)
                    {
                        return(SqlConvert.ChangeType2((decimal)SqlConvert.ChangeType2(argumentValues[0], StorageType.Decimal, typeof(decimal), base.FormatProvider), storageType, dataType, base.FormatProvider));
                    }
                }
                return(SqlConvert.ChangeType2(argumentValues[0], storageType, dataType, base.FormatProvider));
            }

            case FunctionId.cInt:
                return(Convert.ToInt32(argumentValues[0], base.FormatProvider));

            case FunctionId.cBool:
            {
                StorageType type4 = DataStorage.GetStorageType(argumentValues[0].GetType());
                if (type4 > StorageType.Int32)
                {
                    switch (type4)
                    {
                    case StorageType.Double:
                        return(!(((double)argumentValues[0]) == 0.0));

                    case StorageType.String:
                        return(bool.Parse((string)argumentValues[0]));
                    }
                    break;
                }
                switch (type4)
                {
                case StorageType.Boolean:
                    return((bool)argumentValues[0]);

                case StorageType.Int32:
                    return(((int)argumentValues[0]) != 0);
                }
                break;
            }

            case FunctionId.cDate:
                return(Convert.ToDateTime(argumentValues[0], base.FormatProvider));

            case FunctionId.cDbl:
                return(Convert.ToDouble(argumentValues[0], base.FormatProvider));

            case FunctionId.cStr:
                return(Convert.ToString(argumentValues[0], base.FormatProvider));

            case FunctionId.Abs:
                type = DataStorage.GetStorageType(argumentValues[0].GetType());
                if (!ExpressionNode.IsInteger(type))
                {
                    if (!ExpressionNode.IsNumeric(type))
                    {
                        throw ExprException.ArgumentTypeInteger(funcs[this.info].name, 1);
                    }
                    return(Math.Abs((double)argumentValues[0]));
                }
                return(Math.Abs((long)argumentValues[0]));

            case FunctionId.In:
                throw ExprException.NYI(funcs[this.info].name);

            case FunctionId.Trim:
                if (!DataStorage.IsObjectNull(argumentValues[0]))
                {
                    if (argumentValues[0] is SqlString)
                    {
                        SqlString str = (SqlString)argumentValues[0];
                        argumentValues[0] = str.Value;
                    }
                    return(((string)argumentValues[0]).Trim());
                }
                return(DBNull.Value);

            case FunctionId.DateTimeOffset:
                if (((argumentValues[0] != DBNull.Value) && (argumentValues[1] != DBNull.Value)) && (argumentValues[2] != DBNull.Value))
                {
                    DateTime time = (DateTime)argumentValues[0];
                    switch (time.Kind)
                    {
                    case DateTimeKind.Utc:
                        if ((((int)argumentValues[1]) != 0) && (((int)argumentValues[2]) != 0))
                        {
                            throw ExprException.MismatchKindandTimeSpan();
                        }
                        break;

                    case DateTimeKind.Local:
                        if ((DateTimeOffset.Now.Offset.Hours != ((int)argumentValues[1])) && (DateTimeOffset.Now.Offset.Minutes != ((int)argumentValues[2])))
                        {
                            throw ExprException.MismatchKindandTimeSpan();
                        }
                        break;
                    }
                    if ((((int)argumentValues[1]) < -14) || (((int)argumentValues[1]) > 14))
                    {
                        throw ExprException.InvalidHoursArgument();
                    }
                    if ((((int)argumentValues[2]) < -59) || (((int)argumentValues[2]) > 0x3b))
                    {
                        throw ExprException.InvalidMinutesArgument();
                    }
                    if ((((int)argumentValues[1]) == 14) && (((int)argumentValues[2]) > 0))
                    {
                        throw ExprException.InvalidTimeZoneRange();
                    }
                    if ((((int)argumentValues[1]) == -14) && (((int)argumentValues[2]) < 0))
                    {
                        throw ExprException.InvalidTimeZoneRange();
                    }
                    return(new DateTimeOffset((DateTime)argumentValues[0], new TimeSpan((int)argumentValues[1], (int)argumentValues[2], 0)));
                }
                return(DBNull.Value);

            default:
                throw ExprException.UndefinedFunction(funcs[this.info].name);
            }
            throw ExprException.DatatypeConvertion(argumentValues[0].GetType(), typeof(bool));
Label_02D4:
            return(((string)argumentValues[0]).Length);
        }
Пример #2
0
        internal void HandleColumn(XmlElement node, DataTable table)
        {
            Debug.Assert(FEqualIdentity(node, Keywords.XDR_ELEMENT, Keywords.XDRNS) ||
                         FEqualIdentity(node, Keywords.XDR_ATTRIBUTE, Keywords.XDRNS), "Illegal node type");

            string     instanceName;
            string     strName;
            Type       type;
            string     strType;
            string     strValues;
            int        minOccurs = 0;
            int        maxOccurs = 1;
            string     strDefault;
            DataColumn column;

            // Get the name
            if (node.Attributes.Count > 0)
            {
                string strRef = node.GetAttribute(Keywords.REF);

                if (strRef != null && strRef.Length > 0)
                {
                    return; //skip ref nodes. B2 item
                }
                strName = instanceName = GetInstanceName(node);
                column  = table.Columns[instanceName, _schemaUri];
                if (column != null)
                {
                    if (column.ColumnMapping == MappingType.Attribute)
                    {
                        if (FEqualIdentity(node, Keywords.XDR_ATTRIBUTE, Keywords.XDRNS))
                        {
                            throw ExceptionBuilder.DuplicateDeclaration(strName);
                        }
                    }
                    else
                    {
                        if (FEqualIdentity(node, Keywords.XDR_ELEMENT, Keywords.XDRNS))
                        {
                            throw ExceptionBuilder.DuplicateDeclaration(strName);
                        }
                    }
                    instanceName = GenUniqueColumnName(strName, table);
                }
            }
            else
            {
                strName = instanceName = string.Empty;
            }

            // Now get the type
            XmlElement typeNode = FindTypeNode(node);

            SimpleType xsdType = null;

            if (typeNode == null)
            {
                strType = node.GetAttribute(Keywords.TYPE);
                throw ExceptionBuilder.UndefinedDatatype(strType);
            }

            strType   = typeNode.GetAttribute(Keywords.DT_TYPE, Keywords.DTNS);
            strValues = typeNode.GetAttribute(Keywords.DT_VALUES, Keywords.DTNS);
            if (strType == null || strType.Length == 0)
            {
                strType = string.Empty;
                type    = typeof(string);
            }
            else
            {
                type = ParseDataType(strType, strValues);
                // HACK: temp work around special types
                if (strType == "float")
                {
                    strType = string.Empty;
                }

                if (strType == "char")
                {
                    strType = string.Empty;
                    xsdType = SimpleType.CreateSimpleType(StorageType.Char, type);
                }


                if (strType == "enumeration")
                {
                    strType = string.Empty;
                    xsdType = SimpleType.CreateEnumeratedType(strValues);
                }

                if (strType == "bin.base64")
                {
                    strType = string.Empty;
                    xsdType = SimpleType.CreateByteArrayType("base64");
                }

                if (strType == "bin.hex")
                {
                    strType = string.Empty;
                    xsdType = SimpleType.CreateByteArrayType("hex");
                }
            }

            bool isAttribute = FEqualIdentity(node, Keywords.XDR_ATTRIBUTE, Keywords.XDRNS);

            GetMinMax(node, isAttribute, ref minOccurs, ref maxOccurs);

            strDefault = null;

            // Does XDR has default?
            strDefault = node.GetAttribute(Keywords.DEFAULT);


            bool bNullable = false;

            column = new DataColumn(XmlConvert.DecodeName(instanceName), type, null,
                                    isAttribute ? MappingType.Attribute : MappingType.Element);

            SetProperties(column, node.Attributes); // xmlschema.SetProperties will skipp setting expressions
            column.XmlDataType = strType;
            column.SimpleType  = xsdType;
            column.AllowDBNull = (minOccurs == 0) || bNullable;
            column.Namespace   = (isAttribute) ? string.Empty : _schemaUri;

            // We will skip handling expression columns in SetProperties, so we need set the expressions here
            if (node.Attributes != null)
            {
                for (int i = 0; i < node.Attributes.Count; i++)
                {
                    if (node.Attributes[i].NamespaceURI == Keywords.MSDNS)
                    {
                        if (node.Attributes[i].LocalName == "Expression")
                        {
                            column.Expression = node.Attributes[i].Value;
                            break;
                        }
                    }
                }
            }

            string targetNamespace = node.GetAttribute(Keywords.TARGETNAMESPACE);

            if (targetNamespace != null && targetNamespace.Length > 0)
            {
                column.Namespace = targetNamespace;
            }

            table.Columns.Add(column);
            if (strDefault != null && strDefault.Length != 0)
            {
                try
                {
                    column.DefaultValue = SqlConvert.ChangeTypeForXML(strDefault, type);
                }
                catch (System.FormatException)
                {
                    throw ExceptionBuilder.CannotConvert(strDefault, type.FullName);
                }
            }
        }
Пример #3
0
        private int Eval(BinaryNode expr, DataRow row, DataRowVersion version)
        {
            if (expr._op == Operators.And)
            {
                int lResult = Eval((BinaryNode)expr._left, row, version);
                if (lResult != 0)
                {
                    return(lResult);
                }
                int rResult = Eval((BinaryNode)expr._right, row, version);
                if (rResult != 0)
                {
                    return(rResult);
                }
                return(0);
            }

            long   c     = 0;
            object vLeft = expr._left.Eval(row, version);

            if (expr._op != Operators.Is && expr._op != Operators.IsNot)
            {
                object vRight   = expr._right.Eval(row, version);
                bool   isLConst = (expr._left is ConstNode);
                bool   isRConst = (expr._right is ConstNode);

                if ((vLeft == DBNull.Value) || (expr._left.IsSqlColumn && DataStorage.IsObjectSqlNull(vLeft)))
                {
                    return(-1);
                }
                if ((vRight == DBNull.Value) || (expr._right.IsSqlColumn && DataStorage.IsObjectSqlNull(vRight)))
                {
                    return(1);
                }

                StorageType leftType = DataStorage.GetStorageType(vLeft.GetType());
                if (StorageType.Char == leftType)
                {
                    if ((isRConst) || (!expr._right.IsSqlColumn))
                    {
                        vRight = Convert.ToChar(vRight, _table.FormatProvider);
                    }
                    else
                    {
                        vRight = SqlConvert.ChangeType2(vRight, StorageType.Char, typeof(char), _table.FormatProvider);
                    }
                }

                StorageType rightType = DataStorage.GetStorageType(vRight.GetType());
                StorageType resultType;
                if (expr._left.IsSqlColumn || expr._right.IsSqlColumn)
                {
                    resultType = expr.ResultSqlType(leftType, rightType, isLConst, isRConst, expr._op);
                }
                else
                {
                    resultType = expr.ResultType(leftType, rightType, isLConst, isRConst, expr._op);
                }
                if (StorageType.Empty == resultType)
                {
                    expr.SetTypeMismatchError(expr._op, vLeft.GetType(), vRight.GetType());
                }

                // if comparing a Guid column value against a string literal
                // use InvariantCulture instead of DataTable.Locale because in the Danish related cultures
                // sorting a Guid as a string has different results than in Invariant and English related cultures.
                // This fix is restricted to DataTable.Select("GuidColumn = 'string literal'") types of queries
                NameNode namedNode = null;
                System.Globalization.CompareInfo comparer =
                    ((isLConst && !isRConst && (leftType == StorageType.String) && (rightType == StorageType.Guid) && (null != (namedNode = expr._right as NameNode)) && (namedNode._column.DataType == typeof(Guid))) ||
                     (isRConst && !isLConst && (rightType == StorageType.String) && (leftType == StorageType.Guid) && (null != (namedNode = expr._left as NameNode)) && (namedNode._column.DataType == typeof(Guid))))
                     ? System.Globalization.CultureInfo.InvariantCulture.CompareInfo : null;

                c = expr.BinaryCompare(vLeft, vRight, resultType, expr._op, comparer);
            }
            switch (expr._op)
            {
            case Operators.EqualTo: c = (c == 0 ? 0 : c < 0 ? -1 : 1); break;

            case Operators.GreaterThen: c = (c > 0 ? 0 : -1); break;

            case Operators.LessThen: c = (c < 0 ? 0 : 1); break;

            case Operators.GreaterOrEqual: c = (c >= 0 ? 0 : -1); break;

            case Operators.LessOrEqual: c = (c <= 0 ? 0 : 1); break;

            case Operators.Is: c = (vLeft == DBNull.Value ? 0 : -1); break;

            case Operators.IsNot: c = (vLeft != DBNull.Value ? 0 : 1); break;

            default: Debug.Assert(true, "Unsupported Binary Search Operator!"); break;
            }
            return((int)c);
        }
Пример #4
0
        public void TestConvertToNumeric()
        {
            object x;

            // decimal
            decimal defaut = -1m;

            x = 3.5m;
            decimal i = SqlConvert.To(x, defaut);

            AssertAreEqual(i, 3.5m, "Decimal 1");

            x = null;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Decimal null 2");

            x = "toto";
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Decimal null 3");

            x = DBNull.Value;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Decimal null 4");

            // double
            double defaut2 = -1;

            x = 3.5m;
            double j = SqlConvert.To(x, defaut2);

            AssertAreEqual(j, 3.5, "Double 1");

            x = null;
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "Double null 2");

            x = "toto";
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "Double null 3");

            x = DBNull.Value;
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "Double null 4");

            // Float
            float defaut3 = -1f;

            x = 3.5f;
            float k = SqlConvert.To(x, defaut3);

            AssertAreEqual(k, 3.5f, "Float 1");

            x = null;
            k = SqlConvert.To(x, defaut3);
            AssertAreEqual(k, defaut3, "Float null 2");

            x = "toto";
            k = SqlConvert.To(x, defaut3);
            AssertAreEqual(j, defaut3, "Float null 3");

            x = DBNull.Value;
            k = SqlConvert.To(x, defaut3);
            AssertAreEqual(j, defaut3, "Float null 4");
        }
Пример #5
0
        public void TestConvertToByte()
        {
            object x;
            byte   defaut = 255;

            // byte
            x = 1;
            byte i = SqlConvert.To(x, defaut);

            AssertAreEqual(i, 1, "Byte 1");

            x = null;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Byte null 2");

            x = "toto";
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Byte null 3");

            x = DBNull.Value;
            i = SqlConvert.To(x, defaut);
            AssertAreEqual(i, defaut, "Byte null 4");

            x = ETestByte.b1;
            ETestByte ti = SqlConvert.To(x, ETestByte.b2);

            AssertAreEqual(ti, ETestByte.b1, "Enum Byte 5");

            // SByte
            sbyte defaut2 = 127;

            x = 1;
            sbyte j = SqlConvert.To(x, defaut2);

            AssertAreEqual(j, (sbyte)1, "SByte 1");

            x = null;
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "SByte null 2");

            x = "toto";
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "SByte null 3");

            x = DBNull.Value;
            j = SqlConvert.To(x, defaut2);
            AssertAreEqual(j, defaut2, "SByte null 4");

            x = ETestSByte.sb1;
            ETestSByte sti = SqlConvert.To(x, ETestSByte.sb2);

            AssertAreEqual(sti, ETestSByte.sb1, "Enum SByte 5");

            ////// byte Array
            ////byte[] defaut3 = new byte[3] { 1, 0, 1 };
            ////x = new byte[2] { 1, 0 };
            ////byte[] k = SqlConvert.To(x, defaut3);
            ////Assert.IsNotNull(k, "Byte Array null");
            ////AssertAreEqual(k.Length, 2, "Taille du byte array");
            ////AssertAreEqual(k[0], 1, "Byte Array 1[0]");
            ////AssertAreEqual(k[1], 0, "Byte Array 1[1]");

            ////x = null;
            ////k = SqlConvert.To(x, defaut3);
            ////AssertAreEqual(k, defaut3, "Byte Array Null 2");

            ////x = "toto";
            ////k = SqlConvert.To(x, defaut3);
            ////AssertAreEqual(k, defaut3, "Byte Array Null 3");

            ////x = DBNull.Value;
            ////k = SqlConvert.To(x, defaut3);
            ////AssertAreEqual(k, defaut3, "Byte Array Null 4");
        }
Пример #6
0
        private object EvalFunction(FunctionId id, object[] argumentValues, DataRow row, DataRowVersion version)
        {
            StorageType storageType;

            switch (id)
            {
            case FunctionId.Abs:
                Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider));

                storageType = DataStorage.GetStorageType(argumentValues[0].GetType());
                if (ExpressionNode.IsInteger(storageType))
                {
                    return(Math.Abs((long)argumentValues[0]));
                }
                if (ExpressionNode.IsNumeric(storageType))
                {
                    return(Math.Abs((double)argumentValues[0]));
                }

                throw ExprException.ArgumentTypeInteger(s_funcs[_info]._name, 1);

            case FunctionId.cBool:
                Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider));

                storageType = DataStorage.GetStorageType(argumentValues[0].GetType());
                switch (storageType)
                {
                case StorageType.Boolean:
                    return((bool)argumentValues[0]);

                case StorageType.Int32:
                    return((int)argumentValues[0] != 0);

                case StorageType.Double:
                    return((double)argumentValues[0] != 0.0);

                case StorageType.String:
                    return(bool.Parse((string)argumentValues[0]));

                default:
                    throw ExprException.DatatypeConvertion(argumentValues[0].GetType(), typeof(bool));
                }

            case FunctionId.cInt:
                Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider));
                return(Convert.ToInt32(argumentValues[0], FormatProvider));

            case FunctionId.cDate:
                Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider));
                return(Convert.ToDateTime(argumentValues[0], FormatProvider));

            case FunctionId.cDbl:
                Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider));
                return(Convert.ToDouble(argumentValues[0], FormatProvider));

            case FunctionId.cStr:
                Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider));
                return(Convert.ToString(argumentValues[0], FormatProvider));

            case FunctionId.Charindex:
                Debug.Assert(_argumentCount == 2, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider));

                Debug.Assert(argumentValues[0] is string, "Invalid argument type for " + s_funcs[_info]._name);
                Debug.Assert(argumentValues[1] is string, "Invalid argument type for " + s_funcs[_info]._name);

                if (DataStorage.IsObjectNull(argumentValues[0]) || DataStorage.IsObjectNull(argumentValues[1]))
                {
                    return(DBNull.Value);
                }

                if (argumentValues[0] is SqlString)
                {
                    argumentValues[0] = ((SqlString)argumentValues[0]).Value;
                }

                if (argumentValues[1] is SqlString)
                {
                    argumentValues[1] = ((SqlString)argumentValues[1]).Value;
                }

                return(((string)argumentValues[1]).IndexOf((string)argumentValues[0], StringComparison.Ordinal));

            case FunctionId.Iif:
                Debug.Assert(_argumentCount == 3, "Invalid argument argumentCount: " + _argumentCount.ToString(FormatProvider));

                object first = _arguments[0].Eval(row, version);

                if (DataExpression.ToBoolean(first) != false)
                {
                    return(_arguments[1].Eval(row, version));
                }
                else
                {
                    return(_arguments[2].Eval(row, version));
                }

            case FunctionId.In:
                // we never evaluate IN directly: IN as a binary operator, so evaluation of this should be in
                // BinaryNode class
                throw ExprException.NYI(s_funcs[_info]._name);

            case FunctionId.IsNull:
                Debug.Assert(_argumentCount == 2, "Invalid argument argumentCount: ");

                if (DataStorage.IsObjectNull(argumentValues[0]))
                {
                    return(argumentValues[1]);
                }
                else
                {
                    return(argumentValues[0]);
                }

            case FunctionId.Len:
                Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider));
                Debug.Assert((argumentValues[0] is string) || (argumentValues[0] is SqlString), "Invalid argument type for " + s_funcs[_info]._name);

                if (argumentValues[0] is SqlString)
                {
                    if (((SqlString)argumentValues[0]).IsNull)
                    {
                        return(DBNull.Value);
                    }
                    else
                    {
                        argumentValues[0] = ((SqlString)argumentValues[0]).Value;
                    }
                }

                return(((string)argumentValues[0]).Length);


            case FunctionId.Substring:
                Debug.Assert(_argumentCount == 3, "Invalid argument argumentCount: " + _argumentCount.ToString(FormatProvider));
                Debug.Assert((argumentValues[0] is string) || (argumentValues[0] is SqlString), "Invalid first argument " + argumentValues[0].GetType().FullName + " in " + s_funcs[_info]._name);
                Debug.Assert(argumentValues[1] is int, "Invalid second argument " + argumentValues[1].GetType().FullName + " in " + s_funcs[_info]._name);
                Debug.Assert(argumentValues[2] is int, "Invalid third argument " + argumentValues[2].GetType().FullName + " in " + s_funcs[_info]._name);

                // work around the differences in .NET and VBA implementation of the Substring function
                // 1. The <index> Argument is 0-based in .NET, and 1-based in VBA
                // 2. If the <Length> argument is longer then the string length .NET throws an ArgumentException
                //    but our users still want to get a result.

                int start = (int)argumentValues[1] - 1;

                int length = (int)argumentValues[2];

                if (start < 0)
                {
                    throw ExprException.FunctionArgumentOutOfRange("index", "Substring");
                }

                if (length < 0)
                {
                    throw ExprException.FunctionArgumentOutOfRange("length", "Substring");
                }

                if (length == 0)
                {
                    return(string.Empty);
                }

                if (argumentValues[0] is SqlString)
                {
                    argumentValues[0] = ((SqlString)argumentValues[0]).Value;
                }

                int src_length = ((string)argumentValues[0]).Length;

                if (start > src_length)
                {
                    return(DBNull.Value);
                }

                if (start + length > src_length)
                {
                    length = src_length - start;
                }

                return(((string)argumentValues[0]).Substring(start, length));

            case FunctionId.Trim:
            {
                Debug.Assert(_argumentCount == 1, "Invalid argument argumentCount for " + s_funcs[_info]._name + " : " + _argumentCount.ToString(FormatProvider));
                Debug.Assert((argumentValues[0] is string) || (argumentValues[0] is SqlString), "Invalid argument type for " + s_funcs[_info]._name);

                if (DataStorage.IsObjectNull(argumentValues[0]))
                {
                    return(DBNull.Value);
                }

                if (argumentValues[0] is SqlString)
                {
                    argumentValues[0] = ((SqlString)argumentValues[0]).Value;
                }

                return(((string)argumentValues[0]).Trim());
            }

            case FunctionId.Convert:
                if (_argumentCount != 2)
                {
                    throw ExprException.FunctionArgumentCount(_name);
                }

                if (argumentValues[0] == DBNull.Value)
                {
                    return(DBNull.Value);
                }

                Type        type   = (Type)argumentValues[1];
                StorageType mytype = DataStorage.GetStorageType(type);
                storageType = DataStorage.GetStorageType(argumentValues[0].GetType());

                if (mytype == StorageType.DateTimeOffset)
                {
                    if (storageType == StorageType.String)
                    {
                        return(SqlConvert.ConvertStringToDateTimeOffset((string)argumentValues[0], FormatProvider));
                    }
                }

                if (StorageType.Object != mytype)
                {
                    if ((mytype == StorageType.Guid) && (storageType == StorageType.String))
                    {
                        return(new Guid((string)argumentValues[0]));
                    }

                    if (ExpressionNode.IsFloatSql(storageType) && ExpressionNode.IsIntegerSql(mytype))
                    {
                        if (StorageType.Single == storageType)
                        {
                            return(SqlConvert.ChangeType2((float)SqlConvert.ChangeType2(argumentValues[0], StorageType.Single, typeof(float), FormatProvider), mytype, type, FormatProvider));
                        }
                        else if (StorageType.Double == storageType)
                        {
                            return(SqlConvert.ChangeType2((double)SqlConvert.ChangeType2(argumentValues[0], StorageType.Double, typeof(double), FormatProvider), mytype, type, FormatProvider));
                        }
                        else if (StorageType.Decimal == storageType)
                        {
                            return(SqlConvert.ChangeType2((decimal)SqlConvert.ChangeType2(argumentValues[0], StorageType.Decimal, typeof(decimal), FormatProvider), mytype, type, FormatProvider));
                        }
                        return(SqlConvert.ChangeType2(argumentValues[0], mytype, type, FormatProvider));
                    }

                    return(SqlConvert.ChangeType2(argumentValues[0], mytype, type, FormatProvider));
                }

                return(argumentValues[0]);

            case FunctionId.DateTimeOffset:
                if (argumentValues[0] == DBNull.Value || argumentValues[1] == DBNull.Value || argumentValues[2] == DBNull.Value)
                {
                    return(DBNull.Value);
                }
                switch (((DateTime)argumentValues[0]).Kind)
                {
                case DateTimeKind.Utc:
                    if ((int)argumentValues[1] != 0 && (int)argumentValues[2] != 0)
                    {
                        throw ExprException.MismatchKindandTimeSpan();
                    }
                    break;

                case DateTimeKind.Local:
                    if (DateTimeOffset.Now.Offset.Hours != (int)argumentValues[1] && DateTimeOffset.Now.Offset.Minutes != (int)argumentValues[2])
                    {
                        throw ExprException.MismatchKindandTimeSpan();
                    }
                    break;

                case DateTimeKind.Unspecified: break;
                }
                if ((int)argumentValues[1] < -14 || (int)argumentValues[1] > 14)
                {
                    throw ExprException.InvalidHoursArgument();
                }
                if ((int)argumentValues[2] < -59 || (int)argumentValues[2] > 59)
                {
                    throw ExprException.InvalidMinutesArgument();
                }
                // range should be within -14 hours and  +14 hours
                if ((int)argumentValues[1] == 14 && (int)argumentValues[2] > 0)
                {
                    throw ExprException.InvalidTimeZoneRange();
                }
                if ((int)argumentValues[1] == -14 && (int)argumentValues[2] < 0)
                {
                    throw ExprException.InvalidTimeZoneRange();
                }

                return(new DateTimeOffset((DateTime)argumentValues[0], new TimeSpan((int)argumentValues[1], (int)argumentValues[2], 0)));

            default:
                throw ExprException.UndefinedFunction(s_funcs[_info]._name);
            }
        }
Пример #7
0
        /// <summary>
        /// Create text delimited file from DataSet
        /// </summary>
        public static void create_txt_file(DataSet ds, string destination, txt_data_type type, string txt_qualifier, bool output_field_names)
        {
            if (SqlConvert.ToString(destination) == "")
            {
                throw new Err("You must specify the destination location and filename of the xls file to create.");
            }

            System.Text.StringBuilder sh = new System.Text.StringBuilder();
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            bool      visible;
            DataTable tbl = ds.Tables[0];

            #region Set Export Visible Properties
            try
            {
                foreach (DataColumn col in tbl.Columns)
                {
                    string strColName = col.ColumnName.ToLower();
                    if (strColName == "password" || strColName == "pass" || strColName == "exhibitorpassword" || strColName == "administratorpassword" || strColName == "customerpassword")
                    {
                        col.ExtendedProperties["export_visible"] = false;
                    }
                    if (strColName == "username" || strColName == "user" || strColName == "exhibitorusername" || strColName == "administratorusername" || strColName == "customerusername")
                    {
                        col.ExtendedProperties["export_visible"] = false;
                    }
                }
            }
            catch (Exception ex)
            {
                General.Debugging.Report.SendError("Error Setting ExportVisible property in DataTable", ex);
            }
            #endregion

            if (output_field_names)
            {
                foreach (DataColumn col in tbl.Columns)
                {
                    try
                    {
                        visible = (bool)col.ExtendedProperties["export_visible"];
                    }
                    catch
                    {
                        visible = true;
                    }
                    if (visible)
                    {
                        if (txt_qualifier != null)
                        {
                            sh.Append(txt_qualifier);
                        }
                        sh.Append(col.ColumnName);
                        if (txt_qualifier != null)
                        {
                            sh.Append(txt_qualifier);
                        }
                        sh.Append(seperator(type));
                    }
                }
                if (sh.Length > 0)
                {
                    sh.Remove(sh.Length - seperator(type).Length, seperator(type).Length);
                }
                sh.Append("\r\n");
            }

            foreach (DataRow row in tbl.Rows)
            {
                foreach (DataColumn col in tbl.Columns)
                {
                    try
                    {
                        visible = (bool)col.ExtendedProperties["export_visible"];
                    }
                    catch
                    {
                        visible = true;
                    }
                    if (visible)
                    {
                        if (txt_qualifier != null)
                        {
                            sb.Append(txt_qualifier);
                        }
                        sb.Append(row[col].ToString());
                        if (txt_qualifier != null)
                        {
                            sb.Append(txt_qualifier);
                        }
                        sb.Append(seperator(type));
                    }
                }
                if (sb.Length > 0)
                {
                    sb.Remove(sb.Length - seperator(type).Length, seperator(type).Length);
                }
                sb.Append("\r\n");
            }

            System.IO.StreamWriter sw = new System.IO.StreamWriter(destination, false);
            if (output_field_names)
            {
                sw.Write(sh.ToString());
            }
            sw.Write(sb.ToString());
            sw.Close();
        }