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