@@ -43,7 +43,7 @@ PRIMARY KEY("id" AUTOINCREMENT)
4343 "display_title" TEXT,
4444 "senderapp" TEXT,
4545 "icon_path" TEXT,
46- "icon_id" INTEGER ,
46+ "icon_id" TEXT ,
4747 "preview_image_path" TEXT,
4848 "content_type" INTEGER,
4949 "score" INTEGER,
@@ -52,7 +52,8 @@ PRIMARY KEY("id" AUTOINCREMENT)
5252 "create_time" TEXT,
5353 "pined" INTEGER,
5454 PRIMARY KEY("id" AUTOINCREMENT),
55- FOREIGN KEY("icon_id") REFERENCES "assets"("md5")
55+ FOREIGN KEY("icon_id") REFERENCES "assets"("md5"),
56+ FOREIGN KEY("data") REFERENCES "assets"("md5")
5657 );
5758 """ ;
5859 command . ExecuteNonQuery ( ) ;
@@ -63,30 +64,24 @@ FOREIGN KEY("icon_id") REFERENCES "assets"("md5")
6364 public void AddOneRecord ( ClipboardData data )
6465 {
6566 Connection . Open ( ) ;
67+ // insert image data to table assets
6668 var iconB64 = data . Icon . ToBase64 ( ) ;
6769 var iconMd5 = iconB64 . GetMd5 ( ) ;
70+ // insert data to table assets
71+ string insertData = data . DataToString ( ) ;
72+ var dataMd5 = insertData . GetMd5 ( ) ;
6873 var command = Connection . CreateCommand ( ) ;
69- command . CommandText = "INSERT OR IGNORE INTO assets(data_b64, md5) VALUES ($data_b64, $md5)" ;
74+ command . CommandText =
75+ """
76+ INSERT OR IGNORE INTO assets(data_b64, md5) VALUES ($data_b64, $md5);
77+
78+ INSERT OR IGNORE INTO assets(data_b64, md5) VALUES ($data_b64_data, $md5_data);
79+ """ ;
7080 command . Parameters . AddWithValue ( "$data_b64" , iconB64 ) ;
7181 command . Parameters . AddWithValue ( "$md5" , iconMd5 ) ;
72- command . ExecuteNonQuery ( ) ;
73-
74- string ? insertData = "" ;
75- switch ( data . Type )
76- {
77- case CbMonitor . ContentTypes . Text :
78- insertData = data . Data as string ;
79- break ;
80- case CbMonitor . ContentTypes . Image :
81- insertData = iconB64 ;
82- break ;
83- case CbMonitor . ContentTypes . Files :
84- insertData = data . Data is not string [ ] t ? "" : string . Join ( '\n ' , t ) ;
85- break ;
86- default :
87- // don't process others
88- return ;
89- }
82+ command . Parameters . AddWithValue ( "$data_b64_data" , insertData ) ;
83+ command . Parameters . AddWithValue ( "$md5_data" , dataMd5 ) ;
84+ command . ExecuteNonQueryAsync ( ) ;
9085
9186 command . CommandText =
9287 """
@@ -95,14 +90,14 @@ INSERT OR IGNORE INTO record(hash_id, data, text, display_title,
9590 preview_image_path, content_type,
9691 score, init_score, time,
9792 create_time, pined)
98- VALUES ($hash_id, $data , $text, $display_title,
93+ VALUES ($hash_id, $data_md5 , $text, $display_title,
9994 $senderapp, $icon_path, $icon,
10095 $preview_image_path, $content_type,
10196 $score, $init_score, $time,
10297 $create_time, $pined);
10398 """ ;
10499 command . Parameters . AddWithValue ( "$hash_id" , data . GetHashCode ( ) ) ;
105- command . Parameters . AddWithValue ( "$data " , insertData ) ;
100+ command . Parameters . AddWithValue ( "$data_md5 " , dataMd5 ) ;
106101 command . Parameters . AddWithValue ( "$text" , data . Text ) ;
107102 command . Parameters . AddWithValue ( "$display_title" , data . DisplayTitle ) ;
108103 command . Parameters . AddWithValue ( "$senderapp" , data . SenderApp ) ;
@@ -116,25 +111,32 @@ INSERT OR IGNORE INTO record(hash_id, data, text, display_title,
116111 command . Parameters . AddWithValue ( "$create_time" , data . CreateTime ) ;
117112 command . Parameters . AddWithValue ( "$pined" , data . Pined ) ;
118113
119- command . ExecuteNonQuery ( ) ;
114+ command . ExecuteNonQueryAsync ( ) ;
120115 CloseIfNotKeep ( ) ;
121116 }
122117
123118 public void DeleteOneRecord ( ClipboardData clipboardData )
124119 {
125120 Connection . Open ( ) ;
126121 var command = Connection . CreateCommand ( ) ;
127- command . CommandText = "DELETE from record WHERE hash_id == $hash_id" ;
122+ var dataMd5 = clipboardData . DataToString ( ) . GetMd5 ( ) ;
123+ command . CommandText =
124+ """
125+ DELETE from record WHERE hash_id=$hash_id OR data=$md5;
126+
127+ DELETE from assets WHERE md5=$md5;
128+ """ ;
128129 command . Parameters . AddWithValue ( "$hash_id" , clipboardData . GetHashCode ( ) ) ;
129- command . ExecuteNonQuery ( ) ;
130+ command . Parameters . AddWithValue ( "$md5" , dataMd5 ) ;
131+ command . ExecuteNonQueryAsync ( ) ;
130132 CloseIfNotKeep ( ) ;
131133 }
132134
133135 public void PinOneRecord ( ClipboardData clipboardData )
134136 {
135137 Connection . Open ( ) ;
136138 var command = Connection . CreateCommand ( ) ;
137- command . CommandText = "UPDATE record SET pined=$pined WHERE hash_id == $hash_id" ;
139+ command . CommandText = "UPDATE record SET pined=$pined WHERE hash_id= $hash_id" ;
138140 command . Parameters . AddWithValue ( "$pined" , clipboardData . Pined ) ;
139141 command . Parameters . AddWithValue ( "$hash_id" , clipboardData . GetHashCode ( ) ) ;
140142 command . ExecuteNonQuery ( ) ;
@@ -146,10 +148,15 @@ public LinkedList<ClipboardData> GetAllRecord()
146148 Connection . Open ( ) ;
147149 var command = Connection . CreateCommand ( ) ;
148150 command . CommandText =
149- "SELECT r.data, r.text, r.display_title, r.senderapp, r.icon_path, " +
150- "a.data_b64, r.preview_image_path, r.content_type,score, r.init_score, " +
151- "r.time, r.create_time, r.pined " +
152- "FROM record r, assets a WHERE r.icon_id=a.md5" ;
151+ """
152+ SELECT a.data_b64 as data, r.text, r.display_title, r.senderapp, r.icon_path,
153+ b.data_b64, r.preview_image_path, r.content_type,score, r.init_score,
154+ r.time, r.create_time, r.pined
155+ FROM record r
156+ LEFT JOIN assets a ON r.data=a.md5
157+ LEFT JOIN assets b ON r.icon_id=b.md5;
158+ """ ;
159+ // command.Parameters.AddWithValue("$image_type", CbMonitor.ContentTypes.Image);
153160 LinkedList < ClipboardData > allRecord = new ( ) ;
154161 using var reader = command . ExecuteReader ( ) ;
155162 while ( reader . Read ( ) )
@@ -199,8 +206,21 @@ public void DeleteRecordByKeepTime(int contentType, uint keepTime)
199206 {
200207 Connection . Open ( ) ;
201208 var command = Connection . CreateCommand ( ) ;
202- command . CommandText = "DELETE FROM record WHERE julianday(create_time) + $keepTime / 24 < julianday('now')" ;
209+ command . CommandText =
210+ """
211+ DELETE FROM assets
212+ WHERE EXISTS(
213+ SELECT id from record
214+ WHERE julianday(create_time) + $keepTime / 24 < julianday('now')
215+ AND content_type=$content_type
216+ AND (assets.md5=record.data OR assets.md5=record.icon_id));
217+
218+ DELETE FROM record
219+ WHERE julianday(create_time) + $keepTime / 24 < julianday('now')
220+ AND content_type=$content_type;
221+ """ ;
203222 command . Parameters . AddWithValue ( "$keepTime" , keepTime ) ;
223+ command . Parameters . AddWithValue ( "$content_type" , contentType ) ;
204224 command . ExecuteNonQuery ( ) ;
205225 CloseIfNotKeep ( ) ;
206226 }
@@ -214,6 +234,6 @@ public void Close()
214234 private void CloseIfNotKeep ( )
215235 {
216236 if ( ! KeepConnection )
217- Connection . Close ( ) ;
237+ Connection . CloseAsync ( ) ;
218238 }
219239}
0 commit comments