forked from files-community/Files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersistentSQLiteCacheAdapter.cs
134 lines (120 loc) · 4.85 KB
/
PersistentSQLiteCacheAdapter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Microsoft.Data.Sqlite;
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
namespace Files.Helpers.FileListCache
{
internal class PersistentSQLiteCacheAdapter : IFileListCache, IDisposable
{
private SqliteConnection connection;
private bool disposedValue;
public async Task SaveFileDisplayNameToCache(string path, string displayName)
{
if (!await InitializeIfNeeded())
{
return;
}
try
{
if (displayName == null)
{
using var deleteCommand = new SqliteCommand("DELETE FROM FileDisplayNameCache WHERE Id = @Id", connection);
deleteCommand.Parameters.Add("@Id", SqliteType.Text).Value = path;
await deleteCommand.ExecuteNonQueryAsync();
return;
}
using var cmd = new SqliteCommand("SELECT Id FROM FileDisplayNameCache WHERE Id = @Id", connection);
cmd.Parameters.Add("@Id", SqliteType.Text).Value = path;
using var reader = await cmd.ExecuteReaderAsync();
if (reader.HasRows)
{
// need to update entry
using var updateCommand = new SqliteCommand("UPDATE FileDisplayNameCache SET DisplayName = @DisplayName WHERE Id = @Id", connection);
updateCommand.Parameters.Add("@Id", SqliteType.Text).Value = path;
updateCommand.Parameters.Add("@DisplayName", SqliteType.Text).Value = displayName;
await updateCommand.ExecuteNonQueryAsync();
}
else
{
// need to insert entry
using var insertCommand = new SqliteCommand("INSERT INTO FileDisplayNameCache (Id, DisplayName) VALUES (@Id, @DisplayName)", connection);
insertCommand.Parameters.Add("@Id", SqliteType.Text).Value = path;
insertCommand.Parameters.Add("@DisplayName", SqliteType.Text).Value = displayName;
await insertCommand.ExecuteNonQueryAsync();
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
public async Task<string> ReadFileDisplayNameFromCache(string path, CancellationToken cancellationToken)
{
if (!await InitializeIfNeeded())
{
return null;
}
try
{
using var cmd = new SqliteCommand("SELECT DisplayName FROM FileDisplayNameCache WHERE Id = @Id", connection);
cmd.Parameters.Add("@Id", SqliteType.Text).Value = path;
using var reader = await cmd.ExecuteReaderAsync(cancellationToken);
if (!await reader.ReadAsync())
{
return null;
}
return reader.GetString(0);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
return null;
}
}
public void Dispose()
{
if (!disposedValue)
{
connection.Dispose();
disposedValue = true;
}
}
private void RunCleanupRoutine()
{
}
private async Task<bool> InitializeIfNeeded()
{
if (disposedValue) return false;
if (connection != null) return true;
string dbPath = null;
try
{
await ApplicationData.Current.LocalFolder.CreateFileAsync("cache.db", CreationCollisionOption.OpenIfExists);
dbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "cache.db");
SQLitePCL.Batteries_V2.Init();
connection = new SqliteConnection($"Data Source='{dbPath}'");
connection.Open();
// create db schema
var createFileDisplayNameCacheTable = @"CREATE TABLE IF NOT EXISTS ""FileDisplayNameCache"" (
""Id"" VARCHAR(5000) NOT NULL,
""DisplayName"" TEXT NOT NULL,
PRIMARY KEY(""Id"")
)";
using var cmdFileDisplayNameCacheTable = new SqliteCommand(createFileDisplayNameCacheTable, connection);
cmdFileDisplayNameCacheTable.ExecuteNonQuery();
RunCleanupRoutine();
return true;
}
catch (Exception ex)
{
NLog.LogManager.GetCurrentClassLogger().Warn(ex, $"Failed initializing database with path: {dbPath}");
return false;
}
}
}
}