Beispiel #1
0
        /// <summary>
        /// Gets escaped TSql identifier describing this entity set.
        /// </summary>
        /// <returns></returns>
        internal static string GetTargetTSql(EntitySetBase entitySetBase)
        {
            // construct escaped T-SQL referencing entity set
            StringBuilder builder       = new StringBuilder(50);
            string        definingQuery = MetadataHelpers.TryGetValueForMetadataProperty <string>(entitySetBase, "DefiningQuery");

            if (!string.IsNullOrEmpty(definingQuery))
            {
                builder.Append("(");
                builder.Append(definingQuery);
                builder.Append(")");
            }
            else
            {
                string tableName = MetadataHelpers.TryGetValueForMetadataProperty <string>(entitySetBase, "Table");
                if (!string.IsNullOrEmpty(tableName))
                {
                    builder.Append(DB2ProviderManifestHelper.QuoteIdentifier(tableName));
                }
                else
                {
                    builder.Append(DB2ProviderManifestHelper.QuoteIdentifier(entitySetBase.Name));
                }
            }
            return(builder.ToString());
        }
        public void AppendIdentifier(string identifier)
        {
            string correctIdentifier;

            correctIdentifier = identifier.ToLower().StartsWith("dbo.") ? identifier.Substring(4) : identifier;

            if (correctIdentifier.Length > DB2ProviderManifestHelper.MaxObjectNameLength)
            {
                string guid = Guid.NewGuid().ToString().Replace("-", "");
                correctIdentifier = correctIdentifier.Substring(0, DB2ProviderManifestHelper.MaxObjectNameLength - guid.Length) + guid;
            }


            AppendSql(DB2ProviderManifestHelper.QuoteIdentifier(correctIdentifier));
        }
Beispiel #3
0
 public override void Visit(DbPropertyExpression expression)
 {
     _commandText.Append(DB2ProviderManifestHelper.QuoteIdentifier(expression.Property.Name));
 }
Beispiel #4
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 dbo.MyTable
        ///     where IdentityValue = @@Identity
        ///
        /// </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)
        {
            // Nothing to do if there is no Returning expression
            if (returning == null)
            {
                return;
            }

            // select
            commandText.Append("select ");
            returning.Accept(translator);
            commandText.AppendLine();

            // from
            commandText.Append("from ");
            tree.Target.Expression.Accept(translator);
            commandText.AppendLine();

            // where
            commandText.Append("where ");
            EntitySetBase table = ((DbScanExpression)tree.Target.Expression).Target;

            bool identity = false;
            bool first    = true;

            foreach (EdmMember keyMember in table.ElementType.KeyMembers)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    commandText.Append(" and ");
                }

                commandText.Append(DB2ProviderManifestHelper.QuoteIdentifier(keyMember.Name));
                commandText.Append(" = ");

                // retrieve member value sql. the translator remembers member values
                // as it constructs the DML statement (which precedes the "returning"
                // SQL)
                DbParameter value;
                if (translator.MemberValues.TryGetValue(keyMember, out value))
                {
                    commandText.Append(value.ParameterName);
                }
                else
                {
                    // if no value is registered for the key member, it means it is an identity
                    // which can be retrieved using the @@identity function
                    if (identity)
                    {
                        // there can be only one server generated key
                        throw new NotSupportedException(string.Format("Server generated keys are only supported for identity columns. More than one key column is marked as server generated in table '{0}'.", table.Name));
                    }
                    if (keyMember.TypeUsage.EdmType.Name.ToLower() == "guid")
                    {
                        // We can't retrieve the latest inserted guid from Access
                        //commandText.Append("@@guid");
                        commandText.AppendFormat("{{{0}}}", _lastGuid);
                    }
                    else
                    {
                        commandText.Append("@@identity");
                    }
                    identity = true;
                }
            }
        }
Beispiel #5
0
        internal static string GenerateInsertSql(DbInsertCommandTree tree, out List <DbParameter> parameters, bool insertParametersValuesInSql = false)
        {
            StringBuilder        commandText = new StringBuilder(COMMANDTEXT_STRINGBUILDER_INITIALCAPACITY);
            ExpressionTranslator translator  = new ExpressionTranslator(commandText, tree, tree.Returning != null, insertParametersValuesInSql);

            commandText.Append("insert into ");
            tree.Target.Expression.Accept(translator);

            // Actually is not possible to retrieve the last inserted guid from Access
            // We can understand if there is a guid checking the returning value of the insert
            // statement
            string guidAutogeneratedColumn = null;

            if (tree.Returning is DbNewInstanceExpression)
            {
                guidAutogeneratedColumn = GetGuidArgs(tree.Returning as DbNewInstanceExpression);
            }

            if (tree.SetClauses.Count != 0)
            {
                bool first = true;

                // (c1, c2, c3, ...)
                commandText.Append("(");
                if (!string.IsNullOrEmpty(guidAutogeneratedColumn))
                {
                    commandText.Append(DB2ProviderManifestHelper.QuoteIdentifier(guidAutogeneratedColumn));
                    first = false;
                }

                foreach (DbSetClause setClause in tree.SetClauses)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        commandText.Append(", ");
                    }

                    setClause.Property.Accept(translator);
                }
                commandText.AppendLine(")");

                // values c1, c2, ...
                first = true;
                commandText.Append("values (");
                if (!string.IsNullOrEmpty(guidAutogeneratedColumn))
                {
                    _lastGuid = Guid.NewGuid();
                    commandText.Append(string.Format("{{{0}}}", _lastGuid));
                    first = false;
                }

                foreach (DbSetClause setClause in tree.SetClauses)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        commandText.Append(", ");
                    }

                    setClause.Value.Accept(translator);

                    translator.RegisterMemberValue(setClause.Property, setClause.Value);
                }
                commandText.AppendLine(")");
            }
            else
            {
                commandText.AppendLine(" default values");
            }

            // generate returning sql
            GenerateReturningSql(commandText, tree, translator, tree.Returning);

            parameters = translator.Parameters;
            return(commandText.ToString());
        }