WingMan – Blame information for rev 37
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
23 | office | 1 | using System; |
37 | office | 2 | using System.IO; |
3 | using System.Reflection; |
||
23 | office | 4 | using System.Runtime.InteropServices; |
5 | using System.Threading; |
||
6 | using System.Threading.Tasks; |
||
7 | using System.Windows.Forms; |
||
8 | using Mono.Data.Sqlite; |
||
9 | |||
10 | namespace WingMan.AutoCompletion |
||
11 | { |
||
12 | public class AutoCompletion : IDisposable |
||
13 | { |
||
14 | public delegate void LoadFailed(object sender, AutoCompletionFailedEventArgs args); |
||
15 | |||
16 | |||
17 | public delegate void SaveFailed(object sender, AutoCompletionFailedEventArgs args); |
||
18 | |||
19 | public AutoCompletion() |
||
20 | { |
||
21 | } |
||
22 | |||
23 | public AutoCompletion(TaskScheduler taskScheduler, CancellationToken cancellationToken) : this() |
||
24 | { |
||
25 | TaskScheduler = taskScheduler; |
||
26 | CancellationToken = cancellationToken; |
||
27 | } |
||
28 | |||
29 | private TaskScheduler TaskScheduler { get; } |
||
30 | private CancellationToken CancellationToken { get; } |
||
31 | |||
32 | public void Dispose() |
||
33 | { |
||
34 | } |
||
35 | |||
36 | public event SaveFailed OnSaveFailed; |
||
37 | |||
38 | public event LoadFailed OnLoadFailed; |
||
39 | |||
40 | public async Task Save(string name, AutoCompleteStringCollection autoCompleteStringCollection) |
||
41 | { |
||
42 | try |
||
43 | { |
||
44 | using (var sqliteConnection = |
||
45 | new SqliteConnection($"URI=file:{"Autocomplete.db"}")) |
||
46 | { |
||
47 | await sqliteConnection.OpenAsync(CancellationToken); |
||
48 | |||
49 | // Create table if it does not exist. |
||
50 | using (var sqliteCommand = |
||
51 | new SqliteCommand($"CREATE TABLE IF NOT EXISTS {name} (data text UNIQUE NOT NULL)", |
||
52 | sqliteConnection)) |
||
53 | { |
||
54 | using (var dbtransaction = sqliteConnection.BeginTransaction()) |
||
55 | { |
||
56 | try |
||
57 | { |
||
58 | await sqliteCommand.ExecuteReaderAsync(CancellationToken); |
||
59 | |||
60 | dbtransaction.Commit(); |
||
61 | } |
||
62 | catch |
||
63 | { |
||
64 | dbtransaction.Rollback(); |
||
65 | throw; |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | |||
70 | // Add all items to the database. |
||
71 | await Task.Delay(0, CancellationToken).ContinueWith(async _ => |
||
72 | { |
||
73 | foreach (var sourceItem in autoCompleteStringCollection) |
||
74 | { |
||
75 | var data = sourceItem.ToString(); |
||
76 | |||
77 | using (var sqliteCommand = |
||
78 | new SqliteCommand($"REPLACE INTO {name} (data) VALUES (:data)", |
||
79 | sqliteConnection)) |
||
80 | { |
||
81 | sqliteCommand |
||
82 | .Parameters |
||
83 | .Add(new SqliteParameter("data", data)); |
||
84 | |||
85 | using (var dbtransaction = sqliteConnection.BeginTransaction()) |
||
86 | { |
||
87 | try |
||
88 | { |
||
89 | await sqliteCommand.ExecuteReaderAsync(CancellationToken); |
||
90 | |||
91 | dbtransaction.Commit(); |
||
92 | } |
||
93 | catch |
||
94 | { |
||
95 | dbtransaction.Rollback(); |
||
96 | throw; |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | }, CancellationToken, |
||
102 | TaskContinuationOptions.None, TaskScheduler); |
||
103 | } |
||
104 | } |
||
105 | catch (Exception ex) |
||
106 | { |
||
107 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnSaveFailed?.Invoke(this, |
||
108 | new AutoCompletionFailedEventArgs(AutoCompletionFailedType.Save, name, ex)), CancellationToken, |
||
109 | TaskContinuationOptions.None, TaskScheduler); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | public async Task Load(string name, AutoCompleteStringCollection autoCompleteStringCollection) |
||
114 | { |
||
115 | try |
||
116 | { |
||
117 | using (var sqliteConnection = |
||
118 | new SqliteConnection($"URI=file:{"Autocomplete.db"}")) |
||
119 | { |
||
120 | await sqliteConnection.OpenAsync(CancellationToken); |
||
36 | office | 121 | |
122 | // Create table if it does not exist. |
||
23 | office | 123 | using (var sqliteCommand = |
36 | office | 124 | new SqliteCommand($"CREATE TABLE IF NOT EXISTS {name} (data text UNIQUE NOT NULL)", |
125 | sqliteConnection)) |
||
126 | { |
||
127 | using (var dbtransaction = sqliteConnection.BeginTransaction()) |
||
128 | { |
||
129 | try |
||
130 | { |
||
131 | await sqliteCommand.ExecuteReaderAsync(CancellationToken); |
||
132 | |||
133 | dbtransaction.Commit(); |
||
134 | } |
||
135 | catch |
||
136 | { |
||
137 | dbtransaction.Rollback(); |
||
138 | throw; |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | |||
143 | using (var sqliteCommand = |
||
23 | office | 144 | new SqliteCommand($"SELECT data FROM {name}", sqliteConnection)) |
145 | { |
||
146 | using (var dbtransaction = sqliteConnection.BeginTransaction()) |
||
147 | { |
||
148 | try |
||
149 | { |
||
150 | using (var reader = await sqliteCommand.ExecuteReaderAsync(CancellationToken)) |
||
151 | { |
||
152 | while (await reader.ReadAsync(CancellationToken)) |
||
153 | for (var i = 0; i < reader.FieldCount; ++i) |
||
154 | { |
||
155 | var value = reader.GetString(i); |
||
156 | if (string.IsNullOrEmpty(value)) |
||
157 | continue; |
||
158 | |||
159 | await Task.Delay(0, CancellationToken).ContinueWith( |
||
160 | _ => autoCompleteStringCollection.Add(value), CancellationToken, |
||
161 | TaskContinuationOptions.None, TaskScheduler); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | dbtransaction.Commit(); |
||
166 | } |
||
167 | catch |
||
168 | { |
||
169 | dbtransaction.Rollback(); |
||
170 | throw; |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | catch (Exception ex) |
||
177 | { |
||
178 | await Task.Delay(0, CancellationToken).ContinueWith(_ => OnLoadFailed?.Invoke(this, |
||
179 | new AutoCompletionFailedEventArgs(AutoCompletionFailedType.Load, name, ex)), CancellationToken, |
||
180 | TaskContinuationOptions.None, TaskScheduler); |
||
181 | } |
||
182 | } |
||
183 | } |
||
36 | office | 184 | } |