Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement savepoint API for Microsoft.Data.Sqlite #22273

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<PropertyGroup>
<DefaultNetCoreTargetFramework>netcoreapp5.0</DefaultNetCoreTargetFramework>
<DefaultNetCoreTargetFramework>net5.0</DefaultNetCoreTargetFramework>
</PropertyGroup>

<PropertyGroup>
Expand Down
7 changes: 7 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<Project>
<Import Project="Sdk.targets" Sdk="Microsoft.DotNet.Arcade.Sdk" />

<ItemGroup>
<FrameworkReference Update="Microsoft.NETCore.App"
Condition="'$(TargetFramework)' == '$(DefaultNetCoreTargetFramework)'"
RuntimeFrameworkVersion="$(MicrosoftNETCoreAppRuntimewinx64PackageVersion)"
TargetingPackVersion="$(MicrosoftNETCoreAppRefPackageVersion)" />
</ItemGroup>
</Project>
8 changes: 8 additions & 0 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>7d2968eb2d6030287d412d21c7a012831e9f42f0</Sha>
</Dependency>
<Dependency Name="Microsoft.NETCore.App.Ref" Version="5.0.0-rc.1.20427.1">
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>7d2968eb2d6030287d412d21c7a012831e9f42f0</Sha>
</Dependency>
<Dependency Name="Microsoft.NETCore.App.Runtime.win-x64" Version="5.0.0-rc.1.20427.1">
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>7d2968eb2d6030287d412d21c7a012831e9f42f0</Sha>
</Dependency>
<Dependency Name="System.Collections.Immutable" Version="5.0.0-rc.1.20427.1">
<Uri>https://github.com/dotnet/runtime</Uri>
<Sha>7d2968eb2d6030287d412d21c7a012831e9f42f0</Sha>
Expand Down
2 changes: 2 additions & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<MicrosoftExtensionsDependencyModelVersion>5.0.0-rc.1.20427.1</MicrosoftExtensionsDependencyModelVersion>
<MicrosoftExtensionsHostFactoryResolverSourcesVersion>5.0.0-rc.1.20427.1</MicrosoftExtensionsHostFactoryResolverSourcesVersion>
<MicrosoftExtensionsLoggingVersion>5.0.0-rc.1.20427.1</MicrosoftExtensionsLoggingVersion>
<MicrosoftNETCoreAppRefPackageVersion>5.0.0-rc.1.20427.1</MicrosoftNETCoreAppRefPackageVersion>
<MicrosoftNETCoreAppRuntimewinx64PackageVersion>5.0.0-rc.1.20427.1</MicrosoftNETCoreAppRuntimewinx64PackageVersion>
</PropertyGroup>
<PropertyGroup Label="Other dependencies">
<MicrosoftCodeAnalysisVersion>3.7.0</MicrosoftCodeAnalysisVersion>
Expand Down
3 changes: 3 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"tools": {
"dotnet": "5.0.100-preview.8.20417.9",
"runtimes": {
"dotnet": [
"$(MicrosoftNETCoreAppRuntimewinx64PackageVersion)"
],
"dotnet": [
"3.1.7"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Microsoft.Data.Sqlite.SqliteException
Microsoft.Data.Sqlite.SqliteFactory
Microsoft.Data.Sqlite.SqliteParameter
Microsoft.Data.Sqlite.SqliteTransaction</Description>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net5.0</TargetFrameworks>
bricelam marked this conversation as resolved.
Show resolved Hide resolved
<MinClientVersion>3.6</MinClientVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<CodeAnalysisRuleSet>Microsoft.Data.Sqlite.Core.ruleset</CodeAnalysisRuleSet>
Expand Down
68 changes: 68 additions & 0 deletions src/Microsoft.Data.Sqlite.Core/SqliteTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,74 @@ public override void Rollback()
RollbackInternal();
}

#if NET5_0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should expose the same API on all targets

/// <inheritdoc />
public override bool SupportsSavepoints => true;

/// <inheritdoc />
public override void Save(string savepointName)
{
if (savepointName == null)
{
throw new ArgumentNullException(nameof(savepointName));
}

if (string.IsNullOrWhiteSpace(savepointName))
{
throw new ArgumentException($"{nameof(savepointName)} can't be empty", nameof(savepointName));
}

if (_completed || _connection.State != ConnectionState.Open)
{
throw new InvalidOperationException(Resources.TransactionCompleted);
}

_connection.ExecuteNonQuery("SAVEPOINT " + savepointName);
Copy link
Contributor

@bricelam bricelam Aug 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delimit and escape the identifier. Terminate statements.

SAVEPOINT "The ""SaveChanges"" Savepoint";

}

/// <inheritdoc />
public override void Rollback(string savepointName)
{
if (savepointName == null)
{
throw new ArgumentNullException(nameof(savepointName));
}

if (string.IsNullOrWhiteSpace(savepointName))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whitespace is allowed

{
throw new ArgumentException($"{nameof(savepointName)} can't be empty", nameof(savepointName));
}

if (_completed || _connection.State != ConnectionState.Open)
{
throw new InvalidOperationException(Resources.TransactionCompleted);
}

_connection.ExecuteNonQuery("ROLLBACK TO " + savepointName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ROLLBACK TO SAVEPOINT (the SQL standard syntax)

}

/// <inheritdoc />
public override void Release(string savepointName)
{
if (savepointName == null)
{
throw new ArgumentNullException(nameof(savepointName));
}

if (string.IsNullOrWhiteSpace(savepointName))
{
throw new ArgumentException($"{nameof(savepointName)} can't be empty", nameof(savepointName));
}

if (_completed || _connection.State != ConnectionState.Open)
{
throw new InvalidOperationException(Resources.TransactionCompleted);
}

_connection.ExecuteNonQuery("RELEASE SAVEPOINT " + savepointName);
}
#endif

/// <summary>
/// Releases any resources used by the transaction and rolls it back.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions test/Microsoft.Data.Sqlite.Tests/SqliteTransactionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,28 @@ public void Dispose_can_be_called_more_than_once()
}
}

#if NET5_0
[Fact]
public void Savepoint()
{
using var connection = new SqliteConnection("Data Source=:memory:");
connection.Open();
CreateTestTable(connection);

var transaction = connection.BeginTransaction();
transaction.Save("MySavepointName");

connection.ExecuteNonQuery("INSERT INTO TestTable (TestColumn) VALUES (8)");
Assert.Equal(1L, connection.ExecuteScalar<long>("SELECT COUNT(*) FROM TestTable;"));

transaction.Rollback("MySavepointName");
Assert.Equal(0L, connection.ExecuteScalar<long>("SELECT COUNT(*) FROM TestTable;"));

transaction.Release("MySavepointName");
Assert.Throws<SqliteException>(() => transaction.Rollback("MySavepointName"));
}
#endif

private static void CreateTestTable(SqliteConnection connection)
{
connection.ExecuteNonQuery(
Expand Down