Skip to content

Commit

Permalink
Support long ints (#458)
Browse files Browse the repository at this point in the history
Add UseLegacyIdentityColumn option
  • Loading branch information
sebastienros committed Sep 29, 2022
1 parent 97d05ef commit b2f85e8
Show file tree
Hide file tree
Showing 50 changed files with 285 additions and 364 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Expand Up @@ -75,7 +75,10 @@ jobs:
run: dotnet test --configuration Release --filter YesSql.Tests.MySqlTests --no-restore --no-build --framework net6.0

- name: Test - SQL Server 2019 .NET 6.0
run: dotnet test --configuration release --filter yessql.tests.sqlserver2019tests --no-restore --no-build --framework net6.0
run: dotnet test --configuration release --filter YesSql.Tests.SqlServer2019Tests --no-restore --no-build --framework net6.0

- name: Test - PostgresQL Legacy Identity - No Schema
run: dotnet test --configuration release --filter YesSql.Tests.PostgreSqlLegacyIdentityTests --no-restore --no-build --framework net6.0

- name: Pack with dotnet
run: dotnet pack --output artifacts --configuration Release --no-restore --no-build -p:PackageVersion=$GITHUB_RUN_NUMBER
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/pr.yml
Expand Up @@ -76,4 +76,7 @@ jobs:
run: dotnet test --configuration Release --filter YesSql.Tests.MySqlTests --no-restore --no-build --framework net6.0

- name: Test - SQL Server 2019 .NET 6.0
run: dotnet test --configuration release --filter yessql.tests.sqlserver2019tests --no-restore --no-build --framework net6.0
run: dotnet test --configuration release --filter YesSql.Tests.SqlServer2019Tests --no-restore --no-build --framework net6.0

- name: Test - PostgresQL Legacy Identity - No Schema
run: dotnet test --configuration release --filter YesSql.Tests.PostgreSqlLegacyIdentityTests --no-restore --no-build --framework net6.0
5 changes: 4 additions & 1 deletion .github/workflows/publish.yml
Expand Up @@ -65,7 +65,10 @@ jobs:
run: dotnet test --configuration Release --filter YesSql.Tests.MySqlTests --no-restore --no-build --framework net6.0

- name: Test - SQL Server 2019 .NET 6.0
run: dotnet test --configuration Release --filter YesSql.Tests.SqlServer2019Tests --no-restore --no-build --framework net6.0
run: dotnet test --configuration release --filter YesSql.Tests.SqlServer2019Tests --no-restore --no-build --framework net6.0

- name: Test - PostgresQL Legacy Identity - No Schema
run: dotnet test --configuration release --filter YesSql.Tests.PostgreSqlLegacyIdentityTests --no-restore --no-build --framework net6.0

- name: Pack
run: |
Expand Down
2 changes: 1 addition & 1 deletion samples/YesSql.Bench/Program.cs
Expand Up @@ -72,7 +72,7 @@ static async Task MainAsync(string[] args)

public class User
{
public int Id { get; set; }
public long Id { get; set; }
public string Name { get; set; }
public bool Adult { get; set; }
public int Age { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions samples/YesSql.Samples.Web/Models/BlogPost.cs
@@ -1,10 +1,10 @@
using System;
using System;

namespace YesSql.Samples.Web.Models
{
public class BlogPost
{
public int Id { get; set; }
public long Id { get; set; }

public string Title { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Copyright>Sebastien Ros</Copyright>
<Authors>Sebastien Ros</Authors>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DebugType>portable</DebugType>
<PackageProjectUrl>https://github.com/sebastienros/yessql</PackageProjectUrl>
Expand Down
2 changes: 1 addition & 1 deletion src/YesSql.Abstractions/Commands/ICreateTableCommand.cs
@@ -1,11 +1,11 @@
using System;
using System.Data;

namespace YesSql.Sql.Schema
{
public interface ICreateTableCommand : ISchemaCommand
{
ICreateTableCommand Column(string columnName, Type dbType, Action<ICreateColumnCommand> column = null);
ICreateTableCommand Column<T>(string columnName, Action<ICreateColumnCommand> column = null);
public ICreateTableCommand Column(bool useInt64Type, string columnName, Action<ICreateColumnCommand> column = null) => useInt64Type ? Column<int>(columnName, column) : Column<long>(columnName, column);
}
}
2 changes: 1 addition & 1 deletion src/YesSql.Abstractions/Document.cs
Expand Up @@ -8,7 +8,7 @@ public class Document
/// <summary>
/// The unique identifier of the document in the database.
/// </summary>
public int Id { get; set; }
public long Id { get; set; }

/// <summary>
/// The type of the document.
Expand Down
5 changes: 5 additions & 0 deletions src/YesSql.Abstractions/IConfiguration.cs
Expand Up @@ -85,6 +85,11 @@ public interface IConfiguration
/// Gets or sets the <see cref="ISqlDialect" /> instance.
/// </summary>
ISqlDialect SqlDialect { get; set; }

/// <summary>
/// Whether to use legacy Int64 identity columns.
/// </summary>
bool UseLegacyIdentityColumn { get; set; }
}

public static class ConfigurationExtensions
Expand Down
12 changes: 9 additions & 3 deletions src/YesSql.Abstractions/ISession.cs
Expand Up @@ -39,7 +39,7 @@ public interface ISession : IDisposable, IAsyncDisposable
/// <returns>
/// <c>true</c> if the object was imported, <c>false</c> otherwise.
/// </returns>
bool Import(object item, int id, int version, string collection = null);
bool Import(object item, long id, long version, string collection = null);

/// <summary>
/// Removes an item from the identity map.
Expand All @@ -55,7 +55,7 @@ public interface ISession : IDisposable, IAsyncDisposable
/// Loads objects by id.
/// </summary>
/// <returns>A collection of objects in the same order they were defined.</returns>
Task<IEnumerable<T>> GetAsync<T>(int[] ids, string collection = null) where T : class;
Task<IEnumerable<T>> GetAsync<T>(long[] ids, string collection = null) where T : class;

/// <summary>
/// Creates a new <see cref="IQuery"/> object.
Expand Down Expand Up @@ -135,11 +135,17 @@ public static class SessionExtensions
/// Loads an object by its id.
/// </summary>
/// <returns>The object or <c>null</c>.</returns>
public async static Task<T> GetAsync<T>(this ISession session, int id, string collection = null) where T : class
public async static Task<T> GetAsync<T>(this ISession session, long id, string collection = null) where T : class
{
return (await session.GetAsync<T>(new[] { id }, collection)).FirstOrDefault();
}

/// <summary>
/// Loads objects by id.
/// </summary>
/// <returns>A collection of objects in the same order they were defined.</returns>
public static Task<IEnumerable<T>> GetAsync<T>(this ISession session, int[] ids, string collection = null) where T : class => session.GetAsync<T>(ids.Select(x => (long)x).ToArray(), collection);

/// <summary>
/// Imports an object in the local identity map.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/YesSql.Abstractions/ISqlDialect.cs
Expand Up @@ -90,6 +90,11 @@ public interface ISqlDialect
/// </summary>
string IdentityColumnString { get; }

/// <summary>
/// Gets the Int64 primary key with identity column SQL statement.
/// </summary>
string LegacyIdentityColumnString { get; }

/// <summary>
/// Gets the identity select SQL statement to append to an insert in order to return the last generated identifier.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/YesSql.Abstractions/Indexes/IIndex.cs
@@ -1,10 +1,10 @@
using System.Collections.Generic;
using System.Collections.Generic;

namespace YesSql.Indexes
{
public interface IIndex
{
int Id { get; set; }
long Id { get; set; }
void AddDocument(Document document);
void RemoveDocument(Document document);
IEnumerable<Document> GetAddedDocuments();
Expand Down
4 changes: 2 additions & 2 deletions src/YesSql.Abstractions/Indexes/MapIndex.cs
@@ -1,12 +1,12 @@
using System.Collections.Generic;
using System.Collections.Generic;

namespace YesSql.Indexes
{
public abstract class MapIndex : IIndex
{
private Document Document { get; set; }

public int Id { get; set; }
public long Id { get; set; }

void IIndex.AddDocument(Document document)
{
Expand Down
4 changes: 2 additions & 2 deletions src/YesSql.Abstractions/Indexes/ReduceIndex.cs
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;

namespace YesSql.Indexes
{
Expand All @@ -9,7 +9,7 @@ public ReduceIndex()
Documents = new List<Document>();
}

public int Id { get; set; }
public long Id { get; set; }

List<Document> RemovedDocuments = new List<Document>();

Expand Down
8 changes: 4 additions & 4 deletions src/YesSql.Abstractions/Storage/DocumentIdentity.cs
@@ -1,24 +1,24 @@
using System;
using System;

namespace YesSql.Storage
{
public class DocumentIdentity : IIdentityEntity
{
public DocumentIdentity(int id, object entity)
public DocumentIdentity(long id, object entity)
{
Id = id;
Entity = entity;
EntityType = entity.GetType();
}

public DocumentIdentity(int id, Type type)
public DocumentIdentity(long id, Type type)
{
Id = id;
Entity = null;
EntityType = type;
}

public int Id { get; set; }
public long Id { get; set; }
public object Entity { get; set; }
public Type EntityType { get; set; }
}
Expand Down
4 changes: 2 additions & 2 deletions src/YesSql.Abstractions/Storage/IIdentityEntity.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -8,7 +8,7 @@ namespace YesSql.Storage
{
public interface IIdentityEntity
{
int Id { get; set; }
long Id { get; set; }
object Entity { get; set; }
Type EntityType { get; set; }
}
Expand Down
4 changes: 0 additions & 4 deletions src/YesSql.Abstractions/YesSql.Abstractions.csproj
@@ -1,8 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="System.Memory" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<!-- Latest minimum LTS version. We don't want to force a higher version to applications -->
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.0" />
Expand Down
10 changes: 5 additions & 5 deletions src/YesSql.Core/Commands/CreateIndexCommand.cs
Expand Up @@ -11,13 +11,13 @@ namespace YesSql.Commands
{
public sealed class CreateIndexCommand : IndexCommand
{
private readonly int[] _addedDocumentIds;
private readonly long[] _addedDocumentIds;

public override int ExecutionOrder { get; } = 2;

public CreateIndexCommand(
IIndex index,
IEnumerable<int> addedDocumentIds,
IEnumerable<long> addedDocumentIds,
IStore store,
string collection) : base(index, store, collection)
{
Expand All @@ -44,11 +44,11 @@ public override async Task ExecuteAsync(DbConnection connection, DbTransaction t
command.CommandText = sql;
GetProperties(command, Index, "", dialect);
command.AddParameter($"DocumentId", Index.GetAddedDocuments().Single().Id);
Index.Id = Convert.ToInt32(await command.ExecuteScalarAsync());
Index.Id = Convert.ToInt64(await command.ExecuteScalarAsync());
}
else
{
Index.Id = await connection.ExecuteScalarAsync<int>(sql, Index, transaction);
Index.Id = await connection.ExecuteScalarAsync<long>(sql, Index, transaction);

var reduceIndex = Index as ReduceIndex;
var bridgeTableName = _store.Configuration.TableNameConvention.GetIndexTable(type, Collection) + "_" + documentTable;
Expand Down Expand Up @@ -93,7 +93,7 @@ public override bool AddToBatch(ISqlDialect dialect, List<string> queries, DbCom
actions.Add(dr =>
{
dr.Read();
Index.Id = Convert.ToInt32(dr[0]);
Index.Id = Convert.ToInt64(dr[0]);
dr.NextResult();
});

Expand Down
2 changes: 1 addition & 1 deletion src/YesSql.Core/Commands/DeleteDocumentCommand.cs
Expand Up @@ -37,7 +37,7 @@ public override bool AddToBatch(ISqlDialect dialect, List<string> queries, DbCom

var deleteCmd = $"delete from {dialect.QuoteForTableName(_store.Configuration.TablePrefix + documentTable, _store.Configuration.Schema)} where {dialect.QuoteForColumnName("Id")} = @Id_{index};";
queries.Add(deleteCmd);
command.AddParameter($"Id_{index}", Document.Id, DbType.Int32);
command.AddParameter($"Id_{index}", Document.Id);

return true;
}
Expand Down
7 changes: 3 additions & 4 deletions src/YesSql.Core/Commands/DeleteMapIndexCommand.cs
Expand Up @@ -4,20 +4,19 @@
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Threading.Tasks;

namespace YesSql.Commands
{
public sealed class DeleteMapIndexCommand : IIndexCommand, ICollectionName
{
private readonly IStore _store;
public int DocumentId { get; }
public long DocumentId { get; }
public Type IndexType { get; }
public string Collection { get; }
public int ExecutionOrder { get; } = 1;

public DeleteMapIndexCommand(Type indexType, int documentId, IStore store, string collection)
public DeleteMapIndexCommand(Type indexType, long documentId, IStore store, string collection)
{
IndexType = indexType;
DocumentId = documentId;
Expand All @@ -43,7 +42,7 @@ public bool AddToBatch(ISqlDialect dialect, List<string> queries, DbCommand comm

queries.Add(sql);

command.AddParameter($"Id_{index}", DocumentId, DbType.Int32);
command.AddParameter($"Id_{index}", DocumentId);

return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/YesSql.Core/Commands/UpdateDocumentCommand.cs
Expand Up @@ -72,9 +72,9 @@ public override bool AddToBatch(ISqlDialect dialect, List<string> queries, DbCom
queries.Add(updateCmd);

batchCommand
.AddParameter("Id_" + index, Document.Id, DbType.Int32)
.AddParameter("Content_" + index, Document.Content, DbType.String)
.AddParameter("Version_" + index, Document.Version, DbType.Int64);
.AddParameter("Id_" + index, Document.Id)
.AddParameter("Content_" + index, Document.Content)
.AddParameter("Version_" + index, Document.Version);

return true;
}
Expand Down
17 changes: 8 additions & 9 deletions src/YesSql.Core/Commands/UpdateIndexCommand.cs
Expand Up @@ -6,21 +6,20 @@
using System.Linq;
using System.Threading.Tasks;
using YesSql.Indexes;
using YesSql.Sql.Schema;

namespace YesSql.Commands
{
public sealed class UpdateIndexCommand : IndexCommand
{
private readonly int[] _addedDocumentIds;
private readonly int[] _deletedDocumentIds;
private readonly long[] _addedDocumentIds;
private readonly long[] _deletedDocumentIds;

public override int ExecutionOrder { get; } = 3;

public UpdateIndexCommand(
IIndex index,
IEnumerable<int> addedDocumentIds,
IEnumerable<int> deletedDocumentIds,
IEnumerable<long> addedDocumentIds,
IEnumerable<long> deletedDocumentIds,
IStore _store,
string collection) : base(index, _store, collection)
{
Expand Down Expand Up @@ -93,7 +92,7 @@ public override bool AddToBatch(ISqlDialect dialect, List<string> queries, DbCom
var parameter = command.CreateParameter();
parameter.ParameterName = $"Id{index}";
parameter.Value = Index.Id;
parameter.DbType = System.Data.DbType.Int32;
parameter.DbType = System.Data.DbType.Int64;
command.Parameters.Add(parameter);

// Update the documents list
Expand All @@ -106,7 +105,7 @@ public override bool AddToBatch(ISqlDialect dialect, List<string> queries, DbCom
parameter = command.CreateParameter();
parameter.ParameterName = $"Id_{index}";
parameter.Value = Index.Id;
parameter.DbType = System.Data.DbType.Int32;
parameter.DbType = System.Data.DbType.Int64;
command.Parameters.Add(parameter);

for (var i = 0; i < _addedDocumentIds.Length; i++)
Expand All @@ -117,7 +116,7 @@ public override bool AddToBatch(ISqlDialect dialect, List<string> queries, DbCom
parameter = command.CreateParameter();
parameter.ParameterName = $"AddedId_{index}_{i}";
parameter.Value = _addedDocumentIds[i];
parameter.DbType = System.Data.DbType.Int32;
parameter.DbType = System.Data.DbType.Int64;
command.Parameters.Add(parameter);
}

Expand All @@ -129,7 +128,7 @@ public override bool AddToBatch(ISqlDialect dialect, List<string> queries, DbCom
parameter = command.CreateParameter();
parameter.ParameterName = $"RemovedId_{index}_{i}";
parameter.Value = _deletedDocumentIds[i];
parameter.DbType = System.Data.DbType.Int32;
parameter.DbType = System.Data.DbType.Int64;
command.Parameters.Add(parameter);
}
}
Expand Down

0 comments on commit b2f85e8

Please sign in to comment.