/// <include file='docs/MySqlConnection.xml' path='docs/BeginTransaction1/*'/> public new MySqlTransaction BeginTransaction(IsolationLevel iso) { //TODO: check note in help if (State != ConnectionState.Open) { Throw(new InvalidOperationException(Resources.ConnectionNotOpen)); } // First check to see if we are in a current transaction if (driver.HasStatus(ServerStatusFlags.InTransaction)) { Throw(new InvalidOperationException(Resources.NoNestedTransactions)); } MySqlTransaction t = new MySqlTransaction(this, iso); MySqlCommand cmd = new MySqlCommand("", this); cmd.CommandText = "SET SESSION TRANSACTION ISOLATION LEVEL "; switch (iso) { case IsolationLevel.ReadCommitted: cmd.CommandText += "READ COMMITTED"; break; case IsolationLevel.ReadUncommitted: cmd.CommandText += "READ UNCOMMITTED"; break; case IsolationLevel.RepeatableRead: cmd.CommandText += "REPEATABLE READ"; break; case IsolationLevel.Serializable: cmd.CommandText += "SERIALIZABLE"; break; case IsolationLevel.Chaos: Throw(new NotSupportedException(Resources.ChaosNotSupported)); break; case IsolationLevel.Snapshot: Throw(new NotSupportedException(Resources.SnapshotNotSupported)); break; } cmd.ExecuteNonQuery(); cmd.CommandText = "BEGIN"; cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); return(t); }
public ResultSet(Driver d, int statementId, int numCols) { affectedRows = -1; insertedId = -1; driver = d; this.statementId = statementId; rowIndex = -1; LoadColumns(numCols); isOutputParameters = driver.HasStatus(ServerStatusFlags.OutputParameters); hasRows = GetNextRow(); readDone = !hasRows; }
private bool IsOutputParameterResultSet() { if (driver.HasStatus(ServerStatusFlags.OutputParameters)) { return(true); } if (fields.Length == 0) { return(false); } for (int x = 0; x < fields.Length; x++) { if (!fields[x].ColumnName.StartsWith("@" + StoredProcedure.ParameterPrefix)) { return(false); } } return(true); }
private bool IsOutputParameterResultSet() { if (_driver.HasStatus(ServerStatusFlags.OutputParameters)) { return(true); } if (Fields.Length == 0) { return(false); } for (int x = 0; x < Fields.Length; x++) { if (!Fields[x].ColumnName.StartsWith("@" + StoredProcedure.ParameterPrefix, StringComparison.OrdinalIgnoreCase)) { return(false); } } return(true); }
/// <include file='docs/mysqlcommand.xml' path='docs/ExecuteReader1/*'/> public new MySqlDataReader ExecuteReader(CommandBehavior behavior) { // give our interceptors a shot at it first MySqlDataReader interceptedReader = null; if (connection?.commandInterceptor != null && connection.commandInterceptor.ExecuteReader(CommandText, behavior, ref interceptedReader)) { return(interceptedReader); } // interceptors didn't handle this so we fall through bool success = false; CheckState(); Driver driver = connection.driver; cmdText = cmdText.Trim(); if (String.IsNullOrEmpty(cmdText)) { Throw(new InvalidOperationException(Resources.CommandTextNotInitialized)); } string sql = cmdText.Trim(';'); #if !NETSTANDARD1_3 // Load balancing getting a new connection if (connection.hasBeenOpen && !driver.HasStatus(ServerStatusFlags.InTransaction)) { ReplicationManager.GetNewConnection(connection.Settings.Server, !IsReadOnlyCommand(sql), connection); } #endif lock (driver) { // We have to recheck that there is no reader, after we got the lock if (connection.Reader != null) { Throw(new MySqlException(Resources.DataReaderOpen)); } #if !NETSTANDARD1_3 System.Transactions.Transaction curTrans = System.Transactions.Transaction.Current; if (curTrans != null) { bool inRollback = false; //TODO: ADD support for 452 and 46X if (driver.currentTransaction != null) { inRollback = driver.currentTransaction.InRollback; } if (!inRollback) { System.Transactions.TransactionStatus status = System.Transactions.TransactionStatus.InDoubt; try { // in some cases (during state transitions) this throws // an exception. Ignore exceptions, we're only interested // whether transaction was aborted or not. status = curTrans.TransactionInformation.Status; } catch (System.Transactions.TransactionException) { } if (status == System.Transactions.TransactionStatus.Aborted) { Throw(new System.Transactions.TransactionAbortedException()); } } } #endif commandTimer = new CommandTimer(connection, CommandTimeout); LastInsertedId = -1; if (CommandType == CommandType.TableDirect) { sql = "SELECT * FROM " + sql; } else if (CommandType == CommandType.Text) { // validates single word statetment (maybe is a stored procedure call) if (sql.IndexOf(" ") == -1) { if (AddCallStatement(sql)) { sql = "call " + sql; } } } // if we are on a replicated connection, we are only allow readonly statements if (connection.Settings.Replication && !InternallyCreated) { EnsureCommandIsReadOnly(sql); } if (statement == null || !statement.IsPrepared) { if (CommandType == CommandType.StoredProcedure) { statement = new StoredProcedure(this, sql); } else { statement = new PreparableStatement(this, sql); } } // stored procs are the only statement type that need do anything during resolve statement.Resolve(false); // Now that we have completed our resolve step, we can handle our // command behaviors HandleCommandBehaviors(behavior); try { MySqlDataReader reader = new MySqlDataReader(this, statement, behavior); connection.Reader = reader; Canceled = false; // execute the statement statement.Execute(); // wait for data to return reader.NextResult(); success = true; return(reader); } catch (TimeoutException tex) { connection.HandleTimeoutOrThreadAbort(tex); throw; //unreached } #if !NETSTANDARD1_3 catch (ThreadAbortException taex) { connection.HandleTimeoutOrThreadAbort(taex); throw; } #endif catch (IOException ioex) { connection.Abort(); // Closes connection without returning it to the pool throw new MySqlException(Resources.FatalErrorDuringExecute, ioex); } catch (MySqlException ex) { if (ex.InnerException is TimeoutException) { throw; // already handled } try { ResetReader(); ResetSqlSelectLimit(); } catch (Exception) { // Reset SqlLimit did not work, connection is hosed. Connection.Abort(); throw new MySqlException(ex.Message, true, ex); } // if we caught an exception because of a cancel, then just return null if (ex.IsQueryAborted) { return(null); } if (ex.IsFatal) { Connection.Close(); } if (ex.Number == 0) { throw new MySqlException(Resources.FatalErrorDuringExecute, ex); } throw; } finally { if (connection != null) { if (connection.Reader == null) { // Something went seriously wrong, and reader would not // be able to clear timeout on closing. // So we clear timeout here. ClearCommandTimer(); } if (!success) { // ExecuteReader failed.Close Reader and set to null to // prevent subsequent errors with DataReaderOpen ResetReader(); } } } } }
public new MySqlDataReader ExecuteReader(CommandBehavior behavior) { MySqlDataReader result = null; if (this.connection != null && this.connection.commandInterceptor != null && this.connection.commandInterceptor.ExecuteReader(this.CommandText, behavior, ref result)) { return(result); } bool flag = false; this.CheckState(); Driver driver = this.connection.driver; this.cmdText = this.cmdText.Trim(); if (string.IsNullOrEmpty(this.cmdText)) { this.Throw(new InvalidOperationException(Resources.CommandTextNotInitialized)); } string text = this.cmdText.Trim(new char[] { ';' }); if (this.connection.hasBeenOpen && !driver.HasStatus(ServerStatusFlags.InTransaction)) { ReplicationManager.GetNewConnection(this.connection.Settings.Server, !this.IsReadOnlyCommand(text), this.connection); } Driver obj = driver; MySqlDataReader result2; lock (obj) { if (this.connection.Reader != null) { this.Throw(new MySqlException(Resources.DataReaderOpen)); } Transaction current = System.Transactions.Transaction.Current; if (current != null) { bool flag3 = false; if (driver.CurrentTransaction != null) { flag3 = driver.CurrentTransaction.InRollback; } if (!flag3) { TransactionStatus transactionStatus = TransactionStatus.InDoubt; try { transactionStatus = current.TransactionInformation.Status; } catch (TransactionException) { } if (transactionStatus == TransactionStatus.Aborted) { this.Throw(new TransactionAbortedException()); } } } this.commandTimer = new CommandTimer(this.connection, this.CommandTimeout); this.lastInsertedId = -1L; if (this.CommandType == CommandType.TableDirect) { text = "SELECT * FROM " + text; } else if (this.CommandType == CommandType.Text && text.IndexOf(" ") == -1 && this.AddCallStatement(text)) { text = "call " + text; } if (this.connection.Settings.Replication && !this.InternallyCreated) { this.EnsureCommandIsReadOnly(text); } if (this.statement == null || !this.statement.IsPrepared) { if (this.CommandType == CommandType.StoredProcedure) { this.statement = new StoredProcedure(this, text); } else { this.statement = new PreparableStatement(this, text); } } this.statement.Resolve(false); this.HandleCommandBehaviors(behavior); this.updatedRowCount = -1L; try { MySqlDataReader mySqlDataReader = new MySqlDataReader(this, this.statement, behavior); this.connection.Reader = mySqlDataReader; this.canceled = false; this.statement.Execute(); mySqlDataReader.NextResult(); flag = true; result2 = mySqlDataReader; } catch (TimeoutException ex) { this.connection.HandleTimeoutOrThreadAbort(ex); throw; } catch (ThreadAbortException ex2) { this.connection.HandleTimeoutOrThreadAbort(ex2); throw; } catch (IOException ex3) { this.connection.Abort(); throw new MySqlException(Resources.FatalErrorDuringExecute, ex3); } catch (MySqlException ex4) { if (ex4.InnerException is TimeoutException) { throw; } try { this.ResetReader(); this.ResetSqlSelectLimit(); } catch (Exception) { this.Connection.Abort(); throw new MySqlException(ex4.Message, true, ex4); } if (ex4.IsQueryAborted) { result2 = null; } else { if (ex4.IsFatal) { this.Connection.Close(); } if (ex4.Number == 0) { throw new MySqlException(Resources.FatalErrorDuringExecute, ex4); } throw; } } finally { if (this.connection != null) { if (this.connection.Reader == null) { this.ClearCommandTimer(); } if (!flag) { this.ResetReader(); } } } } return(result2); }