private string IntroduceRequiredLocalVariables(
            EntityType entityType,
            DbInsertCommandTree commandTree)
        {
            List <EdmProperty> list        = entityType.KeyProperties.Where <EdmProperty>((Func <EdmProperty, bool>)(p => p.IsStoreGeneratedIdentity)).ToList <EdmProperty>();
            SqlStringBuilder   commandText = new SqlStringBuilder()
            {
                UpperCaseKeywords = true
            };

            if (list.Any <EdmProperty>())
            {
                foreach (EdmProperty edmProperty in list)
                {
                    commandText.Append(commandText.Length == 0 ? "DECLARE " : ", ");
                    commandText.Append("@");
                    commandText.Append(edmProperty.Name);
                    commandText.Append(" ");
                    commandText.Append(DmlSqlGenerator.GetVariableType(this._sqlGenerator, (EdmMember)edmProperty));
                }
                commandText.AppendLine();
                DmlSqlGenerator.ExpressionTranslator translator = new DmlSqlGenerator.ExpressionTranslator(commandText, (DbModificationCommandTree)commandTree, true, this._sqlGenerator, (ICollection <EdmProperty>)entityType.KeyProperties, true);
                DmlSqlGenerator.GenerateReturningSql(commandText, (DbModificationCommandTree)commandTree, entityType, translator, commandTree.Returning, DmlSqlGenerator.UseGeneratedValuesVariable(commandTree, this._sqlGenerator.SqlVersion));
                commandText.AppendLine();
                commandText.AppendLine();
            }
            return(commandText.ToString());
        }
        internal static string GenerateUpdateSql(
            DbUpdateCommandTree tree,
            SqlGenerator sqlGenerator,
            out List <SqlParameter> parameters,
            bool generateReturningSql = true,
            bool upperCaseKeywords    = true)
        {
            SqlStringBuilder commandText = new SqlStringBuilder(256)
            {
                UpperCaseKeywords = upperCaseKeywords
            };

            DmlSqlGenerator.ExpressionTranslator translator = new DmlSqlGenerator.ExpressionTranslator(commandText, (DbModificationCommandTree)tree, null != tree.Returning, sqlGenerator, (ICollection <EdmProperty>)null, true);
            if (tree.SetClauses.Count == 0)
            {
                commandText.AppendKeyword("declare ");
                commandText.AppendLine("@p int");
            }
            commandText.AppendKeyword("update ");
            tree.Target.Expression.Accept((DbExpressionVisitor)translator);
            commandText.AppendLine();
            bool flag = true;

            commandText.AppendKeyword("set ");
            foreach (DbSetClause setClause in (IEnumerable <DbModificationClause>)tree.SetClauses)
            {
                if (flag)
                {
                    flag = false;
                }
                else
                {
                    commandText.Append(", ");
                }
                setClause.Property.Accept((DbExpressionVisitor)translator);
                commandText.Append(" = ");
                setClause.Value.Accept((DbExpressionVisitor)translator);
            }
            if (flag)
            {
                commandText.Append("@p = 0");
            }
            commandText.AppendLine();
            commandText.AppendKeyword("where ");
            tree.Predicate.Accept((DbExpressionVisitor)translator);
            commandText.AppendLine();
            if (generateReturningSql)
            {
                DmlSqlGenerator.GenerateReturningSql(commandText, (DbModificationCommandTree)tree, (EntityType)null, translator, tree.Returning, false);
            }
            parameters = translator.Parameters;
            return(commandText.ToString());
        }
        private string IntroduceRequiredLocalVariables(EntityType entityType, DbInsertCommandTree commandTree)
        {
            DebugCheck.NotNull(entityType);
            DebugCheck.NotNull(commandTree);

            var storeGeneratedKeys
                = entityType
                  .KeyProperties
                  .Where(p => p.IsStoreGeneratedIdentity)
                  .ToList();

            var sql = new SqlStringBuilder {
                UpperCaseKeywords = true
            };

            if (storeGeneratedKeys.Any())
            {
                foreach (var keyProperty in storeGeneratedKeys)
                {
                    sql.Append(sql.Length == 0 ? "DECLARE " : ", ");
                    sql.Append("@");
                    sql.Append(keyProperty.Name);
                    sql.Append(" ");
                    sql.Append(DmlSqlGenerator.GetVariableType(_sqlGenerator, keyProperty));
                }

                sql.AppendLine();

                var translator
                    = new DmlSqlGenerator.ExpressionTranslator(
                          sql,
                          commandTree,
                          true,
                          _sqlGenerator,
                          entityType.KeyProperties);

                DmlSqlGenerator.GenerateReturningSql(
                    sql,
                    commandTree,
                    entityType,
                    translator,
                    commandTree.Returning,
                    DmlSqlGenerator.UseGeneratedValuesVariable(commandTree, _sqlGenerator.SqlVersion));

                sql.AppendLine();
                sql.AppendLine();
            }

            return(sql.ToString());
        }
        internal static string GenerateInsertSql(
            DbInsertCommandTree tree,
            SqlGenerator sqlGenerator,
            out List <SqlParameter> parameters,
            bool generateReturningSql = true,
            bool upperCaseKeywords    = true,
            bool createParameters     = true)
        {
            SqlStringBuilder commandText = new SqlStringBuilder(256)
            {
                UpperCaseKeywords = upperCaseKeywords
            };

            DmlSqlGenerator.ExpressionTranslator translator = new DmlSqlGenerator.ExpressionTranslator(commandText, (DbModificationCommandTree)tree, null != tree.Returning, sqlGenerator, (ICollection <EdmProperty>)null, createParameters);
            bool       useGeneratedValuesVariable           = DmlSqlGenerator.UseGeneratedValuesVariable(tree, sqlGenerator.SqlVersion);
            EntityType elementType = (EntityType)((DbScanExpression)tree.Target.Expression).Target.ElementType;

            if (useGeneratedValuesVariable)
            {
                commandText.AppendKeyword("declare ").Append("@generated_keys").Append(" table(");
                bool flag = true;
                foreach (EdmMember keyMember in elementType.KeyMembers)
                {
                    if (flag)
                    {
                        flag = false;
                    }
                    else
                    {
                        commandText.Append(", ");
                    }
                    commandText.Append(DmlSqlGenerator.GenerateMemberTSql(keyMember)).Append(" ").Append(DmlSqlGenerator.GetVariableType(sqlGenerator, keyMember));
                    Facet facet;
                    if (keyMember.TypeUsage.Facets.TryGetValue("Collation", false, out facet))
                    {
                        string s = facet.Value as string;
                        if (!string.IsNullOrEmpty(s))
                        {
                            commandText.AppendKeyword(" collate ").Append(s);
                        }
                    }
                }
                commandText.AppendLine(")");
            }
            commandText.AppendKeyword("insert ");
            tree.Target.Expression.Accept((DbExpressionVisitor)translator);
            if (0 < tree.SetClauses.Count)
            {
                commandText.Append("(");
                bool flag = true;
                foreach (DbSetClause setClause in (IEnumerable <DbModificationClause>)tree.SetClauses)
                {
                    if (flag)
                    {
                        flag = false;
                    }
                    else
                    {
                        commandText.Append(", ");
                    }
                    setClause.Property.Accept((DbExpressionVisitor)translator);
                }
                commandText.AppendLine(")");
            }
            else
            {
                commandText.AppendLine();
            }
            if (useGeneratedValuesVariable)
            {
                commandText.AppendKeyword("output ");
                bool flag = true;
                foreach (EdmMember keyMember in elementType.KeyMembers)
                {
                    if (flag)
                    {
                        flag = false;
                    }
                    else
                    {
                        commandText.Append(", ");
                    }
                    commandText.Append("inserted.");
                    commandText.Append(DmlSqlGenerator.GenerateMemberTSql(keyMember));
                }
                commandText.AppendKeyword(" into ").AppendLine("@generated_keys");
            }
            if (0 < tree.SetClauses.Count)
            {
                bool flag = true;
                commandText.AppendKeyword("values (");
                foreach (DbSetClause setClause in (IEnumerable <DbModificationClause>)tree.SetClauses)
                {
                    if (flag)
                    {
                        flag = false;
                    }
                    else
                    {
                        commandText.Append(", ");
                    }
                    setClause.Value.Accept((DbExpressionVisitor)translator);
                    translator.RegisterMemberValue(setClause.Property, setClause.Value);
                }
                commandText.AppendLine(")");
            }
            else
            {
                commandText.AppendKeyword("default values");
                commandText.AppendLine();
            }
            if (generateReturningSql)
            {
                DmlSqlGenerator.GenerateReturningSql(commandText, (DbModificationCommandTree)tree, elementType, translator, tree.Returning, useGeneratedValuesVariable);
            }
            parameters = translator.Parameters;
            return(commandText.ToString());
        }