Beispiel #1
0
        //internal static Task<List<T>> FromSqlAsync<T>(this ObjectContext db, string format, params object[] parameters)
        //    where T : class
        //{
        //    return FromSqlAsync<T>(db, TemplateQuery.FromString(format, parameters));
        //}

        ///// <summary>
        /////
        ///// </summary>
        ///// <param name="db"></param>
        ///// <param name="format"></param>
        ///// <param name="parameters"></param>
        ///// <returns></returns>
        //internal static JArray FromSqlToJson(
        //    this ObjectContext db,
        //    string format,
        //    params object[] parameters)
        //{
        //    return FromSqlToJson(db, TemplateQuery.FromString(format, parameters));
        //}


        /// <summary>
        ///
        /// </summary>
        /// <param name="db"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public static JArray FromSqlToJson(
            this ObjectContext db,
            TemplateQuery query)
        {
            JArray list  = new JArray();
            var    props = new List <(int index, string name)>();

            using (var dbReader = DbReader.Create(db, query))
            {
                var reader = dbReader.Reader;
                while (reader.Read())
                {
                    if (props.Count == 0)
                    {
                        if (reader.FieldCount == 0)
                        {
                            return(list);
                        }
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            var n = reader.GetName(i);
                            props.Add((i, n));
                        }
                    }

                    var item = new JObject();

                    foreach (var(index, n) in props)
                    {
                        var value = reader.GetValue(index);
                        if (value == null || value == DBNull.Value)
                        {
                            continue;
                        }
                        item.Add(n, JToken.FromObject(value));
                    }
                    list.Add(item);
                }
                return(list);
            }
        }
Beispiel #2
0
        public static List <T> FromSql <T>(
            this ObjectContext db,
            TemplateQuery query,
            bool ignoreUnmatchedProperties = false)
            where T : class
        {
            List <T> list  = new List <T>();
            var      props = new List <(int index, PropertyInfo property, string name)>();

            using (var dbReader = DbReader.Create(db, query))
            {
                var reader = dbReader.Reader;
                while (reader.Read())
                {
                    if (props.Count == 0)
                    {
                        if (reader.FieldCount == 0)
                        {
                            return(list);
                        }
                        Type          type     = typeof(T);
                        List <string> notFound = new List <string>();
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            var n   = reader.GetName(i);
                            var key = $"{type.FullName}.{n}";
                            var p   = propertyCache.GetOrAdd(key, a => type.GetProperty(n, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.SetProperty | BindingFlags.Public));
                            props.Add((i, p, n));
                        }

                        var empty = props.Where(x => x.property == null).Select(x => x.name);
                        if (empty.Any())
                        {
                            if (!ignoreUnmatchedProperties)
                            {
                                throw new InvalidOperationException($"Properties {string.Join(",", empty)} not found in {type.FullName}");
                            }
                            props = props.Where(x => x.property != null).ToList();
                        }
                    }

                    var item = Activator.CreateInstance <T>();

                    foreach (var(index, property, n) in props)
                    {
                        var value = reader.GetValue(index);
                        if (value == null || value == DBNull.Value)
                        {
                            continue;
                        }
                        var type = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
                        if (value.GetType() != type)
                        {
                            value = Convert.ChangeType(value, type);
                        }

                        property.SetValue(item, value);
                    }
                    list.Add(item);
                }
                return(list);
            }
        }