Exemplo n.º 1
0
        public int ExecuteNonQuery(ActionContext context)
        {
            ITable table = null;

            Expression expr = DbCommandActionHelper.GetEnumeratorExpression(this.commandTree.Predicate, this.commandTree, context.DbContainer, out table);
            IQueryable entitiesToDelete = DatabaseReflectionHelper.CreateTableQuery(expr, context.DbContainer.Internal);

            return DatabaseReflectionHelper.DeleteEntities(entitiesToDelete, context.Transaction);
        }
Exemplo n.º 2
0
        public DbDataReader ExecuteDataReader(ActionContext context)
        {
            // Find returning fields
            FieldDescription[] returningFields = DbCommandActionHelper.GetReturningFields(this.commandTree.Returning);
            List<IDictionary<string, object>> returningValues = new List<IDictionary<string, object>>();

            // Find NMemory table
            ITable table = DbCommandActionHelper.GetTable(this.commandTree, context.DbContainer);

            // Collect the SetClause DbExpressions into a dictionary
            IDictionary<string, DbExpression> setClauses = DbCommandActionHelper.GetSetClauseExpressions(this.commandTree.SetClauses);

            // Collection for collection member bindings
            IList<MemberBinding> memberBindings = new List<MemberBinding>();
            TransformVisitor transform = new TransformVisitor(context.DbContainer.TypeConverter);

            // Initialize member bindings
            foreach (PropertyInfo property in table.EntityType.GetProperties())
            {
                Expression setter = null;

                // Check if member has set clause
                if (setClauses.ContainsKey(property.Name))
                {
                    setter = transform.Visit(setClauses[property.Name]);
                }

                // If setter was found, insert it
                if (setter != null)
                {
                    // Type correction
                    setter = ExpressionHelper.CorrectType(setter, property.PropertyType);

                    // Register binding
                    memberBindings.Add(Expression.Bind(property, setter));
                }
            }

            object entity = CreateAndInsertEntity(table, memberBindings, context.Transaction);

            Dictionary<string, object> entityReturningValues = DbCommandActionHelper.CreateReturningEntity(context, returningFields, entity);
            returningValues.Add(entityReturningValues);

            return new EffortDataReader(
                returningValues.ToArray(),
                1,
                returningFields,
                context.DbContainer);
        }
Exemplo n.º 3
0
        public DbDataReader ExecuteDataReader(ActionContext context)
        {
            TransformVisitor visitor = new TransformVisitor(context.DbContainer.TypeConverter);
            visitor.TableProvider = context.DbContainer;

            // Transform command tree
            Expression queryExpression =
                visitor.Visit(this.commandTree.Query);

            LambdaExpression query =
                Expression.Lambda(queryExpression, Expression.Parameter(typeof(IDatabase)));

            // Create a stored procedure from the expression
            ISharedStoredProcedure procedure =
                DatabaseReflectionHelper.CreateSharedStoredProcedure(query);

            // Format the parameter values
            IDictionary<string, object> parameters =
                DbCommandActionHelper.FormatParameters(
                    context.Parameters,
                    procedure.Parameters,
                    context.DbContainer.TypeConverter);

            IEnumerable result = null;

            if (context.Transaction != null)
            {
                result = procedure.Execute(
                    context.DbContainer.Internal,
                    parameters,
                    context.Transaction);
            }
            else
            {
                result = procedure.Execute(
                    context.DbContainer.Internal,
                    parameters);
            }

            List<FieldDescription> fields = GetReturningFields(this.commandTree);

            return new EffortDataReader(
                result,
                -1,
                fields.ToArray(),
                context.DbContainer);
        }
Exemplo n.º 4
0
        public static Dictionary<string, object> CreateReturningEntity(
            ActionContext context,
            FieldDescription[] returningFields,
            object entity)
        {
            Dictionary<string, object> entityReturningValues = new Dictionary<string, object>();

            for (int i = 0; i < returningFields.Length; i++)
            {
                string property = returningFields[i].Name;

                object value = entity.GetType().GetProperty(property).GetValue(entity, null);

                entityReturningValues[property] = context
                    .DbContainer
                    .TypeConverter
                    .ConvertClrObject(value, returningFields[i].Type);
            }

            return entityReturningValues;
        }
