Skip to content

Commit

Permalink
update to support generics
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstonis committed Jan 20, 2022
1 parent 80ffbe6 commit b7e86e0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
25 changes: 25 additions & 0 deletions Tycho.UnitTests/TychoDbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1039,13 +1039,30 @@ public async Task TychoDb_CreateDataIndex_ShouldBeSuccessful (IJsonSerializer js

using var db =
BuildDatabaseConnection(jsonSerializer)
.AddTypeRegistration<TestClassD>()
.Connect ();

var successful = await db.CreateIndexAsync<TestClassD> (x => x.DoubleProperty, "double_index");

successful.Should ().Be (expected);
}

[DataTestMethod]
[DynamicData(nameof(JsonSerializers))]
public async Task TychoDb_CreateDataIndexWithGeneric_ShouldBeSuccessful(IJsonSerializer jsonSerializer)
{
var expected = true;

using var db =
BuildDatabaseConnection(jsonSerializer)
.AddTypeRegistration<TestClassG<object>>()
.Connect();

var successful = await db.CreateIndexAsync<TestClassG<object>>(x => x.Id, "id_index");

successful.Should().Be(expected);
}

[DataTestMethod]
[DynamicData(nameof(JsonSerializers))]
public async Task TychoDb_CreateDataIndexWithMultipleProperties_ShouldBeSuccessful(IJsonSerializer jsonSerializer)
Expand All @@ -1054,6 +1071,7 @@ public async Task TychoDb_CreateDataIndexWithMultipleProperties_ShouldBeSuccessf

using var db =
BuildDatabaseConnection(jsonSerializer)
.AddTypeRegistration<TestClassB>()
.Connect();

var successful = await db.CreateIndexAsync<TestClassB>(new Expression<Func<TestClassB, object>>[] { x => x.StringProperty, x => x.DoubleProperty, } , "string_double_index");
Expand Down Expand Up @@ -1389,6 +1407,13 @@ public class TestClassF
public TestClassD Value { get; set; }
}

public class TestClassG<T>
{
public T InnerClass { get; set; }

public Guid Id { get; set; }
}

public class Patient : ModelBase
{
public long PatientId { get; set; }
Expand Down
14 changes: 14 additions & 0 deletions Tycho/RegisteredTypeInformation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
Expand All @@ -23,6 +24,8 @@ public class RegisteredTypeInformation

public string TypeName { get; private set; }

public string SafeTypeName { get; private set; }

public string TypeNamespace { get; private set; }

public Type ObjectType { get; private set; }
Expand Down Expand Up @@ -103,6 +106,7 @@ public static RegisteredTypeInformation Create<T>()
RequiresIdMapping = true,
TypeFullName = type.FullName,
TypeName = type.Name,
SafeTypeName = GetSafeTypeName(type),
TypeNamespace = type.Namespace,
ObjectType = type
};
Expand All @@ -128,5 +132,15 @@ private static string GetExpressionMemberName(Expression method)

throw new TychoDbException("The provided expression is not valid member expression");
}

private static string GetSafeTypeName(Type type)
{
return
!type.IsGenericType || type.IsGenericTypeDefinition
? !type.IsGenericTypeDefinition
? type.Name
: type.Name.Replace('`', '_')
: $"{GetSafeTypeName(type.GetGenericTypeDefinition())}__{string.Join(',', type.GetGenericArguments().Select(x => GetSafeTypeName(x)))}__";
}
}
}
6 changes: 3 additions & 3 deletions Tycho/TychoDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private StringBuilder ReusableStringBuilder
}
}

public TychoDb(string dbPath, IJsonSerializer jsonSerializer, string dbName = "tycho_cache.db", string password = null, bool persistConnection = true, bool rebuildCache = false, bool requireTypeRegistration = false, uint? cacheSizeBytes = null)
public TychoDb(string dbPath, IJsonSerializer jsonSerializer, string dbName = "tycho_cache.db", string password = null, bool persistConnection = true, bool rebuildCache = false, bool requireTypeRegistration = true)
{
SQLitePCL.Batteries_V2.Init ();

Expand Down Expand Up @@ -1018,7 +1018,7 @@ public ValueTask<bool> CreateIndexAsync<TObj>(Expression<Func<TObj, object>> pro
CheckHasRegisteredType<TObj>();
}

return CreateIndexAsync(QueryPropertyPath.BuildPath(propertyPath), QueryPropertyPath.IsNumeric(propertyPath), typeof(TObj).Name, indexName, cancellationToken);
return CreateIndexAsync(QueryPropertyPath.BuildPath(propertyPath), QueryPropertyPath.IsNumeric(propertyPath), _registeredTypeInformation[typeof(TObj)].SafeTypeName, indexName, cancellationToken);
}

public ValueTask<bool> CreateIndexAsync(string propertyPathString, bool isNumeric, string objectTypeName, string indexName, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -1073,7 +1073,7 @@ public TychoDb CreateIndex<TObj>(Expression<Func<TObj, object>>[] propertyPaths,
{
var transaction = _connection.BeginTransaction(IsolationLevel.Serializable);

var fullIndexName = $"idx_{indexName}_{typeof(TObj).Name}";
var fullIndexName = $"idx_{indexName}_{_registeredTypeInformation[typeof(TObj)].SafeTypeName}";
try
{
if (!_persistConnection)
Expand Down

0 comments on commit b7e86e0

Please sign in to comment.