-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
558 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Microsoft.Data.Entity/Identity/GuidIdentityGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Data.Entity.Identity | ||
{ | ||
public class GuidIdentityGenerator : IIdentityGenerator<Guid> | ||
{ | ||
public virtual Task<Guid> NextAsync() | ||
{ | ||
return Task.FromResult(Guid.NewGuid()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. | ||
|
||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Data.Entity.Identity | ||
{ | ||
public interface IIdentityGenerator<T> | ||
{ | ||
Task<T> NextAsync(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Data.Entity.Identity; | ||
|
||
namespace Microsoft.Data.InMemory | ||
{ | ||
public class InMemoryIdentityGenerator : IIdentityGenerator<long> | ||
{ | ||
private static long _current; | ||
|
||
public Task<long> NextAsync() | ||
{ | ||
return Task.FromResult(Interlocked.Increment(ref _current)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/Microsoft.Data.SqlServer/SequenceIdentityGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. | ||
|
||
using JetBrains.Annotations; | ||
#if NET45 | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
using System.Globalization; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Data.Entity.Identity; | ||
|
||
namespace Microsoft.Data.SqlServer | ||
{ | ||
/// <remarks>Not threadsafe</remarks> | ||
public class SequenceIdentityGenerator : IIdentityGenerator<long> | ||
{ | ||
public const string DefaultSequenceName = "EF_IdentityGenerator"; | ||
|
||
private readonly string _connectionString; | ||
private readonly int _increment; | ||
private readonly string _name; | ||
private readonly string _schema; | ||
private readonly string _schemaSql; | ||
|
||
private long _current; | ||
private long _max; | ||
|
||
public SequenceIdentityGenerator([NotNull] string connectionString) | ||
: this(connectionString, 10) | ||
{ | ||
} | ||
|
||
public SequenceIdentityGenerator([NotNull] string connectionString, int increment) | ||
: this(connectionString, increment, DefaultSequenceName) | ||
{ | ||
} | ||
|
||
public SequenceIdentityGenerator([NotNull] string connectionString, int increment, [NotNull] string name) | ||
: this(connectionString, increment, name, null) | ||
{ | ||
} | ||
|
||
public SequenceIdentityGenerator( | ||
[NotNull] string connectionString, int increment, [NotNull] string name, [CanBeNull] string schema) | ||
{ | ||
_connectionString = connectionString; | ||
_increment = increment; | ||
_name = name; | ||
_schema = schema; | ||
_schemaSql = !string.IsNullOrWhiteSpace(_schema) ? "[" + _schema + "]." : string.Empty; | ||
} | ||
|
||
public virtual async Task<long> NextAsync() | ||
{ | ||
if (_current == _max) | ||
{ | ||
using (var connection = new SqlConnection(_connectionString)) | ||
{ | ||
await connection.OpenAsync(); | ||
|
||
using (var command = connection.CreateCommand()) | ||
{ | ||
command.CommandText | ||
= string.Format( | ||
CultureInfo.InvariantCulture, | ||
"SELECT NEXT VALUE FOR {1}[{0}]", | ||
_name, | ||
_schemaSql); | ||
|
||
_current = (long)await command.ExecuteScalarAsync(); | ||
_max = _current + _increment; | ||
} | ||
} | ||
} | ||
|
||
return Interlocked.Increment(ref _current); | ||
} | ||
|
||
public virtual async Task EnsureSequenceAsync() | ||
{ | ||
using (var connection = new SqlConnection(_connectionString)) | ||
{ | ||
await connection.OpenAsync(); | ||
|
||
using (var transaction = connection.BeginTransaction(IsolationLevel.Serializable)) | ||
{ | ||
using (var command = new SqlCommand()) | ||
{ | ||
command.Connection = connection; | ||
command.Transaction = transaction; | ||
command.CommandText | ||
= string.Format( | ||
CultureInfo.InvariantCulture, | ||
@"IF NOT EXISTS (SELECT * FROM sys.sequences WHERE name = N'{0}'{1}) | ||
CREATE SEQUENCE {2}[{0}] AS bigint START WITH 0 INCREMENT BY {3}", | ||
_name, | ||
!string.IsNullOrWhiteSpace(_schema) | ||
? " AND schema_id = SCHEMA_ID(N'" + _schema + "')" | ||
: string.Empty, | ||
_schemaSql, | ||
_increment); | ||
|
||
await command.ExecuteNonQueryAsync(); | ||
} | ||
|
||
transaction.Commit(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#endif |
36 changes: 36 additions & 0 deletions
36
src/Microsoft.Data.SqlServer/SequentialGuidIdentityGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Data.Entity.Identity; | ||
|
||
namespace Microsoft.Data.SqlServer | ||
{ | ||
public class SequentialGuidIdentityGenerator : IIdentityGenerator<Guid> | ||
{ | ||
private static long _counter; | ||
|
||
static SequentialGuidIdentityGenerator() | ||
{ | ||
_counter = DateTime.Now.Ticks; | ||
} | ||
|
||
public Task<Guid> NextAsync() | ||
{ | ||
var guidBytes = Guid.NewGuid().ToByteArray(); | ||
var counterBytes = BitConverter.GetBytes(Interlocked.Increment(ref _counter)); | ||
|
||
guidBytes[08] = counterBytes[1]; | ||
guidBytes[09] = counterBytes[0]; | ||
guidBytes[10] = counterBytes[7]; | ||
guidBytes[11] = counterBytes[6]; | ||
guidBytes[12] = counterBytes[5]; | ||
guidBytes[13] = counterBytes[4]; | ||
guidBytes[14] = counterBytes[3]; | ||
guidBytes[15] = counterBytes[2]; | ||
|
||
return Task.FromResult(new Guid(guidBytes)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
test/Microsoft.Data.Entity.UnitTest/ApiConsistencyFacts.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. | ||
|
||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.Data.Entity.Metadata; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.Entity | ||
{ | ||
public class ApiConsistencyTest : ApiConsistencyTestBase | ||
{ | ||
[Fact] | ||
public void Fluent_api_methods_should_not_return_void() | ||
{ | ||
var fluentApiTypes = new[] { typeof(ModelBuilder) }; | ||
|
||
var voidMethods | ||
= from t in GetAllTypes(fluentApiTypes) | ||
where t.IsVisible | ||
from m in t.GetMethods(PublicInstance) | ||
where m.DeclaringType != null | ||
&& m.DeclaringType.Assembly == TargetAssembly | ||
&& m.ReturnType == typeof(void) | ||
select t.Name + "." + m.Name; | ||
|
||
Assert.Equal("", string.Join("\r\n", voidMethods)); | ||
} | ||
|
||
protected override Assembly TargetAssembly | ||
{ | ||
get { return typeof(Metadata.Entity).Assembly; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.