Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit 8b19105

Browse files
committed
improve database, sql execute async
1 parent e2d9d6d commit 8b19105

File tree

6 files changed

+91
-36
lines changed

6 files changed

+91
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
.vscode/
44
*.svg
55
*.xcf
6+
*.bak
67

78
## Ignore Visual Studio temporary files, build results, and
89
## files generated by popular Visual Studio add-ons.

build/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,13 @@ public override void Run(BuildContext context)
124124
FilePath? versionFile = null;
125125
foreach (var f in files)
126126
{
127-
if (f == null || f.ToString().EndsWith("e_sqlite3.dll"))
127+
var fstr = f.ToString();
128+
if (f == null || (fstr.EndsWith("e_sqlite3.dll") && ! fstr.EndsWith(".e_sqlite3.dll")))
128129
{
129130
files.Remove(f);
130131
continue;
131132
}
132-
if (f.ToString().EndsWith("plugin.json"))
133+
if (fstr.EndsWith("plugin.json"))
133134
versionFile = f;
134135
if (!Regex.IsMatch(f.GetFilename().ToString(), ptn))
135136
{

src/ClipboardR.Core.Test/ExtensionTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,13 @@ public void TestBase64ToImage()
4646
var img1 = s.ToImage();
4747
var imgBitmap = s.ToBitmapImage();
4848
}
49+
50+
[Theory]
51+
[InlineData("", "D41D8CD98F00B204E9800998ECF8427E")]
52+
[InlineData("Test", "0CBC6611F5540BD0809A388DC95A615B")]
53+
public void TestStringToMd5(string s, string md5)
54+
{
55+
_testOutputHelper.WriteLine(s.GetMd5());
56+
Assert.True(s.GetMd5() == md5);
57+
}
4958
}

src/ClipboardR.Core/DbHelper.cs

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/ClipboardR.Core/Extension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static BitmapImage ToBitmapImage(this Image img)
2525
public static string ToBase64(this Image img)
2626
{
2727
using var m = new MemoryStream();
28-
img.Save(m, img.RawFormat);
28+
img.Save(m, ImageFormat.Png);
2929
return Convert.ToBase64String(m.ToArray());
3030
}
3131

src/ClipboardR.Core/Struct.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,28 @@ public string GetMd5()
3434
{
3535
return Text.GetMd5() + DisplayTitle.GetMd5();
3636
}
37+
38+
public string DataToString()
39+
{
40+
string? dataString;
41+
switch (Type)
42+
{
43+
case CbMonitor.ContentTypes.Text:
44+
dataString = Data as string;
45+
break;
46+
case CbMonitor.ContentTypes.Image:
47+
var im = Data as Image;
48+
dataString = im?.ToBase64();
49+
break;
50+
case CbMonitor.ContentTypes.Files:
51+
dataString = Data is not string[] t ? "" : string.Join('\n', t);
52+
break;
53+
default:
54+
// don't process others
55+
throw new NotImplementedException(
56+
"Data to string for type not in Text, Image, Files are not implemented now.");
57+
}
58+
59+
return dataString ?? "";
60+
}
3761
}

0 commit comments

Comments
 (0)