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 #22869

Merged
1 commit merged into from
Oct 11, 2020
Merged
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
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
92 changes: 92 additions & 0 deletions src/Microsoft.Data.Sqlite.Core/SqliteTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Data;
using System.Data.Common;
using System.Text;
using Microsoft.Data.Sqlite.Properties;
using static SQLitePCL.raw;

Expand Down Expand Up @@ -113,6 +114,97 @@ public override void Rollback()
RollbackInternal();
}

#if NET
roji marked this conversation as resolved.
Show resolved Hide resolved
/// <inheritdoc />
public override bool SupportsSavepoints => true;
#endif

/// <summary>
/// Creates a savepoint in the transaction. This allows all commands that are executed after the savepoint was
/// established to be rolled back, restoring the transaction state to what it was at the time of the savepoint.
/// </summary>
/// <param name="savepointName">The name of the savepoint to be created.</param>
#if NET
public override void Save(string savepointName)
#else
public void Save(string savepointName)
#endif
{
if (savepointName is null)
{
throw new ArgumentNullException(nameof(savepointName));
}

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

_connection.ExecuteNonQuery(
new StringBuilder()
.Append("SAVEPOINT \"")
.Append(savepointName.Replace("\"", "\"\""))
.Append("\";")
.ToString());
}

/// <summary>
/// Rolls back all commands that were executed after the specified savepoint was established.
/// </summary>
/// <param name="savepointName">The name of the savepoint to roll back to.</param>
#if NET
public override void Rollback(string savepointName)
#else
public void Rollback(string savepointName)
#endif
{
if (savepointName is null)
{
throw new ArgumentNullException(nameof(savepointName));
}

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

_connection.ExecuteNonQuery(
new StringBuilder()
.Append("ROLLBACK TO SAVEPOINT \"")
.Append(savepointName.Replace("\"", "\"\""))
.Append("\";")
.ToString());
}

/// <summary>
/// Destroys a savepoint previously defined in the current transaction. This allows the system to
/// reclaim some resources before the transaction ends.
/// </summary>
/// <param name="savepointName">The name of the savepoint to release.</param>
#if NET
public override void Release(string savepointName)
#else
public void Release(string savepointName)
#endif
{
if (savepointName is null)
{
throw new ArgumentNullException(nameof(savepointName));
}

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

_connection.ExecuteNonQuery(
new StringBuilder()
.Append("RELEASE SAVEPOINT \"")
.Append(savepointName.Replace("\"", "\"\""))
.Append("\";")
.ToString());
}

/// <summary>
/// Releases any resources used by the transaction and rolls it back.
/// </summary>
Expand Down
20 changes: 20 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,26 @@ public void Dispose_can_be_called_more_than_once()
}
}

[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"));
}

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