Skip to content

Commit

Permalink
Simplify and Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Dec 29, 2023
1 parent 482ed90 commit f850380
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 49 deletions.
22 changes: 0 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,6 @@ A .NET document database interface for relational databases, because in SQL we (
[![NuGet](https://img.shields.io/nuget/v/YesSql.svg)](https://www.nuget.org/packages/YesSql)
[![MyGet](https://img.shields.io/myget/yessql/vpre/yessql.svg?label=MyGet)](https://www.myget.org/feed/yessql/package/nuget/yessql)

Dynamic index type support
-------------------

```c#
public class PropertyDynamicIndexProvider : IndexProvider<Property>
{
public override void Describe(DescribeContext<Property> context)
{
var propertyType = typeof(PropertyIndex);
context
.For(propertyType) //Specify a type directly
.Map(property => new PropertyIndex
{
Name = property.Name,
ForRent = property.ForRent,
IsOccupied = property.IsOccupied,
Location = property.Location
});
}
}
```

How does it work ?
-------------------

Expand Down
19 changes: 9 additions & 10 deletions src/YesSql.Abstractions/Indexes/DescribeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace YesSql.Indexes
{
public class DescribeContext<T> : IDescriptor
{
private readonly Dictionary<Type, List<IDescribeFor>> _describes = new Dictionary<Type, List<IDescribeFor>>();
private readonly Dictionary<Type, List<IDescribeFor>> _describes = [];

public IEnumerable<IndexDescriptor> Describe(params Type[] types)
{
Expand All @@ -32,30 +32,29 @@ public IMapFor<T, IIndex> For(Type indexType)

public IMapFor<T, TIndex> For<TIndex>() where TIndex : IIndex
{
return For<TIndex, object>();
return For<TIndex, object>(typeof(TIndex));
}

public IMapFor<T, TIndex> For<TIndex, TKey>() where TIndex : IIndex
{
return For<TIndex, object>(null);
return For<TIndex, object>(typeof(TIndex));
}

public IMapFor<T, TIndex> For<TIndex, TKey>(Type indexType) where TIndex : IIndex
{
ArgumentNullException.ThrowIfNull(indexType, nameof(indexType));

List<IDescribeFor> descriptors;

if (!_describes.TryGetValue(typeof(T), out descriptors))
{
descriptors = _describes[typeof(T)] = new List<IDescribeFor>();
descriptors = _describes[typeof(T)] = [];
}

var describeFor = new IndexDescriptor<T, TIndex, TKey>();

// if indexType is null , use default value :typeof(TIndex)
if (indexType != null)
var describeFor = new IndexDescriptor<T, TIndex, TKey>()
{
describeFor.IndexType = indexType;
}
IndexType = indexType,
};

descriptors.Add(describeFor);

Expand Down
24 changes: 9 additions & 15 deletions src/YesSql.Core/Commands/IndexCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ namespace YesSql.Commands
public abstract class IndexCommand : IIndexCommand
{
protected const string ParameterSuffix = "_$$$";
private const string _separator = ", ";

protected readonly IStore _store;

private static readonly ConcurrentDictionary<PropertyInfo, PropertyInfoAccessor> PropertyAccessors = new();
private static readonly ConcurrentDictionary<Type, PropertyInfo[]> TypeProperties = new();
private static readonly ConcurrentDictionary<string, PropertyInfo[]> TypeProperties = new();
private static readonly ConcurrentDictionary<CompoundKey, string> InsertsList = new();
private static readonly ConcurrentDictionary<CompoundKey, string> UpdatesList = new();

protected static PropertyInfo[] KeysProperties = new[] { typeof(IIndex).GetProperty("Id") };
protected static PropertyInfo[] KeysProperties = [typeof(IIndex).GetProperty("Id")];

public abstract int ExecutionOrder { get; }

Expand Down Expand Up @@ -67,20 +68,13 @@ protected static void GetProperties(DbCommand command, object item, string suffi

protected static PropertyInfo[] TypePropertiesCache(Type type)
{
if (TypeProperties.TryGetValue(type, out var pis))
if (TypeProperties.TryGetValue(type.FullName, out var pis))
{
return pis;
}

// Clean up cache entries with the same name and different type
var exists = TypeProperties.Keys.FirstOrDefault(x => x.FullName == type.FullName);
if (exists != null)
{
TypeProperties.Remove(exists, out _);
}

var properties = type.GetProperties().Where(IsWriteable).ToArray();
TypeProperties[type] = properties;
TypeProperties[type.FullName] = properties;
return properties;
}

Expand Down Expand Up @@ -109,7 +103,7 @@ protected string Inserts(Type type, ISqlDialect dialect)
sbColumnList.Append(dialect.QuoteForColumnName(property.Name));
if (i < allProperties.Length - 1)
{
sbColumnList.Append(", ");
sbColumnList.Append(_separator);
}
}

Expand All @@ -120,14 +114,14 @@ protected string Inserts(Type type, ISqlDialect dialect)
sbParameterList.Append("@").Append(property.Name).Append(ParameterSuffix);
if (i < allProperties.Length - 1)
{
sbParameterList.Append(", ");
sbParameterList.Append(_separator);
}
}

if (typeof(MapIndex).IsAssignableFrom(type))
{
// We can set the document id
sbColumnList.Append(", ").Append(dialect.QuoteForColumnName("DocumentId"));
sbColumnList.Append(_separator).Append(dialect.QuoteForColumnName("DocumentId"));
sbParameterList.Append(", @DocumentId").Append(ParameterSuffix);
}

Expand Down Expand Up @@ -171,7 +165,7 @@ protected string Updates(Type type, ISqlDialect dialect)
values.Append(dialect.QuoteForColumnName(property.Name) + " = @" + property.Name + ParameterSuffix);
if (i < allProperties.Length - 1)
{
values.Append(", ");
values.Append(_separator);
}
}

Expand Down
3 changes: 1 addition & 2 deletions test/YesSql.Tests/Indexes/PropertyIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ public class PropertyDynamicIndexProvider : IndexProvider<Property>
{
public override void Describe(DescribeContext<Property> context)
{
var propertyType = typeof(PropertyIndex);
context
.For(propertyType)
.For(typeof(PropertyIndex))
.Map(property => new PropertyIndex
{
Name = property.Name,
Expand Down

0 comments on commit f850380

Please sign in to comment.