Skip to content

Commit 60717df

Browse files
Ticket #3 : Query with specified condition
1 parent 93aabfd commit 60717df

11 files changed

Lines changed: 82 additions & 201 deletions

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<VersionPrefix>1.0.0</VersionPrefix>
3+
<VersionPrefix>2.0.0</VersionPrefix>
44
<Authors>SimpleIdServer</Authors>
55
<Owners>SimpleIdServer</Owners>
66
</PropertyGroup>

samples/EFCore.Cassandra.Samples/EFCore.Cassandra.Samples.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
44
<TargetFramework>netcoreapp3.1</TargetFramework>
@@ -9,4 +9,7 @@
99
<ItemGroup>
1010
<Folder Include="Migrations\" />
1111
</ItemGroup>
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
14+
</ItemGroup>
1215
</Project>

samples/EFCore.Cassandra.Samples/FakeDbContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2525
modelBuilder.ForCassandraAddKeyspace("cv", new KeyspaceReplicationSimpleStrategyClass(2));
2626
modelBuilder.Entity<Applicant>()
2727
.ToTable("applicants", "cv")
28-
.HasKey(p => new { p.Id, p.LastName });
28+
.HasKey(p => new { p.Id, p.Order });
2929
modelBuilder.Entity<Applicant>()
30-
.ForCassandraSetClusterColumns(s => new { s.LastName });
30+
.ForCassandraSetClusterColumns(s => new { s.Order });
3131
modelBuilder.Entity<Applicant>()
3232
.Property(p => p.TimeUuid)
3333
.HasConversion(new TimeUuidToGuidConverter());

samples/EFCore.Cassandra.Samples/Migrations/20200520135456_Init.Designer.cs renamed to samples/EFCore.Cassandra.Samples/Migrations/20200521175814_Init.Designer.cs

Lines changed: 14 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/EFCore.Cassandra.Samples/Migrations/20200520135456_Init.cs renamed to samples/EFCore.Cassandra.Samples/Migrations/20200521175814_Init.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ protected override void Up(MigrationBuilder migrationBuilder)
2020
columns: table => new
2121
{
2222
id = table.Column<Guid>(nullable: false),
23-
LastName = table.Column<string>(nullable: false),
23+
Order = table.Column<int>(nullable: false),
24+
ApplicantId = table.Column<Guid>(nullable: false),
25+
LastName = table.Column<string>(nullable: true),
2426
Long = table.Column<long>(nullable: false),
2527
Bool = table.Column<bool>(nullable: false),
2628
Decimal = table.Column<decimal>(nullable: false),
@@ -42,7 +44,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
4244
},
4345
constraints: table =>
4446
{
45-
table.PrimaryKey("PK_applicants", x => new { x.id, x.LastName });
47+
table.PrimaryKey("PK_applicants", x => new { x.id, x.Order });
4648
});
4749

4850
migrationBuilder.CreateTable(
@@ -51,6 +53,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
5153
columns: table => new
5254
{
5355
Id = table.Column<Guid>(nullable: false),
56+
CvId = table.Column<Guid>(nullable: false),
5457
Name = table.Column<string>(nullable: true)
5558
},
5659
constraints: table =>

samples/EFCore.Cassandra.Samples/Migrations/FakeDbContextModelSnapshot.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ protected override void BuildModel(ModelBuilder modelBuilder)
2727
.HasColumnName("id")
2828
.HasColumnType("uuid");
2929

30-
b.Property<string>("LastName")
31-
.HasColumnType("text");
30+
b.Property<int>("Order")
31+
.HasColumnType("int");
32+
33+
b.Property<Guid>("ApplicantId")
34+
.HasColumnType("uuid");
3235

3336
b.Property<BigInteger>("BigInteger")
3437
.HasColumnType("varint");
@@ -60,6 +63,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
6063
b.Property<IPAddress>("Ip")
6164
.HasColumnType("inet");
6265

66+
b.Property<string>("LastName")
67+
.HasColumnType("text");
68+
6369
b.Property<LocalDate>("LocalDate")
6470
.HasColumnType("date");
6571

@@ -84,11 +90,11 @@ protected override void BuildModel(ModelBuilder modelBuilder)
8490
b.Property<Guid>("TimeUuid")
8591
.HasColumnType("uuid");
8692

87-
b.HasKey("Id", "LastName");
93+
b.HasKey("Id", "Order");
8894

8995
b.ToTable("applicants","cv");
9096

91-
b.HasAnnotation("Cassandra:ClusterColumns", new[] { "LastName" });
97+
b.HasAnnotation("Cassandra:ClusterColumns", new[] { "Order" });
9298
});
9399