Exemplo n.º 5
0
        public DbDataReader ExecuteDataReader(ActionContext context)
        {
            FieldDescription[] returningFields = DbCommandActionHelper.GetReturningFields(this.commandTree.Returning);
            IList<IDictionary<string, object>> returningEntities = new List<IDictionary<string, object>>();

            ITable table = null;

            Expression expr = DbCommandActionHelper.GetEnumeratorExpression(this.commandTree.Predicate, this.commandTree, context.DbContainer, out table);
            IQueryable entitiesToUpdate = DatabaseReflectionHelper.CreateTableQuery(expr, context.DbContainer.Internal);

            Type type = TypeHelper.GetElementType(table.GetType());

            // Collect the SetClause DbExpressions into a dictionary
            IDictionary<string, DbExpression> setClauses = DbCommandActionHelper.GetSetClauseExpressions(this.commandTree.SetClauses);

            // Collection for collection member bindings
            IList<MemberBinding> memberBindings = new List<MemberBinding>();

            TransformVisitor transform = new TransformVisitor(context.DbContainer.TypeConverter);

            // Setup context for the predicate
            ParameterExpression param = Expression.Parameter(type, "context");
            using (transform.CreateVariable(param, this.commandTree.Target.VariableName))
            {
                // Initialize member bindings
                foreach (PropertyInfo property in type.GetProperties())
                {
                    Expression setter = null;

                    // Check if member has set clause
                    if (setClauses.ContainsKey(property.Name))
                    {
                        setter = transform.Visit(setClauses[property.Name]);
                    }

                    // If setter was found, insert it
                    if (setter != null)
                    {
                        // Type correction
                        setter = ExpressionHelper.CorrectType(setter, property.PropertyType);

                        memberBindings.Add(Expression.Bind(property, setter));
                    }
                }
            }

            Expression updater =
                Expression.Lambda(
                    Expression.MemberInit(Expression.New(type), memberBindings),
                    param);

            IEnumerable<object> updatedEntities = DatabaseReflectionHelper.UpdateEntities(entitiesToUpdate, updater, context.Transaction);
            int affectedRecords = 0;

            foreach (object entity in updatedEntities)
            {
                affectedRecords++;
                Dictionary<string, object> returningEntity = 
                    DbCommandActionHelper.CreateReturningEntity(context, returningFields, entity);

                returningEntities.Add(returningEntity);
            }

            return new EffortDataReader(
                returningEntities,
                affectedRecords,
                returningFields, 
                context.DbContainer);
        }
Exemplo n.º 6
0
 public object ExecuteScalar(ActionContext context)
 {
     throw new NotSupportedException();
 }
Exemplo n.º 7
0
        private ActionContext CreateActionContext()
        {
            ActionContext context = new ActionContext(this.EffortConnection.DbContainer);

            // Store parameters in the context
            foreach (DbParameter parameter in this.Parameters)
            {
                string name = parameter.ParameterName;
                object value = parameter.Value;

                if (value != null)
                {
                    Type originalType = value.GetType();

                    // Resolve enum types
                    if (originalType.IsEnum)
                    {
                        Type primitive = Enum.GetUnderlyingType(originalType);
                        value = Convert.ChangeType(value, primitive);
                    }
                }

                CommandActionParameter commandActionParameter =
                    new CommandActionParameter(name, value);

                context.Parameters.Add(commandActionParameter);
            }

            if (this.EffortTransaction != null)
            {
                context.Transaction = this.EffortTransaction.InternalTransaction;
            }

            return context;
        }
Exemplo n.º 8
0
 public int ExecuteNonQuery(ActionContext context)
 {
     throw new NotSupportedException();
 }
Exemplo n.º 9
0
        public int ExecuteNonQuery(ActionContext context)
        {
            // Get the source table
            ITable table = DbCommandActionHelper.GetTable(this.commandTree, context.DbContainer);

            // Collect the SetClause DbExpressions into a dictionary
            IDictionary<string, DbExpression> setClauses =
                DbCommandActionHelper.GetSetClauseExpressions(this.commandTree.SetClauses);

            // Collection for collection member bindings
            IList<MemberBinding> memberBindings = new List<MemberBinding>();
            TransformVisitor transform = new TransformVisitor(context.DbContainer.TypeConverter);

            // Initialize member bindings
            foreach (PropertyInfo property in table.EntityType.GetProperties())
            {
                Expression setter = null;

                // Check if member has set clause
                if (setClauses.ContainsKey(property.Name))
                {
                    setter = transform.Visit(setClauses[property.Name]);
                }

                // If setter was found, insert it
                if (setter != null)
                {
                    // Type correction
                    setter = ExpressionHelper.CorrectType(setter, property.PropertyType);

                    // Register binding
                    memberBindings.Add(Expression.Bind(property, setter));
                }
            }

            CreateAndInsertEntity(table, memberBindings, context.Transaction);

            return 1;
        }
Exemplo n.º 10
0
 public DbDataReader ExecuteDataReader(ActionContext context)
 {
     throw new NotSupportedException();
 }