public static Object ConvertTo(Object value, Type to)
 {
     if (value == null)
     {
         return(DBNull.Value);
     }
     else if (value is ExpandoObject)
     {
         var id = (value as ExpandoObject).GetObject("Id");
         if (DataHelpers.IsIdIsNull(id))
         {
             return(DBNull.Value);
         }
         return(Convert.ChangeType(id, to, CultureInfo.InvariantCulture));
     }
     else if (value is String)
     {
         var str = value.ToString();
         if (String.IsNullOrEmpty(str))
         {
             return(DBNull.Value);
         }
         if (to == typeof(Guid))
         {
             return(Guid.Parse(value.ToString()));
         }
         return(value);
     }
     else
     {
         return(Convert.ChangeType(value, to, CultureInfo.InvariantCulture));
     }
 }
예제 #2
0
        public FieldInfo(String name)
        {
            PropertyName = null;
            TypeName     = null;
            FieldType    = FieldType.Scalar;
            SpecType     = SpecType.Unknown;
            IsLazy       = false;
            var x = name.Split('!');

            if (x.Length > 0)
            {
                PropertyName = x[0];
            }
            CheckField(x);
            if (x.Length > 1)
            {
                TypeName  = x[1];
                FieldType = FieldType.Object;
            }
            if (x.Length > 2)
            {
                FieldType = DataHelpers.TypeName2FieldType(x[2]);
                if (FieldType == FieldType.Scalar || FieldType == FieldType.Array)
                {
                    SpecType = DataHelpers.TypeName2SpecType(x[2]);
                }
                IsLazy = x[2].Contains("Lazy");
            }
            IsComplexField = PropertyName.Contains('.');
            CheckReservedWords();
        }
예제 #3
0
        void ProcessSystemRecord(IDataReader rdr)
        {
            ExpandoObject _createModelInfo(String elem)
            {
                return(_root.GetOrCreate <ExpandoObject>("$ModelInfo").GetOrCreate <ExpandoObject>(elem));
            }

            // from 1
            for (Int32 i = 1; i < rdr.FieldCount; i++)
            {
                var fn      = rdr.GetName(i);
                var fi      = new FieldInfo(fn);
                var dataVal = rdr.GetValue(i);
                switch (fi.SpecType)
                {
                case SpecType.Id:
                    _sys.Add("Id", dataVal);
                    break;

                case SpecType.PageSize:
                    Int32 pageSize = (Int32)dataVal;
                    if (!String.IsNullOrEmpty(fi.TypeName))
                    {
                        _createModelInfo(fi.TypeName).Set("PageSize", pageSize);
                    }
                    else
                    {
                        // for compatibility with older versions
                        _sys.Add("PageSize", pageSize);
                    }
                    break;

                case SpecType.Offset:
                    if (String.IsNullOrEmpty(fi.TypeName))
                    {
                        throw new DataLoaderException("For the Offset modifier, the field name must be specified");
                    }
                    Int32 offset = (Int32)dataVal;
                    _createModelInfo(fi.TypeName).Set("Offset", offset);
                    break;

                case SpecType.HasRows:
                    if (String.IsNullOrEmpty(fi.TypeName))
                    {
                        throw new DataLoaderException("For the HasRows modifier, the field name must be specified");
                    }
                    if (dataVal is Int32 intHasRows)
                    {
                        _createModelInfo(fi.TypeName).Set("HasRows", intHasRows != 0);
                    }
                    else if (dataVal is Boolean boolHasRows)
                    {
                        _createModelInfo(fi.TypeName).Set("HasRows", boolHasRows);
                    }
                    else
                    {
                        throw new DataLoaderException("Invalid data type for the TotalRows modifier. Expected 'int' or 'bit'");
                    }
                    break;

                case SpecType.SortDir:
                    if (String.IsNullOrEmpty(fi.TypeName))
                    {
                        throw new DataLoaderException("For the SortDir modifier, the field name must be specified");
                    }
                    String dir = dataVal.ToString();
                    _createModelInfo(fi.TypeName).Set("SortDir", dir);
                    break;

                case SpecType.SortOrder:
                    if (String.IsNullOrEmpty(fi.TypeName))
                    {
                        throw new DataLoaderException("For the SortOrder modifier, the field name must be specified");
                    }
                    String order = dataVal.ToString();
                    _createModelInfo(fi.TypeName).Set("SortOrder", order);
                    break;

                case SpecType.GroupBy:
                    if (String.IsNullOrEmpty(fi.TypeName))
                    {
                        throw new DataLoaderException("For the Group modifier, the field name must be specified");
                    }
                    String group = dataVal.ToString();
                    _createModelInfo(fi.TypeName).Set("GroupBy", group);
                    break;

                case SpecType.Filter:
                    if (String.IsNullOrEmpty(fi.TypeName))
                    {
                        throw new DataLoaderException("For the Filter modifier, the field name must be specified");
                    }
                    Object filter = dataVal;
                    var    xs     = fi.TypeName.Split('.');
                    if (xs.Length < 2)
                    {
                        throw new DataLoaderException("For the Filter modifier, the field name must be as ItemProperty.FilterProperty");
                    }
                    var fmi = _createModelInfo(xs[0]).GetOrCreate <ExpandoObject>("Filter");
                    if (filter is DateTime)
                    {
                        filter = DataHelpers.DateTime2StringWrap(filter);
                    }
                    else if (filter is String)
                    {
                        filter = _localizer.Localize(filter.ToString());
                    }
                    for (Int32 ii = 1; ii < xs.Length; ii++)
                    {
                        if (ii == xs.Length - 1)
                        {
                            fmi.Set(xs[ii], filter);
                        }
                        else
                        {
                            fmi = fmi.GetOrCreate <ExpandoObject>(xs[ii]);
                        }
                    }
                    break;

                case SpecType.ReadOnly:
                    _sys.Add("ReadOnly", DataHelpers.SqlToBoolean(dataVal));
                    break;

                case SpecType.Copy:
                    _sys.Add("Copy", DataHelpers.SqlToBoolean(dataVal));
                    break;

                default:
                    _sys.Add(fn, dataVal);
                    break;
                }
            }
        }
 public Object DateTime2StringWrap(Object val)
 {
     return(DataHelpers.DateTime2StringWrap(val));
 }