Skip to content

Commit

Permalink
Add Update event.
Browse files Browse the repository at this point in the history
This reverts commit 643a286.
  • Loading branch information
bricelam committed Jan 24, 2018
1 parent e6ded0f commit a94aaf2
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Microsoft.Data.Sqlite.sln
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BulkInsertSample", "samples
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CollationSample", "samples\CollationSample\CollationSample.csproj", "{A5E96F24-953A-4E71-9081-736E11D8229F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataChangedSample", "samples\DataChangedSample\DataChangedSample.csproj", "{61F83459-79DD-4640-AF11-574ABAB529F1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DateAndTimeSample", "samples\DateAndTimeSample\DateAndTimeSample.csproj", "{EBC5675D-1FD0-4F77-93D0-F970EB7A29EC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DirtyReadSample", "samples\DirtyReadSample\DirtyReadSample.csproj", "{CB405B7D-81E5-48B6-9E24-AC901D0423EE}"
Expand Down Expand Up @@ -98,6 +100,10 @@ Global
{A5E96F24-953A-4E71-9081-736E11D8229F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5E96F24-953A-4E71-9081-736E11D8229F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5E96F24-953A-4E71-9081-736E11D8229F}.Release|Any CPU.Build.0 = Release|Any CPU
{61F83459-79DD-4640-AF11-574ABAB529F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{61F83459-79DD-4640-AF11-574ABAB529F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{61F83459-79DD-4640-AF11-574ABAB529F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{61F83459-79DD-4640-AF11-574ABAB529F1}.Release|Any CPU.Build.0 = Release|Any CPU
{EBC5675D-1FD0-4F77-93D0-F970EB7A29EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EBC5675D-1FD0-4F77-93D0-F970EB7A29EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBC5675D-1FD0-4F77-93D0-F970EB7A29EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -143,6 +149,7 @@ Global
{7EFA4FBC-33F1-44FF-9E8F-57EC0FE32201} = {D30CC1C7-D46C-4640-8CE6-EC9ED34DEDA5}
{7BC06AAD-FAD0-4FDE-BB3E-4A3D56D1FEBC} = {D30CC1C7-D46C-4640-8CE6-EC9ED34DEDA5}
{A5E96F24-953A-4E71-9081-736E11D8229F} = {D30CC1C7-D46C-4640-8CE6-EC9ED34DEDA5}
{61F83459-79DD-4640-AF11-574ABAB529F1} = {D30CC1C7-D46C-4640-8CE6-EC9ED34DEDA5}
{EBC5675D-1FD0-4F77-93D0-F970EB7A29EC} = {D30CC1C7-D46C-4640-8CE6-EC9ED34DEDA5}
{CB405B7D-81E5-48B6-9E24-AC901D0423EE} = {D30CC1C7-D46C-4640-8CE6-EC9ED34DEDA5}
{67F8FB83-302C-4FBE-A2DC-F4FF5CEC8776} = {D30CC1C7-D46C-4640-8CE6-EC9ED34DEDA5}
Expand Down
16 changes: 16 additions & 0 deletions samples/DataChangedSample/DataChangedSample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.Data.Sqlite.Core\Microsoft.Data.Sqlite.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="SQLitePCLRaw.bundle_green" Version="$(SQLitePCLRawBundleGreenPackageVersion)" />
</ItemGroup>

</Project>
46 changes: 46 additions & 0 deletions samples/DataChangedSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using Microsoft.Data.Sqlite;

namespace DataChangedSample
{
class Program
{
static void Main()
{
var connection = new SqliteConnection("Data Source=:memory:");
connection.Open();

var createCommand = connection.CreateCommand();
createCommand.CommandText =
@"
CREATE TABLE user (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
);
INSERT INTO user (name)
VALUES ('Bryce'),
('Jon');
";
createCommand.ExecuteNonQuery();

connection.Update +=
(_, e) => Console.WriteLine($"{e.Event} {e.Table} {e.RowId}");

var updateCommand = connection.CreateCommand();
updateCommand.CommandText =
@"
UPDATE user
SET name = 'Brice'
WHERE name = 'Bryce';
DELETE FROM user
WHERE name = 'Jon';
INSERT INTO user (name)
VALUES ('Seth');
";
updateCommand.ExecuteNonQuery();
}
}
}
18 changes: 18 additions & 0 deletions src/Microsoft.Data.Sqlite.Core/SqliteConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public partial class SqliteConnection : DbConnection
static SqliteConnection()
=> BundleInitializer.Initialize();

/// <summary>
/// Occurs whenever a row is updated, inserted or deleted in a rowid table.
/// </summary>
public event EventHandler<UpdateEventArgs> Update;

/// <summary>
/// Initializes a new instance of the <see cref="SqliteConnection" /> class.
/// </summary>
Expand Down Expand Up @@ -217,6 +222,12 @@ public override void Open()
SqliteException.ThrowExceptionForRC(rc, _db);

SetState(ConnectionState.Open);

raw.sqlite3_update_hook(
_db,
(_, type, database, table, rowid)
=> OnUpdate(new UpdateEventArgs((UpdateEventType)type, database, table, rowid)),
null);
}

/// <summary>
Expand Down Expand Up @@ -442,6 +453,13 @@ public virtual void BackupDatabase(SqliteConnection destination, string destinat
}
}

/// <summary>
/// Raises the Microsoft.Data.Sqlite.SqliteConnection.Update event.
/// </summary>
/// <param name="e">A Microsoft.Data.Sqlite.UpdateEventArgs that contains the event data.</param>
protected virtual void OnUpdate(UpdateEventArgs e)
=> Update?.Invoke(this, e);

private void CreateFunctionCore<TState, TResult>(
string name,
int arity,
Expand Down
60 changes: 60 additions & 0 deletions src/Microsoft.Data.Sqlite.Core/UpdateEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.Data.Sqlite
{
/// <summary>
/// Provides data for the Update event of SqliteConnection.
/// </summary>
public class UpdateEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="UpdateEventArgs" /> class.
/// </summary>
/// <param name="eventType">The event that changed the data.</param>
/// <param name="database">The database name.</param>
/// <param name="table">The table name.</param>
/// <param name="rowId">The rowid of the affected row.</param>
public UpdateEventArgs(UpdateEventType eventType, string database, string table, long rowId)
{
Event = eventType;
Database = database;
Table = table;
RowId = rowId;
}

/// <summary>
/// Gets the event that changed the data.
/// </summary>
/// <value>
/// The event that changed the data.
/// </value>
public UpdateEventType Event { get; }

/// <summary>
/// Gets the database name.
/// </summary>
/// <value>
/// The database name.
/// </value>
public string Database { get; }

/// <summary>
/// Gets the table name.
/// </summary>
/// <value>
/// The table name.
/// </value>
public string Table { get; }

/// <summary>
/// Gets the rowid of the affected row.
/// </summary>
/// <value>
/// The rowid of the affected row.
/// </value>
public long RowId { get; }
}
}
28 changes: 28 additions & 0 deletions src/Microsoft.Data.Sqlite.Core/UpdateEventType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using SQLitePCL;

namespace Microsoft.Data.Sqlite
{
/// <summary>
/// Represents the event that changed the table data.
/// </summary>
public enum UpdateEventType
{
/// <summary>
/// Row was updated.
/// </summary>
Update = raw.SQLITE_UPDATE,

/// <summary>
/// Row was deleted.
/// </summary>
Delete = raw.SQLITE_DELETE,

/// <summary>
/// Row was inserted.
/// </summary>
Insert = raw.SQLITE_INSERT
}
}
42 changes: 42 additions & 0 deletions test/Microsoft.Data.Sqlite.Tests/SqliteConnectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,48 @@ public void EnableExtensions_throws_when_closed()
}
}

[Fact]
public void DataChange_event_works()
{
using (var connection = new SqliteConnection("Data Source=:memory:"))
{
var list = new List<UpdateEventArgs>();
connection.Update += (sender, e) =>
{
Assert.Equal(connection, sender);
list.Add(e);
};

connection.Open();
connection.ExecuteNonQuery(
"CREATE TABLE Person (ID INTEGER PRIMARY KEY, FirstName TEXT, LastName TEXT NOT NULL, Code INT UNIQUE);");
Assert.Empty(list);

connection.ExecuteNonQuery("INSERT INTO Person VALUES(101, 'John', 'Dee', 123);");
Assert.Single(list);
Assert.Equal(UpdateEventType.Insert, list[0].Event);
Assert.Equal("main", list[0].Database);
Assert.Equal("Person", list[0].Table);
Assert.Equal(101, list[0].RowId);
list.Clear();

connection.ExecuteNonQuery("UPDATE Person SET Code=234 WHERE ID=101;");
Assert.Single(list);
Assert.Equal(UpdateEventType.Update, list[0].Event);
Assert.Equal("main", list[0].Database);
Assert.Equal("Person", list[0].Table);
Assert.Equal(101, list[0].RowId);
list.Clear();

connection.ExecuteNonQuery("DELETE FROM Person WHERE ID=101;");
Assert.Single(list);
Assert.Equal(UpdateEventType.Delete, list[0].Event);
Assert.Equal("main", list[0].Database);
Assert.Equal("Person", list[0].Table);
Assert.Equal(101, list[0].RowId);
}
}

#if !NETCOREAPP2_0
[Fact]
public void DbProviderFactory_works()
Expand Down

0 comments on commit a94aaf2

Please sign in to comment.