Skip to content

Commit

Permalink
IdentityProperty override KeyProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavAntonyuk committed Aug 25, 2021
1 parent d91d8fb commit 47abcb7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/MicroOrm.Dapper.Repositories/Config/MicroOrmConfig.cs
Expand Up @@ -23,8 +23,8 @@ public static class MicroOrmConfig
public static string TablePrefix { get; set; }

/// <summary>
/// Use Key attribute as Identity
/// Allow Key attribute as Identity if Identity is not set
/// </summary>
public static bool UseKeyAsIdentity { get; set; }
public static bool AllowKeyAsIdentity { get; set; }
}
}
Expand Up @@ -39,7 +39,12 @@ private void InitProperties()
KeySqlProperties = props.Where(p => p.GetCustomAttributes<KeyAttribute>().Any()).Select(p => new SqlPropertyMetadata(p)).ToArray();

// Use identity as key pattern
var identityProperty = props.FirstOrDefault(p => MicroOrmConfig.UseKeyAsIdentity ? p.GetCustomAttributes<KeyAttribute>().Any() : p.GetCustomAttributes<IdentityAttribute>().Any());
var identityProperty = props.FirstOrDefault(p => p.GetCustomAttributes<IdentityAttribute>().Any());
if (identityProperty == null && MicroOrmConfig.AllowKeyAsIdentity)
{
identityProperty = props.FirstOrDefault(p => p.GetCustomAttributes<KeyAttribute>().Any());
}

IdentitySqlProperty = identityProperty != null ? new SqlPropertyMetadata(identityProperty) : null;

var dateChangedProperty = props.FirstOrDefault(p => p.GetCustomAttributes<UpdatedAtAttribute>().Any());
Expand Down
Expand Up @@ -20,7 +20,7 @@ protected RepositoriesTests(IDbContext db, ITestOutputHelper testOutputHelper)
{
_db = db;
_testOutputHelper = testOutputHelper;
MicroOrmConfig.UseKeyAsIdentity = false;
MicroOrmConfig.AllowKeyAsIdentity = false;
}

[Fact]
Expand Down
Expand Up @@ -218,12 +218,12 @@ public static void BulkInsertOne()
[Fact]
public static void BulkInsertOneKeyAsIdentity()
{
MicroOrmConfig.UseKeyAsIdentity = true;
MicroOrmConfig.AllowKeyAsIdentity = true;
ISqlGenerator<AddressKeyAsIdentity> userSqlGenerator = new SqlGenerator<AddressKeyAsIdentity>(_sqlConnector, true);
var sqlQuery = userSqlGenerator.GetBulkInsert(new List<AddressKeyAsIdentity> { new AddressKeyAsIdentity() });

Assert.Equal("INSERT INTO [Addresses] ([Street], [CityId]) VALUES (@Street0, @CityId0)", sqlQuery.GetSql());
MicroOrmConfig.UseKeyAsIdentity = false;
MicroOrmConfig.AllowKeyAsIdentity = false;
}

[Fact]
Expand Down Expand Up @@ -373,14 +373,14 @@ public static void InsertQuoMarks()
}

[Fact]
public void Insert_UseKeyAsIdentity_QuoMarks()
public void Insert_AllowKeyAsIdentity_QuoMarks()
{
MicroOrmConfig.UseKeyAsIdentity = true;
MicroOrmConfig.AllowKeyAsIdentity = true;
ISqlGenerator<AddressKeyAsIdentity> userSqlGenerator = new SqlGenerator<AddressKeyAsIdentity>(_sqlConnector, true);
var sqlQuery = userSqlGenerator.GetInsert(new AddressKeyAsIdentity());

Assert.Equal("INSERT INTO [Addresses] ([Street], [CityId]) VALUES (@Street, @CityId) SELECT SCOPE_IDENTITY() AS [Id]", sqlQuery.GetSql());
MicroOrmConfig.UseKeyAsIdentity = false;
MicroOrmConfig.AllowKeyAsIdentity = false;
}

[Fact]
Expand Down
Expand Up @@ -103,14 +103,14 @@ public void Insert_QuoMarks()
}

[Fact]
public void Insert_UseKeyAsIdentity_QuoMarks()
public void Insert_AllowKeyAsIdentity_QuoMarks()
{
MicroOrmConfig.UseKeyAsIdentity = true;
MicroOrmConfig.AllowKeyAsIdentity = true;
ISqlGenerator<AddressKeyAsIdentity> userSqlGenerator = new SqlGenerator<AddressKeyAsIdentity>(_sqlConnector, true);
var sqlQuery = userSqlGenerator.GetInsert(new AddressKeyAsIdentity());

Assert.Equal("INSERT INTO `Addresses` (`Street`, `CityId`) VALUES (@Street, @CityId); SELECT CONVERT(LAST_INSERT_ID(), SIGNED INTEGER) AS `Id`", sqlQuery.GetSql());
MicroOrmConfig.UseKeyAsIdentity = false;
MicroOrmConfig.AllowKeyAsIdentity = false;
}

[Fact]
Expand Down

0 comments on commit 47abcb7

Please sign in to comment.