Пример #1
0
        protected virtual Expression VisitStatementList(SqlStatementListExpression statementListExpression)
        {
            List <Expression> newStatements = null;

            for (var i = 0; i < statementListExpression.Statements.Count; i++)
            {
                var expression = this.Visit(statementListExpression.Statements[i]);

                if (expression != statementListExpression.Statements[i])
                {
                    if (newStatements == null)
                    {
                        newStatements = new List <Expression>();

                        for (var j = 0; j < i; j++)
                        {
                            newStatements.Add(statementListExpression.Statements[j]);
                        }
                    }
                }

                if (newStatements != null)
                {
                    newStatements.Add(expression);
                }
            }

            return(newStatements == null ? statementListExpression : new SqlStatementListExpression(newStatements));
        }
Пример #2
0
        protected virtual IDbCommand BuildInsertCommand(TypeDescriptor typeDescriptor, DataAccessObject dataAccessObject)
        {
            IDbCommand      command;
            SqlCommandValue sqlCommandValue;

            var updatedProperties = dataAccessObject.GetAdvanced().GetChangedPropertiesFlattened();
            var commandKey        = new SqlCommandKey(dataAccessObject.GetType(), updatedProperties);

            if (this.TryGetInsertCommand(commandKey, out sqlCommandValue))
            {
                command             = this.CreateCommand();
                command.CommandText = sqlCommandValue.commandText;
                this.FillParameters(command, updatedProperties, null);

                return(command);
            }

            IReadOnlyList <string> returningAutoIncrementColumnNames = null;

            if (dataAccessObject.GetAdvanced().DefinesAnyDirectPropertiesGeneratedOnTheServerSide)
            {
                var propertyDescriptors = typeDescriptor.PersistedProperties.Where(c => c.IsPropertyThatIsCreatedOnTheServerSide).ToList();

                returningAutoIncrementColumnNames = new ReadOnlyList <string>(propertyDescriptors.Select(c => c.PersistedName).ToList());
            }

            var        columnNames      = new ReadOnlyList <string>(updatedProperties.Select(c => c.PersistedName).ToList());
            var        valueExpressions = new ReadOnlyList <Expression>(updatedProperties.Select(c => (Expression)Expression.Constant(c.Value)).ToList());
            Expression expression       = new SqlInsertIntoExpression(new SqlTableExpression(typeDescriptor.PersistedName), columnNames, returningAutoIncrementColumnNames, valueExpressions);

            if (this.SqlDatabaseContext.SqlDialect.SupportsFeature(SqlFeature.PragmaIdentityInsert) && dataAccessObject.ToObjectInternal().HasAnyChangedPrimaryKeyServerSideProperties)
            {
                var list = new List <Expression>
                {
                    new SqlSetCommandExpression("IdentityInsert", new SqlTableExpression(typeDescriptor.PersistedName), Expression.Constant(true)),
                    expression,
                    new SqlSetCommandExpression("IdentityInsert", new SqlTableExpression(typeDescriptor.PersistedName), Expression.Constant(false)),
                };

                expression = new SqlStatementListExpression(list);
            }

            var result = this.SqlDatabaseContext.SqlQueryFormatterManager.Format(expression, SqlQueryFormatterOptions.Default & ~SqlQueryFormatterOptions.OptimiseOutConstantNulls);

            Debug.Assert(result.ParameterValues.Count() == updatedProperties.Count);

            command = this.CreateCommand();

            var commandText = result.CommandText;

            command.CommandText = commandText;
            this.CacheInsertCommand(commandKey, new SqlCommandValue {
                commandText = command.CommandText
            });
            this.FillParameters(command, updatedProperties, null);

            return(command);
        }
        protected override Expression VisitStatementList(SqlStatementListExpression statementListExpression)
        {
            var retval = (SqlStatementListExpression)base.VisitStatementList(statementListExpression);

            if (this.additionalStatements != null)
            {
                retval = retval.ChangeStatements(retval.Statements.Concat(this.additionalStatements));
            }

            return(retval);
        }
Пример #4
0
        protected override Expression VisitStatementList(SqlStatementListExpression statementListExpression)
        {
            var i = 0;

            foreach (var statement in statementListExpression.Statements)
            {
                this.Visit(statement);

                if (i != statementListExpression.Statements.Count - 1)
                {
                    this.WriteLine();
                }
            }

            return(statementListExpression);
        }
