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