internal static T GetValueOrDefault <T>(this DbDataRecord record, string name)
        {
            var idx = record.GetOrdinal(name);

            return(record.IsDBNull(idx)
                ? default
                : (T)record.GetValue(idx));
        }
        public static T GetValueOrDefault <T>([NotNull] this DbDataRecord record, [NotNull] string name)
        {
            var idx = record.GetOrdinal(name);

            return(record.IsDBNull(idx)
                ? default
                : (T)record.GetValue(idx));
        }
        public static object IsDBNull(this DbDataRecord dr, string columnName)
        {
            Check.ArgNotNull(dr, nameof(dr));

            int ordinal = dr.GetOrdinal(columnName);

            return(dr.IsDBNull(ordinal));
        }
示例#4
0
        /// <summary>
        /// 获得值
        /// </summary>
        /// <param name="record">DbDataRecord</param>
        /// <param name="name">名称</param>
        /// <returns></returns>
        public static object GetValue(this DbDataRecord record, [NotNull] string name)
        {
            int ordinal = record.GetOrdinal(name);

            if (!record.IsDBNull(ordinal))
            {
                return(record.GetValue(ordinal));
            }
            return(null);
        }
示例#5
0
        public static object GetNextKey(this EntityObjectStore.LocalContext context, Type type)
        {
            PropertyInfo idMember = context.GetIdMembers(type).Single();
            string       query    = string.Format("max(it.{0}) + 1", idMember.Name);

            dynamic os = context.GetObjectSet(type);
            ObjectQuery <DbDataRecord> results = os.Select(query);
            DbDataRecord result = results.Single();

            return(GetNextKey(type, result.IsDBNull(0) ? 1 : (int)result[0]));
        }
示例#6
0
 public static Int32 Int32OrDefault(DbDataRecord reader, int column, int defValue = 0)
 {
     if (reader.IsDBNull(column))
     {
         return(defValue);
     }
     else
     {
         return(reader.GetInt32(column));
     }
 }
示例#7
0
 public static string StringOrNull(DbDataRecord reader, int column)
 {
     if (reader.IsDBNull(column))
     {
         return(null);
     }
     else
     {
         return(reader.GetString(column));
     }
 }
示例#8
0
        /// <summary>
        /// 获得值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="record">DbDataRecord</param>
        /// <param name="name">名称</param>
        /// <returns></returns>
        public static T GetValueOrDefault <T>(
            [NotNull] this DbDataRecord record,
            [NotNull] string name)
        {
            int ordinal = record.GetOrdinal(name);

            if (!record.IsDBNull(ordinal))
            {
                return((T)GetValue <T>(record.GetValue(ordinal)));
            }
            return(default(T));
        }
        public void Write(object o)
        {
            if (o == null)
            {
                Write("null");
            }
            else if (o == _omittedValue)
            {
                Write("{...}");
            }
            else if (o is DateTime)
            {
                Write("{");
                Write(((DateTime)o).ToString());
                Write("}");
            }
            else if (o is ValueType)
            {
                Write(o.ToString());
            }
            else if (o is Type)
            {
                Write(((Type)o).Name);
            }
            else if (o is Exception)
            {
                Write("EXCEPTION: " + o.ToString());
            }
            else if (o is byte[])
            {
                byte[] arr    = (byte[])o;
                int    length = Math.Min(arr.Length, 32);
                string t      = "Byte[" + arr.Length + "] = " + BitConverter.ToString(arr, 0, length) + ((length != arr.Length) ? "..." : "");
                Write(t);
            }
            else if (o is string)
            {
                Write("\"");
                Write(o as string);
                Write("\"");
            }
            else
            {
                if (o is ObjectCollectionCache)
                {
                    Write(((ObjectCollectionCache)o).OriginalType);
                }
                else
                {
                    Write(o.GetType());
                }
                Write(" ");

                if (_ancestors.Contains(o) || (Level > _maximumDepth + 1))
                {
                    Write("{...}");
                }
                else
                {
                    _ancestors.Push(o);
                    if (o is IEnumerable)
                    {
                        var members = from object element in (o as IEnumerable)
                                      select new Member {
                            Name = null, Value = element
                        };

                        Write(members);
                    }
                    else if (o is DbDataRecord)
                    {
                        DbDataRecord rec = o as DbDataRecord;

                        var members = from element in Enumerable.Range(0, rec.FieldCount)
                                      select new Member {
                            Name = rec.GetName(element), Value = rec.IsDBNull(element) ? null : rec.GetValue(element)
                        };

                        Write(members);
                    }
                    else
                    {
                        var members = from element in o.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance)
                                      let p                 = element as PropertyInfo
                                                      let f = element as FieldInfo
                                                              where p != null || f != null
                                                              select new Member {
                            Name = element.Name, Value = p != null?p.GetValue(o, null) : f.GetValue(o)
                        };

                        // remove members which cause explosion of the tree
                        if (o is EntityReference)
                        {
                            members = members.Select(c => new Member {
                                Name = c.Name, Value = (c.Name == "RelationshipSet" ? _omittedValue : c.Value)
                            });
                        }

                        Write(members);
                    }
                }
            }
        }