Пример #5
0
        protected override Expression VisitStatementList(SqlStatementListExpression statementListExpression)
        {
            var localFoundStatementList = this.foundStatementList;

            this.foundStatementList = true;

            var retval = (SqlStatementListExpression)base.VisitStatementList(statementListExpression);

            if (!localFoundStatementList)
            {
                if (this.amendments.Count > 0)
                {
                    var newList = new List <Expression>(retval.Statements);

                    newList.AddRange(this.amendments);
                    retval = new SqlStatementListExpression(newList);
                }
            }

            return(retval);
        }
        protected virtual IDbCommand BuildInsertCommand(TypeDescriptor typeDescriptor, DataAccessObject dataAccessObject)
        {
            var        success = false;
            IDbCommand command = null;
            SqlCachedUpdateInsertFormatValue cachedValue;
            bool predicated;

            var updatedProperties = dataAccessObject.ToObjectInternal().GetChangedPropertiesFlattened(out predicated);

            if (predicated)
            {
                return(BuildInsertCommandForDeflatedPredicated(typeDescriptor, dataAccessObject, updatedProperties));
            }

            var commandKey = new SqlCachedUpdateInsertFormatKey(dataAccessObject.GetType(), updatedProperties);

            if (this.TryGetInsertCommand(commandKey, out cachedValue))
            {
                try
                {
                    command             = this.CreateCommand();
                    command.CommandText = cachedValue.formatResult.CommandText;
                    this.FillParameters(command, cachedValue, updatedProperties, null);

                    success = true;

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

            IReadOnlyList <string> returningAutoIncrementColumnNames = null;

            if (dataAccessObject.GetAdvanced().DefinesAnyDirectPropertiesGeneratedOnTheServerSide)
            {
                var propertyDescriptors = typeDescriptor.PersistedPropertiesWithoutBackreferences.Where(c => c.IsPropertyThatIsCreatedOnTheServerSide).ToList();

                returningAutoIncrementColumnNames = propertyDescriptors.Select(c => c.PersistedName).ToReadOnlyCollection();
            }

            var constantPlaceholdersCount = 0;
            var valueIndexesToParameterPlaceholderIndexes = new int[updatedProperties.Count];

            var columnNames = updatedProperties.Select(c => c.PersistedName).ToReadOnlyCollection();

            var valueExpressions = new List <Expression>(updatedProperties.Count);

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

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

                valueExpressions.Add(value);
            }

            Expression expression = new SqlInsertIntoExpression(new SqlTableExpression(typeDescriptor.PersistedName), columnNames, returningAutoIncrementColumnNames, valueExpressions);

            for (var i = 0; i < constantPlaceholdersCount; i++)
            {
                valueIndexesToParameterPlaceholderIndexes[i] = i;
            }

            if (this.SqlDatabaseContext.SqlDialect.SupportsCapability(SqlCapability.PragmaIdentityInsert) && dataAccessObject.ToObjectInternal().HasAnyChangedPrimaryKeyServerSideProperties)
            {
                var list = new List <Expression>
                {
                    new SqlSetCommandExpression("IdentityInsert", new SqlTableExpression(typeDescriptor.PersistedName), Expression.Constant(true)),
                    expression,
                    new SqlSetCommandExpression("IdentityInsert", new SqlTableExpression(typeDescriptor.PersistedName), Expression.Constant(false)),
                };

                expression = new SqlStatementListExpression(list);
            }

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

            try
            {
                command = this.CreateCommand();

                var commandText = result.CommandText;

                command.CommandText = commandText;
                cachedValue         = new SqlCachedUpdateInsertFormatValue {
                    formatResult = result, valueIndexesToParameterPlaceholderIndexes = valueIndexesToParameterPlaceholderIndexes, primaryKeyIndexesToParameterPlaceholderIndexes = null
                };

                if (result.Cacheable)
                {
                    this.CacheInsertCommand(commandKey, cachedValue);
                }

                this.FillParameters(command, cachedValue, updatedProperties, null);

                success = true;

                return(command);
            }
            finally
            {
                if (!success)
                {
                    command?.Dispose();
                }
            }
        }
        protected IDbCommand BuildInsertCommandForDeflatedPredicated(TypeDescriptor typeDescriptor, DataAccessObject dataAccessObject, List <ObjectPropertyValue> updatedProperties)
        {
            var constantPlaceholdersCount = 0;
            var assignments = new List <Expression>();
            var success     = false;

            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> >)))
                         .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 = SqlQueryProvider.Bind(this.DataAccessModel, this.sqlDataTypeProvider, expression);
            expression = SqlQueryProvider.Optimize(this.DataAccessModel, this.SqlDatabaseContext, expression);
            var projectionExpression = expression as SqlProjectionExpression;

            expression = projectionExpression.Select.From;

            if (this.SqlDatabaseContext.SqlDialect.SupportsCapability(SqlCapability.PragmaIdentityInsert) && dataAccessObject.ToObjectInternal().HasAnyChangedPrimaryKeyServerSideProperties)
            {
                var list = new List <Expression>
                {
                    new SqlSetCommandExpression("IdentityInsert", new SqlTableExpression(typeDescriptor.PersistedName), Expression.Constant(true)),
                    expression,
                    new SqlSetCommandExpression("IdentityInsert", new SqlTableExpression(typeDescriptor.PersistedName), Expression.Constant(false)),
                };

                expression = new SqlStatementListExpression(list);
            }

            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();
                }
            }
        }