Esempio n. 1
0
        /// <summary>
        /// Perform a multi-results set query
        /// </summary>
        /// <param name="query">The SQL query to be executed</param>
        /// <param name="args">Arguments to any embedded parameters in the SQL</param>
        /// <typeparam name="T">The Type representing Model in which Result Set Data will be Bind</typeparam>
        /// <returns>Return Object of Generic Type T</returns>
        public T GetMultipleEntity <T>(string query, params object[] args) where T : new()
        {
            IGridReader grd        = _db.QueryMultiple(query, args);
            T           obj        = new T();
            Type        returnType = typeof(T);

            PropertyInfo[] propertyInfo = returnType.GetProperties();
            foreach (var info in propertyInfo)
            {
                bool isFirstorDefault = false;
                Type t;
                if (info.PropertyType.GetGenericArguments().Any())
                {
                    t = info.PropertyType.GetGenericArguments()[0];
                }
                else
                {
                    isFirstorDefault = true;
                    t = info.PropertyType;
                }
                var value =
                    (typeof(BaseDataProvider).GetMethod("GetValueOf")
                     .MakeGenericMethod(t)
                     .Invoke(null, new object[] { grd, isFirstorDefault }));
                info.SetValue(obj, value, null);
            }
            _db.CloseSharedConnection();
            return(obj);
        }
Esempio n. 2
0
        public IGridReader QueryMultiple(Sql sql)
        {
            IGridReader grs = _db.QueryMultiple(sql);

            NLogger.Debug(_db.LastCommand, "PetaPoco FirstOrDefault");
            return(grs);
        }
        private SaleEntity ReadSaleInternal(string command, object param = null)
        {
            using (IGridReader reader = QueryMultiple(command, param))
            {
                SaleEntity sale = reader.Read <SaleEntity, ShippingEntity, AddressEntity, SaleEntity>((s, sh, a) =>
                {
                    s.Delivery = new Delivery();

                    if (sh != null)
                    {
                        s.Delivery.Shipping = sh;
                    }
                    else
                    {
                        s.Delivery.DeliveryType = DeliveryType.PickUpOnly;
                    }

                    s.Delivery.DeliveryAddress = a;

                    return(s);
                }, splitOn: "ShippingId,AddressId").First();

                sale.Items = reader.Read <SaleItemEntity>().ToList();

                return(sale);
            }
        }
 public CategoryEntity Read(int categoryId)
 {
     using (IGridReader reader = QueryMultiple("dbo.SPReadCategory", new { categoryId }))
     {
         CategoryEntity category = reader.Read <CategoryEntity>().SingleOrDefault();
         category.Thumb = reader.Read <FileEntity>().FirstOrDefault();
         return(category);
     }
 }
Esempio n. 5
0
        /// <summary>
        /// Returns multiple recordsets for the provided types.
        /// </summary>
        /// <typeparam name="TFirst"></typeparam>
        /// <typeparam name="TSecond"></typeparam>
        /// <param name="sqlQuery"></param>
        /// <param name="sqlText"></param>
        /// <param name="onExecute"></param>
        /// <returns></returns>
        public static IEnumerable <IEnumerable> QueryMultiple <TFirst, TSecond>(this ISqlQuery sqlQuery, SqlText sqlText, Action <IDbConnection> onExecute = null)
        {
            using (IGridReader gridReader = sqlQuery.QueryMultiple(sqlText.Sql(), sqlText.Parameters, commandType: CommandType.Text, onExecute: onExecute))
            {
                yield return(gridReader.Read <TFirst>());

                yield return(gridReader.Read <TSecond>());
            }
        }
 public SaleEntity FindSale(string query)
 {
     using (IGridReader reader = QueryMultiple("dbo.SPFindSale", new { query = ToSqlSafe(query) }))
     {
         SaleEntity sale = reader.Read <SaleEntity>().First();
         sale.Items = reader.Read <SaleItemEntity>();
         return(sale);
     }
 }
        private void QueryMultipleTest <T1, T2, T3, T4>(T1 value1, T2 value2, T3 value3 = default(T3), T4 value4 = default(T4))
        {
            // set-up
            ISqlQuery   sqlQuery   = A.Fake <ISqlQuery>();
            SqlText     sqlText    = A.Fake <SqlText>();
            IGridReader gridReader = A.Fake <IGridReader>();

            if (value1 != null)
            {
                A.CallTo(() => gridReader.Read <T1>()).Returns(new[] { value1 });
            }

            if (value2 != null)
            {
                A.CallTo(() => gridReader.Read <T2>()).Returns(new[] { value2 });
            }

            if (value3 != null)
            {
                A.CallTo(() => gridReader.Read <T3>()).Returns(new[] { value3 });
            }

            if (value4 != null)
            {
                A.CallTo(() => gridReader.Read <T4>()).Returns(new[] { value4 });
            }

            A.CallTo(() => sqlQuery.QueryMultiple(A <string> .Ignored, A <ParameterCollection> .Ignored, CommandType.Text, A <int?> .Ignored, A <Action <IDbConnection> > .Ignored)).Returns(gridReader);

            IEnumerable <IEnumerable> result;

            // call and assert

            if (value4 != null)
            {
                result = sqlQuery.QueryMultiple <T1, T2, T3, T4>(sqlText);
                ((IEnumerable <T1>)result.First()).First().MustBe(value1);
                ((IEnumerable <T2>)result.Skip(1).First()).First().MustBe(value2);
                ((IEnumerable <T3>)result.Skip(2).First()).First().MustBe(value3);
                ((IEnumerable <T4>)result.Skip(3).First()).First().MustBe(value4);
            }
            else if (value3 != null)
            {
                result = sqlQuery.QueryMultiple <T1, T2, T3>(sqlText);
                ((IEnumerable <T1>)result.First()).First().MustBe(value1);
                ((IEnumerable <T2>)result.Skip(1).First()).First().MustBe(value2);
                ((IEnumerable <T3>)result.Skip(2).First()).First().MustBe(value3);
            }
            else
            {
                result = sqlQuery.QueryMultiple <T1, T2>(sqlText);
                ((IEnumerable <T1>)result.First()).First().MustBe(value1);
                ((IEnumerable <T2>)result.Skip(1).First()).First().MustBe(value2);
            }
        }