示例#10
0
        public TreeNode GetTreeNode(object o)
        {
            if (o == null)
            {
                return(GetNullNode());
            }

            if (o is Exception)
            {
                return(GetExceptionNode((Exception)o));
            }

            if (o is DateTime)
            {
                return(GetDateTimeNode((DateTime)o));
            }

            if (o is String)
            {
                return(GetStringNode((string)o));
            }

            if (o is Type)
            {
                return(GetTypeNode((Type)o));
            }

            if (o is byte[])
            {
                return(GetByteArrayNode((byte[])o));
            }

            if (o is ValueType)
            {
                return(GetValueTypeNode(o));
            }

            if (_ancestors.Contains(o) || _level >= _maxLoadDepth)
            {
                return(new TreeNode("{ ... }"));
            }

            TreeNode parentNode = new TreeNode(GetNiceTypeName(o.GetType()));

            if (o is ObjectCollectionCache)
            {
                parentNode.Text = GetNiceTypeName(((ObjectCollectionCache)o).OriginalType);
            }

            _ancestors.Push(o);
            int oldLevel = _level;

            _level++;

            if (o is IEnumerable)
            {
                parentNode.SelectedImageIndex = parentNode.ImageIndex = 4;
                var members = (from object element in (o as IEnumerable)
                               select new Member {
                    Name = null, Value = element
                }).ToList();

                AttachChildren(parentNode, members);;
                parentNode.Text += " (" + members.Count + " item" + (members.Count != 1 ? "s" : "") + ")";
            }
            else if (o is DbDataRecord)
            {
                parentNode.SelectedImageIndex = parentNode.ImageIndex = 2;
                DbDataRecord rec = o as DbDataRecord;

                var members = from element in Enumerable.Range(0, rec.FieldCount)
                              select new Member {
                    Name = rec.GetName(element), Value = rec.IsDBNull(element) ? null : rec.GetValue(element)
                };

                AttachChildren(parentNode, members);;
            }
            else
            {
                parentNode.SelectedImageIndex = parentNode.ImageIndex = 1;

                var members = from element in o.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance)
                              let p                 = element as PropertyInfo
                                              let f = element as FieldInfo
                                                      where p != null || f != null && !element.Name.StartsWith("_")
                                                      select new Member {
                    Name = element.Name, Value = p != null?p.GetValue(o, null) : f.GetValue(o)
                };

                AttachChildren(parentNode, members);;

                IEntityWithKey ewk = o as IEntityWithKey;
                if (ewk != null)
                {
                    StringBuilder sb = new StringBuilder(parentNode.Text.Length + 20);
                    sb.Append(parentNode.Text);
                    sb.Append("(");

                    for (int i = 0; i < ewk.EntityKey.EntityKeyValues.Length; ++i)
                    {
                        if (i > 0)
                        {
                            sb.Append(", ");
                        }
                        sb.Append(ewk.EntityKey.EntityKeyValues[i].Key);
                        sb.Append("=");
                        sb.Append(ewk.EntityKey.EntityKeyValues[i].Value);
                    }
                    sb.Append(")");
                    parentNode.Text = sb.ToString();
                }
            }
            if (_level <= _expandDepth)
            {
                parentNode.Expand();
            }
            _ancestors.Pop();
            _level = oldLevel;

            return(parentNode);
        }
示例#11
0
 /// <summary>
 /// GetNullSafeDecimal() - Checks the Decimal column to be accessed and if it is DBNull,
 /// 0.0M will be returned.
 /// </summary>
 /// <param name="rec">Database Record</param>
 /// <param name="colIndex">Column Index</param>
 /// <returns>Actual DateTime or MinValue (if null)</returns>
 public decimal GetNullSafeDecimal(DbDataRecord rec, int colIndex)
 {
     return(rec.IsDBNull(colIndex) ? 0.0M : rec.GetDecimal(colIndex));
 }
