示例#1
0
        protected IDbCommand BuildInsertCommandForDeflatedPredicated(TypeDescriptor typeDescriptor, DataAccessObject dataAccessObject, List <ObjectPropertyValue> updatedProperties)
        {
            var constantPlaceholdersCount = 0;
            var assignments = new List <Expression>();
            var success     = false;

            var requiresIdentityInsert = dataAccessObject.ToObjectInternal().HasAnyChangedPrimaryKeyServerSideProperties;

            var parameter1 = Expression.Parameter(typeDescriptor.Type);

            foreach (var updated in updatedProperties)
            {
                var placeholder = updated.Value as Expression ?? new SqlConstantPlaceholderExpression(constantPlaceholdersCount++, Expression.Constant(updated.Value, updated.PropertyType.CanBeNull() ? updated.PropertyType : updated.PropertyType.MakeNullable()));

                if (placeholder.Type != updated.PropertyType)
                {
                    placeholder = Expression.Convert(placeholder, updated.PropertyType);
                }

                var m = TypeUtils.GetMethod(() => default(DataAccessObject).SetColumnValue(default(string), default(int)))
                        .GetGenericMethodDefinition()
                        .MakeGenericMethod(typeDescriptor.Type, updated.PropertyType);

                assignments.Add(Expression.Call(null, m, parameter1, Expression.Constant(updated.PersistedName), placeholder));
            }

            var method = TypeUtils.GetMethod(() => default(IQueryable <DataAccessObject>).InsertHelper(default(Expression <Action <DataAccessObject> >), default(bool)))
                         .GetGenericMethodDefinition()
                         .MakeGenericMethod(typeDescriptor.Type);

            var source     = Expression.Constant(this.DataAccessModel.GetDataAccessObjects(typeDescriptor.Type));
            var selector   = Expression.Lambda(Expression.Block(assignments), parameter1);
            var expression = (Expression)Expression.Call(null, method, source, Expression.Quote(selector), Expression.Constant(requiresIdentityInsert));

            expression = SqlQueryProvider.Bind(this.DataAccessModel, this.sqlDataTypeProvider, expression);
            expression = SqlQueryProvider.Optimize(this.DataAccessModel, this.SqlDatabaseContext, expression);
            var projectionExpression = expression as SqlProjectionExpression;

            expression = projectionExpression.Select.From;

            var result = this.SqlDatabaseContext.SqlQueryFormatterManager.Format(expression);

            IDbCommand command = null;

            try
            {
                command             = this.CreateCommand();
                command.CommandText = result.CommandText;

                var cachedValue = new SqlCachedUpdateInsertFormatValue {
                    formatResult = result
                };

                FillParameters(command, cachedValue, null, null);

                success = true;

                return(command);
            }
            finally
            {
                if (!success)
                {
                    command?.Dispose();
                }
            }
        }
示例#2
0
        protected IDbCommand BuildUpdateCommandForDeflatedPredicated(TypeDescriptor typeDescriptor, DataAccessObject dataAccessObject, bool valuesPredicated, bool primaryKeysPredicated, List <ObjectPropertyValue> updatedProperties, ObjectPropertyValue[] primaryKeys)
        {
            var constantPlaceholdersCount = 0;
            var assignments = new List <Expression>();
            var success     = false;

            var parameter1             = Expression.Parameter(typeDescriptor.Type);
            var requiresIdentityInsert = dataAccessObject.ToObjectInternal().HasAnyChangedPrimaryKeyServerSideProperties;

            foreach (var updated in updatedProperties)
            {
                var value       = updated.Value;
                var placeholder = updated.Value as Expression;

                if (placeholder == null)
                {
                    placeholder = (Expression) new SqlConstantPlaceholderExpression(constantPlaceholdersCount++, Expression.Constant(updated.Value, updated.PropertyType.CanBeNull() ? updated.PropertyType : updated.PropertyType.MakeNullable()));
                }

                if (placeholder.Type != updated.PropertyType)
                {
                    placeholder = Expression.Convert(placeholder, updated.PropertyType);
                }

                var m = TypeUtils.GetMethod(() => default(DataAccessObject).SetColumnValue(default(string), default(int)))
                        .GetGenericMethodDefinition()
                        .MakeGenericMethod(typeDescriptor.Type, updated.PropertyType);

                assignments.Add(Expression.Call(null, m, parameter1, Expression.Constant(updated.PersistedName), placeholder));
            }

            var parameter = Expression.Parameter(typeDescriptor.Type);

            if (primaryKeys.Length <= 0)
            {
                throw new InvalidOperationException("Expected more than 1 primary key");
            }

            Expression where = null;

            foreach (var primaryKey in primaryKeys)
            {
                var value       = primaryKey.Value;
                var placeholder = primaryKey.Value as Expression;

                if (placeholder == null)
                {
                    placeholder = new SqlConstantPlaceholderExpression(constantPlaceholdersCount++, Expression.Constant(value, primaryKey.PropertyType.CanBeNull() ? primaryKey.PropertyType : primaryKey.PropertyType.MakeNullable()));
                }

                if (placeholder.Type != primaryKey.PropertyType)
                {
                    placeholder = Expression.Convert(placeholder, primaryKey.PropertyType);
                }

                var pathComponents     = primaryKey.PropertyName.Split('.');
                var propertyExpression = pathComponents.Aggregate <string, Expression>(parameter, Expression.Property);
                var currentExpression  = Expression.Equal(propertyExpression, placeholder);

                where = where == null ? currentExpression : Expression.And(where, currentExpression);
            }

            var predicate = Expression.Lambda(where, parameter);
            var method    = TypeUtils.GetMethod(() => default(IQueryable <DataAccessObject>).UpdateHelper(default(Expression <Action <DataAccessObject> >), requiresIdentityInsert))
                            .GetGenericMethodDefinition()
                            .MakeGenericMethod(typeDescriptor.Type);

            var source     = Expression.Call(null, MethodInfoFastRef.QueryableWhereMethod.MakeGenericMethod(typeDescriptor.Type), Expression.Constant(this.DataAccessModel.GetDataAccessObjects(typeDescriptor.Type)), Expression.Quote(predicate));
            var selector   = Expression.Lambda(Expression.Block(assignments), parameter1);
            var expression = (Expression)Expression.Call(null, method, source, Expression.Quote(selector), Expression.Constant(requiresIdentityInsert));

            expression = SqlQueryProvider.Bind(this.DataAccessModel, this.sqlDataTypeProvider, expression);
            expression = SqlQueryProvider.Optimize(this.DataAccessModel, this.SqlDatabaseContext, expression);
            var projectionExpression = expression as SqlProjectionExpression;

            expression = projectionExpression.Select.From;

            var result = this.SqlDatabaseContext.SqlQueryFormatterManager.Format(expression);

            IDbCommand command = null;

            try
            {
                command = this.CreateCommand();

                command.CommandText = result.CommandText;

                var cachedValue = new SqlCachedUpdateInsertFormatValue {
                    formatResult = result
                };

                FillParameters(command, cachedValue, null, null);

                success = true;

                return(command);
            }
            finally
            {
                if (!success)
                {
                    command?.Dispose();
                }
            }
        }