public void HandlIdentity_throws_for_multiple_identity_type()
        {
            var typeUsage = ProviderRegistry.SqlCe4_ProviderManifest.GetStoreType(
                TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)));

            var mockMember = new Mock <EdmMember>();

            mockMember.Setup(m => m.TypeUsage).Returns(typeUsage);

            var mockTransalator = new Mock <DmlSqlGenerator.ExpressionTranslator>();

            mockTransalator.Setup(m => m.MemberValues).Returns(new Dictionary <EdmMember, DbParameter>());

            var builder = new SqlStringBuilder();

            var mockSet = new Mock <EntitySetBase>();

            mockSet.Setup(m => m.Name).Returns("Pickle");

            Assert.Equal(
                ADP1.Update_NotSupportedServerGenKey("Pickle"),
                Assert.Throws <NotSupportedException>(
                    () => DmlSqlGenerator.HandleIdentity(
                        builder, mockTransalator.Object, mockMember.Object, true, mockSet.Object)).Message);
        }
Example #2
0
        internal static bool HandleIdentity(
            StringBuilder commandText, ExpressionTranslator translator, EdmMember member, bool flag, EntitySetBase target)
        {
            DebugCheck.NotNull(commandText);
            DebugCheck.NotNull(translator);
            DebugCheck.NotNull(member);
            DebugCheck.NotNull(target);

            DbParameter parameter;

            if (translator.MemberValues.TryGetValue(member, out parameter))
            {
                commandText.Append(parameter.ParameterName);
            }
            else
            {
                if (flag)
                {
                    throw ADP1.NotSupported(ADP1.Update_NotSupportedServerGenKey(target.Name));
                }
                if (!IsValidIdentityColumnType(member.TypeUsage))
                {
                    throw ADP1.InvalidOperation(ADP1.Update_NotSupportedIdentityType(member.Name, member.TypeUsage.ToString()));
                }
                commandText.AppendFormat("CAST (@@IDENTITY AS {0})", member.TypeUsage.EdmType.Name);
                flag = true;
            }
            return(flag);
        }
Example #3
0
        /// <summary>
        ///     Generates SQL fragment returning server-generated values.
        ///     Requires: translator knows about member values so that we can figure out
        ///     how to construct the key predicate.
        ///     <code>Sample SQL:
        ///
        ///         select IdentityValue
        ///         from MyTable
        ///         where IdentityValue = @@identity
        ///
        ///         NOTE: not scope_identity() because we don't support it.</code>
        /// </summary>
        /// <param name="commandText"> Builder containing command text </param>
        /// <param name="tree"> Modification command tree </param>
        /// <param name="translator"> Translator used to produce DML SQL statement for the tree </param>
        /// <param name="returning"> Returning expression. If null, the method returns immediately without producing a SELECT statement. </param>
        private static void GenerateReturningSql(
            StringBuilder commandText, DbModificationCommandTree tree,
            ExpressionTranslator translator, DbExpression returning)
        {
            if (returning != null)
            {
                commandText.Append("select ");
                returning.Accept(translator);
                commandText.AppendLine();
                commandText.Append("from ");
                tree.Target.Expression.Accept(translator);
                commandText.AppendLine();
                commandText.Append("where ");
                var target  = ((DbScanExpression)tree.Target.Expression).Target;
                var flag    = false;
                var isFirst = true;
                foreach (var member in target.ElementType.KeyMembers)
                {
                    DbParameter parameter;
                    if (!isFirst)
                    {
                        commandText.Append(" and ");
                    }
                    else
                    {
                        isFirst = false;
                    }

                    commandText.Append(GenerateMemberTSql(member));
                    commandText.Append(" = ");
                    if (translator.MemberValues.TryGetValue(member, out parameter))
                    {
                        commandText.Append(parameter.ParameterName);
                    }
                    else
                    {
                        if (flag)
                        {
                            throw ADP1.NotSupported(ADP1.Update_NotSupportedServerGenKey(target.Name));
                        }
                        if (!IsValidIdentityColumnType(member.TypeUsage))
                        {
                            throw ADP1.InvalidOperation(ADP1.Update_NotSupportedIdentityType(member.Name, member.TypeUsage.ToString()));
                        }
                        commandText.Append("@@IDENTITY");
                        flag = true;
                    }
                }
            }
        }