Skip to content

Commit

Permalink
Ticket #25 : Can pass NULL value
Browse files Browse the repository at this point in the history
  • Loading branch information
thabart committed Sep 19, 2021
1 parent fe03584 commit 4d97223
Show file tree
Hide file tree
Showing 16 changed files with 378 additions and 9 deletions.
3 changes: 2 additions & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"sdk": {
"version": "5.0.103"
"version": "5.0.103",
"rollForward": "latestFeature"
}
}
2 changes: 1 addition & 1 deletion samples/EFCore.Cassandra.Samples/FakeDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void ConfigureDataxAstra(DbContextOptionsBuilder optionsBuilder)

private void ConfigureLocalDB(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseCassandra("Contact Points=127.0.0.1;", SCHEMA_NAME, opt =>
optionsBuilder.UseCassandra("Contact Points=127.0.0.1;Username=cassandra;Password=password", SCHEMA_NAME, opt =>
{
opt.MigrationsHistoryTable(HistoryRepository.DefaultTableName);
}, o => {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace EFCore.Cassandra.Samples.Migrations
{
public partial class NullableInteger : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.EnsureSchema(
name: "cv");

migrationBuilder.AddColumn<int>(
name: "NullableInteger",
schema: "cv",
table: "applicants",
type: "int",
nullable: true);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "NullableInteger",
schema: "cv",
table: "applicants");

migrationBuilder.EnsureSchema(
name: "cv");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<IList<int>>("LstInt")
.HasColumnType("list<int>");
b.Property<int?>("NullableInteger")
.HasColumnType("int");
b.Property<ApplicantPhone[]>("Phones")
.HasColumnType("list<frozen<ApplicantPhone>>");
Expand Down
1 change: 1 addition & 0 deletions samples/EFCore.Cassandra.Samples/Models/Applicant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Applicant
public double Double { get; set; }
public float Float { get; set; }
public int Integer { get; set; }
public Nullable<int> NullableInteger { get; set; }
public short SmallInt { get; set; }
public DateTimeOffset DateTimeOffset { get; set; }
public TimeUuid TimeUuid { get; set; }
Expand Down
54 changes: 54 additions & 0 deletions src/EFCore.Cassandra/Storage/BaseCassandraTypeMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) SimpleIdServer. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using System;
using System.Data;
using System.Data.Common;

namespace Microsoft.EntityFrameworkCore.Storage
{
public abstract class BaseCassandraTypeMapping : RelationalTypeMapping
{
public BaseCassandraTypeMapping(
string storeType,
Type clrType,
DbType? dbType)
: base(storeType, clrType, dbType)
{
}

protected BaseCassandraTypeMapping(RelationalTypeMappingParameters parameters) : base(parameters) { }

public override DbParameter CreateParameter(DbCommand command, string name, object value, bool? nullable = null)
{
var parameter = command.CreateParameter();
parameter.Direction = ParameterDirection.Input;
parameter.ParameterName = name;
value = ConvertUnderlyingEnumValueToEnum(value);
if (Converter != null)
{
value = Converter.ConvertToProvider(value);
}

parameter.Value = value ?? null;

if (nullable.HasValue)
{
parameter.IsNullable = nullable.Value;
}

if (DbType.HasValue)
{
parameter.DbType = DbType.Value;
}

ConfigureParameter(parameter);

return parameter;
}

private object? ConvertUnderlyingEnumValueToEnum(object? value)
=> value?.GetType().IsInteger() == true && ClrType.UnwrapNullableType().IsEnum
? Enum.ToObject(ClrType.UnwrapNullableType(), value)
: value;
}
}
18 changes: 18 additions & 0 deletions src/EFCore.Cassandra/Storage/CassandraBoolTypeMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) SimpleIdServer. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Microsoft.EntityFrameworkCore.Storage
{
public class CassandraBoolTypeMapping: BaseCassandraTypeMapping
{
public CassandraBoolTypeMapping(string storeType)
: base(storeType, typeof(bool), System.Data.DbType.Boolean)
{
}

protected CassandraBoolTypeMapping(RelationalTypeMappingParameters parameters) : base(parameters) { }

protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new CassandraBoolTypeMapping(parameters);
}
}
23 changes: 23 additions & 0 deletions src/EFCore.Cassandra/Storage/CassandraDecimalTypeMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) SimpleIdServer. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Microsoft.EntityFrameworkCore.Storage
{
public class CassandraDecimalTypeMapping : BaseCassandraTypeMapping
{
private const string DecimalFormatConst = "{0:0.0###########################}";

public CassandraDecimalTypeMapping(string storeType)
: base(storeType, typeof(decimal), System.Data.DbType.Decimal)
{
}

protected CassandraDecimalTypeMapping(RelationalTypeMappingParameters parameters) : base(parameters) { }

protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new CassandraDecimalTypeMapping(parameters);

protected override string SqlLiteralFormatString
=> DecimalFormatConst;
}
}
18 changes: 18 additions & 0 deletions src/EFCore.Cassandra/Storage/CassandraDoubleTypeMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) SimpleIdServer. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Microsoft.EntityFrameworkCore.Storage
{
public class CassandraDoubleTypeMapping : BaseCassandraTypeMapping
{
public CassandraDoubleTypeMapping(string storeType)
: base(storeType, typeof(double), System.Data.DbType.Double)
{
}

protected CassandraDoubleTypeMapping(RelationalTypeMappingParameters parameters) : base(parameters) { }

protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new CassandraDoubleTypeMapping(parameters);
}
}
24 changes: 24 additions & 0 deletions src/EFCore.Cassandra/Storage/CassandraFloatTypeMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) SimpleIdServer. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using System;
using System.Globalization;

namespace Microsoft.EntityFrameworkCore.Storage
{
public class CassandraFloatTypeMapping : BaseCassandraTypeMapping
{
public CassandraFloatTypeMapping(string storeType)
: base(storeType, typeof(float), System.Data.DbType.Single)
{
}

protected CassandraFloatTypeMapping(RelationalTypeMappingParameters parameters) : base(parameters) { }

protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new CassandraFloatTypeMapping(parameters);

protected override string GenerateNonNullSqlLiteral(object value)
=> Convert.ToSingle(value).ToString("R", CultureInfo.InvariantCulture);
}
}
18 changes: 18 additions & 0 deletions src/EFCore.Cassandra/Storage/CassandraIntTypeMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) SimpleIdServer. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Microsoft.EntityFrameworkCore.Storage
{
public class CassandraIntTypeMapping: BaseCassandraTypeMapping
{
public CassandraIntTypeMapping(string storeType)
: base(storeType, typeof(int), System.Data.DbType.Int32)
{
}

protected CassandraIntTypeMapping(RelationalTypeMappingParameters parameters) : base(parameters) { }

protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new CassandraIntTypeMapping(parameters);
}
}
18 changes: 18 additions & 0 deletions src/EFCore.Cassandra/Storage/CassandraLongTypeMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) SimpleIdServer. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Microsoft.EntityFrameworkCore.Storage
{
public class CassandraLongTypeMapping: BaseCassandraTypeMapping
{
public CassandraLongTypeMapping(string storeType)
: base(storeType, typeof(long), System.Data.DbType.Int64)
{
}

protected CassandraLongTypeMapping(RelationalTypeMappingParameters parameters) : base(parameters) { }

protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new CassandraLongTypeMapping(parameters);
}
}
Loading

0 comments on commit 4d97223

Please sign in to comment.