示例#12
0
 /// <summary>
 /// GetNullSafeInt32() - Checks the Int32 column to be accessed and if it is DBNull,
 /// 0 will be returned.
 /// </summary>
 /// <param name="rec">Database Record</param>
 /// <param name="colIndex">Column Index</param>
 /// <returns>Actual DateTime or MinValue (if null)</returns>
 public int GetNullSafeInt32(DbDataRecord rec, int colIndex)
 {
     return(rec.IsDBNull(colIndex) ? 0 : rec.GetInt32(colIndex));
 }
示例#13
0
 /// <summary>
 /// GetNullSafeDateTime() - Checks the DateTime column to be accessed and if it is DBNull,
 /// the DateTime.MinValue constant will be returned.
 /// </summary>
 /// <param name="rec">Database Record</param>
 /// <param name="colIndex">Column Index</param>
 /// <returns>Actual DateTime or MinValue (if null)</returns>
 public DateTime GetNullSafeDateTime(DbDataRecord rec, int colIndex)
 {
     return(rec.IsDBNull(colIndex) ? DateTime.MinValue : rec.GetDateTime(colIndex));
 }
        public TreeNode GetTreeNode(object o)
        {
            if (o == null)
            {
                return(GetNullNode());
            }

            if (o == _omittedValue)
            {
                return(new TreeNode("{ ... }"));
            }

            if (o is Exception)
            {
                return(GetExceptionNode((Exception)o));
            }

            if (o is DateTime)
            {
                return(GetDateTimeNode((DateTime)o));
            }

            if (o is String)
            {
                return(GetStringNode((string)o));
            }

            if (o is Type)
            {
                return(GetTypeNode((Type)o));
            }

            if (o is byte[])
            {
                return(GetByteArrayNode((byte[])o));
            }

            if (o is ValueType)
            {
                return(GetValueTypeNode(o));
            }

            if (_ancestors.Contains(o) || _level >= _maxLoadDepth)
            {
                return(new TreeNode("{ ... }"));
            }

            TreeNode parentNode = new TreeNode(GetNiceTypeName(o.GetType()));

            if (o is ObjectCollectionCache)
            {
                parentNode.Text = GetNiceTypeName(((ObjectCollectionCache)o).OriginalType);
            }

            _ancestors.Push(o);
            int oldLevel = _level;

            _level++;

            if (o is IEnumerable)
            {
                parentNode.SelectedImageIndex = parentNode.ImageIndex = 4;
                var members = (from object element in (o as IEnumerable)
                               select new Member {
                    Name = null, Value = element
                }).ToList();

                AttachChildren(parentNode, members);;
                parentNode.Text += " (" + members.Count + " item" + (members.Count != 1 ? "s" : "") + ")";
            }
            else if (o is DbDataRecord)
            {
                parentNode.SelectedImageIndex = parentNode.ImageIndex = 2;
                DbDataRecord rec = o as DbDataRecord;

                var members = from element in Enumerable.Range(0, rec.FieldCount)
                              select new Member {
                    Name = rec.GetName(element), Value = rec.IsDBNull(element) ? null : rec.GetValue(element)
                };

                AttachChildren(parentNode, members);;
            }
            else
            {
                parentNode.SelectedImageIndex = parentNode.ImageIndex = 1;

                var members = from element in o.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance)
                              let p                 = element as PropertyInfo
                                              let f = element as FieldInfo
                                                      where p != null || f != null
                                                      select new Member {
                    Name = element.Name, Value = p != null?p.GetValue(o, null) : f.GetValue(o)
                };

                // replace members who would lead to tree explosion with _omittedValue
                // which emits { ... }

                if (o is EntityReference)
                {
                    members = members.Select(c => new Member {
                        Name = c.Name, Value = (c.Name == "RelationshipSet" ? _omittedValue : c.Value)
                    });
                }

                AttachChildren(parentNode, members);;

                IEntityWithKey ewk = o as IEntityWithKey;
                if (ewk != null)
                {
                    parentNode.Text += " { " + GetEntityKeyText(ewk.EntityKey) + " }";
                }
            }
            if (_level <= _expandDepth)
            {
                parentNode.Expand();
            }
            _ancestors.Pop();
            _level = oldLevel;

            return(parentNode);
        }
