示例#1
0
        /// <summary>
        /// Cria a consulta para recuperar os itens filhos.
        /// </summary>
        /// <param name="parentUid">Identificador da entidade pai.</param>
        /// <param name="parentLoader">Loader do pai.</param>
        /// <param name="parentDataModel">Instancia com os dados do pai.</param>
        /// <param name="sourceContext">Contexto de origem.</param>
        /// <returns></returns>
        internal EntityInfoQuery CreateQuery(int parentUid, IEntityLoader parentLoader, Colosoft.Data.IModel parentDataModel, Colosoft.Query.ISourceContext sourceContext)
        {
            var query = sourceContext.CreateQuery().From(new Query.EntityInfo(DataModelType.FullName));

            if (ExecutePredicate != null)
            {
                query.ExecutePredicate = (Colosoft.Query.QueryExecutePredicate)ExecutePredicate.Clone();
            }
            string conditionalExpression = null;

            if (parentLoader.HasUid)
            {
                query.Add("?parentUid", parentUid);
                conditionalExpression = string.Format("{0} == ?parentUid", _foreignPropertyName);
            }
            if (Conditional != null)
            {
                if (parentLoader.HasUid)
                {
                    conditionalExpression = string.Format("{0} && ({1})", conditionalExpression, this.Conditional.Expression);
                }
                else
                {
                    conditionalExpression = this.Conditional.Expression;
                }
                foreach (var i in this.Conditional.Parameters)
                {
                    if (i.Value is Query.ReferenceParameter)
                    {
                        var reference = (Query.ReferenceParameter)i.Value;
                        var property  = parentDataModel.GetType().GetProperty(reference.ColumnName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                        if (property == null)
                        {
                            throw new Exception(string.Format("Property {0} not found in type {1}", reference.ColumnName, parentDataModel.GetType().FullName));
                        }
                        try
                        {
                            var value = property.GetValue(parentDataModel, null);
                            query.Add(i.Name, value);
                        }
                        catch (System.Reflection.TargetInvocationException ex)
                        {
                            throw ex.InnerException;
                        }
                    }
                    else
                    {
                        query.Add(i.Name, i.Value);
                    }
                }
            }
            query.Where(conditionalExpression);
            return(new EntityInfoQuery(DataModelType, EntityType, query));
        }
示例#2
0
        /// <summary>
        /// Recupera a instancia do descritor da entidade pelo uid informador.
        /// </summary>
        /// <param name="uid">Identificador unico da instancia.</param>
        /// <param name="sourceContext">Contexto de origem.</param>
        /// <param name="uiContext">Contexto visual.</param>
        /// <returns></returns>
        public IEntityDescriptor GetDescriptorByUid <TEntity>(int uid, Colosoft.Query.ISourceContext sourceContext, string uiContext = null) where TEntity : class, IEntity
        {
            sourceContext.Require("sourceContext").NotNull();
            var loader = _entityTypeManager.GetLoader(typeof(TEntity));

            if (!loader.HasUid)
            {
                throw new InvalidOperationException("Entity has not uid.");
            }
            var query = sourceContext.CreateQuery().From(new Query.EntityInfo(loader.DataModelType.FullName)).Where(string.Format("{0} = ?entityuid", loader.UidPropertyName)).Add("?entityuid", uid);

            return(ProcessResultDescriptor <TEntity>(query, sourceContext, uiContext).FirstOrDefault());
        }
示例#3
0
        /// <summary>
        /// Cria a consulta para recuperar os itens filhos.
        /// </summary>
        /// <param name="parentDataModel">Instancia com os dados do pai.</param>
        /// <param name="sourceContext">Contexto de origem.</param>
        /// <returns></returns>
        internal EntityInfoQuery[] CreateQueries(Colosoft.Data.IModel parentDataModel, Colosoft.Query.ISourceContext sourceContext)
        {
            var    query = sourceContext.CreateQuery().From(new Query.EntityInfo(DataModelType.FullName));
            string conditionalExpression = null;
            var    conditional           = GetConditional();

            if (conditional != null)
            {
                conditionalExpression = conditional.Expression;
                foreach (var i in conditional.Parameters)
                {
                    object value = i.Value;
                    if (value is Colosoft.Query.ReferenceParameter)
                    {
                        var referenceParameter = (Colosoft.Query.ReferenceParameter)i.Value;
                        var prop = parentDataModel.GetType().GetProperty(referenceParameter.ColumnName);
                        if (prop != null)
                        {
                            try
                            {
                                value = prop.GetValue(parentDataModel, null);
                            }
                            catch (System.Reflection.TargetInvocationException ex)
                            {
                                throw ex.InnerException;
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException(ResourceMessageFormatter.Create(() => Properties.Resources.EntityLoaderReference_ParentPropertyNotFound, referenceParameter.ColumnName, parentDataModel.GetType().Name).Format());
                        }
                    }
                    query.Add(i.Name, value);
                }
            }
            return(new EntityInfoQuery[] {
                new EntityInfoQuery(DataModelType, EntityType, query.Where(conditionalExpression))
            });
        }
示例#4
0
        /// <summary>
        /// Cria a consulta para recuperar os itens filhos.
        /// </summary>
        /// <param name="parentUid">Identificador da entidade pai.</param>
        /// <param name="parentDataModel">Instancia com os dados do pai.</param>
        /// <param name="sourceContext">Contexto de origem.</param>
        /// <returns></returns>
        public EntityInfoQuery[] CreateQueries(int parentUid, Colosoft.Data.IModel parentDataModel, Colosoft.Query.ISourceContext sourceContext)
        {
            var    query = sourceContext.CreateQuery().From(new Query.EntityInfo(DataModelType.FullName));
            string conditionalExpression = null;

            if (_parentLoader.HasUid)
            {
                query.Add("?parentUid", parentUid);
                conditionalExpression = string.Format("{0} == ?parentUid", _foreignPropertyName);
            }
            if (Conditional != null)
            {
                var conditional = this.Conditional();
                if (conditional != null)
                {
                    if (!string.IsNullOrEmpty(conditionalExpression))
                    {
                        conditionalExpression = string.Format("{0} && ({1})", conditionalExpression, conditional.Expression);
                    }
                    else
                    {
                        conditionalExpression = conditional.Expression;
                    }
                    foreach (var i in conditional.Parameters)
                    {
                        query.Add(i.Name, i.Value);
                    }
                }
            }
            if (string.IsNullOrEmpty(conditionalExpression))
            {
                throw new InvalidOperationException(string.Format("Not support child '{0}', because not found conditional expression.", Name));
            }
            return(new EntityInfoQuery[] {
                new EntityInfoQuery(DataModelType, EntityType, query.Where(conditionalExpression))
            });
        }
示例#5
0
 /// <summary>
 /// Cria a consulta para recuperar os itens filhos.
 /// </summary>
 /// <param name="parentUid">Identificador da entidade pai.</param>
 /// <param name="parentUidProperty"></param>
 /// <param name="sourceContext">Contexto de origem.</param>
 /// <returns></returns>
 public Colosoft.Query.Queryable CreateQuery(int parentUid, string parentUidProperty, Colosoft.Query.ISourceContext sourceContext)
 {
     return(sourceContext.CreateQuery().From(new Query.EntityInfo(LinkDataModelType.FullName, "t1")).Join(ChildDataModelType.FullName, Query.JoinType.Inner, Query.ConditionalContainer.Parse(string.Format("t1.[{0}] == t2.[{1}]", ChildPropertyName, ForeignPropertyName)), "t2").Where(string.Format("t2.[{0}] == ?parentUid", parentUidProperty)).Add("?parentUid", parentUid));
 }