94100
modelBuilder.Entity("EFCore.Cassandra.Samples.Models.CV", b =>
@@ -97,6 +103,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
97103
.ValueGeneratedOnAdd()
98104
.HasColumnType("uuid");
99105

106+
b.Property<Guid>("CvId")
107+
.HasColumnType("uuid");
108+
100109
b.Property<string>("Name")
101110
.HasColumnType("text");
102111

samples/EFCore.Cassandra.Samples/Models/Applicant.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace EFCore.Cassandra.Samples.Models
1111
public class Applicant
1212
{
1313
public Guid Id { get; set; }
14+
public int Order { get; set; }
15+
public Guid ApplicantId { get; set; }
1416
public string LastName { get; set; }
1517
public long Long { get; set; }
1618
public bool Bool { get; set; }

samples/EFCore.Cassandra.Samples/Models/CV.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace EFCore.Cassandra.Samples.Models
77
public class CV
88
{
99
public Guid Id { get; set; }
10+
public Guid CvId { get; set; }
1011
public string Name { get; set; }
1112
}
1213
}

samples/EFCore.Cassandra.Samples/Program.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ namespace EFCore.Cassandra.Samples
1111
{
1212
class Program
1313
{
14+
private static Guid ApplicantPartitionId = Guid.Parse("be2106c5-791f-45d2-890a-50fc221f96e8");
15+
private static Guid ApplicantId = Guid.Parse("09e0f68e-8818-452a-9a47-3c8ca2c941c8");
16+
1417
static void Main(string[] args)
1518
{
1619
using (var dbContext = new FakeDbContext())
@@ -19,26 +22,38 @@ static void Main(string[] args)
1922
var timeUuid = TimeUuid.NewId();
2023
dbContext.Applicants.Add(BuildApplicant());
2124
dbContext.SaveChanges();
25+
Console.WriteLine("Applicant is added");
2226

2327
var appls = dbContext.Applicants.ToList();
28+
Console.WriteLine($"Number of applicants '{dbContext.Applicants.LongCount()}'");
29+
30+
Console.WriteLine("Get applicants by partition key");
31+
var filteredApplicants = dbContext.Applicants.Where(_ => _.Id == ApplicantPartitionId).ToList();
32+
Console.WriteLine($"Number of applicants '{filteredApplicants.Count}'");
2433

25-
Console.WriteLine($"Number of applicants : {dbContext.Applicants.LongCount()}");
34+
Console.WriteLine("Order applicants by 'order'");
35+
var orderedApplicants = dbContext.Applicants.Where(_ => _.Id == ApplicantPartitionId).OrderBy(_ => _.Order).ToList();
36+
Console.WriteLine($"Number of applicants {orderedApplicants.Count}");
2637

2738
Console.WriteLine("Update the applicant");
2839
var applicant = dbContext.Applicants.First();
40+
applicant = dbContext.Applicants.First();
2941
applicant.Decimal = 10;
3042
applicant.Dic = new Dictionary<string, string>
3143
{
3244
{ "toto", "toto" }
3345
};
3446
dbContext.SaveChanges();
47+
Console.WriteLine("Applicant is updated");
3548

3649
Console.WriteLine("Remove the applicant");
3750
applicant = dbContext.Applicants.First();
3851
dbContext.Applicants.Remove(applicant);
3952
dbContext.SaveChanges();
53+
Console.WriteLine("Applicant is removed");
4054

41-
Console.WriteLine($"Number of applicants : {dbContext.Applicants.LongCount()}");
55+
Console.WriteLine($"Number of applicants '{dbContext.Applicants.LongCount()}'");
56+
Console.ReadLine();
4257
}
4358
}
4459

@@ -47,7 +62,9 @@ private static Applicant BuildApplicant()
4762
var timeUuid = TimeUuid.NewId();
4863
return new Applicant
4964
{
50-
Id = Guid.NewGuid(),
65+
Id = ApplicantPartitionId,
66+
ApplicantId = ApplicantId,
67+
Order = 0,
5168
Lst = new List<string>
5269
{
5370
"1",

src/EFCore.Cassandra/Query/CassandraQuerySqlGenerator.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@ protected override Expression VisitSelect(SelectExpression selectExpression)
141141
return selectExpression;
142142
}
143143

144+
protected override Expression VisitSqlParameter(SqlParameterExpression sqlParameterExpression)
145+
{
146+
// Sql.Append(" ? ");
147+
// return sqlParameterExpression;
148+
var parameterNameInCommand = _sqlGenerationHelper.GenerateParameterName(sqlParameterExpression.Name).Replace("_", "").TrimStart(':');
149+
150+
if (Sql.Parameters.All(p => p.InvariantName != sqlParameterExpression.Name))
151+
{
152+
Sql.AddParameter(
153+
sqlParameterExpression.Name,
154+
parameterNameInCommand,
155+
sqlParameterExpression.TypeMapping,
156+
sqlParameterExpression.Type.IsNullableType());
157+
}
158+
159+
// Sql.Append(_sqlGenerationHelper.GenerateParameterNamePlaceholder(sqlParameterExpression.Name));
160+
Sql.Append($" :{parameterNameInCommand} ");
161+
return sqlParameterExpression;
162+
}
163+
144164
protected override void GenerateLimitOffset(SelectExpression selectExpression)
145165
{
146166
if (selectExpression.Limit != null)

0 commit comments

Comments
 (0)