/// <summary> /// Compile a query, and produce a plan /// </summary> /// <param name="ctree">the input CQT</param> /// <param name="providerCommands">list of provider commands</param> /// <param name="resultColumnMap">column map for result assembly</param> /// <param name="entitySets">the entity sets referenced in this query</param> /// <returns>a compiled plan object</returns> internal static void Compile(cqt.DbCommandTree ctree, out List <ProviderCommandInfo> providerCommands, out ColumnMap resultColumnMap, out int columnCount, out Common.Utils.Set <md.EntitySet> entitySets) { PlanCompiler.Assert(ctree != null, "Expected a valid, non-null Command Tree input"); PlanCompiler pc = new PlanCompiler(ctree); pc.Compile(out providerCommands, out resultColumnMap, out columnCount, out entitySets); }
private void TranslateCommandTree(DbCommandTree commandTree, DbCommand command) { SqlBaseGenerator sqlGenerator = null; DbQueryCommandTree select; DbInsertCommandTree insert; DbUpdateCommandTree update; DbDeleteCommandTree delete; if ((select = commandTree as DbQueryCommandTree) != null) { sqlGenerator = new SqlSelectGenerator(select); } else if ((insert = commandTree as DbInsertCommandTree) != null) { sqlGenerator = new SqlInsertGenerator(insert); } else if ((update = commandTree as DbUpdateCommandTree) != null) { sqlGenerator = new SqlUpdateGenerator(update); } else if ((delete = commandTree as DbDeleteCommandTree) != null) { sqlGenerator = new SqlDeleteGenerator(delete); } else { // TODO: get a message (unsupported DbCommandTree type) throw new ArgumentException(); } sqlGenerator.BuildCommand(command); }
public virtual void VisitCommandTree(DbCommandTree commandTree) { EntityUtil.CheckArgumentNull(commandTree, "commandTree"); switch (commandTree.CommandTreeKind) { case DbCommandTreeKind.Delete: this.VisitDeleteCommandTree((DbDeleteCommandTree)commandTree); break; case DbCommandTreeKind.Function: this.VisitFunctionCommandTree((DbFunctionCommandTree)commandTree); break; case DbCommandTreeKind.Insert: this.VisitInsertCommandTree((DbInsertCommandTree)commandTree); break; case DbCommandTreeKind.Query: this.VisitQueryCommandTree((DbQueryCommandTree)commandTree); break; case DbCommandTreeKind.Update: this.VisitUpdateCommandTree((DbUpdateCommandTree)commandTree); break; default: throw EntityUtil.NotSupported(); } }
/// <summary> /// Create a SqlCommand object given a command tree /// </summary> /// <param name="commandTree">command tree for the statement</param> /// <returns>a command object</returns> internal override DbCommand CreateCommand(DbCommandTree commandTree) { EntityUtil.CheckArgumentNull(commandTree, "commandTree"); StoreItemCollection storeMetadata = (StoreItemCollection)commandTree.MetadataWorkspace.GetItemCollection(DataSpace.SSpace); Debug.Assert(storeMetadata.StoreProviderManifest != null, "StoreItemCollection has null StoreProviderManifest?"); return this.CreateCommand(storeMetadata.StoreProviderManifest, commandTree); }
/// <summary> /// Creates the command definition wrapper. /// </summary> /// <param name="wrappedCommandDefinition">The wrapped command definition.</param> /// <param name="commandTree">The command tree.</param> /// <returns><see cref="DbCommandDefinitionWrapper"/> object.</returns> public virtual DbCommandDefinitionWrapper CreateCommandDefinitionWrapper(DbCommandDefinition wrappedCommandDefinition, DbCommandTree commandTree) { return new DbCommandDefinitionWrapper( wrappedCommandDefinition, commandTree, (cmd, def) => new DbCommandWrapper(cmd, def)); }
public override string GenerateSQL(DbCommandTree commandTree) { DbFunctionCommandTree tree = (commandTree as DbFunctionCommandTree); EdmFunction function = tree.EdmFunction; CommandType = CommandType.StoredProcedure; string cmdText = (string)function.MetadataProperties["CommandTextAttribute"].Value; if (String.IsNullOrEmpty(cmdText)) { string schema = (string)function.MetadataProperties["Schema"].Value; if (String.IsNullOrEmpty(schema)) schema = function.NamespaceName; string functionName = (string)function.MetadataProperties["StoreFunctionNameAttribute"].Value; if (String.IsNullOrEmpty(functionName)) functionName = function.Name; return String.Format("`{0}`", functionName); } else { CommandType = CommandType.Text; return cmdText; } }
internal static EntityCommandDefinition CreateCommandDefinition(DbProviderFactory storeProviderFactory, DbCommandTree commandTree) { //Contract.Requires(storeProviderFactory != null); //Contract.Requires(commandTree != null); return new EntityCommandDefinition(storeProviderFactory, commandTree); }
public virtual void VisitCommandTree(DbCommandTree commandTree) { //Contract.Requires(commandTree != null); switch (commandTree.CommandTreeKind) { case DbCommandTreeKind.Delete: VisitDeleteCommandTree((DbDeleteCommandTree)commandTree); break; case DbCommandTreeKind.Function: VisitFunctionCommandTree((DbFunctionCommandTree)commandTree); break; case DbCommandTreeKind.Insert: VisitInsertCommandTree((DbInsertCommandTree)commandTree); break; case DbCommandTreeKind.Query: VisitQueryCommandTree((DbQueryCommandTree)commandTree); break; case DbCommandTreeKind.Update: VisitUpdateCommandTree((DbUpdateCommandTree)commandTree); break; default: throw new NotSupportedException(); } }
public override string GenerateSQL(DbCommandTree tree) { DbUpdateCommandTree commandTree = tree as DbUpdateCommandTree; UpdateStatement statement = new UpdateStatement(); _onReturningSelect = false; statement.Target = commandTree.Target.Expression.Accept(this); scope.Add("target", statement.Target as InputFragment); if (values == null) values = new Dictionary<EdmMember, SqlFragment>(); foreach (DbSetClause setClause in commandTree.SetClauses) { statement.Properties.Add(setClause.Property.Accept(this)); DbExpression value = setClause.Value; SqlFragment valueFragment = value.Accept(this); statement.Values.Add(valueFragment); if (value.ExpressionKind != DbExpressionKind.Null) { EdmMember property = ((DbPropertyExpression)setClause.Property).Property; values.Add(property, valueFragment); } } statement.Where = commandTree.Predicate.Accept(this); _onReturningSelect = true; if (commandTree.Returning != null) statement.ReturningSelect = GenerateReturningSql(commandTree, commandTree.Returning); return statement.ToString(); }
public override string GenerateSQL(DbCommandTree tree) { DbInsertCommandTree commandTree = tree as DbInsertCommandTree; InsertStatement statement = new InsertStatement(); DbExpressionBinding e = commandTree.Target; statement.Target = (InputFragment)e.Expression.Accept(this); foreach (DbSetClause setClause in commandTree.SetClauses) statement.Sets.Add(setClause.Property.Accept(this)); foreach (DbSetClause setClause in commandTree.SetClauses) { DbExpression value = setClause.Value; SqlFragment valueFragment = value.Accept(this); statement.Values.Add(valueFragment); if (values == null) values = new Dictionary<EdmMember, SqlFragment>(); if (value.ExpressionKind != DbExpressionKind.Null) { EdmMember property = ((DbPropertyExpression)setClause.Property).Property; values.Add(property, valueFragment); } } if (commandTree.Returning != null) statement.ReturningSelect = GenerateReturningSql(commandTree, commandTree.Returning); return statement.ToString(); }
/// <summary> /// Create a Command Definition object, given the connection and command tree /// </summary> /// <param name="connection">connection to the underlying provider</param> /// <param name="commandTree">command tree for the statement</param> /// <returns>an exectable command definition object</returns> /// <exception cref="ArgumentNullException">connection and commandTree arguments must not be null</exception> protected override DbCommandDefinition CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree) { EntityUtil.CheckArgumentNull(providerManifest, "providerManifest"); EntityUtil.CheckArgumentNull(commandTree, "commandTree"); StoreItemCollection storeMetadata = (StoreItemCollection)commandTree.MetadataWorkspace.GetItemCollection(DataSpace.SSpace); return this.CreateCommandDefinition(storeMetadata.StoreProviderFactory, commandTree); }
internal ParseResult(DbCommandTree commandTree, List<FunctionDefinition> functionDefs) { //Contract.Requires(commandTree != null); //Contract.Requires(functionDefs != null); _commandTree = commandTree; _functionDefs = functionDefs.AsReadOnly(); }
/// <summary> /// Ensures that the data space of the specified command tree is the model (C-) space /// </summary> /// <param name="commandTree">The command tree for which the data space should be validated</param> internal override void ValidateDataSpace(DbCommandTree commandTree) { if (commandTree.DataSpace != DataSpace.CSpace) { throw new ProviderIncompatibleException(Strings.EntityClient_RequiresNonStoreCommandTree); } }
/// <summary> /// Create a Command Definition object, given the connection and command tree /// </summary> /// <param name="providerManifest">provider manifest that was determined from metadata</param> /// <param name="commandTree">command tree for the statement</param> /// <returns>an exectable command definition object</returns> protected override DbCommandDefinition CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree) { Debug.Assert(providerManifest != null, "CreateCommandDefinition passed null provider manifest to CreateDbCommandDefinition?"); Debug.Assert(commandTree != null, "CreateCommandDefinition did not validate commandTree argument?"); DbCommand prototype = CreateCommand(providerManifest, commandTree); DbCommandDefinition result = this.CreateCommandDefinition(prototype); return result; }
/// <summary> /// Creates the command definition wrapper for a given provider manifest and command tree. /// </summary> /// <param name="providerManifest">The provider manifest.</param> /// <param name="commandTree">The command tree.</param> /// <returns><see cref="DbCommandDefinition"/> object.</returns> protected override DbCommandDefinition CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree) { var wrapperManifest = (DbProviderManifestWrapper)providerManifest; var createDbCommandDefinitionFunction = this.GetCreateDbCommandDefinitionFunction(wrapperManifest.WrappedProviderManifestInvariantName); DbCommandDefinition definition = createDbCommandDefinitionFunction(wrapperManifest.WrappedProviderManifest, commandTree); return this.CreateCommandDefinitionWrapper(definition, commandTree); }
internal ParseResult(DbCommandTree commandTree, List<FunctionDefinition> functionDefs) { EntityUtil.CheckArgumentNull(commandTree, "commandTree"); EntityUtil.CheckArgumentNull(functionDefs, "functionDefs"); this._commandTree = commandTree; this._functionDefs = functionDefs.AsReadOnly(); }
/// <summary> /// Constructs a new ObjectSpanRewriter that will attempt to apply spanning to the specified query /// (represented as a DbExpression) when <see cref="RewriteQuery"/> is called. /// </summary> /// <param name="toRewrite">A <see cref="DbExpression"/> representing the query to span.</param> internal ObjectSpanRewriter(DbCommandTree tree, DbExpression toRewrite, AliasGenerator aliasGenerator) { Debug.Assert(toRewrite != null, "Expression to rewrite cannot be null"); _toRewrite = toRewrite; _tree = tree; _aliasGenerator = aliasGenerator; }
/// <summary> /// Create a Command Definition object given a command tree. /// </summary> /// <param name="commandTree">command tree for the statement</param> /// <returns>an executable command definition object</returns> /// <remarks> /// This method simply delegates to the provider's implementation of CreateDbCommandDefinition. /// </remarks> public DbCommandDefinition CreateCommandDefinition(DbCommandTree commandTree) { //Contract.Requires(commandTree != null); ValidateDataSpace(commandTree); var storeMetadata = (StoreItemCollection)commandTree.MetadataWorkspace.GetItemCollection(DataSpace.SSpace); Contract.Assert(storeMetadata.StoreProviderManifest != null, "StoreItemCollection has null StoreProviderManifest?"); return CreateDbCommandDefinition(storeMetadata.StoreProviderManifest, commandTree); }
/// <summary> /// Initializes a new instance of the DbCommandDefinitionWrapper class. /// </summary> /// <param name="wrappedCommandDefinition">The wrapped command definition.</param> /// <param name="commandTree">The command tree.</param> /// <param name="commandCreator">The command creator delegate.</param> public DbCommandDefinitionWrapper( DbCommandDefinition wrappedCommandDefinition, DbCommandTree commandTree, Func<DbCommand, DbCommandDefinitionWrapper, DbCommand> commandCreator) { this.wrappedCommandDefinition = wrappedCommandDefinition; this.commandTree = commandTree; this.commandCreator = commandCreator; }
/// <summary> /// Create a Command Definition object given a command tree. /// </summary> /// <param name="commandTree">command tree for the statement</param> /// <returns>an exectable command definition object</returns> /// <remarks> /// This method simply delegates to the provider's implementation of CreateDbCommandDefinition. /// </remarks> public DbCommandDefinition CreateCommandDefinition(DbCommandTree commandTree) { EntityUtil.CheckArgumentNull(commandTree, "commandTree"); ValidateDataSpace(commandTree); StoreItemCollection storeMetadata = (StoreItemCollection)commandTree.MetadataWorkspace.GetItemCollection(DataSpace.SSpace); Debug.Assert(storeMetadata.StoreProviderManifest != null, "StoreItemCollection has null StoreProviderManifest?"); return CreateDbCommandDefinition(storeMetadata.StoreProviderManifest, commandTree); }
/// <summary> /// Ensures that the data space of the specified command tree is the model (C-) space /// </summary> /// <param name="commandTree">The command tree for which the data space should be validated</param> internal override void ValidateDataSpace(DbCommandTree commandTree) { Debug.Assert(commandTree != null, "Ensure command tree is non-null before calling ValidateDataSpace"); if (commandTree.DataSpace != DataSpace.CSpace) { throw EntityUtil.ProviderIncompatible(Strings.EntityClient_RequiresNonStoreCommandTree); } }
protected override DbCommandDefinition CreateDbCommandDefinition( DbProviderManifest providerManifest, DbCommandTree commandTree) { if (commandTree == null) throw new ArgumentNullException("commandTree"); SqlGenerator generator = null; if (commandTree is DbQueryCommandTree) generator = new SelectGenerator(); else if (commandTree is DbInsertCommandTree) generator = new InsertGenerator(); else if (commandTree is DbUpdateCommandTree) generator = new UpdateGenerator(); else if (commandTree is DbDeleteCommandTree) generator = new DeleteGenerator(); else if (commandTree is DbFunctionCommandTree) generator = new FunctionGenerator(); string sql = generator.GenerateSQL(commandTree); EFMySqlCommand cmd = new EFMySqlCommand(); cmd.CommandText = sql; if (generator is FunctionGenerator) cmd.CommandType = (generator as FunctionGenerator).CommandType; SetExpectedTypes(commandTree, cmd); EdmFunction function = null; if (commandTree is DbFunctionCommandTree) function = (commandTree as DbFunctionCommandTree).EdmFunction; // Now make sure we populate the command's parameters from the CQT's parameters: foreach (KeyValuePair<string, TypeUsage> queryParameter in commandTree.Parameters) { DbParameter parameter = cmd.CreateParameter(); parameter.ParameterName = queryParameter.Key; parameter.Direction = ParameterDirection.Input; parameter.DbType = Metadata.GetDbType(queryParameter.Value); FunctionParameter funcParam; if (function != null && function.Parameters.TryGetValue(queryParameter.Key, false, out funcParam)) { parameter.ParameterName = funcParam.Name; parameter.Direction = Metadata.ModeToDirection(funcParam.Mode); parameter.DbType = Metadata.GetDbType(funcParam.TypeUsage); } cmd.Parameters.Add(parameter); } // Now add parameters added as part of SQL gen foreach (DbParameter p in generator.Parameters) cmd.Parameters.Add(p); return CreateCommandDefinition(cmd); }
public override string GenerateSQL(DbCommandTree tree) { DbDeleteCommandTree commandTree = tree as DbDeleteCommandTree; DeleteStatement statement = new DeleteStatement(); //scope.Push(null); statement.Target = commandTree.Target.Expression.Accept(this); statement.Where = commandTree.Predicate.Accept(this); return statement.ToString(); }
/// <summary> /// Internal constructor for a ProviderCommandInfo object /// </summary> /// <param name="commandTree">command tree for the provider command</param> /// <param name="children">children command infos</param> internal ProviderCommandInfo(cqt.DbCommandTree commandTree, List <ProviderCommandInfo> children) { _commandTree = commandTree; _children = children; if (_children == null) { _children = new List <ProviderCommandInfo>(); } foreach (ProviderCommandInfo child in _children) { child._parent = this; } }
/// <summary> /// Internal constructor for a ProviderCommandInfo object /// </summary> /// <param name="commandTree">command tree for the provider command</param> /// <param name="children">children command infos</param> internal ProviderCommandInfo(cqt.DbCommandTree commandTree, List<ProviderCommandInfo> children) { _commandTree = commandTree; _children = children; if (_children == null) { _children = new List<ProviderCommandInfo>(); } foreach (ProviderCommandInfo child in _children) { child._parent = this; } }
public override string GenerateSQL(DbCommandTree tree) { DbQueryCommandTree commandTree = tree as DbQueryCommandTree; SqlFragment fragment = null; DbExpression e = commandTree.Query; switch (commandTree.Query.ExpressionKind) { case DbExpressionKind.Project: fragment = e.Accept(this); Debug.Assert(fragment is SelectStatement); break; } return fragment.ToString(); }
public override string GenerateSQL(DbCommandTree tree) { DbQueryCommandTree commandTree = tree as DbQueryCommandTree; SqlFragment fragment = null; DbExpression e = commandTree.Query; switch (commandTree.Query.ExpressionKind) { case DbExpressionKind.Project: fragment = e.Accept(this); Debug.Assert(fragment is SelectStatement); break; } // Apply post-optimizations here: fragment = TryFlatteningGroupBy(fragment); return fragment.ToString(); }
private DbCommand CreateDbCommand(DbCommandTree commandTree) { if (commandTree == null) throw new ArgumentNullException("commandTree"); DbCommand command = NpgsqlFactory.Instance.CreateCommand(); foreach (KeyValuePair<string, TypeUsage> parameter in commandTree.Parameters) { DbParameter dbParameter = command.CreateParameter(); dbParameter.ParameterName = parameter.Key; command.Parameters.Add(dbParameter); } TranslateCommandTree(commandTree, command); return command; }
/// <summary> /// Create a Command Definition object given a command tree. /// </summary> /// <param name="commandTree">command tree for the statement</param> /// <returns>an exectable command definition object</returns> /// <remarks> /// This method simply delegates to the provider's implementation of CreateDbCommandDefinition. /// </remarks> public DbCommandDefinition CreateCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree) { try { return CreateDbCommandDefinition(providerManifest, commandTree); } catch (ProviderIncompatibleException) { throw; } catch (Exception e) { if (EntityUtil.IsCatchableExceptionType(e)) { throw EntityUtil.ProviderIncompatible(Strings.ProviderDidNotCreateACommandDefinition, e); } throw; } }
/// <summary> /// Create a Command Definition object given a command tree. /// </summary> /// <param name="commandTree">command tree for the statement</param> /// <returns>an executable command definition object</returns> /// <remarks> /// This method simply delegates to the provider's implementation of CreateDbCommandDefinition. /// </remarks> public DbCommandDefinition CreateCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree) { //Contract.Requires(providerManifest != null); //Contract.Requires(commandTree != null); try { return CreateDbCommandDefinition(providerManifest, commandTree); } catch (ProviderIncompatibleException) { throw; } catch (Exception e) { if (e.IsCatchableExceptionType()) { throw new ProviderIncompatibleException(Strings.ProviderDidNotCreateACommandDefinition, e); } throw; } }
internal ObjectFullSpanRewriter(DbCommandTree tree, DbExpression toRewrite, Span span, AliasGenerator aliasGenerator) : base(tree, toRewrite, aliasGenerator) { Debug.Assert(span != null, "Span cannot be null"); Debug.Assert(span.SpanList.Count > 0, "At least one span path is required"); // Retrieve the effective 'T' of the ObjectQuery<T> that produced // the Command Tree that is being rewritten. This could be either // literally 'T' or Collection<T>. EntityType entityType = null; if (!TryGetEntityType(this.Query.ResultType, out entityType)) { // If the result type of the query is neither an Entity type nor a collection // type with an Entity element type, then full Span is currently not allowed. throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.ObjectQuery_Span_IncludeRequiresEntityOrEntityCollection); } // Construct the SpanPathInfo navigation property tree using the // list of Include Span paths from the Span object: // Create a SpanPathInfo instance that represents the root of the tree // and takes its Entity type from the Entity type of the result type of the query. SpanPathInfo spanRoot = new SpanPathInfo(entityType); // Populate the tree of navigation properties based on the navigation property names // in the Span paths from the Span object. Commonly rooted span paths are merged, so // that paths of "Customer.Order" and "Customer.Address", for example, will share a // common SpanPathInfo for "Customer" in the Children collection of the root SpanPathInfo, // and that SpanPathInfo will contain one child for "Order" and another for "Address". foreach (Span.SpanPath path in span.SpanList) { AddSpanPath(spanRoot, path.Navigations); } // The 'current' span path is initialized to the root of the Include span tree _currentSpanPath.Push(spanRoot); }
/// <summary> /// Sets the expected column types /// </summary> private void SetExpectedTypes(DbCommandTree commandTree, EFMySqlCommand cmd) { if (commandTree is DbQueryCommandTree) SetQueryExpectedTypes(commandTree as DbQueryCommandTree, cmd); else if (commandTree is DbFunctionCommandTree) SetFunctionExpectedTypes(commandTree as DbFunctionCommandTree, cmd); }
/// <summary> /// private constructor /// </summary> /// <param name="ctree">the input cqt</param> private PlanCompiler(cqt.DbCommandTree ctree) { m_ctree = ctree; // the input command tree }
protected override DbCommandDefinition CreateDbCommandDefinition(DbProviderManifest providerManifest, System.Data.Common.CommandTrees.DbCommandTree commandTree) { if (providerManifest == null) { throw new ArgumentNullException("providerManifest"); } if (commandTree == null) { throw new ArgumentNullException("commandTree"); } NuoDbCommand command = new NuoDbCommand(PrepareTypeCoercions(commandTree)); List <DbParameter> parameters; CommandType commandType; command.CommandText = SqlGenerator.GenerateSql(commandTree, out parameters, out commandType); 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 is DbFunctionCommandTree) { function = ((DbFunctionCommandTree)commandTree).EdmFunction; } foreach (KeyValuePair <string, TypeUsage> queryParameter in commandTree.Parameters) { NuoDbParameter 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(functionParameter.Name, functionParameter.TypeUsage, functionParameter.Mode, DBNull.Value); } else { parameter = CreateSqlParameter(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(CreateCommandDefinition(command)); }