@@ -13,34 +13,34 @@ public class DbHelper
1313 private string SqlCheckTableRecord = "SELECT name from sqlite_master WHERE name='record'" ;
1414
1515 private string SqlCreateDb = """
16- CREATE TABLE assets (
17- id INTEGER NOT NULL UNIQUE,
18- data_b64 TEXT,
19- md5 TEXT UNIQUE ,
20- PRIMARY KEY("id" AUTOINCREMENT)
21- );
16+ CREATE TABLE assets (
17+ id INTEGER NOT NULL UNIQUE,
18+ data_b64 TEXT,
19+ md5 TEXT UNIQUE ,
20+ PRIMARY KEY("id" AUTOINCREMENT)
21+ );
2222
23- CREATE TABLE "record" (
24- "id" INTEGER NOT NULL UNIQUE,
25- "hash_id" TEXT UNIQUE,
26- "data_md5" TEXT,
27- "text" TEXT,
28- "display_title" TEXT,
29- "senderapp" TEXT,
30- "icon_path" TEXT,
31- "icon_md5" TEXT,
32- "preview_image_path" TEXT,
33- "content_type" INTEGER,
34- "score" INTEGER,
35- "init_score" INTEGER,
36- "time" TEXT,
37- "create_time" TEXT,
38- "pined" INTEGER,
39- PRIMARY KEY("id" AUTOINCREMENT),
40- FOREIGN KEY("icon_md5") REFERENCES "assets"("md5"),
41- FOREIGN KEY("data_md5") REFERENCES "assets"("md5") ON DELETE CASCADE
42- );
43- """ ;
23+ CREATE TABLE "record" (
24+ "id" INTEGER NOT NULL UNIQUE,
25+ "hash_id" TEXT UNIQUE,
26+ "data_md5" TEXT,
27+ "text" TEXT,
28+ "display_title" TEXT,
29+ "senderapp" TEXT,
30+ "icon_path" TEXT,
31+ "icon_md5" TEXT,
32+ "preview_image_path" TEXT,
33+ "content_type" INTEGER,
34+ "score" INTEGER,
35+ "init_score" INTEGER,
36+ "time" TEXT,
37+ "create_time" TEXT,
38+ "pined" INTEGER,
39+ PRIMARY KEY("id" AUTOINCREMENT),
40+ FOREIGN KEY("icon_md5") REFERENCES "assets"("md5"),
41+ FOREIGN KEY("data_md5") REFERENCES "assets"("md5") ON DELETE CASCADE
42+ );
43+ """ ;
4444
4545 public DbHelper ( SqliteConnection connection )
4646 {
@@ -131,6 +131,7 @@ await Connection.ExecuteAsync(
131131 sql = "PRAGMA foreign_keys = ON; DELETE FROM assets WHERE md5=@DataMd5;" ;
132132 await Connection . ExecuteAsync ( sql , new { DataMd5 = dataMd5 } ) ;
133133 }
134+
134135 CloseIfNotKeep ( ) ;
135136 }
136137
@@ -141,28 +142,43 @@ public async void DeleteAllRecords()
141142 CreateDb ( ) ;
142143 }
143144
144- public async void PinOneRecord ( ClipboardData clipboardData )
145+ public async void PinOneRecord ( ClipboardData data )
145146 {
146- var sql = "UPDATE record SET pined=@Pin WHERE hash_id=@HashId" ;
147+ var sql = "INSERT OR IGNORE INTO assets(data_b64, md5) VALUES (@DataB64, @Md5);" ;
148+ var iconB64 = data . Icon . ToBase64 ( ) ;
149+ var iconMd5 = iconB64 . GetMd5 ( ) ;
150+ var rows = await Connection . ExecuteAsync (
151+ sql ,
152+ new List < Assets >
153+ {
154+ new ( ) { DataB64 = iconB64 , Md5 = iconMd5 } ,
155+ }
156+ ) ;
157+ sql = "UPDATE record SET pined=@Pin, icon_md5=@IconMd5 WHERE hash_id=@HashId" ;
147158 await Connection . ExecuteAsync (
148159 sql ,
149- new { Pin = clipboardData . Pined , HashId = clipboardData . HashId }
160+ new
161+ {
162+ Pin = data . Pined ,
163+ HashId = data . HashId ,
164+ IconMd5 = iconMd5
165+ }
150166 ) ;
151167 CloseIfNotKeep ( ) ;
152168 }
153169
154170 public async Task < LinkedList < ClipboardData > > GetAllRecord ( )
155171 {
156172 var sql = """
157- SELECT r.id as Id, a.data_b64 as DataMd5, r.text as Text, r.display_title as DisplayTitle,
158- r.senderapp as SenderApp, r.icon_path as IconPath, b.data_b64 as IconMd5,
159- r.preview_image_path as PreviewImagePath, r.content_type as ContentType,
160- r.score as Score, r.init_score as InitScore, r.time as Time,
161- r.create_time as CreateTime, r.pined as Pined, r.hash_id as HashId
162- FROM record r
163- LEFT JOIN assets a ON r.data_md5=a.md5
164- LEFT JOIN assets b ON r.icon_md5=b.md5;
165- """ ;
173+ SELECT r.id as Id, a.data_b64 as DataMd5, r.text as Text, r.display_title as DisplayTitle,
174+ r.senderapp as SenderApp, r.icon_path as IconPath, b.data_b64 as IconMd5,
175+ r.preview_image_path as PreviewImagePath, r.content_type as ContentType,
176+ r.score as Score, r.init_score as InitScore, r.time as Time,
177+ r.create_time as CreateTime, r.pined as Pined, r.hash_id as HashId
178+ FROM record r
179+ LEFT JOIN assets a ON r.data_md5=a.md5
180+ LEFT JOIN assets b ON r.icon_md5=b.md5;
181+ """ ;
166182 var results = await Connection . QueryAsync < Record > ( sql ) ;
167183 LinkedList < ClipboardData > allRecord = new ( results . Select ( Record . ToClipboardData ) ) ;
168184 CloseIfNotKeep ( ) ;
@@ -172,10 +188,10 @@ FROM record r
172188 public async void DeleteRecordByKeepTime ( int contentType , int keepTime )
173189 {
174190 var sql = """
175- DELETE FROM record
176- WHERE strftime('%s', 'now') - strftime('%s', create_time) > @KeepTime*3600
177- AND content_type=@ContentType;
178- """ ;
191+ DELETE FROM record
192+ WHERE strftime('%s', 'now') - strftime('%s', create_time) > @KeepTime*3600
193+ AND content_type=@ContentType;
194+ """ ;
179195 var r = await Connection . ExecuteAsync (
180196 sql ,
181197 new { KeepTime = keepTime , ContentType = contentType }
@@ -227,17 +243,21 @@ public class Record
227243 public int Score { get ; set ; }
228244 public int InitScore { get ; set ; }
229245 public DateTime _time ;
246+
230247 public string Time
231248 {
232249 get => _time . ToString ( "O" ) ;
233250 set => _time = DateTime . Parse ( value ) ;
234251 }
252+
235253 public DateTime _create_time ;
254+
236255 public string CreateTime
237256 {
238257 get => _create_time . ToString ( "O" ) ;
239258 set => _create_time = DateTime . Parse ( value ) ;
240259 }
260+
241261 public bool Pined { get ; set ; }
242262
243263 public static Record FromClipboardData ( ClipboardData data )
0 commit comments