예제 #1
0
        /// <summary>
        /// Escapes the wildcard characters and the escape character in the given argument.
        /// </summary>
        /// <param name="argument"></param>
        /// <returns>Equivalent to the argument, with the wildcard characters and the escape character escaped</returns>
        public override string EscapeLikeArgument(string argument)
        {
            EntityUtil.CheckArgumentNull(argument, "argument");

            bool usedEscapeCharacter;

            return(SqlProviderManifest.EscapeLikeText(argument, true, out usedEscapeCharacter));
        }
        private static SqlVersion GetSqlVersion(StoreItemCollection storeItemCollection)
        {
            SqlProviderManifest sqlManifest = (storeItemCollection.StoreProviderManifest as SqlProviderManifest);

            if (sqlManifest == null)
            {
                throw EntityUtil.Argument(System.Data.Entity.Strings.Mapping_Provider_WrongManifestType(typeof(SqlProviderManifest)));
            }
            SqlVersion sqlVersion = sqlManifest.SqlVersion;

            return(sqlVersion);
        }
예제 #3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="manifestToken">A token used to infer the capabilities of the store</param>
 public SqlProviderManifest(string manifestToken)
     : base(SqlProviderManifest.GetProviderManifest())
 {
     // GetSqlVersion will throw ArgumentException if manifestToken is null, empty, or not recognized.
     _version = SqlVersionUtils.GetSqlVersion(manifestToken);
 }
        private DbCommand CreateCommand(DbProviderManifest providerManifest, DbCommandTree commandTree)
        {
            EntityUtil.CheckArgumentNull(providerManifest, "providerManifest");
            EntityUtil.CheckArgumentNull(commandTree, "commandTree");

            SqlProviderManifest sqlManifest = (providerManifest as SqlProviderManifest);

            if (sqlManifest == null)
            {
                throw EntityUtil.Argument(System.Data.Entity.Strings.Mapping_Provider_WrongManifestType(typeof(SqlProviderManifest)));
            }

            SqlVersion sqlVersion = sqlManifest.SqlVersion;
            SqlCommand command    = new SqlCommand();

            List <SqlParameter> parameters;
            CommandType         commandType;
            HashSet <string>    paramsToForceNonUnicode;

            command.CommandText = System.Data.SqlClient.SqlGen.SqlGenerator.GenerateSql(commandTree, sqlVersion, out parameters, out commandType, out paramsToForceNonUnicode);
            command.CommandType = commandType;

            // Get the function (if any) implemented by the command tree since this influences our interpretation of parameters
            EdmFunction function = null;

            if (commandTree.CommandTreeKind == DbCommandTreeKind.Function)
            {
                function = ((DbFunctionCommandTree)commandTree).EdmFunction;
            }
            // Now make sure we populate the command's parameters from the CQT's parameters:
            foreach (KeyValuePair <string, TypeUsage> queryParameter in commandTree.Parameters)
            {
                SqlParameter parameter;

                // Use the corresponding function parameter TypeUsage where available (currently, the SSDL facets and
                // type trump user-defined facets and type in the EntityCommand).
                FunctionParameter functionParameter;
                if (null != function && function.Parameters.TryGetValue(queryParameter.Key, false, out functionParameter))
                {
                    const bool preventTruncation = false;
                    parameter = CreateSqlParameter(functionParameter.Name, functionParameter.TypeUsage, functionParameter.Mode, DBNull.Value, preventTruncation, sqlVersion);
                }
                else
                {
                    TypeUsage parameterType;
                    if ((paramsToForceNonUnicode != null) &&               //Reached when a Function Command Tree is passed an incorrect parameter name by the user.
                        (paramsToForceNonUnicode.Contains(queryParameter.Key)))
                    {
                        parameterType = queryParameter.Value.ShallowCopy(new FacetValues {
                            Unicode = false
                        });
                    }
                    else
                    {
                        parameterType = queryParameter.Value;
                    }
                    const bool preventTruncation = false;
                    parameter = CreateSqlParameter(queryParameter.Key, parameterType, ParameterMode.In, DBNull.Value, preventTruncation, sqlVersion);
                }
                command.Parameters.Add(parameter);
            }

            // Now add parameters added as part of SQL gen (note: this feature is only safe for DML SQL gen which
            // does not support user parameters, where there is no risk of name collision)
            if (null != parameters && 0 < parameters.Count)
            {
                if (commandTree.CommandTreeKind != DbCommandTreeKind.Delete &&
                    commandTree.CommandTreeKind != DbCommandTreeKind.Insert &&
                    commandTree.CommandTreeKind != DbCommandTreeKind.Update)
                {
                    throw EntityUtil.InternalError(EntityUtil.InternalErrorCode.SqlGenParametersNotPermitted);
                }
                foreach (SqlParameter parameter in parameters)
                {
                    command.Parameters.Add(parameter);
                }
            }

            return(command);
        }