private ObjectQuery <DbDataRecord> GetDatabaseValuesQuery()
        {
            // Build an Entity SQL query that will materialize all the properties for the entity into
            // a DbDataRecord, including nested DbDataRecords for complex properties.
            // This is preferable to a no-tracking query because it doesn't materialize an object only
            // to throw it away again after the properties have been read.
            // Theoretically, it should also work for shadow state,

            var queryBuilder = new StringBuilder();

            queryBuilder.Append("SELECT ");

            // Build the list of properties to query
            AppendEntitySqlRow(queryBuilder, "X", OriginalValues);

            // Add in a WHERE clause for the primary key values
            var quotedEntitySetName = String.Format(
                CultureInfo.InvariantCulture,
                "{0}.{1}",
                DbHelpers.QuoteIdentifier(_stateEntry.EntitySet.EntityContainer.Name),
                DbHelpers.QuoteIdentifier(_stateEntry.EntitySet.Name));

            var quotedTypeName = String.Format(
                CultureInfo.InvariantCulture,
                "{0}.{1}",
                DbHelpers.QuoteIdentifier(EntityType.NestingNamespace()),
                DbHelpers.QuoteIdentifier(EntityType.Name));

            queryBuilder.AppendFormat(
                CultureInfo.InvariantCulture,
                " FROM (SELECT VALUE TREAT (Y AS {0}) FROM {1} AS Y) AS X WHERE ",
                quotedTypeName,
                quotedEntitySetName);

            var entityKeyValues = _stateEntry.EntityKey.EntityKeyValues;
            var parameters      = new ObjectParameter[entityKeyValues.Length];

            for (var i = 0; i < entityKeyValues.Length; i++)
            {
                if (i > 0)
                {
                    queryBuilder.Append(" AND ");
                }

                var name = string.Format(CultureInfo.InvariantCulture, "p{0}", i.ToString(CultureInfo.InvariantCulture));
                queryBuilder.AppendFormat(
                    CultureInfo.InvariantCulture, "X.{0} = @{1}", DbHelpers.QuoteIdentifier(entityKeyValues[i].Key),
                    name);
                parameters[i] = new ObjectParameter(name, entityKeyValues[i].Value);
            }

            return(_internalContext.ObjectContext.CreateQuery <DbDataRecord>(queryBuilder.ToString(), parameters));
        }
        // <summary>
        // Appends a query for the properties in the entity to the given string builder that is being used to
        // build the eSQL query.  This method may be called recursively to query for all the sub-properties of
        // a complex property.
        // </summary>
        // <param name="queryBuilder"> The query builder. </param>
        // <param name="prefix"> The qualifier with which to prefix each property name. </param>
        // <param name="templateValues"> The dictionary that acts as a template for the properties to query. </param>
        private void AppendEntitySqlRow(
            StringBuilder queryBuilder, string prefix, InternalPropertyValues templateValues)
        {
            var commaRequired = false;

            foreach (var propertyName in templateValues.PropertyNames)
            {
                if (commaRequired)
                {
                    queryBuilder.Append(", ");
                }
                else
                {
                    commaRequired = true;
                }

                var quotedName = DbHelpers.QuoteIdentifier(propertyName);

                var templateItem = templateValues.GetItem(propertyName);

                if (templateItem.IsComplex)
                {
                    var nestedValues = templateItem.Value as InternalPropertyValues;
                    if (nestedValues == null)
                    {
                        throw Error.DbPropertyValues_CannotGetStoreValuesWhenComplexPropertyIsNull(
                                  propertyName, EntityType.Name);
                    }

                    // Call the same method recursively to get all the values of the complex property
                    queryBuilder.Append("ROW(");
                    AppendEntitySqlRow(
                        queryBuilder, String.Format(CultureInfo.InvariantCulture, "{0}.{1}", prefix, quotedName),
                        nestedValues);
                    queryBuilder.AppendFormat(CultureInfo.InvariantCulture, ") AS {0}", quotedName);
                }
                else
                {
                    queryBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}.{1} ", prefix, quotedName);
                }
            }
        }
Beispiel #3
0
        private void AppendEntitySqlRow(
            StringBuilder queryBuilder,
            string prefix,
            InternalPropertyValues templateValues)
        {
            bool flag = false;

            foreach (string propertyName in (IEnumerable <string>)templateValues.PropertyNames)
            {
                if (flag)
                {
                    queryBuilder.Append(", ");
                }
                else
                {
                    flag = true;
                }
                string str = DbHelpers.QuoteIdentifier(propertyName);
                IPropertyValuesItem propertyValuesItem = templateValues.GetItem(propertyName);
                if (propertyValuesItem.IsComplex)
                {
                    InternalPropertyValues templateValues1 = propertyValuesItem.Value as InternalPropertyValues;
                    if (templateValues1 == null)
                    {
                        throw Error.DbPropertyValues_CannotGetStoreValuesWhenComplexPropertyIsNull((object)propertyName, (object)this.EntityType.Name);
                    }
                    queryBuilder.Append("ROW(");
                    this.AppendEntitySqlRow(queryBuilder, string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0}.{1}", (object)prefix, (object)str), templateValues1);
                    queryBuilder.AppendFormat((IFormatProvider)CultureInfo.InvariantCulture, ") AS {0}", (object)str);
                }
                else
                {
                    queryBuilder.AppendFormat((IFormatProvider)CultureInfo.InvariantCulture, "{0}.{1} ", (object)prefix, (object)str);
                }
            }
        }
Beispiel #4
0
        private ObjectQuery <DbDataRecord> GetDatabaseValuesQuery()
        {
            StringBuilder queryBuilder = new StringBuilder();

            queryBuilder.Append("SELECT ");
            this.AppendEntitySqlRow(queryBuilder, "X", this.OriginalValues);
            string str1 = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0}.{1}", (object)DbHelpers.QuoteIdentifier(this._stateEntry.EntitySet.EntityContainer.Name), (object)DbHelpers.QuoteIdentifier(this._stateEntry.EntitySet.Name));
            string str2 = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0}.{1}", (object)DbHelpers.QuoteIdentifier(this.EntityType.NestingNamespace()), (object)DbHelpers.QuoteIdentifier(this.EntityType.Name));

            queryBuilder.AppendFormat((IFormatProvider)CultureInfo.InvariantCulture, " FROM (SELECT VALUE TREAT (Y AS {0}) FROM {1} AS Y) AS X WHERE ", (object)str2, (object)str1);
            EntityKeyMember[] entityKeyValues      = this._stateEntry.EntityKey.EntityKeyValues;
            ObjectParameter[] objectParameterArray = new ObjectParameter[entityKeyValues.Length];
            for (int index = 0; index < entityKeyValues.Length; ++index)
            {
                if (index > 0)
                {
                    queryBuilder.Append(" AND ");
                }
                string name = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "p{0}", (object)index.ToString((IFormatProvider)CultureInfo.InvariantCulture));
                queryBuilder.AppendFormat((IFormatProvider)CultureInfo.InvariantCulture, "X.{0} = @{1}", (object)DbHelpers.QuoteIdentifier(entityKeyValues[index].Key), (object)name);
                objectParameterArray[index] = new ObjectParameter(name, entityKeyValues[index].Value);
            }
            return(this._internalContext.ObjectContext.CreateQuery <DbDataRecord>(queryBuilder.ToString(), objectParameterArray));
        }