public void Token_Function_Constant_Linq_Test() { string query = null; object[] parameters = null; var session = GetSession((q, v) => { query = q; parameters = v; }); var table = GetTable <AllTypesEntity>(session, new Map <AllTypesEntity>().TableName("tbl1")); var token = CqlToken.Create("abc1", 200L); table.Where(t => CqlToken.Create(t.StringValue, t.Int64Value) > token).Select(t => new { t.IntValue }) .Execute(); Assert.AreEqual("SELECT IntValue FROM tbl1 WHERE token(StringValue, Int64Value) > token(?, ?)", query); Assert.AreEqual(token.Values, parameters); }
public void testPagination() { var table = ents.GetTable <Tweets>(); int RowsNb = 3000; for (int i = 0; i < RowsNb; i++) { table.AddNew(new Tweets() { tweet_id = Guid.NewGuid(), idx = i, isok = i % 2 == 0, author = "author" + i.ToString(), body = "bla bla bla" }); } ents.SaveChanges(SaveChangesMode.Batch); //test filtering var evens = (from ent in table where ent.isok == true select ent).Execute(); Assert.True(evens.All(ev => ev.idx % 2 == 0)); //test pagination int PerPage = 1234; var firstPage = (from ent in table select ent).Take(PerPage).Execute(); var continuation = CqlToken.Create(firstPage.Last().tweet_id); int pages = 1; int lastcnt = 0; while (true) { var nextPage = (from ent in table where CqlToken.Create(ent.tweet_id) > continuation select ent).Take(PerPage).Execute().ToList(); if (nextPage.Count < PerPage) { lastcnt = nextPage.Count; break; } continuation = CqlToken.Create(nextPage.Last().tweet_id); pages++; } Assert.Equal(pages, RowsNb / PerPage); Assert.Equal(lastcnt, RowsNb % PerPage); }
public void TestCqlFromLinq() { var table = SessionExtensions.GetTable <TestTable>(null); Assert.Equal( (from ent in table select ent).ToString(), @"SELECT * FROM ""x_t"" ALLOW FILTERING"); Assert.Equal( (from ent in table select ent.f1).ToString(), @"SELECT ""x_f1"" FROM ""x_t"" ALLOW FILTERING"); Assert.Equal( (from ent in table where ent.pk == "koko" select ent.f1).ToString(), @"SELECT ""x_f1"" FROM ""x_t"" WHERE ""x_pk"" = 'koko' ALLOW FILTERING"); Assert.Equal( (from ent in table where ent.pk == "koko" select new { ent.f1, ent.ck2 }).ToString(), @"SELECT ""x_f1"", ""x_ck2"" FROM ""x_t"" WHERE ""x_pk"" = 'koko' ALLOW FILTERING"); Assert.Equal( (from ent in table where ent.pk == "koko" && ent.ck2 == 10 select new { ent.f1, ent.ck2 }).ToString(), @"SELECT ""x_f1"", ""x_ck2"" FROM ""x_t"" WHERE ""x_pk"" = 'koko' AND ""x_ck2"" = 10 ALLOW FILTERING"); Assert.Equal( (from ent in table where ent.pk == "koko" && ent.ck2 == 10 select new { ent.f1, ent.ck2 }).Take(10).ToString(), @"SELECT ""x_f1"", ""x_ck2"" FROM ""x_t"" WHERE ""x_pk"" = 'koko' AND ""x_ck2"" = 10 LIMIT 10 ALLOW FILTERING"); Assert.Equal( (from ent in table where ent.pk == "koko" && ent.ck2 == 10 select new { ent.f1, ent.ck2 }).OrderBy(c => c.ck2).ToString(), @"SELECT ""x_f1"", ""x_ck2"" FROM ""x_t"" WHERE ""x_pk"" = 'koko' AND ""x_ck2"" = 10 ORDER BY ""x_ck2"" ASC ALLOW FILTERING"); Assert.Equal( (from ent in table where ent.pk == "koko" && ent.ck2 == 10 select new { ent.f1, ent.ck2, ent.ck1 }).OrderBy(c => c.ck2).OrderByDescending(c => c.ck1).ToString(), @"SELECT ""x_f1"", ""x_ck2"", ""x_ck1"" FROM ""x_t"" WHERE ""x_pk"" = 'koko' AND ""x_ck2"" = 10 ORDER BY ""x_ck2"" ASC, ""x_ck1"" DESC ALLOW FILTERING"); Assert.Equal( (from ent in table where ent.pk == "koko" && ent.ck2 == 10 select new { ent.f1, ent.ck2, ent.ck1 }).OrderBy(c => c.ck2).OrderByDescending(c => c.ck1).ToString(), @"SELECT ""x_f1"", ""x_ck2"", ""x_ck1"" FROM ""x_t"" WHERE ""x_pk"" = 'koko' AND ""x_ck2"" = 10 ORDER BY ""x_ck2"" ASC, ""x_ck1"" DESC ALLOW FILTERING"); Assert.Equal( (from ent in table where CqlToken.Create(ent.pk, ent.ck2, ent.ck2) > CqlToken.Create("x", 2) select new { ent.f1, ent.ck2, ent.ck1 }).OrderBy(c => c.ck2).OrderByDescending(c => c.ck1).ToString(), @"SELECT ""x_f1"", ""x_ck2"", ""x_ck1"" FROM ""x_t"" WHERE token(""x_pk"", ""x_ck2"", ""x_ck2"") > token('x', 2) ORDER BY ""x_ck2"" ASC, ""x_ck1"" DESC ALLOW FILTERING"); Assert.Equal( (from ent in table where ent.ck2 > ent.ck1 select ent).ToString(), @"SELECT * FROM ""x_t"" WHERE ""x_ck2"" > ""x_ck1"" ALLOW FILTERING"); Assert.Equal( (from ent in table select ent).Count().ToString(), @"SELECT count(*) FROM ""x_t"""); Assert.Equal( (from ent in table select ent).FirstOrDefault().ToString(), @"SELECT * FROM ""x_t"" LIMIT 1 ALLOW FILTERING"); Assert.Equal( (from ent in table select ent).First().ToString(), @"SELECT * FROM ""x_t"" LIMIT 1 ALLOW FILTERING"); Assert.Equal( (from ent in table select ent).Where(e => e.pk.CompareTo("a") > 0).First().ToString(), @"SELECT * FROM ""x_t"" WHERE ""x_pk"" > 'a' LIMIT 1 ALLOW FILTERING"); try { Assert.Equal( (from ent in table where ent.pk == "x" || ent.ck2 == 1 select ent).ToString(), @"?"); } catch (CqlLinqNotSupportedException) { } Assert.Equal( (from ent in table where new int[] { 10, 30, 40 }.Contains(ent.ck2) select ent).ToString(), @"SELECT * FROM ""x_t"" WHERE ""x_ck2"" IN (10, 30, 40) ALLOW FILTERING"); try { Assert.Equal( (from ent in table where new int[] { }.Contains(ent.ck2) select ent).ToString(), @"?"); } catch (CqlArgumentException) { } Assert.Equal( (from ent in table where new int[] { 10, 30, 40 }.Contains(ent.ck2) select ent).Delete().ToString(), @"DELETE FROM ""x_t"" WHERE ""x_ck2"" IN (10, 30, 40)"); Assert.Equal( (table.Insert(new TestTable() { ck1 = 1, ck2 = 2, f1 = 3, pk = "x" })).ToString(), @"INSERT INTO ""x_t""(""x_pk"", ""x_ck1"", ""x_ck2"", ""x_f1"") VALUES ('x', 1, 2, 3)"); try { Assert.Equal( (from ent in table where new int[] { 10, 30, 40 }.Contains(ent.ck2) select new { x = ent.pk, e = ent }).ToString(), @"?"); } catch (CqlLinqNotSupportedException) { } { var batch = SessionExtensions.CreateBatch(null); batch.Append(table.Insert(new TestTable() { ck1 = 1, ck2 = 2, f1 = 3, pk = "x" })); batch.Append((from ent in table where new int[] { 10, 30, 40 }.Contains(ent.ck2) select new { f1 = 1223 }).Update()); batch.Append((from ent in table where new int[] { 10, 30, 40 }.Contains(ent.ck2) select ent).Delete()); Assert.Equal(batch.ToString().Replace("\r", ""), @"BEGIN BATCH INSERT INTO ""x_t""(""x_pk"", ""x_ck1"", ""x_ck2"", ""x_f1"") VALUES ('x', 1, 2, 3); UPDATE ""x_t"" SET ""x_f1"" = 1223 WHERE ""x_ck2"" IN (10, 30, 40); DELETE FROM ""x_t"" WHERE ""x_ck2"" IN (10, 30, 40); APPLY BATCH".Replace("\r", "")); } { var batch = SessionExtensions.CreateBatch(null); batch.Append(table.Insert(new TestTable() { ck1 = 1, ck2 = 2, f1 = 3, pk = "x" })); batch.Append(table.Where(ent => new int[] { 10, 30, 40 }.Contains(ent.ck2)).Select(ent => new { f1 = 1223 }).Update()); batch.Append(table.Where(ent => new int[] { 10, 30, 40 }.Contains(ent.ck2)).Delete()); Assert.Equal(batch.ToString().Replace("\r", ""), @"BEGIN BATCH INSERT INTO ""x_t""(""x_pk"", ""x_ck1"", ""x_ck2"", ""x_f1"") VALUES ('x', 1, 2, 3); UPDATE ""x_t"" SET ""x_f1"" = 1223 WHERE ""x_ck2"" IN (10, 30, 40); DELETE FROM ""x_t"" WHERE ""x_ck2"" IN (10, 30, 40); APPLY BATCH".Replace("\r", "")); } }
public void TestCqlFromLinq() { var table = SessionExtensions.GetTable <LinqDecoratedEntity>(GetSession((_, __) => {})); Assert.AreEqual( (from ent in table select ent).ToString(), @"SELECT ""x_ck1"", ""x_ck2"", ""x_f1"", ""x_pk"" FROM ""x_t"" ALLOW FILTERING"); Assert.AreEqual( (from ent in table select ent.f1).ToString(), @"SELECT ""x_f1"" FROM ""x_t"" ALLOW FILTERING"); Assert.AreEqual( (from ent in table where ent.pk == "koko" select ent.f1).ToString(), @"SELECT ""x_f1"" FROM ""x_t"" WHERE ""x_pk"" = ? ALLOW FILTERING"); Assert.AreEqual( (from ent in table where ent.pk == "koko" select new { ent.f1, ent.ck2 }).ToString(), @"SELECT ""x_f1"", ""x_ck2"" FROM ""x_t"" WHERE ""x_pk"" = ? ALLOW FILTERING"); Assert.AreEqual( (from ent in table where ent.pk == "koko" && ent.ck2 == 10 select new { ent.f1, ent.ck2 }).ToString(), @"SELECT ""x_f1"", ""x_ck2"" FROM ""x_t"" WHERE ""x_pk"" = ? AND ""x_ck2"" = ? ALLOW FILTERING"); Assert.AreEqual( (from ent in table where ent.pk == "koko" && ent.ck2 == 10 select new { ent.f1, ent.ck2 }).Take(10).ToString(), @"SELECT ""x_f1"", ""x_ck2"" FROM ""x_t"" WHERE ""x_pk"" = ? AND ""x_ck2"" = ? LIMIT ? ALLOW FILTERING"); Assert.AreEqual( (from ent in table where ent.pk == "koko" && ent.ck2 == 10 select new { ent.f1, ent.ck2 }).OrderBy(c => c.ck2).ToString(), @"SELECT ""x_f1"", ""x_ck2"" FROM ""x_t"" WHERE ""x_pk"" = ? AND ""x_ck2"" = ? ORDER BY ""x_ck2"" ALLOW FILTERING"); Assert.AreEqual( (from ent in table where ent.pk == "koko" && ent.ck2 == 10 select new { ent.f1, ent.ck2, ent.ck1 }).OrderBy(c => c.ck2).OrderByDescending(c => c.ck1).ToString(), @"SELECT ""x_f1"", ""x_ck2"", ""x_ck1"" FROM ""x_t"" WHERE ""x_pk"" = ? AND ""x_ck2"" = ? ORDER BY ""x_ck2"", ""x_ck1"" DESC ALLOW FILTERING"); Assert.AreEqual( (from ent in table where ent.pk == "koko" && ent.ck2 == 10 select new { ent.f1, ent.ck2, ent.ck1 }).OrderBy(c => c.ck2).OrderByDescending(c => c.ck1).ToString(), @"SELECT ""x_f1"", ""x_ck2"", ""x_ck1"" FROM ""x_t"" WHERE ""x_pk"" = ? AND ""x_ck2"" = ? ORDER BY ""x_ck2"", ""x_ck1"" DESC ALLOW FILTERING"); Assert.AreEqual( (from ent in table where CqlToken.Create(ent.pk, ent.ck2, ent.ck2) > CqlToken.Create("x", 2) select new { ent.f1, ent.ck2, ent.ck1 }).OrderBy(c => c.ck2).OrderByDescending(c => c.ck1).ToString(), @"SELECT ""x_f1"", ""x_ck2"", ""x_ck1"" FROM ""x_t"" WHERE token(""x_pk"", ""x_ck2"", ""x_ck2"") > token(?, ?) ORDER BY ""x_ck2"", ""x_ck1"" DESC ALLOW FILTERING"); Assert.AreEqual( (from ent in table select ent).FirstOrDefault().ToString(), @"SELECT ""x_ck1"", ""x_ck2"", ""x_f1"", ""x_pk"" FROM ""x_t"" LIMIT ? ALLOW FILTERING"); Assert.AreEqual( (from ent in table select ent).First().ToString(), @"SELECT ""x_ck1"", ""x_ck2"", ""x_f1"", ""x_pk"" FROM ""x_t"" LIMIT ? ALLOW FILTERING"); Assert.AreEqual( (from ent in table select ent).Where(e => e.pk.CompareTo("a") > 0).First().ToString(), @"SELECT ""x_ck1"", ""x_ck2"", ""x_f1"", ""x_pk"" FROM ""x_t"" WHERE ""x_pk"" > ? LIMIT ? ALLOW FILTERING"); Assert.Throws <CqlLinqNotSupportedException>(() => (from ent in table where ent.pk == "x" || ent.ck2 == 1 select ent).ToString()); Assert.AreEqual( (from ent in table where new[] { 10, 30, 40 }.Contains(ent.ck2) select ent).ToString(), @"SELECT ""x_ck1"", ""x_ck2"", ""x_f1"", ""x_pk"" FROM ""x_t"" WHERE ""x_ck2"" IN ? ALLOW FILTERING"); Assert.AreEqual( @"SELECT ""x_ck1"", ""x_ck2"", ""x_f1"", ""x_pk"" FROM ""x_t"" WHERE ""x_ck2"" IN ? ALLOW FILTERING", (from ent in table where new int[] {}.Contains(ent.ck2) select ent).ToString()); Assert.AreEqual( (from ent in table where new int[] { 10, 30, 40 }.Contains(ent.ck2) select ent).Delete().ToString(), @"DELETE FROM ""x_t"" WHERE ""x_ck2"" IN ?"); Assert.AreEqual( @"INSERT INTO ""x_t"" (""x_ck1"", ""x_ck2"", ""x_f1"", ""x_pk"") VALUES (?, ?, ?, ?)", (table.Insert(new LinqDecoratedEntity() { ck1 = 1, ck2 = 2, f1 = 3, pk = "x" })).ToString()); Assert.Throws <CqlLinqNotSupportedException>(() => (from ent in table where new int[] { 10, 30, 40 }.Contains(ent.ck2) select new { x = ent.pk, e = ent }).ToString()); }
static void Main(string[] args) { Cluster cluster = Cluster.Builder().WithConnectionString("Contact Points=cassi.cloudapp.net;Port=9042").Build(); using (var session = cluster.Connect()) { string keyspaceName = "Excelsior" + Guid.NewGuid().ToString("N"); try { session.ChangeKeyspace(keyspaceName); } catch (InvalidQueryException) { session.CreateKeyspaceIfNotExists(keyspaceName); session.ChangeKeyspace(keyspaceName); } var table = session.GetTable <NerdMovie>(); table.CreateIfNotExists(); { var batch = session.CreateBatch(); var movies = new List <NerdMovie>() { new NerdMovie() { Movie = "Serenity", Maker = "20CentFox", Director = "Joss Whedon", MainActor = "Nathan Fillion", Year = 2005, exampleSet = new List <string>() { "x", "y" } }, new NerdMovie() { Movie = "Pulp Fiction", Maker = "Pixar", Director = "Quentin Tarantino", MainActor = "John Travolta", Year = 1994, exampleSet = new List <string>() { "1", "2", "3" } }, }; batch.Append(from m in movies select table.Insert(m)); batch.Execute(); } var testmovie = new NerdMovie { Year = 2005, Director = "Quentin Tarantino", Movie = "Pulp Fiction", Maker = "Pixar" }; table.Where(m => m.Movie == testmovie.Movie && m.Maker == testmovie.Maker && m.Director == testmovie.Director).Select(m => new NerdMovie { Year = testmovie.Year }).Update().Execute(); var anonMovie = new { Director = "Quentin Tarantino", Year = 2005 }; table.Where(m => m.Movie == "Pulp Fiction" && m.Maker == "Pixar" && m.Director == anonMovie.Director).Select(m => new NerdMovie { Year = anonMovie.Year, MainActor = "George Clooney" }).Update().Execute(); var all2 = table.Where((m) => CqlToken.Create(m.Movie, m.Maker) > CqlToken.Create("Pulp Fiction", "Pixar")).Execute().ToList(); var all = (from m in table where CqlToken.Create(m.Movie, m.Maker) > CqlToken.Create("Pulp Fiction", "Pixar") select m).Execute().ToList(); var nmT = (from m in table where m.Director == "Quentin Tarantino" select new ExtMovie { TheDirector = m.MainActor, Size = all.Count, TheMaker = m.Director }).Execute().ToList(); var nm1 = (from m in table where m.Director == "Quentin Tarantino" select new { MA = m.MainActor, Z = 10, Y = m.Year }).Execute().ToList(); var nmX = (from m in table where m.Director == "Quentin Tarantino" select new { m.MainActor, Z = 10, m.Year }).Execute().ToList(); (from m in table where m.Movie.Equals("Pulp Fiction") && m.Maker.Equals("Pixar") && m.Director == "Quentin Tarantino" select new NerdMovie { Year = 1994 }).Update().Execute(); table.Where((m) => m.Movie == "Pulp Fiction" && m.Maker == "Pixar" && m.Director == "Quentin Tarantino").Select((m) => new NerdMovie { Year = 1994 }).Update().Execute(); var nm2 = table.Where((m) => m.Director == "Quentin Tarantino").Select((m) => new { MA = m.MainActor, Y = m.Year }).Execute().ToList(); (from m in table where m.Movie == "Pulp Fiction" && m.Maker == "Pixar" && m.Director == "Quentin Tarantino" select m).Delete().Execute(); var nm3 = (from m in table where m.Director == "Quentin Tarantino" select new { MA = m.MainActor, Y = m.Year }).Execute().ToList(); session.DeleteKeyspaceIfExists(keyspaceName); } cluster.Shutdown(); }
public void LinqBatchInsertUpdateSelectTest() { Table <NerdMovie> table = Session.GetTable <NerdMovie>(); table.CreateIfNotExists(); { Batch batch = Session.CreateBatch(); var movies = new List <NerdMovie> { new NerdMovie { Movie = "Serenity", Maker = "20CentFox", Director = "Joss Whedon", MainActor = "Nathan Fillion", Year = 2005, exampleSet = new List <string> { "x", "y" } }, new NerdMovie { Movie = "Pulp Fiction", Maker = "Pixar", Director = "Quentin Tarantino", MainActor = "John Travolta", Year = 1994, exampleSet = new List <string> { "1", "2", "3" } }, }; batch.Append(from m in movies select table.Insert(m)); batch.Execute(); } var testmovie = new NerdMovie { Year = 2005, Director = "Quentin Tarantino", Movie = "Pulp Fiction", Maker = "Pixar" }; table.Where(m => m.Movie == testmovie.Movie && m.Maker == testmovie.Maker && m.Director == testmovie.Director) .Select(m => new NerdMovie { Year = testmovie.Year }) .Update() .Execute(); var anonMovie = new { Director = "Quentin Tarantino", Year = 2005 }; table.Where(m => m.Movie == "Pulp Fiction" && m.Maker == "Pixar" && m.Director == anonMovie.Director) .Select(m => new NerdMovie { Year = anonMovie.Year, MainActor = "George Clooney" }) .Update() .Execute(); List <NerdMovie> all2 = table.Where(m => CqlToken.Create(m.Movie, m.Maker) > CqlToken.Create("Pulp Fiction", "Pixar")).Execute().ToList(); List <NerdMovie> all = (from m in table where CqlToken.Create(m.Movie, m.Maker) > CqlToken.Create("Pulp Fiction", "Pixar") select m).Execute().ToList(); List <ExtMovie> nmT = (from m in table where m.Director == "Quentin Tarantino" select new ExtMovie { TheDirector = m.MainActor, Size = all.Count, TheMaker = m.Director }).Execute().ToList(); var nm1 = (from m in table where m.Director == "Quentin Tarantino" select new { MA = m.MainActor, Z = 10, Y = m.Year }).Execute().ToList(); var nmX = (from m in table where m.Director == "Quentin Tarantino" select new { m.MainActor, Z = 10, m.Year }).Execute().ToList(); (from m in table where m.Movie.Equals("Pulp Fiction") && m.Maker.Equals("Pixar") && m.Director == "Quentin Tarantino" select new NerdMovie { Year = 1994 }).Update().Execute(); table.Where(m => m.Movie == "Pulp Fiction" && m.Maker == "Pixar" && m.Director == "Quentin Tarantino") .Select(m => new NerdMovie { Year = 1994 }) .Update() .Execute(); var nm2 = table.Where(m => m.Director == "Quentin Tarantino").Select(m => new { MA = m.MainActor, Y = m.Year }).Execute().ToList(); (from m in table where m.Movie == "Pulp Fiction" && m.Maker == "Pixar" && m.Director == "Quentin Tarantino" select m).Delete().Execute(); var nm3 = (from m in table where m.Director == "Quentin Tarantino" select new { MA = m.MainActor, Y = m.Year }).Execute().ToList(); }
public void testBuffering() { var table = ents.GetTable <Tweets>(); var q2 = (from e in table where CqlToken.Create(e.idx) <= 0 select e).Take(10).OrderBy((e) => e.idx).ThenByDescending((e) => e.isok); var qxx = q2.ToString(); int RowsNb = 10; for (int i = 0; i < RowsNb; i++) { var ent = new Tweets() { tweet_id = Guid.NewGuid(), author = "author" + i.ToString(), isok = i % 2 == 0, body = "blablabla", idx = i }; table.AddNew(ent, EntityTrackingMode.KeepAttachedAfterSave); } var ent2 = new Tweets() { tweet_id = Guid.NewGuid(), author = "author" + RowsNb + 1, isok = false, body = "blablabla", idx = RowsNb + 1 }; table.AddNew(ent2, EntityTrackingMode.KeepAttachedAfterSave); ents.SaveChanges(SaveChangesMode.OneByOne); table.Attach(ent2, EntityUpdateMode.ModifiedOnly); ent2.author = "Koko"; ents.SaveChanges(SaveChangesMode.OneByOne); var iq = table.Count(); var c = iq.Execute(); foreach (var r in (from e in table where e.isok == true && e.idx == 0 select e).Execute()) { var x = r; } //https://issues.apache.org/jira/browse/CASSANDRA-5303?page=com.atlassian.streams.streams-jira-plugin:activity-stream-issue-tab //foreach (var r in (from e in table where e.isok == true && new int[] { 0, 1, 2 }.Contains(e.idx) select new { x = e.idx, y = e.tweet_id }).Execute()) //{ // var x = r; //} foreach (var r in (from e in table where e.isok == false && e.idx == 0 select new { Key = e.idx }).Execute()) { var x = r; } foreach (var r in (from e in table where e.isok == true && e.idx == 0 select new { Key = e.idx, e.isok }).Execute()) { var x = r; } foreach (var r in (from e in table where e.isok == true && e.idx == 0 select new X() { x = e.author, y = e.idx }).Execute()) { var x = r; } foreach (var r in (from e in table where e.isok == false && e.idx == 0 select e.author).Execute()) { var x = r; } }