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