public async Task<List<string>> Get(string appId, string itemId, string relation, string userId) { using (var conn = new CqlConnection(_connString)) { await conn.OpenAsync().ConfigureAwait(false); var query = string.Format( System.Globalization.CultureInfo.InvariantCulture, "select values from negrapi.relations where app_id = '{0}' and item_id='{1}' and relation='{2}';", appId, GetKey(itemId, userId), relation); var cmd = new CqlCommand( conn, query, CqlConsistency.One); var result = await cmd.ExecuteScalarAsync().ConfigureAwait(false) as string; if (string.IsNullOrEmpty(result)) { return null; } var list = JsonConvert.DeserializeObject<List<RecommendationsDataValue>>(result); return list .Select(i => i.ItemId) .ToList(); } }
public async Task SelectScalarNoValue() { //Assume const string retrieveCql = @"select value from Test.BasicFlow where id=10001;"; //Act using(var connection = new CqlConnection(ConnectionString)) { await connection.OpenAsync(); //select data var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One); string value = (string)await selectCmd.ExecuteScalarAsync(); //if row does not exist value should be null Assert.IsNull(value); } }
public async Task SelectScalarNullValue() { //Assume const string insertCql = @"insert into Test.BasicFlow (id) values (2001);"; const string retrieveCql = @"select value from Test.BasicFlow where id=2001;"; //Act using(var connection = new CqlConnection(ConnectionString)) { await connection.OpenAsync(); //insert data var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); await cmd.ExecuteNonQueryAsync(); //select data var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One); object value = await selectCmd.ExecuteScalarAsync(); Assert.AreEqual(DBNull.Value, value); } }
public async Task BasicInsertSelectScalar() { //Assume const string insertCql = @"insert into Test.BasicFlow (id,value) values (1001,'Hallo 1001');"; const string retrieveCql = @"select value from Test.BasicFlow where id=1001;"; //Act using(var connection = new CqlConnection(ConnectionString)) { await connection.OpenAsync(); //insert data var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); await cmd.ExecuteNonQueryAsync(); //select data var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One); string value = (string)await selectCmd.ExecuteScalarAsync(); Assert.AreEqual("Hallo 1001", value); } }