示例#15
0
            public void Write(object o)
            {
                if (o == null)
                {
                    Write("null");
                }
                else if (o is DateTime)
                {
                    Write("{");
                    Write(((DateTime)o).ToShortDateString());
                    Write("}");
                }
                else if (o is ValueType)
                {
                    Write(o.ToString());
                }
                else if (o is Type)
                {
                    Write(((Type)o).Name);
                }
                else if (o is Exception)
                {
                    Write("EXCEPTION: " + o.ToString());
                }
                else if (o is byte[])
                {
                    byte[] arr    = (byte[])o;
                    int    length = Math.Min(arr.Length, 32);
                    string t      = "Byte[" + arr.Length + "] = " + BitConverter.ToString(arr, 0, length) + ((length != arr.Length) ? "..." : "");
                    Write(t);
                }
                else if (o is string)
                {
                    Write("\"");
                    Write(o as string);
                    Write("\"");
                }
                else
                {
                    if (o is ObjectCollectionCache)
                    {
                        Write(((ObjectCollectionCache)o).OriginalType);
                    }
                    else
                    {
                        Write(o.GetType());
                    }
                    Write(" ");

                    if (ancestors.Contains(o) || (Level > depth + 1))
                    {
                        Write("{...}");
                    }
                    else
                    {
                        ancestors.Push(o);
                        if (o is IEnumerable)
                        {
                            var members = from object element in (o as IEnumerable)
                                          select new Member {
                                Name = null, Value = element
                            };

                            Write(members);
                        }
                        else if (o is DbDataRecord)
                        {
                            DbDataRecord rec = o as DbDataRecord;

                            var members = from element in Enumerable.Range(0, rec.FieldCount)
                                          select new Member {
                                Name = rec.GetName(element), Value = rec.IsDBNull(element) ? null : rec.GetValue(element)
                            };

                            Write(members);
                        }
                        else
                        {
                            var members = from element in o.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance)
                                          let p                 = element as PropertyInfo
                                                          let f = element as FieldInfo
                                                                  where p != null || f != null && !element.Name.StartsWith("_")
                                                                  select new Member {
                                Name = element.Name, Value = p != null?p.GetValue(o, null) : f.GetValue(o)
                            };

                            Write(members);
                        }
                    }
                }
            }
示例#16
0
        /// <summary>
        ///     Map used to specify the type for more dynamic code.
        /// </summary>
        /// <param name="idr">DbDataRecord from DbDataReader</param>
        /// <param name="type">Type of the object to be returned</param>
        /// <returns>Returns instance of the specified type populated from the database.</returns>
        internal static object Map(DbDataRecord idr, Type type)
        {
            object result = Activator.CreateInstance(type);

            if (result == null)
            {
                throw new Exception(@"Unable to materialize the specified type.");
            }

            string[] columNames = new string[idr.FieldCount];

            for (int col = 0; col < idr.FieldCount; col++)
            {
                if (idr.IsDBNull(col))
                {
                    continue;                    /* Only map the columns that have values */
                }
                columNames[col] = idr.GetName(col).ToLower();
            }

            /* Use the extender to get the relevant properties */
            ConcurrentBag <PropertyMap> mappings = new(result.GetMappings(columNames));

            if (mappings.Count <= 0)
            {
                return(null);
            }


            foreach (PropertyMap pm in mappings)
            {
                PropertyInfo pi  = pm.Key;
                Column       col = pm.Value;

                string name     = string.Equals(col.Name, pi.Name, StringComparison.CurrentCultureIgnoreCase) ? pi.Name : col.Name;
                Type   nullable = Nullable.GetUnderlyingType(pi.PropertyType);

                if (idr[name] == DBNull.Value)
                {
                    continue;
                }

                object value = idr[name];

                if (nullable != null)
                {
                    if (col.DataType == typeof(char?))
                    {
                        if (ReferenceEquals(value, ""))
                        {
                            continue;
                        }
                    }

                    pi.SetValue(result, Convert.ChangeType(value, nullable), null);
                    continue; /* Step to the next property */
                }

                if (pi.PropertyType.IsEnum)
                {
                    pi.SetValue(result, Enum.ToObject(pi.PropertyType, value));
                    continue;
                }

                pi.SetValue(result, Convert.ChangeType(value, pi.PropertyType));
            }

            return(result);
        }