-
Notifications
You must be signed in to change notification settings - Fork 126
/
EntityDescriptor(TEntity).cs
76 lines (68 loc) · 3.06 KB
/
EntityDescriptor(TEntity).cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
namespace Dapper.FastCrud.EntityDescriptors
{
using System;
using System.Threading;
using Dapper.FastCrud.Mappings;
using Dapper.FastCrud.Mappings.Registrations;
using Dapper.FastCrud.SqlBuilders;
using Dapper.FastCrud.SqlBuilders.Dialects;
using Dapper.FastCrud.SqlStatements;
/// <summary>
/// Typed entity descriptor, capable of producing statement builders associated with default entity mappings.
/// </summary>
internal class EntityDescriptor<TEntity>:EntityDescriptor
{
private readonly Lazy<EntityRegistration> _defaultEntityMapping;
/// <summary>
/// Default constructor
/// </summary>
public EntityDescriptor()
:base(typeof(TEntity))
{
_defaultEntityMapping = new Lazy<EntityRegistration>(
()=> new AutoGeneratedEntityMapping<TEntity>().AutoGeneratedRegistration,
LazyThreadSafetyMode.PublicationOnly);
}
/// <summary>
/// Returns the sql statements for a single entity, attached to the default entity registration or an overriden entity registration if provided.
/// </summary>
public new ISqlStatements<TEntity> GetSqlStatements(EntityRegistration? entityRegistration = null)
{
return (ISqlStatements<TEntity>)base.GetSqlStatements(entityRegistration);
}
/// <summary>
/// Returns the default entity mapping registration.
/// </summary>
protected override EntityRegistration DefaultEntityMappingRegistration => _defaultEntityMapping.Value;
protected override GenericStatementSqlBuilder ConstructSqlBuilder(EntityRegistration entityRegistration)
{
GenericStatementSqlBuilder statementSqlBuilder;
switch (entityRegistration.Dialect)
{
case SqlDialect.MsSql:
statementSqlBuilder = new MsSqlBuilder(this, entityRegistration);
break;
case SqlDialect.SqlAnywhere:
statementSqlBuilder = new SqlAnywhereSqlBuilder(this, entityRegistration);
break;
case SqlDialect.MySql:
statementSqlBuilder = new MySqlBuilder(this, entityRegistration);
break;
case SqlDialect.PostgreSql:
statementSqlBuilder = new PostgreSqlBuilder(this, entityRegistration);
break;
case SqlDialect.SqLite:
statementSqlBuilder = new SqLiteBuilder(this, entityRegistration);
break;
default:
throw new NotSupportedException($"Dialect {entityRegistration.Dialect} is not supported");
}
return statementSqlBuilder;
}
protected override ISqlStatements ConstructSqlStatements(EntityRegistration entityRegistration)
{
var sqlBuilder = this.GetSqlBuilder(entityRegistration);
return new GenericSqlStatements<TEntity>(sqlBuilder);
}
}
}