/// <summary> /// Gets a IQueryProvider with a new mapping configuration containing the definition provided /// </summary> protected Table <T> GetTable <T>(ISession session, ITypeDefinition definition = null) { var config = new MappingConfiguration(); if (definition != null) { config.Define(definition); } return(new Table <T>(session, config)); }
public void Get_Returns_Mapping_IfExists() { var userMapping = new FluentUserMapping(); var mappingConfig = new MappingConfiguration(); mappingConfig.Define(userMapping); var existingMapping = mappingConfig.Get <FluentUser>(); Assert.IsNotNull(existingMapping); Assert.IsInstanceOf(typeof(FluentUserMapping), existingMapping); }
public static CassandraKeyValueStore <TKey, TData> Initialize(ISession session, string table) { var config = new MappingConfiguration(); config.Define(new Map <KeyValueDto>() .TableName(table) .PartitionKey(s => s.Id)); session.Execute($"CREATE TABLE IF NOT EXISTS {table}(id text, data text, PRIMARY KEY (id));"); return(new CassandraKeyValueStore <TKey, TData>(session, config)); }
public void LinqCounter_BatchTest() { var mappingConfig = new MappingConfiguration(); mappingConfig.Define(new Map <CounterEntityWithLinqAttributes>() .ExplicitColumns() .Column(t => t.KeyPart1) .Column(t => t.KeyPart2) .Column(t => t.Counter, map => map.AsCounter()) .PartitionKey(t => t.KeyPart1, t => t.KeyPart2) .TableName("linqcounter_batchtest_table") ); var counterTable = new Table <CounterEntityWithLinqAttributes>(_session, mappingConfig); counterTable.CreateIfNotExists(); var counter = new CounterEntityWithLinqAttributes { KeyPart1 = Guid.NewGuid(), KeyPart2 = 1, Counter = 1 }; var counter2 = new CounterEntityWithLinqAttributes { KeyPart1 = counter.KeyPart1, KeyPart2 = 2, Counter = 2 }; var batch = counterTable.GetSession().CreateBatch(BatchType.Counter); var update1 = counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = 1 }) .Update(); var update2 = counterTable .Where(t => t.KeyPart1 == counter2.KeyPart1 && t.KeyPart2 == counter2.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = 2 }) .Update(); batch.Append(update1); batch.Append(update2); batch.Execute(); var counters = counterTable.Execute().ToList(); Assert.AreEqual(2, counters.Count); Assert.IsTrue(counters.Contains(counter)); Assert.IsTrue(counters.Contains(counter2)); }
public static IConnection BuildConnection(CassandraConfiguration config) { var cluster = Cluster.Builder() .WithDefaultKeyspace(config.Keyspace) .AddContactPoints(config.ContactPoints) .Build(); var mapConfig = new MappingConfiguration(); mapConfig.Define <TransactionMappings>(); var session = cluster.ConnectAndCreateDefaultKeyspaceIfNotExists(config.ReplicationParameters); var table = new Table <TransactionByIdReadModel>(session, mapConfig); table.CreateIfNotExists(); return(new Connection(cluster, session, mapConfig)); }
public void LinqCount_Where_Sync_AllowFiltering() { var mapping = new MappingConfiguration(); mapping.Define(new Map <Tweet>() .ExplicitColumns() .Column(t => t.AuthorId) .Column(t => t.TweetId) .Column(t => t.Body) .PartitionKey(t => t.AuthorId) .ClusteringKey(t => t.TweetId) .TableName("tweets")); var table = new Table <Tweet>(_session, mapping); table.Create(); table.Insert(new Tweet { AuthorId = "1", Body = "I like", TweetId = Guid.NewGuid() }).Execute(); table.Insert(new Tweet { AuthorId = "1", Body = "to tweet", TweetId = Guid.NewGuid() }).Execute(); table.Insert(new Tweet { AuthorId = "1", Body = "a lot", TweetId = Guid.NewGuid() }).Execute(); table.Insert(new Tweet { AuthorId = "1", Body = "a lot", TweetId = Guid.NewGuid() }).Execute(); table.Insert(new Tweet { AuthorId = "1", Body = "a lot", TweetId = Guid.NewGuid() }).Execute(); Assert.Throws <InvalidQueryException>(() => { table.Where(e => e.Body == "a lot").Count().Execute(); }); long count = table.Where(e => e.Body == "a lot").AllowFiltering().Count().Execute(); Assert.AreEqual(3, count); }
public static void AutoRegister(MappingConfiguration global) { lock (_lockObject) { if (_registered) { return; } var definedTypes = Assembly.GetExecutingAssembly().DefinedTypes; foreach (var definedType in definedTypes) { //var prop = definedType.GetProperty("Mapping"); var requiredType = typeof(Map <>).GetTypeInfo().MakeGenericType(definedType); var prop = definedType.DeclaredProperties.FirstOrDefault(pi => requiredType.Equals(pi.PropertyType.GetTypeInfo())); if (prop != null) { var val = prop.GetValue(null) as ITypeDefinition; Debug.Write($"Mapping '{definedType.FullName}' class to Cassandra table 'val.TableName'."); global.Define(val); } } _registered = true; } }
public void LinqCounter_BatchTest() { var mappingConfig = new MappingConfiguration(); mappingConfig.Define(new Map <CounterEntityWithLinqAttributes>() .ExplicitColumns() .Column(t => t.KeyPart1) .Column(t => t.KeyPart2) .Column(t => t.Counter, map => map.AsCounter()) .PartitionKey(t => t.KeyPart1, t => t.KeyPart2) .TableName("linqcounter_batchtest_table") ); var counterTable = new Table <CounterEntityWithLinqAttributes>(Session, mappingConfig); counterTable.CreateIfNotExists(); var counter = new CounterEntityWithLinqAttributes { KeyPart1 = Guid.NewGuid(), KeyPart2 = 1, Counter = 1 }; var counter2 = new CounterEntityWithLinqAttributes { KeyPart1 = counter.KeyPart1, KeyPart2 = 2, Counter = 2 }; var batch = counterTable.GetSession().CreateBatch(BatchType.Counter); var update1 = counterTable .Where(t => t.KeyPart1 == counter.KeyPart1 && t.KeyPart2 == counter.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = 1 }) .Update(); var update2 = counterTable .Where(t => t.KeyPart1 == counter2.KeyPart1 && t.KeyPart2 == counter2.KeyPart2) .Select(t => new CounterEntityWithLinqAttributes { Counter = 2 }) .Update(); batch.Append(update1); batch.Append(update2); batch.Execute(); VerifyBatchStatement( 1, new[] { "UPDATE linqcounter_batchtest_table SET Counter = Counter + ? WHERE KeyPart1 = ? AND KeyPart2 = ?", "UPDATE linqcounter_batchtest_table SET Counter = Counter + ? WHERE KeyPart1 = ? AND KeyPart2 = ?" }, new[] { new object[] { 1L, counter.KeyPart1, counter.KeyPart2 }, new object[] { 2L, counter2.KeyPart1, counter2.KeyPart2 } }); var expectedCounters = new[] { counter, counter2 }; PrimeLinqCounterRangeQuery(expectedCounters, "linqcounter_batchtest_table", false); var counters = counterTable.Execute().ToList(); Assert.AreEqual(2, counters.Count); Assert.IsTrue(counters.Contains(counter)); Assert.IsTrue(counters.Contains(counter2)); }
public void Should_ReturnCorrectValue_When_EmptyColumnNameAndSchemaParserV2() { using (var simulacronCluster = SimulacronCluster.CreateNew(3)) { simulacronCluster.Prime(new { when = new { query = "SELECT * FROM system_schema.tables WHERE table_name='testtable' AND keyspace_name='testks'" }, then = new { result = "success", delay_in_ms = 0, rows = new[] { new { compression = new { }, compaction = new { }, bloom_filter_fp_chance = 0.1, caching = new { keys = "ALL", rows_per_partition = "NONE" }, comment = "comment", gc_grace_seconds = 60000, dclocal_read_repair_chance = 0.1, read_repair_chance = 0.1, keyspace_name = "testks" } }, column_types = new { compression = "map<ascii, ascii>", compaction = "map<ascii, ascii>", bloom_filter_fp_chance = "double", caching = "map<ascii, ascii>", comment = "ascii", gc_grace_seconds = "int", dclocal_read_repair_chance = "double", read_repair_chance = "double", keyspace_name = "ascii" }, ignore_on_prepare = false } }); simulacronCluster.Prime(new { when = new { query = "SELECT * FROM system_schema.keyspaces" }, then = new { result = "success", delay_in_ms = 0, rows = new[] { new { replication = new { @class = "SimpleStrategy", replication_factor = "1" }, keyspace_name = "testks", durable_writes = true } }, column_types = new { replication = "map<ascii, ascii>", keyspace_name = "ascii", durable_writes = "boolean" }, ignore_on_prepare = false } }); simulacronCluster.Prime(new { when = new { query = "SELECT * FROM system_schema.indexes WHERE table_name='testtable' AND keyspace_name='testks'" }, then = new { result = "success", delay_in_ms = 0, rows = new[] { new { keyspace_name = "ascii", table_name = "ascii", index_name = "ascii", kind = "ascii", options = new { target = "Custom" } } }, column_types = new { keyspace_name = "ascii", table_name = "ascii", index_name = "ascii", kind = "ascii", options = "map<ascii,ascii>" }, ignore_on_prepare = false } }); simulacronCluster.Prime(new { when = new { query = "SELECT * FROM system_schema.columns WHERE table_name='testtable' AND keyspace_name='testks'" }, then = new { result = "success", delay_in_ms = 0, rows = new[] { new { keyspace_name = "testks", table_name = "testtable", column_name = "", clustering_order = "none", column_name_bytes = 0x12, kind = "partition_key", position = 0, type = "text" } }, column_types = new { keyspace_name = "ascii", table_name = "ascii", column_name = "ascii", clustering_order = "ascii", column_name_bytes = "blob", kind = "ascii", position = "int", type = "ascii" }, ignore_on_prepare = false } }); simulacronCluster.Prime(new { when = new { query = "SELECT \"\", \" \" FROM testks.testtable" }, then = new { result = "success", delay_in_ms = 0, rows = new[] { new Dictionary <string, string> { { "", "testval" }, { " ", "testval2" } } }, column_types = new Dictionary <string, string> { { "", "ascii" }, { " ", "ascii" } }, ignore_on_prepare = false } }); simulacronCluster.Prime(new { when = new { query = "SELECT \"\", \" \" FROM testks.testtable WHERE \"\" = ? AND \" \" = ?", @params = new { column1 = "testval", column2 = "testval2" }, param_types = new { column1 = "ascii", column2 = "ascii" } }, then = new { result = "success", delay_in_ms = 0, rows = new[] { new Dictionary <string, string> { { "", "testval" }, { " ", "testval2" } } }, column_types = new Dictionary <string, string> { { "", "ascii" }, { " ", "ascii" } }, ignore_on_prepare = false } }); var mapConfig = new MappingConfiguration(); mapConfig.Define( new Map <TestTable>() .KeyspaceName("testks") .TableName("testtable") .PartitionKey(u => u.TestColumn) .Column(u => u.TestColumn, cm => cm.WithName("")) .Column(u => u.TestColumn2, cm => cm.WithName(" "))); using (var cluster = EmptyColumnTests.BuildCluster(simulacronCluster)) { var session = cluster.Connect(); var testTables = new Table <TestTable>(session); var test = (from t in testTables.Execute() select t).First(); Assert.AreEqual("testval", test.TestColumn); Assert.AreEqual("testval2", test.TestColumn2); var mapper = new Mapper(session, mapConfig); test = mapper.Fetch <TestTable>().First(); Assert.AreEqual("testval", test.TestColumn); Assert.AreEqual("testval2", test.TestColumn2); var tableMetadata = session.Cluster.Metadata.GetTable("testks", "testtable"); Assert.IsNotNull(tableMetadata); var rs = session.Execute("SELECT \"\", \" \" FROM testks.testtable"); AssertRowSetContainsCorrectValues(rs); var ps = session.Prepare("SELECT \"\", \" \" FROM testks.testtable WHERE \"\" = ? AND \" \" = ?"); var bs = ps.Bind("testval", "testval2"); rs = session.Execute(bs); AssertRowSetContainsCorrectValues(rs); } } }
/// <summary> /// Registers the defined /// </summary> /// <param name="config">Mapping configuration, usually MappingConfiguration.Global instance.</param> public static void Register(MappingConfiguration config) { config.Define <TableMapping>(); }