/// <summary> /// Given an entity command and entity transaction, passes through relevant state to store provider /// command. /// </summary> /// <param name="entityCommand">Entity command. Must not be null.</param> /// <param name="entityTransaction">Entity transaction. Must not be null.</param> /// <param name="storeProviderCommand">Store provider command that is being setup. Must not be null.</param> internal static void SetStoreProviderCommandState(EntityCommand entityCommand, EntityTransaction entityTransaction, DbCommand storeProviderCommand) { Debug.Assert(null != entityCommand); Debug.Assert(null != storeProviderCommand); storeProviderCommand.CommandTimeout = entityCommand.CommandTimeout; storeProviderCommand.Connection = ((EntityConnection)entityCommand.Connection).StoreConnection; storeProviderCommand.Transaction = (null != entityTransaction) ? entityTransaction.StoreTransaction : null; storeProviderCommand.UpdatedRowSource = entityCommand.UpdatedRowSource; }
/// <summary> /// Clears the expired nonces. /// </summary> /// <param name="transaction">The transaction to use, if any.</param> internal void ClearExpiredNonces(EntityTransaction transaction) { this.ExecuteCommand(transaction, "DatabaseEntities.ClearExpiredNonces"); }
/// <summary> /// Clears the current DbTransaction for this connection /// </summary> internal void ClearCurrentTransaction() { _currentTransaction = null; }
/// <summary> /// Begins a database transaction /// </summary> /// <param name="isolationLevel">The isolation level of the transaction</param> /// <returns>An object representing the new transaction</returns> protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) { if (CurrentTransaction != null) { throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.EntityClient_TransactionAlreadyStarted); } if (this._storeConnection == null) throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.EntityClient_ConnectionStringNeededBeforeOperation); if (this.State != ConnectionState.Open) { throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.EntityClient_ConnectionNotOpen); } DbTransaction storeTransaction = null; try { storeTransaction = this._storeConnection.BeginTransaction(isolationLevel); } catch (Exception e) { if (EntityUtil.IsCatchableExceptionType(e)) { throw EntityUtil.ProviderExceptionWithMessage( System.Data.Entity.Strings.EntityClient_ErrorInBeginningTransaction, e ); } throw; } // The provider is problematic if it succeeded in beginning a transaction but returned a null // for the transaction object if (storeTransaction == null) { throw EntityUtil.ProviderIncompatible(System.Data.Entity.Strings.EntityClient_ReturnedNullOnProviderMethod("BeginTransaction", _storeConnection.GetType().Name)); } _currentTransaction = new EntityTransaction(this, storeTransaction); return _currentTransaction; }
/// <summary> /// Constructs the EntityCommand object with the given eSQL statement and the connection object to use /// </summary> /// <param name="statement">The eSQL command text to execute</param> /// <param name="connection">The connection object</param> /// <param name="transaction">The transaction object this command executes in</param> public EntityCommand(string statement, EntityConnection connection, EntityTransaction transaction) : this(statement, connection) { // Assign other member fields from the parameters this._transaction = transaction; }
internal EntityCommand( string statement, EntityConnection connection, EntityTransaction transaction, EntityDataReaderFactory factory) : this(statement, connection, factory) { _transaction = transaction; }
/// <summary> /// Constructs the EntityCommand object with the given eSQL statement and the connection object to use /// </summary> /// <param name="statement">The eSQL command text to execute</param> /// <param name="connection">The connection object</param> /// <param name="transaction">The transaction object this command executes in</param> public EntityCommand(string statement, EntityConnection connection, EntityTransaction transaction) : this(statement, connection, transaction, new EntityDataReaderFactory()) { }
/// <summary> /// Executes a SQL command against the SQL connection. /// </summary> /// <param name="objectContext">The object context.</param> /// <param name="transaction">The transaction to use, if any.</param> /// <param name="command">The command to execute.</param> /// <returns>The result of executing the command.</returns> public static int ExecuteCommand(this ObjectContext objectContext, EntityTransaction transaction, string command) { if (objectContext == null) { throw new ArgumentNullException("objectContext"); } if (string.IsNullOrEmpty(command)) { throw new ArgumentNullException("command"); } DbConnection connection = (EntityConnection)objectContext.Connection; bool opening = connection.State == ConnectionState.Closed; if (opening) { connection.Open(); } DbCommand cmd = connection.CreateCommand(); cmd.Transaction = transaction; cmd.CommandText = command; cmd.CommandType = CommandType.StoredProcedure; try { return cmd.ExecuteNonQuery(); } finally { if (opening && connection.State == ConnectionState.Open) { connection.Close(); } } }
/// <summary> /// Execute the store commands, and return IteratorSources for each one /// </summary> /// <param name="entityCommand"></param> /// <param name="behavior"></param> internal DbDataReader ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) { // SQLPT #120007433 is the work item to implement MARS support, which we // need to do here, but since the PlanCompiler doesn't // have it yet, neither do we... if (1 != _mappedCommandDefinitions.Count) { throw EntityUtil.NotSupported("MARS"); } EntityTransaction entityTransaction = CommandHelper.GetEntityTransaction(entityCommand); DbCommandDefinition definition = _mappedCommandDefinitions[0]; DbCommand storeProviderCommand = definition.CreateCommand(); CommandHelper.SetStoreProviderCommandState(entityCommand, entityTransaction, storeProviderCommand); // Copy over the values from the map command to the store command; we // assume that they were not renamed by either the plan compiler or SQL // Generation. // // Note that this pretty much presumes that named parameters are supported // by the store provider, but it might work if we don't reorder/reuse // parameters. // // Note also that the store provider may choose to add parameters to thier // command object for some things; we'll only copy over the values for // parameters that we find in the EntityCommands parameters collection, so // we won't damage anything the store provider did. bool hasOutputParameters = false; if (storeProviderCommand.Parameters != null) // SQLBUDT 519066 { DbProviderServices storeProviderServices = DbProviderServices.GetProviderServices(entityCommand.Connection.StoreProviderFactory); foreach (DbParameter storeParameter in storeProviderCommand.Parameters) { // I could just use the string indexer, but then if I didn't find it the // consumer would get some ParameterNotFound exeception message and that // wouldn't be very meaningful. Instead, I use the IndexOf method and // if I don't find it, it's not a big deal (The store provider must // have added it). int parameterOrdinal = entityCommand.Parameters.IndexOf(storeParameter.ParameterName); if (-1 != parameterOrdinal) { EntityParameter entityParameter = entityCommand.Parameters[parameterOrdinal]; SyncParameterProperties(entityParameter, storeParameter, storeProviderServices); if (storeParameter.Direction != ParameterDirection.Input) { hasOutputParameters = true; } } } } // If the EntityCommand has output parameters, we must synchronize parameter values when // the reader is closed. Tell the EntityCommand about the store command so that it knows // where to pull those values from. if (hasOutputParameters) { entityCommand.SetStoreProviderCommand(storeProviderCommand); } DbDataReader reader = null; try { reader = storeProviderCommand.ExecuteReader(behavior & ~CommandBehavior.SequentialAccess); } catch (Exception e) { // we should not be wrapping all exceptions if (EntityUtil.IsCatchableExceptionType(e)) { // we don't wan't folks to have to know all the various types of exceptions that can // occur, so we just rethrow a CommandDefinitionException and make whatever we caught // the inner exception of it. throw EntityUtil.CommandExecution(System.Data.Entity.Strings.EntityClient_CommandDefinitionExecutionFailed, e); } throw; } return(reader); }