/// <summary> /// Cache a prepared statement created from query for later use on the database. /// </summary> protected void AddPreparedStatement(PreparedStatementId id, string query, params MySqlDbType[] types) { Debug.Assert(types.Length == query.Count(c => c == '?')); Debug.Assert(query.Length != 0); try { using (var connection = new MySqlConnection(connectionString)) { using (var command = new MySqlCommand(query, connection)) { foreach (MySqlDbType type in types) { command.Parameters.Add("", type); } connection.Open(); command.Prepare(); preparedStatements.Add(id, new PreparedStatement(id, query, types)); } } } catch (Exception exception) { log.Fatal(exception, $"An exception occured while preparing statement {id}!"); throw; } }
/// <summary> /// Asynchronous version of SelectPreparedStatement. /// </summary> protected async Task <MySqlResult> SelectPreparedStatementAsync(PreparedStatementId id, params object[] parameters) { if (!preparedStatements.TryGetValue(id, out PreparedStatement preparedStatement)) { throw new InvalidPreparedStatementException(id); } Debug.Assert(parameters.Length == preparedStatement.Types.Count); try { using (var connection = new MySqlConnection(connectionString)) { using (var command = new MySqlCommand(preparedStatement.Query, connection)) { for (int i = 0; i < preparedStatement.Types.Count; i++) { command.Parameters.Add("", preparedStatement.Types[i]).Value = parameters[i]; } await connection.OpenAsync(); using (MySqlDataReader commandReader = (MySqlDataReader)await command.ExecuteReaderAsync()) return(new MySqlResult(commandReader)); } } } catch (Exception exception) { log.Error(exception, $"An exception occured while selecting prepared statement {id}!"); throw; } }
protected (int RowsEffected, long LastInsertedId) ExecutePreparedStatement(PreparedStatementId id, params object[] parameters) { if (!preparedStatements.TryGetValue(id, out PreparedStatement preparedStatement)) { throw new InvalidPreparedStatementException(id); } try { using (var connection = new MySqlConnection(connectionString)) { using (var command = new MySqlCommand(preparedStatement.Query, connection)) { for (int i = 0; i < preparedStatement.Types.Count; i++) { command.Parameters.Add("", preparedStatement.Types[i]).Value = parameters[i]; } connection.Open(); return(command.ExecuteNonQuery(), command.LastInsertedId); } } } catch (Exception exception) { log.Error(exception, $"An exception occured while executing prepared statement {id}!"); throw; } }
public InvalidPreparedStatementException(PreparedStatementId id) : base($"PreparedStatement {id} doesn't exist!") { }
public void AddPreparedStatement(PreparedStatementId id, params object[] parameters) { statements.Add(new Statement(id, parameters)); }
public Statement(PreparedStatementId id, params object[] parameters) { Id = id; Parameters = parameters; }
public PreparedStatement(PreparedStatementId id, string query, params MySqlDbType[] types) { Id = id; Query = query; Types = new List <MySqlDbType>(types); }