Esempio n. 8
0
        private void DoReadGrid(object state)
        {
            TaskState taskstate = state as TaskState;

            if (taskstate.FeatureClass.Disposed || taskstate.FeatureClass.DataSource == null)
            {
                return;
            }
            bool isLoaded = false;

            try
            {
                IGridReader reader = taskstate.FeatureClass.DataSource as IGridReader;
                IGrid       grid   = null;
                if (!reader.IsReady)
                {
                    reader.BeginRead();
                }
                grid = reader.ReadGrid(taskstate.GridNo);
                if (grid != null)
                {
                    taskstate.FeatureClass.AddGrid(grid);
                    isLoaded = true;
                }
            }
            catch (Exception ex)
            {
                Log.WriterException("RuntimeExchanger", "DoReadGrid", ex);
            }
            finally
            {
                if (taskstate.FeatureClass.DataSource != null)
                {
                    if (isLoaded)
                    {
                        (taskstate.FeatureClass.DataSource as IFeatureDataSource).GridStateIndicator.Flaged(taskstate.GridNo);
                    }
                    else//read grid is failed,next time to read
                    {
                        (taskstate.FeatureClass.DataSource as IFeatureDataSource).GridStateIndicator.UnFlaged(taskstate.GridNo);
                    }
                }
                Interlocked.Decrement(ref _readingTaskCount);
                if (isLoaded && _autoRefreshWhileFinishOneGrid)
                {
                    _mapRefresh.ReRender();
                }
                if (_asyncDataArrivedNotify != null)
                {
                    _asyncDataArrivedNotify.SomeDataIsArrived();
                }
                //int n = taskstate.FeatureClass.Grids != null ? taskstate.FeatureClass.Grids.Length : 0;
                //Console.WriteLine("Finished:" + n.ToString());
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Returns multiple recordsets for the provided types.
        /// </summary>
        /// <typeparam name="TFirst"></typeparam>
        /// <typeparam name="TSecond"></typeparam>
        /// <typeparam name="TThird"></typeparam>
        /// <param name="sqlQuery"></param>
        /// <param name="sqlText"></param>
        /// <returns></returns>
        public static IEnumerable <IEnumerable> QueryMultiple <TFirst, TSecond, TThird>(this ISqlQuery sqlQuery, SqlText sqlText)
        {
            using (IGridReader gridReader = sqlQuery.QueryMultiple(sqlText.Sql(), sqlText.Parameters, commandType: CommandType.Text))
            {
                yield return(gridReader.Read <TFirst>());

                yield return(gridReader.Read <TSecond>());

                yield return(gridReader.Read <TThird>());
            }
        }
Esempio n. 10
0
        public TProperty Read <TProperty, TAddress, TMarker>(int propertyId)
            where TProperty : IProperty
            where TAddress : AddressEntity, new()
            where TMarker : Marker
        {
            const string commandName = "dbo.SPReadProperty";

            using (IGridReader reader = QueryMultiple(commandName, new { propertyId }))
            {
                return(Read <TProperty, TAddress, TMarker>(reader));
            }
        }
Esempio n. 11
0
 private void TryEndRead(IGridReader reader, string name)
 {
     if (reader == null)
     {
         return;
     }
     try
     {
         reader.EndRead();
     }
     catch (Exception ex)
     {
         Log.WriterException("RuntimeExchanger", "TryEndRead", ex);
     }
 }
        public ProductEntity Read(int productId)
        {
            using (IGridReader reader = QueryMultiple("dbo.SPReadProduct", new { productId }, onExecute: connection => LicenseHelper.SetContext(connection, DataContext.LicenseSettings)))
            {
                ProductEntity product = reader.Read <ProductEntity, ProductOptionEntity, ProductDetailEntity, CategoryEntity, FileEntity, ProductEntity>((p, o, d, c, f) => { p.MinOption = o; p.MinOption.Detail = d; p.Category = c; p.Thumb = f; return(p); }, splitOn: "ProductOptionId,ProductDetailId,CategoryId,FileId")
                                        .SingleOrDefault();

                if (product != null)
                {
                    product.Options  = new ModelCollection <ProductOptionEntity>(reader.Read <ProductOptionEntity, ProductDetailEntity, ProductOptionEntity>((o, d) => { o.Detail = d; return(o); }, splitOn: "ProductDetailId"));
                    product.MetaData = new MetaCollection(reader.Read <MetaEntity>());
                    product.Files    = new ModelCollection <FileEntity>(reader.Read <FileEntity>());
                }

                return(product);
            }
        }
        public void QueryPage_gets_count_from_data_count_when_includeCount_is_false()
        {
            // set-up
            ISqlQuery   sqlQuery   = A.Fake <ISqlQuery>();
            Select      select     = new Select(includeCount: false);
            IGridReader gridReader = A.Fake <IGridReader>();
            DateTime    now        = DateTime.Now;

            A.CallTo(() => gridReader.Read <DateTime>()).Returns(new[] { now });
            A.CallTo(() => gridReader.Read <int>()).Returns(new[] { 5 });
            A.CallTo(() => sqlQuery.QueryMultiple(select.Sql(), select.Parameters, CommandType.Text, A <int?> .Ignored, A <Action <IDbConnection> > .Ignored)).Returns(gridReader);

            // call
            DataPage <DateTime> dataPage = sqlQuery.QueryPage <DateTime>(select);

            // assert
            dataPage.Count.MustBe(1);
            dataPage.Data.First().MustBe(now);
        }
Esempio n. 14
0
        public TPropertyContact ReadPropertyContact <TPropertyContact, TAddress, TMarker>(int contactId)
            where TPropertyContact : PropertyContactEntity
            where TAddress : AddressEntity, new()
            where TMarker : Marker
        {
            const string commandName = "dbo.SPReadPropertyContact";

            using (IGridReader reader = QueryMultiple(commandName, new { contactId }))
            {
                TPropertyContact propertyContact = reader.Read <TPropertyContact, TAddress, TMarker, TPropertyContact>((p, a, m) =>
                {
                    p.Address        = a ?? new TAddress();
                    p.Address.Marker = m;
                    return(p);
                }, splitOn: "AddressId,Latitude").SingleOrDefault();

                propertyContact.MetaData = new MetaCollection(reader.Read <MetaEntity>());

                return(propertyContact);
            }
        }
Esempio n. 15
0
        public T GetMultipleEntity <T>(string spname, List <SearchValueData> searchParam) where T : new()
        {
            IGridReader grd        = _db.QueryMultiple(GetSPString(spname, searchParam));
            T           obj        = new T();
            Type        returnType = typeof(T);

            PropertyInfo[] propertyInfo = returnType.GetProperties();
            foreach (var info in propertyInfo)
            {
                if (!info.CanWrite)
                {
                    continue;
                }
                bool isFirstorDefault = false;
                Type t;

                if (info.GetCustomAttributes(typeof(IgnoreAttribute), true).Length != 0)
                {
                    continue;
                }
                if (info.PropertyType.GetGenericArguments().Any())
                {
                    t = info.PropertyType.GetGenericArguments()[0];
                }
                else
                {
                    isFirstorDefault = true;
                    t = info.PropertyType;
                }

                var value =
                    (typeof(BaseDataProvider).GetMethod("GetValueOf")
                     .MakeGenericMethod(t)
                     .Invoke(null, new object[] { grd, isFirstorDefault }));
                info.SetValue(obj, value, null);
            }
            _db.CloseSharedConnection();
            return(obj);
        }
Esempio n. 16
0
        private TProperty Read <TProperty, TAddress, TMarker>(IGridReader reader)
            where TProperty : IProperty
            where TAddress : AddressEntity, new()
            where TMarker : Marker
        {
            const string splitOn = "AddressId,Latitude,BranchGuid,DevelopmentId";

            TProperty property = reader.Read <TProperty, TAddress, TMarker, PropertyBranch, DevelopmentEntity, TProperty>((p, a, m, b, d) =>
            {
                p.Address        = a ?? new TAddress();
                p.Address.Marker = m;
                p.Branch         = b;
                p.Development    = d;
                return(p);
            }, splitOn: splitOn).SingleOrDefault();

            if (property != null)
            {
                property.Files = reader.Read <FileEntity>().ToList();

                // nasty hack here.  we have confusion over whether to use the iproperty.branchguid property or the nested one.  this makes sure they are both in sync
                // need to fix :!

                if (property.Branch != null && property.Branch.BranchGuid != property.BranchGuid)
                {
                    property.BranchGuid = property.Branch.BranchGuid;
                }

                if (property.Development != null && property.Development.DevelopmentId != property.DevelopmentId)
                {
                    property.DevelopmentId = property.Development.DevelopmentId;
                }
            }

            return(property);
        }
Esempio n. 17
0
 public ViewGrid(IGridReader persistence)
 {
     _persistence = persistence;
 }