コード例 #1
0
        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());
        }
コード例 #2
0
        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());
        }
コード例 #3
0
        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());
        }
コード例 #4
0
        internal static string GenerateDeleteSql(
            DbDeleteCommandTree tree,
            SqlGenerator sqlGenerator,
            out List <SqlParameter> parameters,
            bool upperCaseKeywords = true,
            bool createParameters  = true)
        {
            SqlStringBuilder commandText = new SqlStringBuilder(256)
            {
                UpperCaseKeywords = upperCaseKeywords
            };

            DmlSqlGenerator.ExpressionTranslator expressionTranslator = new DmlSqlGenerator.ExpressionTranslator(commandText, (DbModificationCommandTree)tree, false, sqlGenerator, (ICollection <EdmProperty>)null, createParameters);
            commandText.AppendKeyword("delete ");
            tree.Target.Expression.Accept((DbExpressionVisitor)expressionTranslator);
            commandText.AppendLine();
            commandText.AppendKeyword("where ");
            tree.Predicate.Accept((DbExpressionVisitor)expressionTranslator);
            parameters = expressionTranslator.Parameters;
            return(commandText.ToString());
        }
コード例 #5
0
        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();
        }
コード例 #6
0
        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());
        }
コード例 #7
0
 internal static void GenerateReturningSql(
     SqlStringBuilder commandText,
     DbModificationCommandTree tree,
     EntityType tableType,
     DmlSqlGenerator.ExpressionTranslator translator,
     DbExpression returning,
     bool useGeneratedValuesVariable)
 {
     if (returning == null)
     {
         return;
     }
     commandText.AppendKeyword("select ");
     if (useGeneratedValuesVariable)
     {
         translator.PropertyAlias = "t";
     }
     returning.Accept((DbExpressionVisitor)translator);
     if (useGeneratedValuesVariable)
     {
         translator.PropertyAlias = (string)null;
     }
     commandText.AppendLine();
     if (useGeneratedValuesVariable)
     {
         commandText.AppendKeyword("from ");
         commandText.Append("@generated_keys");
         commandText.AppendKeyword(" as ");
         commandText.Append("g");
         commandText.AppendKeyword(" join ");
         tree.Target.Expression.Accept((DbExpressionVisitor)translator);
         commandText.AppendKeyword(" as ");
         commandText.Append("t");
         commandText.AppendKeyword(" on ");
         string keyword = string.Empty;
         foreach (EdmMember keyMember in tableType.KeyMembers)
         {
             commandText.AppendKeyword(keyword);
             keyword = " and ";
             commandText.Append("g.");
             string memberTsql = DmlSqlGenerator.GenerateMemberTSql(keyMember);
             commandText.Append(memberTsql);
             commandText.Append(" = t.");
             commandText.Append(memberTsql);
         }
         commandText.AppendLine();
         commandText.AppendKeyword("where @@ROWCOUNT > 0");
     }
     else
     {
         commandText.AppendKeyword("from ");
         tree.Target.Expression.Accept((DbExpressionVisitor)translator);
         commandText.AppendLine();
         commandText.AppendKeyword("where @@ROWCOUNT > 0");
         EntitySetBase target = ((DbScanExpression)tree.Target.Expression).Target;
         bool          flag   = false;
         foreach (EdmMember keyMember in target.ElementType.KeyMembers)
         {
             commandText.AppendKeyword(" and ");
             commandText.Append(DmlSqlGenerator.GenerateMemberTSql(keyMember));
             commandText.Append(" = ");
             SqlParameter sqlParameter;
             if (translator.MemberValues.TryGetValue(keyMember, out sqlParameter))
             {
                 commandText.Append(sqlParameter.ParameterName);
             }
             else
             {
                 if (flag)
                 {
                     throw new NotSupportedException(Strings.Update_NotSupportedServerGenKey((object)target.Name));
                 }
                 if (!DmlSqlGenerator.IsValidScopeIdentityColumnType(keyMember.TypeUsage))
                 {
                     throw new InvalidOperationException(Strings.Update_NotSupportedIdentityType((object)keyMember.Name, (object)keyMember.TypeUsage.ToString()));
                 }
                 commandText.Append("scope_identity()");
                 flag = true;
             }
         }
     }
 }