/// <summary> /// Initializes a new instance of the <see cref="Transaction"/> class. /// </summary> /// /// <param name="session">The parent session that represents the communication channel to QLDB.</param> /// <param name="txnId">Transaction identifier.</param> /// <param name="logger">Logger to be used by this.</param> internal Transaction(Session session, string txnId, ILogger logger) { this.session = session; this.txnId = txnId; this.logger = logger; this.qldbHash = QldbHash.ToQldbHash(txnId); }
/// <summary> /// Calculates the QLDB hash. /// </summary> /// /// <param name="that">Hashed PartiQL statement.</param> /// /// <returns>QldbHash.</returns> internal QldbHash Dot(QldbHash that) { byte[] concatenated = JoinHashesPairwise(this.Hash, that.Hash); HashAlgorithm hashAlgorithm = HashAlgorithm.Create("SHA256"); return(new QldbHash(hashAlgorithm.ComputeHash(concatenated))); }
/// <summary> /// Calculate the QLDB hash from statement and parameters. /// </summary> /// /// <param name="seed">QLDB Hash.</param> /// <param name="statement">PartiQL statement.</param> /// <param name="parameters">Parameters to execute.</param> /// /// <returns>QLDB hash.</returns> internal static QldbHash Dot(QldbHash seed, string statement, List <IIonValue> parameters) { QldbHash statementHash = QldbHash.ToQldbHash(statement); foreach (var ionValue in parameters) { statementHash = statementHash.Dot(QldbHash.ToQldbHash(ionValue)); } return(seed.Dot(statementHash)); }
/// <summary> /// Execute the statement against QLDB and retrieve the result. /// </summary> /// /// <param name="statement">The PartiQL statement to be executed against QLDB.</param> /// <param name="parameters">Parameters to execute.</param> /// /// <returns>Result from executed statement.</returns> /// /// <exception cref="AmazonServiceException">Thrown when there is an error executing against QLDB.</exception> /// <exception cref="QldbDriverException">Thrown when this transaction has been disposed.</exception> public IResult Execute(string statement, List <IIonValue> parameters) { ValidationUtils.AssertStringNotEmpty(statement, "statement"); if (parameters == null) { parameters = new List <IIonValue>(); } this.qldbHash = Dot(this.qldbHash, statement, parameters); ExecuteStatementResult executeStatementResult = this.session.ExecuteStatement( this.txnId, statement, parameters); return(new Result(this.session, this.txnId, executeStatementResult)); }
/// <summary> /// Execute the statement against QLDB and retrieve the result. /// </summary> /// /// <param name="statement">The PartiQL statement to be executed against QLDB.</param> /// <param name="parameters">Parameters to execute.</param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// /// <returns>Result from executed statement.</returns> /// /// <exception cref="AmazonServiceException">Thrown when there is an error executing against QLDB.</exception> /// <exception cref="QldbDriverException">Thrown when this transaction has been disposed.</exception> public async Task <IResult> Execute(string statement, List <IIonValue> parameters, CancellationToken cancellationToken = default) { ValidationUtils.AssertStringNotEmpty(statement, "statement"); if (parameters == null) { parameters = new List <IIonValue>(); } this.qldbHash = Dot(this.qldbHash, statement, parameters); ExecuteStatementResult executeStatementResult = await this.session.ExecuteStatement( this.txnId, statement, parameters, cancellationToken); return(new Result(this.session, this.txnId, executeStatementResult.FirstPage)); }