Skip to content

Commit

Permalink
Add new interface and class for storing the include folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Willford committed Aug 5, 2019
1 parent aa786f9 commit f3527b6
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
1 change: 1 addition & 0 deletions GVFS/GVFS.Common/Database/GVFSDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ private void Initialize()
}

PlaceholderTable.CreateTable(connection);
IncludedFolderTable.CreateTable(connection);
}
}

Expand Down
14 changes: 14 additions & 0 deletions GVFS/GVFS.Common/Database/IIncludedFolderCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace GVFS.Common.Database
{
public interface IIncludedFolderCollection
{
List<string> GetAll();

void Add(string directory);
void Remove(string directory);
}
}
89 changes: 89 additions & 0 deletions GVFS/GVFS.Common/Database/IncludedFolderTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;

namespace GVFS.Common.Database
{
public class IncludedFolderTable : IIncludedFolderCollection
{
private IGVFSConnectionPool connectionPool;

public IncludedFolderTable(IGVFSConnectionPool connectionPool)
{
this.connectionPool = connectionPool;
}

public static void CreateTable(IDbConnection connection)
{
using (IDbCommand command = connection.CreateCommand())
{
command.CommandText = "CREATE TABLE IF NOT EXISTS [IncludedFolder] (path TEXT PRIMARY KEY COLLATE NOCASE) WITHOUT ROWID;";
command.ExecuteNonQuery();
}
}

public void Add(string directory)
{
try
{
using (IDbConnection connection = this.connectionPool.GetConnection())
using (IDbCommand command = connection.CreateCommand())
{
command.CommandText = "INSERT OR REPLACE INTO IncludedFolder (path) VALUES (@path);";
command.AddParameter("@path", DbType.String, directory);
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
throw new GVFSDatabaseException($"{nameof(IncludedFolderTable)}.{nameof(this.Add)}({directory}) Exception: {ex.ToString()}", ex);
}
}

public List<string> GetAll()
{
try
{
using (IDbConnection connection = this.connectionPool.GetConnection())
using (IDbCommand command = connection.CreateCommand())
{
List<string> directories = new List<string>();
command.CommandText = $"SELECT path FROM IncludedFolder;";
using (IDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
directories.Add(reader.GetString(0));
}
}

return directories;
}
}
catch (Exception ex)
{
throw new GVFSDatabaseException($"{nameof(PlaceholderTable)}.{nameof(this.GetAll)} Exception: {ex.ToString()}", ex);
}
}

public void Remove(string directory)
{
try
{
using (IDbConnection connection = this.connectionPool.GetConnection())
using (IDbCommand command = connection.CreateCommand())
{
command.CommandText = "DELETE FROM IncludedFolder WHERE path = @path;";
command.AddParameter("@path", DbType.String, directory);
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
throw new GVFSDatabaseException($"{nameof(IncludedFolderTable)}.{nameof(this.Remove)}({directory}) Exception: {ex.ToString()}", ex);
}
}
}
}
16 changes: 15 additions & 1 deletion GVFS/GVFS.UnitTests/Common/Database/GVFSDatabaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,25 @@ private void TestGVFSDatabase(Action<GVFSDatabase> testCode, bool throwException

mockCommand2.Setup(x => x.Dispose());

Mock<IDbCommand> mockCommand3 = new Mock<IDbCommand>(MockBehavior.Strict);
mockCommand3.SetupSet(x => x.CommandText = "CREATE TABLE IF NOT EXISTS [IncludedFolder] (path TEXT PRIMARY KEY COLLATE NOCASE) WITHOUT ROWID;");
if (throwException)
{
mockCommand3.Setup(x => x.ExecuteNonQuery()).Throws(new Exception("Error"));
}
else
{
mockCommand3.Setup(x => x.ExecuteNonQuery()).Returns(1);
}

mockCommand3.Setup(x => x.Dispose());

List<Mock<IDbConnection>> mockConnections = new List<Mock<IDbConnection>>();
Mock<IDbConnection> mockConnection = new Mock<IDbConnection>(MockBehavior.Strict);
mockConnection.SetupSequence(x => x.CreateCommand())
.Returns(mockCommand.Object)
.Returns(mockCommand2.Object);
.Returns(mockCommand2.Object)
.Returns(mockCommand3.Object);
mockConnection.Setup(x => x.Dispose());
mockConnections.Add(mockConnection);

Expand Down

0 comments on commit f3527b6

Please sign in to comment.