Пример #1
0
        internal DbCommand CreateCommand(
            DbProviderManifest manifest,
            DbCommandTree commandTree,
            bool wantParameters)
        {
            CheckNotNull(manifest, "manifest");
            CheckNotNull(commandTree, "commandTree");

            IngresCommand command = new IngresCommand();

            try
            {
                List <DbParameter> parameters;
                CommandType        commandType;

                command.CommandText = SqlGenerator.GenerateSql((IngresProviderManifest)manifest, commandTree, out parameters, out commandType);
                command.CommandType = commandType;

                if (wantParameters == false)
                {
                    return(command);
                }

                // Get the function (if any) implemented by the command tree since this influences our interpretation of parameters
                EdmFunction function = null;
                if (commandTree is DbFunctionCommandTree)
                {
                    function = ((DbFunctionCommandTree)commandTree).EdmFunction;
                }

                // Now make sure we populate the command's parameters
                // from the Canonical Query Trees's (CQT) parameters:
                foreach (KeyValuePair <string, TypeUsage> queryParameter in commandTree.Parameters)
                {
                    IngresParameter 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))
                    {
                        parameter = CreateSqlParameter((IngresProviderManifest)manifest, functionParameter.Name, functionParameter.TypeUsage, functionParameter.Mode, DBNull.Value);
                    }
                    else
                    {
                        parameter = CreateSqlParameter((IngresProviderManifest)manifest, queryParameter.Key, queryParameter.Value, ParameterMode.In, DBNull.Value);
                    }

                    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 is DbInsertCommandTree) &&
                        !(commandTree is DbUpdateCommandTree) &&
                        !(commandTree is DbDeleteCommandTree))
                    {
                        throw new InvalidOperationException("SqlGenParametersNotPermitted");
                    }

                    foreach (DbParameter parameter in parameters)
                    {
                        command.Parameters.Add(parameter);
                    }
                }

                return(command);
            }
            catch
            {
                command.Dispose();
                throw;
            }
        }