public void ConnectToUnknownDb() { try { //Act using( var connection = new CqlConnection( "Servers=localhost;Database=DoesNotExist;username=cassandra;password=cassandra;loggerfactory=debug;loglevel=verbose") ) { connection.Open(); //invoke random command, as it will try to change database on the connection var command = new CqlCommand(connection, "select * from randomTable;"); var reader = command.ExecuteReader(); reader.Dispose(); } } catch(Exception ex) { Assert.IsInstanceOfType(ex, typeof(InvalidException)); return; } Assert.Fail("Exception should have been thrown;"); }
/// <summary> /// Prepares the db async. /// </summary> /// <returns> </returns> public async Task PrepareDbAsync() { using (var connection = new CqlConnection("cartDB")) { await connection.OpenAsync(); try { var shopExistCommand = new CqlCommand(connection, "select * from system.schema_keyspaces where keyspace_name='shop';"); using (var reader = shopExistCommand.ExecuteReader()) { if (reader.HasRows) { var dropKs = new CqlCommand(connection, @"DROP KEYSPACE Shop;"); await dropKs.ExecuteNonQueryAsync(); } } } catch (AlreadyExistsException) { //ignore } try { //create the keyspace if it does not exist var createKs = new CqlCommand(connection, @"CREATE KEYSPACE Shop WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1} and durable_writes = 'false';"); await createKs.ExecuteNonQueryAsync(); //create the table var createTable = new CqlCommand(connection, @"create table Shop.Carts (id uuid primary key, groupId text, items map<text, int>);"); await createTable.ExecuteNonQueryAsync(); //create corresponding index var createIndex = new CqlCommand(connection, @"create index on Shop.Carts(GroupId)"); await createIndex.ExecuteNonQueryAsync(); } catch (AlreadyExistsException) { } } }
public void NullableDeserializeTest() { const string insertCql = @"insert into Test.Types(aInt) values (4);"; const string selectCql = "select * from Test.Types limit 1;"; using (var connection = new CqlConnection(ConnectionString)) { connection.Open(); var insertCmd = new CqlCommand(connection, insertCql); insertCmd.ExecuteNonQuery(); var selectCmd = new CqlCommand(connection, selectCql); NullableTypes result = null; using (var reader = selectCmd.ExecuteReader <NullableTypes>()) { if (reader.Read()) { result = reader.Current; } } Assert.IsNotNull(result); Assert.IsNull(result.aASCIIString); Assert.IsNull(result.aVarcharString); Assert.IsNull(result.aVarint); Assert.IsNull(result.aTextString); Assert.IsNull(result.aBool); Assert.IsNull(result.aDouble); Assert.IsNull(result.aFloat); Assert.IsNull(result.aInet); Assert.IsNull(result.aLong); Assert.IsNull(result.aTimeUUID); Assert.IsNull(result.aUUID); Assert.IsNull(result.aBlob); Assert.IsNull(result.aList); Assert.IsNull(result.aSet); Assert.IsNull(result.aMap); } }
public void DefaultDeserializeTest() { const string insertCql = @"insert into Test.Types(aInt) values (1);"; const string selectCql = "select * from Test.Types limit 1;"; using (var connection = new CqlConnection(ConnectionString)) { connection.Open(); var insertCmd = new CqlCommand(connection, insertCql); insertCmd.ExecuteNonQuery(); var selectCmd = new CqlCommand(connection, selectCql); Types result = null; using (var reader = selectCmd.ExecuteReader <Types>()) { if (reader.Read()) { result = reader.Current; } } Assert.IsNotNull(result); Assert.AreEqual(result.aASCIIString, default(string)); Assert.AreEqual(result.aVarcharString, default(string)); Assert.AreEqual(result.aVarint, default(BigInteger)); Assert.AreEqual(result.aTextString, default(string)); Assert.AreEqual(result.aBool, default(bool)); Assert.AreEqual(result.aDouble, default(double)); Assert.AreEqual(result.aFloat, default(float)); Assert.AreEqual(result.aInet, default(IPAddress)); Assert.AreEqual(result.aLong, default(long)); Assert.AreEqual(result.aTimeUUID, default(Guid)); Assert.AreEqual(result.aUUID, default(Guid)); Assert.AreEqual(result.aBlob, default(byte[])); Assert.AreEqual(result.aList, default(List <string>)); Assert.AreEqual(result.aSet, default(HashSet <int>)); Assert.AreEqual(result.aMap, default(Dictionary <long, string>)); } }
public void BasicFlowAdo() { //Assume const string insertCql = @"insert into Test.BasicFlow (id,value) values (?,?);"; const string retrieveCql = @"select id,value,ignored from Test.BasicFlow;"; //Act using(IDbConnection connection = new CqlConnection(ConnectionString)) { connection.Open(); IDbCommand cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); cmd.Parameters.Add(new CqlParameter("id", CqlType.Int)); cmd.Parameters.Add(new CqlParameter("value", CqlType.Text)); cmd.Prepare(); ((IDbDataParameter)cmd.Parameters["id"]).Value = 456; ((IDbDataParameter)cmd.Parameters["value"]).Value = "Hallo 456"; cmd.ExecuteNonQuery(); IDbCommand selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One) { EnableTracing = true }; IDataReader reader = selectCmd.ExecuteReader(); DataTable schema = reader.GetSchemaTable(); Assert.AreEqual(3, schema.Rows.Count); Assert.IsTrue( schema.Rows.Cast<DataRow>().Any(row => row[CqlSchemaTableColumnNames.ColumnName].Equals("ignored"))); if(reader.Read()) { int id = reader.GetInt32(0); string value = reader.GetString(1); Assert.AreEqual(456, id); Assert.IsTrue(reader.IsDBNull(2)); Assert.AreEqual("Hallo 456", value); } else Assert.Fail("Read should have succeeded"); } }
public void SelectTypedMultiple() { //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)) { connection.Open(); //insert data var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); cmd.Prepare(); for (int i = 0; i < 100; i++) { cmd.Parameters[0].Value = i; cmd.Parameters[1].Value = "Hello " + i; cmd.ExecuteNonQuery(); } //select data var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One); using (var reader = selectCmd.ExecuteReader<BasicFlowData>()) { Assert.AreEqual(100, reader.Count); var results = new bool[100]; for (int i = 0; i < 100; i++) { if (reader.Read()) { var current = reader.Current; results[current.Id] = true; Assert.AreEqual("Hello " + current.Id, current.Data); } else Assert.Fail("Read should have succeeded"); } Assert.IsFalse(reader.Read()); Assert.IsTrue(results.All(p => p), "Not all expected entries are returned"); } } }
public void SelectWithPaging() { //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)) { connection.Open(); //insert data var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); cmd.Prepare(); for(int i = 0; i < 100; i++) { cmd.Parameters[0].Value = i; cmd.Parameters[1].Value = "Hello " + i; cmd.ExecuteNonQuery(); } //select data var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One); selectCmd.PageSize = 10; using(var reader = selectCmd.ExecuteReader()) { //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(reader.Read()) { 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 void InsertWithTimestamp2() { if (!Cassandra210OrUp) return; using (var connection = new CqlConnection(ConnectionString)) { connection.Open(); var command = new CqlCommand(connection, "insert into testudt.members (id, comment) values (3,('hi','there3'));"); command.ExecuteNonQuery(); var select = new CqlCommand(connection, "select WRITETIME(comment) from testudt.members where id=3;"); using (var reader = select.ExecuteReader()) { Assert.AreEqual(1, reader.Count); if (reader.Read()) { DateTime writeTime = ((long)(reader.GetDouble(0)/1000)).ToDateTime(); Assert.IsTrue(writeTime - DateTime.UtcNow < TimeSpan.FromSeconds(1)); } else { Assert.Fail("Read failed."); } } } }
public void SerializeObjectInOutDefaultsTest() { const string insertCql = @"insert into SerializationTest.Types( aInt, aLong , aVarint , aTextString , aVarcharString , aASCIIString , aBlob , aBool , aDecimal , aDouble , aFloat , aTimestamp , aTimeUUID , aUUID , aInet , aList, aSet, aMap) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; const string selectCql = "select * from SerializationTest.Types limit 1;"; using(var connection = new CqlConnection(ConnectionString)) { connection.Open(); var values = new Types { aInt = 2, aASCIIString = default(string), aBlob = default(byte[]), aBool = default(bool), aDecimal = default(decimal), aDouble = default(double), aFloat = default(float), aInet = default(IPAddress), aLong = default(long), aTextString = default(string), aVarcharString = default(string), aTimeUUID = TimeGuid.Default, aUUID = default(Guid), aTimestamp = default(DateTime), aVarint = default(BigInteger), aList = default(List<string>), aSet = default(HashSet<int>), aMap = default(Dictionary<long, string>) }; var insertCmd = new CqlCommand(connection, insertCql); insertCmd.Prepare(); insertCmd.Parameters.Set(values); insertCmd.ExecuteNonQuery(); var selectCmd = new CqlCommand(connection, selectCql); Types result = null; using(var reader = selectCmd.ExecuteReader<Types>()) { if(reader.Read()) result = reader.Current; } Assert.IsNotNull(result); Assert.AreEqual(result.aASCIIString, default(string)); Assert.AreEqual(result.aVarcharString, default(string)); Assert.AreEqual(result.aVarint, default(BigInteger)); Assert.AreEqual(result.aTextString, default(string)); Assert.AreEqual(result.aBool, default(bool)); Assert.AreEqual(result.aDecimal, default(decimal)); Assert.AreEqual(result.aDouble, default(double)); Assert.AreEqual(result.aFloat, default(float)); Assert.AreEqual(result.aInet, default(IPAddress)); Assert.AreEqual(result.aLong, default(long)); Assert.AreEqual(result.aTimeUUID, TimeGuid.Default); Assert.AreEqual(result.aUUID, default(Guid)); Assert.AreEqual(result.aBlob, default(byte[])); Assert.AreEqual(result.aList, default(List<string>)); Assert.AreEqual(result.aSet, default(HashSet<int>)); Assert.AreEqual(result.aMap, default(Dictionary<long, string>)); } }
public void SerializeOutNullTest() { const string insertCql = @"insert into SerializationTest.Types(aInt) values (1);"; const string selectCql = "select * from SerializationTest.Types limit 1;"; using(var connection = new CqlConnection(ConnectionString)) { string aAsciiString; string aVarcharString; BigInteger? aVarint; string aTextString; bool? aBool; decimal? aDecimal; double? aDouble; float? aFloat; IPAddress aInet; long? aLong; Guid? aTimeUUID; Guid? aUUID; byte[] aBlob; List<string> aList; HashSet<int> aSet; Dictionary<long, string> aMap; connection.Open(); var insertCmd = new CqlCommand(connection, insertCql); insertCmd.ExecuteNonQuery(); var selectCmd = new CqlCommand(connection, selectCql); using(var reader = selectCmd.ExecuteReader()) { if(reader.Read()) { aAsciiString = reader["aasciistring"] as string; aVarcharString = reader["avarcharstring"] as string; aVarint = reader["avarint"] as BigInteger?; aTextString = reader["atextstring"] as string; aBool = reader["abool"] as bool?; aDecimal = reader["adecimal"] as decimal?; aDouble = reader["adouble"] as double?; aFloat = reader["afloat"] as float?; aInet = reader["ainet"] as IPAddress; aLong = reader["along"] as long?; aTimeUUID = reader["atimeuuid"] as Guid?; aUUID = reader["auuid"] as Guid?; aBlob = reader["ablob"] as byte[]; aList = reader["alist"] as List<string>; aSet = reader["aset"] as HashSet<int>; aMap = reader["amap"] as Dictionary<long, string>; } else throw new Exception("No row returned!"); } Assert.AreEqual(aAsciiString, default(string)); Assert.AreEqual(aVarcharString, default(string)); Assert.AreEqual(aVarint, default(BigInteger?)); Assert.AreEqual(aTextString, default(string)); Assert.AreEqual(aBool, default(bool?)); Assert.AreEqual(aDecimal, default(decimal?)); Assert.AreEqual(aDouble, default(double?)); Assert.AreEqual(aFloat, default(float?)); Assert.AreEqual(aInet, default(IPAddress)); Assert.AreEqual(aLong, default(long?)); Assert.AreEqual(aTimeUUID, default(Guid?)); Assert.AreEqual(aUUID, default(Guid?)); Assert.AreEqual(aBlob, default(byte[])); Assert.AreEqual(aList, default(List<string>)); Assert.AreEqual(aSet, default(HashSet<int>)); Assert.AreEqual(aMap, default(Dictionary<long, string>)); } }
public void SerializeInOutTest() { const string insertCql = @"insert into SerializationTest.Types( aInt, aLong , aVarint , aTextString , aVarcharString , aASCIIString , aBlob , aBool , aDecimal , aDouble , aFloat , aTimestamp , aTimeUUID , aUUID , aInet , aList, aSet, aMap) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; const string selectCql = "select * from SerializationTest.Types limit 1;"; using(var connection = new CqlConnection(ConnectionString)) { connection.Open(); var insertCmd = new CqlCommand(connection, insertCql); insertCmd.Prepare(); insertCmd.Parameters["aasciistring"].Value = "hello world!"; insertCmd.Parameters["ablob"].Value = new byte[] {1, 2, 3, 4}; insertCmd.Parameters["abool"].Value = true; insertCmd.Parameters["adecimal"].Value = -1234567890.0987654321m; insertCmd.Parameters["adouble"].Value = 1.234; insertCmd.Parameters["afloat"].Value = 5.789f; insertCmd.Parameters["ainet"].Value = new IPAddress(new byte[] {127, 0, 0, 1}); insertCmd.Parameters["aint"].Value = 10; insertCmd.Parameters["along"].Value = 56789012456; insertCmd.Parameters["atextstring"].Value = "some other text with \u005C unicode"; insertCmd.Parameters["avarcharstring"].Value = "some other varchar with \u005C unicode"; insertCmd.Parameters["atimeuuid"].Value = DateTime.Now.GenerateTimeBasedGuid(); insertCmd.Parameters["auuid"].Value = Guid.NewGuid(); insertCmd.Parameters["atimestamp"].Value = DateTime.Now; insertCmd.Parameters["avarint"].Value = new BigInteger(12345678901234); insertCmd.Parameters["alist"].Value = new List<string> {"string 1", "string 2"}; insertCmd.Parameters["aset"].Value = new HashSet<int> {1, 3, 3}; insertCmd.Parameters["amap"].Value = new Dictionary<long, string> {{1, "value 1"}, {2, "value 2"}, {3, "value 3"}}; insertCmd.ExecuteNonQuery(); string aAsciiString; string aVarcharString; BigInteger aVarint; string aTextString; bool aBool; decimal aDecimal; double aDouble; float aFloat; IPAddress aInet; long aLong; Guid aTimeUUID; Guid aUUID; byte[] aBlob; List<string> aList; HashSet<int> aSet; Dictionary<long, string> aMap; var selectCmd = new CqlCommand(connection, selectCql); using(var reader = selectCmd.ExecuteReader()) { if(reader.Read()) { aAsciiString = reader.GetString(reader.GetOrdinal("aasciistring")); aVarcharString = reader.GetString(reader.GetOrdinal("avarcharstring")); aVarint = reader.GetBigInteger(reader.GetOrdinal("avarint")); aTextString = reader.GetString(reader.GetOrdinal("atextstring")); aBool = reader.GetBoolean(reader.GetOrdinal("abool")); aDecimal = reader.GetDecimal(reader.GetOrdinal("adecimal")); aDouble = reader.GetDouble(reader.GetOrdinal("adouble")); aFloat = reader.GetFloat(reader.GetOrdinal("afloat")); aInet = reader.GetIPAddress(reader.GetOrdinal("ainet")); aLong = reader.GetInt64(reader.GetOrdinal("along")); aTimeUUID = reader.GetGuid(reader.GetOrdinal("atimeuuid")); aUUID = reader.GetGuid(reader.GetOrdinal("auuid")); aBlob = reader.GetBytes(reader.GetOrdinal("ablob")); aList = reader.GetList<string>(reader.GetOrdinal("alist")); aSet = reader.GetSet<int>(reader.GetOrdinal("aset")); aMap = reader.GetDictionary<long, string>(reader.GetOrdinal("amap")); } else throw new Exception("No row returned!"); } Assert.AreEqual(insertCmd.Parameters["aasciistring"].Value, aAsciiString); Assert.AreEqual(insertCmd.Parameters["avarcharstring"].Value, aVarcharString); Assert.AreEqual(insertCmd.Parameters["avarint"].Value, aVarint); Assert.AreEqual(insertCmd.Parameters["atextstring"].Value, aTextString); Assert.AreEqual(insertCmd.Parameters["abool"].Value, aBool); Assert.AreEqual(insertCmd.Parameters["adecimal"].Value, aDecimal); Assert.AreEqual(insertCmd.Parameters["adouble"].Value, aDouble); Assert.AreEqual(insertCmd.Parameters["afloat"].Value, aFloat); Assert.AreEqual(insertCmd.Parameters["ainet"].Value, aInet); Assert.AreEqual(insertCmd.Parameters["along"].Value, aLong); Assert.AreEqual(insertCmd.Parameters["atimeuuid"].Value, aTimeUUID); Assert.AreEqual(insertCmd.Parameters["auuid"].Value, aUUID); Assert.IsTrue(((byte[])insertCmd.Parameters["ablob"].Value).SequenceEqual(aBlob)); Assert.IsTrue(((List<string>)insertCmd.Parameters["alist"].Value).SequenceEqual(aList)); Assert.IsTrue(((HashSet<int>)insertCmd.Parameters["aset"].Value).SequenceEqual(aSet)); foreach(var entry in ((Dictionary<long, string>)insertCmd.Parameters["amap"].Value)) { var val = aMap[entry.Key]; Assert.AreEqual(entry.Value, val); } } }
public void DefaultSerializeTest() { const string insertCql = @"insert into Test.Types( aInt, aLong , aVarint , aTextString , aVarcharString , aASCIIString , aBlob , aBool , aDouble , aFloat , aTimestamp , aTimeUUID , aUUID , aInet , aList, aSet, aMap) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; const string selectCql = "select * from Test.Types limit 1;"; using (var connection = new CqlConnection(ConnectionString)) { connection.Open(); var values = new Types { aInt = 2, aASCIIString = default(string), aBlob = default(byte[]), aBool = default(bool), aDouble = default(double), aFloat = default(float), aInet = default(IPAddress), aLong = default(long), aTextString = default(string), aVarcharString = default(string), aTimeUUID = default(Guid), aUUID = default(Guid), aTimestamp = default(DateTime), aVarint = default(BigInteger), aList = default(List <string>), aSet = default(HashSet <int>), aMap = default(Dictionary <long, string>) }; var insertCmd = new CqlCommand(connection, insertCql); insertCmd.Prepare(); insertCmd.Parameters.Set(values); insertCmd.ExecuteNonQuery(); var selectCmd = new CqlCommand(connection, selectCql); Types result = null; using (var reader = selectCmd.ExecuteReader <Types>()) { if (reader.Read()) { result = reader.Current; } } Assert.IsNotNull(result); Assert.AreEqual(result.aASCIIString, default(string)); Assert.AreEqual(result.aVarcharString, default(string)); Assert.AreEqual(result.aVarint, default(BigInteger)); Assert.AreEqual(result.aTextString, default(string)); Assert.AreEqual(result.aBool, default(bool)); Assert.AreEqual(result.aDouble, default(double)); Assert.AreEqual(result.aFloat, default(float)); Assert.AreEqual(result.aInet, default(IPAddress)); Assert.AreEqual(result.aLong, default(long)); Assert.AreEqual(result.aTimeUUID, default(Guid)); Assert.AreEqual(result.aUUID, default(Guid)); Assert.AreEqual(result.aBlob, default(byte[])); Assert.AreEqual(result.aList, default(List <string>)); Assert.AreEqual(result.aSet, default(HashSet <int>)); Assert.AreEqual(result.aMap, default(Dictionary <long, string>)); } }
public void NullDeserializeParameterTest() { const string insertCql = @"insert into Test.Types(aInt) values (1);"; const string selectCql = "select * from Test.Types limit 1;"; using (var connection = new CqlConnection(ConnectionString)) { string aASCIIString; string aVarcharString; BigInteger? aVarint; string aTextString; bool? aBool; double? aDouble; float? aFloat; IPAddress aInet; long? aLong; Guid? aTimeUUID; Guid? aUUID; byte[] aBlob; List <string> aList; HashSet <int> aSet; Dictionary <long, string> aMap; connection.Open(); var insertCmd = new CqlCommand(connection, insertCql); insertCmd.ExecuteNonQuery(); var selectCmd = new CqlCommand(connection, selectCql); using (var reader = selectCmd.ExecuteReader()) { if (reader.Read()) { aASCIIString = (string)reader["aasciistring"]; aVarcharString = (string)reader["avarcharstring"]; aVarint = (BigInteger?)reader["avarint"]; aTextString = (string)reader["atextstring"]; aBool = (bool?)reader["abool"]; aDouble = (double?)reader["adouble"]; aFloat = (float?)reader["afloat"]; aInet = (IPAddress)reader["ainet"]; aLong = (long?)reader["along"]; aTimeUUID = (Guid?)reader["atimeuuid"]; aUUID = (Guid?)reader["auuid"]; aBlob = (byte[])reader["ablob"]; aList = (List <string>)reader["alist"]; aSet = (HashSet <int>)reader["aset"]; aMap = (Dictionary <long, string>)reader["amap"]; } else { throw new Exception("No row returned!"); } } Assert.AreEqual(aASCIIString, default(string)); Assert.AreEqual(aVarcharString, default(string)); Assert.AreEqual(aVarint, default(BigInteger?)); Assert.AreEqual(aTextString, default(string)); Assert.AreEqual(aBool, default(bool?)); Assert.AreEqual(aDouble, default(double?)); Assert.AreEqual(aFloat, default(float?)); Assert.AreEqual(aInet, default(IPAddress)); Assert.AreEqual(aLong, default(long?)); Assert.AreEqual(aTimeUUID, default(Guid?)); Assert.AreEqual(aUUID, default(Guid?)); Assert.AreEqual(aBlob, default(byte[])); Assert.AreEqual(aList, default(List <string>)); Assert.AreEqual(aSet, default(HashSet <int>)); Assert.AreEqual(aMap, default(Dictionary <long, string>)); } }
public void SerializeTest() { const string insertCql = @"insert into Test.Types( aInt, aLong , aVarint , aTextString , aVarcharString , aASCIIString , aBlob , aBool , aDouble , aFloat , aTimestamp , aTimeUUID , aUUID , aInet , aList, aSet, aMap) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; const string selectCql = "select * from Test.Types limit 1;"; using (var connection = new CqlConnection(ConnectionString)) { connection.Open(); var values = new Types { aASCIIString = "hello world!", aBlob = new byte[] { 1, 2, 3, 4 }, aBool = true, aDouble = 1.234, aFloat = 5.789f, aInet = new IPAddress(new byte[] { 127, 0, 0, 1 }), aInt = 10, aLong = 56789012456, aTextString = "some other text with \u005C unicode", aVarcharString = "some other varchar with \u005C unicode", aTimeUUID = DateTime.Now.GenerateTimeBasedGuid(), aUUID = Guid.NewGuid(), aTimestamp = DateTime.Now, aVarint = new BigInteger(12345678901234), aList = new List <string> { "string 1", "string 2" }, aSet = new HashSet <int> { 1, 3, 3 }, aMap = new Dictionary <long, string> { { 1, "value 1" }, { 2, "value 2" }, { 3, "value 3" } }, }; var insertCmd = new CqlCommand(connection, insertCql); insertCmd.Prepare(); insertCmd.Parameters.Set(values); insertCmd.ExecuteNonQuery(); var selectCmd = new CqlCommand(connection, selectCql); Types result = null; using (var reader = selectCmd.ExecuteReader <Types>()) { if (reader.Read()) { result = reader.Current; } } Assert.IsNotNull(result); Assert.AreEqual(result.aASCIIString, values.aASCIIString); Assert.AreEqual(result.aVarcharString, values.aVarcharString); Assert.AreEqual(result.aVarint, values.aVarint); Assert.AreEqual(result.aTextString, values.aTextString); Assert.AreEqual(result.aBool, values.aBool); Assert.AreEqual(result.aDouble, values.aDouble); Assert.AreEqual(result.aFloat, values.aFloat); Assert.AreEqual(result.aInet, values.aInet); Assert.AreEqual(result.aInt, values.aInt); Assert.AreEqual(result.aLong, values.aLong); Assert.AreEqual(result.aTimeUUID, values.aTimeUUID); Assert.AreEqual(result.aUUID, values.aUUID); Assert.IsTrue(result.aBlob.SequenceEqual(values.aBlob)); Assert.IsTrue(result.aList.SequenceEqual(values.aList)); Assert.IsTrue(result.aSet.SequenceEqual(values.aSet)); } }
public void SerializeTupleAndUDTOutNullTest() { if (!Cassandra210OrUp) return; using(var connection = new CqlConnection(ConnectionString)) { connection.Open(); var command = new CqlCommand(connection, "insert into testudt.members (id) values (1);"); command.ExecuteNonQuery(); var select = new CqlCommand(connection, "select * from testudt.members;"); using(var reader = select.ExecuteReader<Member>()) { Assert.AreEqual(1, reader.Count); if(reader.Read()) { Assert.IsNull(reader.GetUserDefinedType(1)); Assert.IsNull(reader.GetTuple<string, string>(2)); var actual = reader.Current; Assert.IsNotNull(actual); Assert.AreEqual(1, actual.Id); Assert.IsNull(actual.User); Assert.IsNull(actual.Comment); } else { Assert.Fail("Read failed."); } } } }
/// <summary> /// Creates the database. /// </summary> public static void CreateDatabase() { using(var connection = new CqlConnection(ConnectionString)) { connection.Open(); var existCommand = new CqlCommand(connection, "select * from system.schema_keyspaces where keyspace_name='cqlsharptest';"); using(var reader = existCommand.ExecuteReader()) { //check if database exists, if so, use it, otherwise create it if(!reader.Read()) { //create the keyspace var createKs = new CqlCommand(connection, @"CREATE KEYSPACE cqlsharptest WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};"); createKs.ExecuteNonQuery(); //create the table var createTable = new CqlCommand(connection, @"CREATE TABLE cqlsharptest.measurements (id int PRIMARY KEY, customer text, values map<text, int>);"); createTable.ExecuteNonQuery(); //fill table with dummy data var inserts = new List<Task>(25000); for(int i = 0; i < 25000; i++) { //create and prepare the insert command var insertCommand = new CqlCommand(connection, "insert into cqlsharptest.measurements (id, customer, values) values (?,?,?)"); insertCommand.Prepare(); //create new measurement var measurement = new Measurement { Id = i, Customer = Customers[Random.Next(0, Customers.Count)], Values = new Dictionary<string, int> { {"Temp", Random.Next(0, 101)}, {"Humidity", Random.Next(0, 101)}, {"Clouds", Random.Next(0, 101)}, {"Pressure", Random.Next(0, 101)}, {"Rain", Random.Next(0, 101)}, {"Sunshine", Random.Next(0, 101)}, {"Overall", Random.Next(0, 101)} } }; //set insert parameters insertCommand.Parameters.Set(measurement); //add the insert as task to list inserts.Add(insertCommand.ExecuteNonQueryAsync()); } //wait for all inserts to complete Task.WaitAll(inserts.ToArray()); } } } }
/// <summary> /// Gets the measurement. /// </summary> /// <param name="id">The identifier.</param> /// <returns></returns> public static Measurement GetMeasurement(int id) { //return GetMeasurementAsync(id).Result; using(var connection = new CqlConnection(ConnectionString)) { connection.Open(); var selectCommand = new CqlCommand(connection, "select * from cqlsharptest.measurements where id=?"); selectCommand.CommandTimeout = Timeout.Infinite; selectCommand.Prepare(); selectCommand.Parameters[0].Value = id; using(var reader = selectCommand.ExecuteReader<Measurement>()) { return reader.Read() ? reader.Current : null; } } }
public void SelectUDTAndTuplesViaNonGenericReader() { if (!Cassandra210OrUp) return; var address = new Address {Street = "MyWay", Number = 1}; var user = new User { Name = "Joost", Password = new byte[] { 1, 2, 3 }, Address = address, Phones = new List<string> { "call me once", "call me twice", "no answer" } }; var member = new Member {Id = 1, User = user, Comment = Tuple.Create("my title", "phew")}; using(var connection = new CqlConnection(ConnectionString)) { connection.Open(); var command = new CqlCommand(connection, "insert into testudt.members (id, user, comment) values (?,?,?);"); command.Prepare(); command.Parameters.Set(member); command.ExecuteNonQuery(); var select = new CqlCommand(connection, "select id, user, comment from testudt.members;"); using(var reader = select.ExecuteReader()) { Assert.AreEqual(1, reader.Count); if(reader.Read()) { var actualUser = reader.GetUserDefinedType<User>(1); Assert.IsNotNull(actualUser); Assert.AreEqual(member.User.Name, actualUser.Name); Assert.IsNotNull(actualUser.Address); Assert.AreEqual(member.User.Address.Street, actualUser.Address.Street); Assert.IsNotNull(actualUser.Phones); Assert.AreEqual(member.User.Phones[2], actualUser.Phones[2]); var comment = reader.GetTuple<string, string>(2); Assert.IsNotNull(comment); Assert.AreEqual(member.Comment, comment); } else { Assert.Fail("Read failed."); } } } }
public void NullableSerializeTest() { const string insertCql = @"insert into Test.Types( aInt, aLong , aVarint , aTextString , aVarcharString , aASCIIString , aBlob , aBool , aDouble , aFloat , aTimestamp , aTimeUUID , aUUID , aInet , aList, aSet, aMap) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; const string selectCql = "select * from Test.Types limit 1;"; using (var connection = new CqlConnection(ConnectionString)) { connection.Open(); var values = new NullableTypes { aInt = 3, aASCIIString = null, aBlob = null, aBool = null, aDouble = null, aFloat = null, aInet = null, aLong = null, aTextString = null, aVarcharString = null, aTimeUUID = null, aUUID = null, aTimestamp = null, aVarint = null, aList = null, aSet = null, aMap = null }; var insertCmd = new CqlCommand(connection, insertCql); insertCmd.Prepare(); insertCmd.Parameters.Set(values); insertCmd.ExecuteNonQuery(); var selectCmd = new CqlCommand(connection, selectCql); NullableTypes result = null; using (var reader = selectCmd.ExecuteReader <NullableTypes>()) { if (reader.Read()) { result = reader.Current; } } Assert.IsNotNull(result); Assert.IsNull(result.aASCIIString); Assert.IsNull(result.aVarcharString); Assert.IsNull(result.aVarint); Assert.IsNull(result.aTextString); Assert.IsNull(result.aBool); Assert.IsNull(result.aDouble); Assert.IsNull(result.aFloat); Assert.IsNull(result.aInet); Assert.IsNull(result.aLong); Assert.IsNull(result.aTimeUUID); Assert.IsNull(result.aUUID); Assert.IsNull(result.aBlob); Assert.IsNull(result.aList); Assert.IsNull(result.aSet); Assert.IsNull(result.aMap); } }
public void SerializeObjectInOutTest() { const string insertCql = @"insert into SerializationTest.Types( aInt, aLong , aVarint , aTextString , aVarcharString , aASCIIString , aBlob , aBool , aDecimal , aDouble , aFloat , aTimestamp , aTimeUUID , aUUID , aInet , aList, aSet, aMap) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; const string selectCql = "select * from SerializationTest.Types limit 1;"; using(var connection = new CqlConnection(ConnectionString)) { connection.Open(); var values = new Types { aASCIIString = "hello world!", aBlob = new byte[] {1, 2, 3, 4}, aBool = true, aDecimal = decimal.MaxValue/2, aDouble = 1.234, aFloat = 5.789f, aInet = new IPAddress(new byte[] {127, 0, 0, 1}), aInt = 10, aLong = 56789012456, aTextString = "some other text with \u005C unicode", aVarcharString = "some other varchar with \u005C unicode", aTimeUUID = DateTime.Now.GenerateTimeBasedGuid(), aUUID = Guid.NewGuid(), aTimestamp = DateTime.Now, aVarint = new BigInteger(12345678901234), aList = new List<string> {"string 1", "string 2"}, aSet = new HashSet<int> {1, 3, 3}, aMap = new Dictionary<long, string> {{1, "value 1"}, {2, "value 2"}, {3, "value 3"}}, }; var insertCmd = new CqlCommand(connection, insertCql); insertCmd.Prepare(); insertCmd.Parameters.Set(values); insertCmd.ExecuteNonQuery(); var selectCmd = new CqlCommand(connection, selectCql); Types result = null; using(var reader = selectCmd.ExecuteReader<Types>()) { if(reader.Read()) result = reader.Current; } Assert.IsNotNull(result); Assert.AreEqual(values.aASCIIString, result.aASCIIString); Assert.AreEqual(values.aVarcharString, result.aVarcharString); Assert.AreEqual(values.aVarint, result.aVarint); Assert.AreEqual(values.aTextString, result.aTextString); Assert.AreEqual(values.aBool, result.aBool); Assert.AreEqual(values.aDecimal, result.aDecimal); Assert.AreEqual(values.aDouble, result.aDouble); Assert.AreEqual(values.aFloat, result.aFloat); Assert.AreEqual(values.aInet, result.aInet); Assert.AreEqual(values.aInt, result.aInt); Assert.AreEqual(values.aLong, result.aLong); Assert.AreEqual(values.aTimeUUID, result.aTimeUUID); Assert.AreEqual(values.aUUID, result.aUUID); Assert.IsTrue(result.aBlob.SequenceEqual(values.aBlob)); Assert.IsTrue(result.aList.SequenceEqual(values.aList)); Assert.IsTrue(result.aSet.SequenceEqual(values.aSet)); } }
public void SerializeObjectOutNullTest() { const string insertCql = @"insert into SerializationTest.Types(aInt) values (4);"; const string selectCql = "select * from SerializationTest.Types limit 1;"; using(var connection = new CqlConnection(ConnectionString)) { connection.Open(); var insertCmd = new CqlCommand(connection, insertCql); insertCmd.ExecuteNonQuery(); var selectCmd = new CqlCommand(connection, selectCql); NullableTypes result = null; using(var reader = selectCmd.ExecuteReader<NullableTypes>()) { if(reader.Read()) result = reader.Current; } Assert.IsNotNull(result); Assert.IsNull(result.aASCIIString); Assert.IsNull(result.aVarcharString); Assert.IsNull(result.aVarint); Assert.IsNull(result.aTextString); Assert.IsNull(result.aBool); Assert.IsNull(result.aDecimal); Assert.IsNull(result.aDouble); Assert.IsNull(result.aFloat); Assert.IsNull(result.aInet); Assert.IsNull(result.aLong); Assert.IsNull(result.aTimeUUID); Assert.IsNull(result.aUUID); Assert.IsNull(result.aBlob); Assert.IsNull(result.aList); Assert.IsNull(result.aSet); Assert.IsNull(result.aMap); } }
public void SerializeObjectOutDefaultsTest() { const string insertCql = @"insert into SerializationTest.Types(aInt) values (1);"; const string selectCql = "select * from SerializationTest.Types limit 1;"; using(var connection = new CqlConnection(ConnectionString)) { connection.Open(); var insertCmd = new CqlCommand(connection, insertCql); insertCmd.ExecuteNonQuery(); var selectCmd = new CqlCommand(connection, selectCql); Types result = null; using(var reader = selectCmd.ExecuteReader<Types>()) { if(reader.Read()) result = reader.Current; } Assert.IsNotNull(result); Assert.AreEqual(result.aASCIIString, default(string)); Assert.AreEqual(result.aVarcharString, default(string)); Assert.AreEqual(result.aVarint, default(BigInteger)); Assert.AreEqual(result.aTextString, default(string)); Assert.AreEqual(result.aBool, default(bool)); Assert.AreEqual(result.aDecimal, default(decimal)); Assert.AreEqual(result.aDouble, default(double)); Assert.AreEqual(result.aFloat, default(float)); Assert.AreEqual(result.aInet, default(IPAddress)); Assert.AreEqual(result.aLong, default(long)); Assert.AreEqual(result.aTimeUUID, default(Guid)); Assert.AreEqual(result.aUUID, default(Guid)); Assert.AreEqual(result.aBlob, default(byte[])); Assert.AreEqual(result.aList, default(List<string>)); Assert.AreEqual(result.aSet, default(HashSet<int>)); Assert.AreEqual(result.aMap, default(Dictionary<long, string>)); } }
public void InsertAndSelectNestedUDTAndTuples() { if (!Cassandra210OrUp) return; var address = new Address {Street = "MyWay", Number = 1}; var user = new User { Name = "Joost", Password = new byte[] { 1, 2, 3 }, Address = address, Phones = new List<string> { "call me once", "call me twice", "no answer" } }; var group = new Group { Id = 1, Members = new HashSet<Tuple<int, User>> { Tuple.Create(1, user) } }; using(var connection = new CqlConnection(ConnectionString)) { connection.Open(); var command = new CqlCommand(connection, "insert into testudt.groups (id, members) values (?,?);"); command.Prepare(); command.Parameters.Set(group); command.ExecuteNonQuery(); var select = new CqlCommand(connection, "select * from testudt.groups;"); using(var reader = select.ExecuteReader<Group>()) { Assert.AreEqual(1, reader.Count); if(reader.Read()) { var actual = reader.Current; Assert.IsNotNull(actual); Assert.AreEqual(group.Id, actual.Id); Assert.IsNotNull(actual.Members); Assert.IsInstanceOfType(actual.Members, typeof(HashSet<Tuple<int, User>>)); Assert.AreEqual(1, actual.Members.Count); Assert.AreEqual("Joost", actual.Members.First().Item2.Name); Assert.AreEqual("call me twice", actual.Members.First().Item2.Phones[1]); } else { Assert.Fail("Read failed."); } } } }
public void SerializeOutDefaultTest() { const string insertCql = @"insert into SerializationTest.Types(aInt) values (1);"; const string selectCql = "select * from SerializationTest.Types limit 1;"; using(var connection = new CqlConnection(ConnectionString)) { connection.Open(); var insertCmd = new CqlCommand(connection, insertCql); insertCmd.ExecuteNonQuery(); string aAsciiString; string aVarcharString; BigInteger aVarint; string aTextString; bool aBool; decimal aDecimal; double aDouble; float aFloat; IPAddress aInet; long aLong; Guid aTimeUUID; Guid aUUID; byte[] aBlob; List<string> aList; HashSet<int> aSet; Dictionary<long, string> aMap; var selectCmd = new CqlCommand(connection, selectCql); using(var reader = selectCmd.ExecuteReader()) { if(reader.Read()) { aAsciiString = reader.GetString(reader.GetOrdinal("aasciistring")); aVarcharString = reader.GetString(reader.GetOrdinal("avarcharstring")); aVarint = reader.GetBigInteger(reader.GetOrdinal("avarint")); aTextString = reader.GetString(reader.GetOrdinal("atextstring")); aBool = reader.GetBoolean(reader.GetOrdinal("abool")); aDecimal = reader.GetDecimal(reader.GetOrdinal("adecimal")); aDouble = reader.GetDouble(reader.GetOrdinal("adouble")); aFloat = reader.GetFloat(reader.GetOrdinal("afloat")); aInet = reader.GetIPAddress(reader.GetOrdinal("ainet")); aLong = reader.GetInt64(reader.GetOrdinal("along")); aTimeUUID = reader.GetGuid(reader.GetOrdinal("atimeuuid")); aUUID = reader.GetGuid(reader.GetOrdinal("auuid")); aBlob = reader.GetBytes(reader.GetOrdinal("ablob")); aList = reader.GetList<string>(reader.GetOrdinal("alist")); aSet = reader.GetSet<int>(reader.GetOrdinal("aset")); aMap = reader.GetDictionary<long, string>(reader.GetOrdinal("amap")); } else throw new Exception("No row returned!"); } Assert.AreEqual(aAsciiString, default(string)); Assert.AreEqual(aVarcharString, default(string)); Assert.AreEqual(aVarint, default(BigInteger)); Assert.AreEqual(aTextString, default(string)); Assert.AreEqual(aBool, default(bool)); Assert.AreEqual(aDecimal, default(decimal)); Assert.AreEqual(aDouble, default(double)); Assert.AreEqual(aFloat, default(float)); Assert.AreEqual(aInet, default(IPAddress)); Assert.AreEqual(aLong, default(long)); Assert.AreEqual(aTimeUUID, default(Guid)); Assert.AreEqual(aUUID, default(Guid)); Assert.AreEqual(aBlob, default(byte[])); Assert.AreEqual(aList, default(List<string>)); Assert.AreEqual(aSet, default(HashSet<int>)); Assert.AreEqual(aMap, default(Dictionary<long, string>)); } }
public void BasicInsertSelectSynchronousTimeout() { //Assume const string insertCql = @"insert into Test.BasicFlow (id,value) values (13244,'Hallo 54235');"; const string retrieveCql = @"select * from Test.BasicFlow;"; //Act using (var connection = new CqlConnection(ConnectionString)) { connection.Open(); //insert data var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); cmd.ExecuteNonQuery(); //select data var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One); selectCmd.CommandTimeout = Timeout.Infinite; selectCmd.Prepare(); //get connection, and cancel the select as soon as it registers EventHandler<Network.LoadChangeEvent> delayHandler = (src, ev) => Thread.Sleep(1200); Connection networkConnection = connection.GetConnection(); networkConnection.OnLoadChange += delayHandler; try { //set command timeout as low as possible (1 second) selectCmd.CommandTimeout = 1; CqlDataReader reader = selectCmd.ExecuteReader(); } finally { networkConnection.OnLoadChange -= delayHandler; } } }
public void SerializeObjectInOutNullTest() { const string insertCql = @"insert into SerializationTest.Types( aInt, aLong , aVarint , aTextString , aVarcharString , aASCIIString , aBlob , aBool , aDecimal , aDouble , aFloat , aTimestamp , aTimeUUID , aUUID , aInet , aList, aSet, aMap) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; const string selectCql = "select * from SerializationTest.Types limit 1;"; using(var connection = new CqlConnection(ConnectionString)) { connection.Open(); var values = new NullableTypes { aInt = 3, aASCIIString = null, aBlob = null, aBool = null, aDecimal = null, aDouble = null, aFloat = null, aInet = null, aLong = null, aTextString = null, aVarcharString = null, aTimeUUID = null, aUUID = null, aTimestamp = null, aVarint = null, aList = null, aSet = null, aMap = null }; var insertCmd = new CqlCommand(connection, insertCql); insertCmd.Prepare(); insertCmd.Parameters.Set(values); insertCmd.ExecuteNonQuery(); var selectCmd = new CqlCommand(connection, selectCql); NullableTypes result = null; using(var reader = selectCmd.ExecuteReader<NullableTypes>()) { if(reader.Read()) result = reader.Current; } Assert.IsNotNull(result); Assert.IsNull(result.aASCIIString); Assert.IsNull(result.aVarcharString); Assert.IsNull(result.aVarint); Assert.IsNull(result.aTextString); Assert.IsNull(result.aBool); Assert.IsNull(result.aDecimal); Assert.IsNull(result.aDouble); Assert.IsNull(result.aFloat); Assert.IsNull(result.aInet); Assert.IsNull(result.aLong); Assert.IsNull(result.aTimeUUID); Assert.IsNull(result.aUUID); Assert.IsNull(result.aBlob); Assert.IsNull(result.aList); Assert.IsNull(result.aSet); Assert.IsNull(result.aMap); } }
public void InsertWithTimestamp() { if (!Cassandra210OrUp) return; using (var connection = new CqlConnection(ConnectionString)) { connection.Open(); DateTime yesterday = DateTime.UtcNow - TimeSpan.FromDays(1); var command = new CqlCommand(connection, "insert into testudt.members (id, comment) values (2,('hi','there'));"); command.Timestamp = yesterday; command.ExecuteNonQuery(); var select = new CqlCommand(connection, "select WRITETIME(comment) from testudt.members where id=2;"); using (var reader = select.ExecuteReader()) { Assert.AreEqual(1, reader.Count); if (reader.Read()) { DateTime writeTime = ((long)(reader.GetDouble(0) / 1000)).ToDateTime(); Assert.AreEqual(0 , (int)((writeTime-yesterday).TotalMilliseconds)); } else { Assert.Fail("Read failed."); } } } }
public void BasicInsertSelectSynchronous() { //Assume const string insertCql = @"insert into Test.BasicFlow (id,value) values (12367,'Hallo 12367');"; const string retrieveCql = @"select * from Test.BasicFlow;"; //Act using (var connection = new CqlConnection(ConnectionString)) { connection.Open(); //insert data var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One); cmd.ExecuteNonQuery(); //select data var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One); selectCmd.CommandTimeout = Timeout.Infinite; selectCmd.Prepare(); CqlDataReader reader = selectCmd.ExecuteReader(); Assert.AreEqual(1, reader.Count); if (reader.Read()) { Assert.AreEqual(12367, reader["id"]); Assert.AreEqual("Hallo 12367", reader["value"]); Assert.AreEqual(DBNull.Value, reader["ignored"]); } else { Assert.Fail("Read should have succeeded"); } } }
public void InsertAnonymousUDTAndSelect() { if (!Cassandra210OrUp) return; using (var connection = new CqlConnection(ConnectionString)) { connection.Open(); var command = new CqlCommand(connection, "insert into testudt.members (id, user, comment) values (?,?,?);"); command.Prepare(); command.Parameters["id"].Value = 12; command.Parameters["user"].Value = new { Name = "Joost", Password = new byte[] {1, 2, 3}, Address = new {Street = "MyWay", Number = 1}, Phones = new List<string> {"call me once", "call me twice", "no answer"} }; command.Parameters["comment"].Value = Tuple.Create("my title", "phew"); command.ExecuteNonQuery(); var select = new CqlCommand(connection, "select * from testudt.members;"); using (var reader = select.ExecuteReader<Member>()) { Assert.AreEqual(1, reader.Count); if (reader.Read()) { var actual = reader.Current; Assert.IsNotNull(actual); Assert.AreEqual(12, actual.Id); Assert.AreEqual(Tuple.Create("my title", "phew"), actual.Comment); Assert.AreEqual("Joost", actual.User.Name); Assert.AreEqual("MyWay", actual.User.Address.Street); Assert.AreEqual(3, actual.User.Phones.Count); Assert.AreEqual("call me twice", actual.User.Phones[1]); } else { Assert.Fail("Read failed."); } } } }