/// <summary> /// Gets the trace session async. /// </summary> /// <returns> TracingSession if any, null otherwise </returns> public async Task<TracingSession> GetTraceSessionAsync() { TracingSession session = null; var sessionCmd = new CqlCommand(_connection, "select * from system_traces.sessions where session_id=" + _tracingId.ToString() + ";", CqlConsistency.One); using (CqlDataReader<TracingSession> reader = await sessionCmd.ExecuteReaderAsync<TracingSession>().ConfigureAwait(false)) { if (await reader.ReadAsync().ConfigureAwait(false)) { session = reader.Current; } else return null; } var eventsCmd = new CqlCommand(_connection, "select * from system_traces.events where session_id=" + _tracingId.ToString() + ";", CqlConsistency.One); using (CqlDataReader<TracingEvent> reader = await eventsCmd.ExecuteReaderAsync<TracingEvent>().ConfigureAwait(false)) { var events = new List<TracingEvent>(reader.Count); while (await reader.ReadAsync().ConfigureAwait(false)) { events.Add(reader.Current); } session.Events = events; } return session; }
/// <summary> /// Gets the trace session async. /// </summary> /// <returns> TracingSession if any, null otherwise </returns> public async Task<TracingSession> GetTraceSessionAsync(CancellationToken token) { TracingSession session; var sessionCmd = new CqlCommand(_connection, "select * from system_traces.sessions where session_id=" + _tracingId + ";", CqlConsistency.One); using( CqlDataReader<TracingSession> reader = await sessionCmd.ExecuteReaderAsync<TracingSession>(token).AutoConfigureAwait()) { if(await reader.ReadAsync(token).AutoConfigureAwait()) session = reader.Current; else return null; } var eventsCmd = new CqlCommand(_connection, "select * from system_traces.events where session_id=" + _tracingId + ";", CqlConsistency.One); using( CqlDataReader<TracingEvent> reader = await eventsCmd.ExecuteReaderAsync<TracingEvent>(token).AutoConfigureAwait()) { var events = new List<TracingEvent>(reader.Count); while(await reader.ReadAsync().AutoConfigureAwait()) events.Add(reader.Current); session.Events = events; } return session; }
public async Task BasicFlow() { //Assume const string insertCql = @"insert into Test.BasicFlow (id,value) values (?,?);"; const string retrieveCql = @"select * from Test.BasicFlow;"; const int insertCount = 1000; //Act using (var connection = new CqlConnection(ConnectionString)) { await connection.OpenAsync(); var executions = new Task <ICqlQueryResult> [insertCount]; var options = new ParallelOptions() { MaxDegreeOfParallelism = 1 }; Parallel.For(0, insertCount, options, (i) => { var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); cmd.Prepare(); var b = new BasicFlowData { Id = i, Data = "Hallo " + i }; cmd.PartitionKey.Set(b); cmd.Parameters.Set(b); executions[i] = cmd.ExecuteNonQueryAsync(); }); await Task.WhenAll(executions); var presence = new bool[insertCount]; var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One) { EnableTracing = true }; CqlDataReader <BasicFlowData> reader = await selectCmd.ExecuteReaderAsync <BasicFlowData>(); while (await reader.ReadAsync()) { BasicFlowData row = reader.Current; Assert.AreEqual(row.Data, "Hallo " + row.Id); presence[row.Id] = true; } Assert.IsTrue(presence.All(p => p)); var tracer = new QueryTraceCommand(connection, reader.TracingId.Value); TracingSession session = await tracer.GetTraceSessionAsync(); Assert.IsNotNull(session); } }
/// <summary> /// Queries the cart async. /// </summary> /// <param name="guid"> The GUID. </param> /// <returns> </returns> public async Task <Dictionary <string, int> > GetItemsAsync(Guid guid) { using (var connection = new CqlConnection("cartDB")) { await connection.OpenAsync(); var command = new CqlCommand(connection, "select Items from Shop.Carts where id=?;"); await command.PrepareAsync(); command.UseBuffering = true; command.Parameters["id"] = guid; using (var reader = await command.ExecuteReaderAsync()) { return(await reader.ReadAsync() ? (Dictionary <string, int>)reader["items"] : null); } } }
/// <summary> /// Finds the carts by group id async. /// </summary> /// <param name="groupId"> The group id. </param> /// <returns> </returns> public async Task <List <Cart> > FindCartsByGroupIdAsync(string groupId) { using (var connection = new CqlConnection("cartDB")) { await connection.OpenAsync(); var command = new CqlCommand(connection, "select * from Shop.Carts where GroupId=?;"); await command.PrepareAsync(); command.UseBuffering = true; command.Parameters["groupid"] = groupId; using (var reader = await command.ExecuteReaderAsync <Cart>()) { var carts = new List <Cart>(); while (await reader.ReadAsync()) { carts.Add(reader.Current); } return(carts); } } }
/// <summary> /// Gets the trace session async. /// </summary> /// <returns> TracingSession if any, null otherwise </returns> public async Task <TracingSession> GetTraceSessionAsync() { TracingSession session = null; var sessionCmd = new CqlCommand(_connection, "select * from system_traces.sessions where session_id=" + _tracingId.ToString() + ";", CqlConsistency.One); using (CqlDataReader <TracingSession> reader = await sessionCmd.ExecuteReaderAsync <TracingSession>().ConfigureAwait(false)) { if (await reader.ReadAsync().ConfigureAwait(false)) { session = reader.Current; } else { return(null); } } var eventsCmd = new CqlCommand(_connection, "select * from system_traces.events where session_id=" + _tracingId.ToString() + ";", CqlConsistency.One); using (CqlDataReader <TracingEvent> reader = await eventsCmd.ExecuteReaderAsync <TracingEvent>().ConfigureAwait(false)) { var events = new List <TracingEvent>(reader.Count); while (await reader.ReadAsync().ConfigureAwait(false)) { events.Add(reader.Current); } session.Events = events; } return(session); }
public async Task BasicPrepareInsertSelect() { //Assume const string insertCql = @"insert into Test.BasicFlow (id,value) values (?,?);"; const string retrieveCql = @"select * from Test.BasicFlow;"; //Act using(var connection = new CqlConnection(ConnectionString)) { await connection.OpenAsync(); //insert data var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); await cmd.PrepareAsync(); var b = new BasicFlowData {Id = 123, Data = "Hallo", Ignored = "none"}; cmd.PartitionKey.Set(b); cmd.Parameters.Set(b); await cmd.ExecuteNonQueryAsync(); //select data var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One) {EnableTracing = true}; await selectCmd.PrepareAsync(); CqlDataReader<BasicFlowData> reader = await selectCmd.ExecuteReaderAsync<BasicFlowData>(); Assert.AreEqual(1, reader.Count); if(await reader.ReadAsync()) { BasicFlowData row = reader.Current; Assert.AreEqual(123, row.Id); Assert.AreEqual("Hallo", row.Data); Assert.IsNull(row.Ignored); } else Assert.Fail("Read should have succeeded"); } }
public async Task QueryWithBoundParameters() { //Assume const string insertCql = @"insert into Test.BasicFlow (id,value) values (?,?);"; const string retrieveCql = @"select * from Test.BasicFlow;"; //Act using(var connection = new CqlConnection(ConnectionString)) { await connection.OpenAsync(); //define command var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); //add parameters, infer Cql types cmd.Parameters.Add("id", 123); cmd.Parameters.Add("value", "Hallo"); try { //execute await cmd.ExecuteNonQueryAsync(); } catch(InvalidException) { Assert.IsNotNull(connection.ServerVersion); if(String.Compare(connection.ServerVersion, "2.0.0", StringComparison.Ordinal) < 0) return; throw; } //select data var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One); CqlDataReader<BasicFlowData> reader = await selectCmd.ExecuteReaderAsync<BasicFlowData>(); Assert.AreEqual(1, reader.Count); if(await reader.ReadAsync()) { BasicFlowData row = reader.Current; Assert.AreEqual(123, row.Id); Assert.AreEqual("Hallo", row.Data); Assert.IsNull(row.Ignored); } else Assert.Fail("Read should have succeeded"); } }
public async Task InsertSelectCustomParameters() { //Assume const string insertCql = @"insert into Test.BasicFlow (id,value) values (?,?);"; const string retrieveCql = @"select * from Test.BasicFlow;"; //Act using(var connection = new CqlConnection(ConnectionString)) { await connection.OpenAsync(); //insert data var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); cmd.Parameters.Add("myKey", CqlType.Int); cmd.Parameters.Add("myValue", CqlType.Text); await cmd.PrepareAsync(); cmd.Parameters["myKey"].Value = 1234; cmd.Parameters["myValue"].Value = "Hallo 1234"; await cmd.ExecuteNonQueryAsync(); //select data var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One); CqlDataReader reader = await selectCmd.ExecuteReaderAsync(); Assert.AreEqual(1, reader.Count); if(await reader.ReadAsync()) { Assert.AreEqual(1234, reader["id"]); Assert.AreEqual("Hallo 1234", reader["value"]); Assert.AreEqual(DBNull.Value, reader["ignored"]); } else Assert.Fail("Read should have succeeded"); } }
public async Task TransactionRolledBackDoesNotInsertData() { const string insertCql = @"insert into Test.BasicFlow (id,value) values (9876, 'Transaction 9876');"; const string retrieveCql = @"select id from Test.BasicFlow where id=9876;"; //Act using(var connection = new CqlConnection(ConnectionString)) { await connection.OpenAsync(); //create transaction using(var transaction = connection.BeginTransaction()) { //insert data var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); cmd.Transaction = transaction; await cmd.ExecuteNonQueryAsync(); transaction.Rollback(); } //check if data exists var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One); using(CqlDataReader reader = await selectCmd.ExecuteReaderAsync()) { //check if any rows are returned Assert.IsFalse(reader.HasRows); } } }
public async Task BasicFlow() { //Assume const string insertCql = @"insert into Test.BasicFlow (id,value) values (?,?);"; const string retrieveCql = @"select * from Test.BasicFlow;"; const int insertCount = 1000; //Act using (var connection = new CqlConnection(ConnectionString)) { await connection.OpenAsync(); var executions = new Task<ICqlQueryResult>[insertCount]; var options = new ParallelOptions() { MaxDegreeOfParallelism = 1 }; Parallel.For(0, insertCount, options, (i) => { var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); cmd.Prepare(); var b = new BasicFlowData { Id = i, Data = "Hallo " + i }; cmd.PartitionKey.Set(b); cmd.Parameters.Set(b); executions[i] = cmd.ExecuteNonQueryAsync(); }); await Task.WhenAll(executions); var presence = new bool[insertCount]; var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One) { EnableTracing = true }; CqlDataReader<BasicFlowData> reader = await selectCmd.ExecuteReaderAsync<BasicFlowData>(); while (await reader.ReadAsync()) { BasicFlowData row = reader.Current; Assert.AreEqual(row.Data, "Hallo " + row.Id); presence[row.Id] = true; } Assert.IsTrue(presence.All(p => p)); var tracer = new QueryTraceCommand(connection, reader.TracingId.Value); TracingSession session = await tracer.GetTraceSessionAsync(); Assert.IsNotNull(session); } }
public async Task BatchPreparedWithNamedParameters() { //Assume const string insertCql = @"begin batch insert into Test.BasicFlow (id,value) values (:id1,:value1); insert into Test.BasicFlow (id,value) values (:id2,:value2); apply batch;"; const string retrieveCql = @"select * from Test.BasicFlow;"; //Act using(var connection = new CqlConnection(ConnectionString)) { await connection.OpenAsync(); //skip if cqlversion too low if(string.CompareOrdinal(connection.CqlVersion, "3.1.1") < 0) return; //insert data var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); await cmd.PrepareAsync(); cmd.Parameters["id1"].Value = 0; cmd.Parameters["value1"].Value = "Hello 0"; cmd.Parameters["id2"].Value = 1; cmd.Parameters["value2"].Value = "Hello 1"; await cmd.ExecuteNonQueryAsync(); //select data var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One); CqlDataReader reader = await selectCmd.ExecuteReaderAsync(); Assert.AreEqual(2, reader.Count); var results = new bool[2]; for(int i = 0; i < 2; i++) { if(await reader.ReadAsync()) { results[(int)reader["id"]] = true; Assert.AreEqual("Hello " + reader["id"], reader["value"]); } else Assert.Fail("Read should have succeeded"); } Assert.IsTrue(results.All(p => p)); } }
/// <summary> /// Queries the cart async. /// </summary> /// <param name="guid"> The GUID. </param> /// <returns> </returns> public async Task<Dictionary<string, int>> GetItemsAsync(Guid guid) { using (var connection = new CqlConnection("cartDB")) { await connection.OpenAsync(); var command = new CqlCommand(connection, "select Items from Shop.Carts where id=?;"); await command.PrepareAsync(); command.UseBuffering = true; command.Parameters["id"] = guid; using (var reader = await command.ExecuteReaderAsync()) { return await reader.ReadAsync() ? (Dictionary<string, int>) reader["items"] : null; } } }
/// <summary> /// Finds the carts by group id async. /// </summary> /// <param name="groupId"> The group id. </param> /// <returns> </returns> public async Task<List<Cart>> FindCartsByGroupIdAsync(string groupId) { using (var connection = new CqlConnection("cartDB")) { await connection.OpenAsync(); var command = new CqlCommand(connection, "select * from Shop.Carts where GroupId=?;"); await command.PrepareAsync(); command.UseBuffering = true; command.Parameters["groupid"] = groupId; using (var reader = await command.ExecuteReaderAsync<Cart>()) { var carts = new List<Cart>(); while (await reader.ReadAsync()) { carts.Add(reader.Current); } return carts; } } }
private async Task BatchInsertInternal(CqlBatchType batchType) { //Assume const string insertCql = @"insert into Test.BasicFlow (id,value) values (?,?);"; const string retrieveCql = @"select id,value,ignored from Test.BasicFlow;"; //Act using(var connection = new CqlConnection(ConnectionString)) { await connection.OpenAsync(); var transaction = connection.BeginTransaction(); transaction.BatchType = batchType; //insert data var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); cmd.Transaction = transaction; await cmd.PrepareAsync(); for(int i = 0; i < 10; i++) { cmd.Parameters[0].Value = i; cmd.Parameters[1].Value = "Hello " + i; await cmd.ExecuteNonQueryAsync(); } var cmd2 = new CqlCommand(connection, insertCql, CqlConsistency.One); cmd2.Transaction = transaction; cmd2.Parameters.Add("id", CqlType.Int); cmd2.Parameters.Add("value", CqlType.Text); for(int i = 10; i < 20; i++) { cmd2.Parameters[0].Value = i; cmd2.Parameters[1].Value = "Hello " + i; await cmd2.ExecuteNonQueryAsync(); } try { await transaction.CommitAsync(); } catch(ProtocolException pex) { //skip when server version is below 2.0.0 if(pex.Code == ErrorCode.Protocol && String.Compare(connection.ServerVersion, "2.0.0", StringComparison.Ordinal) < 0) return; throw; } //select data var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One); CqlDataReader reader = await selectCmd.ExecuteReaderAsync(); Assert.AreEqual(20, reader.Count); var results = new bool[20]; for(int i = 0; i < 20; i++) { if(await reader.ReadAsync()) { results[(int)reader["id"]] = true; Assert.AreEqual("Hello " + reader["id"], reader["value"]); } else Assert.Fail("Read should have succeeded"); } Assert.IsTrue(results.All(p => p)); Assert.IsNotNull(transaction.LastBatchResult); } }
/// <summary> /// Gets the measurement asynchronous. /// </summary> /// <param name="id">The identifier.</param> /// <returns></returns> public static async Task<Measurement> GetMeasurementAsync(int id) { using(var connection = new CqlConnection(ConnectionString)) { await connection.OpenAsync(); var selectCommand = new CqlCommand(connection, "select * from cqlsharptest.measurements where id=?"); await selectCommand.PrepareAsync(); selectCommand.Parameters[0].Value = id; using(var reader = await selectCommand.ExecuteReaderAsync<Measurement>()) { return (await reader.ReadAsync()) ? reader.Current : null; } } }
public async Task SelectWithPagingAsync() { //Assume const string insertCql = @"insert into Test.BasicFlow (id,value) values (?,?);"; const string retrieveCql = @"select * from Test.BasicFlow;"; //Act using(var connection = new CqlConnection(ConnectionString)) { await connection.OpenAsync(); //insert data var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); await cmd.PrepareAsync(); for(int i = 0; i < 100; i++) { cmd.Parameters[0].Value = i; cmd.Parameters[1].Value = "Hello " + i; await cmd.ExecuteNonQueryAsync(); } //select data var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One); selectCmd.PageSize = 10; CqlDataReader reader = await selectCmd.ExecuteReaderAsync(); //no paging when version < 2.0.0 is used... var expectedCount = String.Compare(connection.ServerVersion, "2.0.0", StringComparison.Ordinal) < 0 ? 100 : 10; Assert.AreEqual(expectedCount, reader.Count); var results = new bool[100]; for(int i = 0; i < 100; i++) { if(await reader.ReadAsync()) { results[(int)reader["id"]] = true; Assert.AreEqual("Hello " + reader["id"], reader["value"]); } else Assert.Fail("Read should have succeeded"); } Assert.IsFalse(reader.Read()); Assert.IsTrue(results.All(p => p)); } }
public async Task CASInsertSelect() { //Assume const string insertCql = @"insert into Test.BasicFlow (id,value) values (901,'Hallo 901') if not exists;"; const string insertCql2 = @"insert into Test.BasicFlow (id,value) values (901,'Hallo 901.2') if not exists;"; const string retrieveCql = @"select * from Test.BasicFlow;"; //Act using(var connection = new CqlConnection(ConnectionString)) { await connection.OpenAsync(); //skip when server version is below 2.0.0 if(String.Compare(connection.ServerVersion, "2.0.0", StringComparison.Ordinal) < 0) return; //insert data var cmd = new CqlCommand(connection, insertCql, CqlConsistency.Any); cmd.UseCASLocalSerial = true; await cmd.ExecuteNonQueryAsync(); var result = cmd.LastQueryResult as CqlDataReader; Assert.IsNotNull(result); Assert.IsTrue(await result.ReadAsync()); Assert.IsTrue((bool)result["[applied]"]); var cmd2 = new CqlCommand(connection, insertCql2, CqlConsistency.Any); await cmd2.ExecuteNonQueryAsync(); var result2 = cmd2.LastQueryResult as CqlDataReader; Assert.IsNotNull(result2); Assert.IsTrue(await result2.ReadAsync()); Assert.IsFalse((bool)result2["[applied]"]); Assert.AreEqual("Hallo 901", result2["value"]); //select data var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One); await selectCmd.PrepareAsync(); CqlDataReader reader = await selectCmd.ExecuteReaderAsync(); Assert.AreEqual(1, reader.Count); if(await reader.ReadAsync()) { Assert.AreEqual(901, reader["id"]); Assert.AreEqual("Hallo 901", reader["value"]); Assert.AreEqual(DBNull.Value, reader["ignored"]); } else Assert.Fail("Read should have succeeded"); } }
public async Task ChangeDatabaseThenInsertSelect() { //Assume const string insertCql = @"insert into BasicFlow (id,value) values (901,'Hallo 901');"; const string retrieveCql = @"select * from BasicFlow;"; //Act using(var connection = new CqlConnection(ConnectionString)) { Assert.AreEqual("", connection.Database); await connection.OpenAsync(); //change database connection.ChangeDatabase("test"); Assert.AreEqual("test", connection.Database); //insert data var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); await cmd.ExecuteNonQueryAsync(); //select data var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One); CqlDataReader reader = await selectCmd.ExecuteReaderAsync(); Assert.AreEqual(1, reader.Count); if(await reader.ReadAsync()) { Assert.AreEqual(901, reader["id"]); Assert.AreEqual("Hallo 901", reader["value"]); Assert.AreEqual(DBNull.Value, reader["ignored"]); } else Assert.Fail("Read should have succeeded"); } }