feat: Refactor database access to use parameterized queries and improve error handling; update launch settings and add VSCode configuration files

This commit is contained in:
jim
2025-11-21 16:26:33 -06:00
parent 81e345b36f
commit 2640854f79
9 changed files with 216 additions and 75 deletions
+41
View File
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/URLNotesGrabberCORE.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/URLNotesGrabberCORE.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/URLNotesGrabberCORE.sln"
],
"problemMatcher": "$msCompile"
}
]
}
+123 -66
View File
@@ -194,10 +194,12 @@ namespace URLNotesGrabberCORE
{
connection.Open();
string sql = "INSERT INTO DailyAPICount (Date) values('" + DateTime.Today.ToShortDateString() + "')";
SQLiteCommand command = new SQLiteCommand(sql, connection);
command.ExecuteNonQuery();
string sql = "INSERT INTO DailyAPICount (Date) values(@date)";
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
command.Parameters.AddWithValue("@date", DateTime.Today.ToShortDateString());
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
@@ -222,10 +224,17 @@ namespace URLNotesGrabberCORE
{
connection.Open();
string sql = "INSERT INTO Notes (rootBlogName, noteBlogName, PostID, TimeStamp, Type) values('" + rootBlogName + "', '" + noteBlogName + "', " + postID + ", " + timestamp + ", '" + type + "')";
SQLiteCommand command = new SQLiteCommand(sql, connection);
string sql = "INSERT INTO Notes (rootBlogName, noteBlogName, PostID, TimeStamp, Type) values(@rootBlogName, @noteBlogName, @PostID, @TimeStamp, @Type)";
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
command.Parameters.AddWithValue("@rootBlogName", rootBlogName);
command.Parameters.AddWithValue("@noteBlogName", noteBlogName);
command.Parameters.AddWithValue("@PostID", postID);
command.Parameters.AddWithValue("@TimeStamp", timestamp);
command.Parameters.AddWithValue("@Type", type ?? string.Empty);
command.ExecuteNonQuery();
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
@@ -377,15 +386,11 @@ namespace URLNotesGrabberCORE
{
connection.Open();
string sql = "SELECT " +
" APICount " +
"FROM " +
" DailyAPICount " +
" WHERE [Date] = '" +
DateTime.Today.ToShortDateString() + "'";
string sql = "SELECT APICount FROM DailyAPICount WHERE [Date] = @date";
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
command.Parameters.AddWithValue("@date", DateTime.Today.ToShortDateString());
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
@@ -417,11 +422,15 @@ namespace URLNotesGrabberCORE
connection.Open();
string sql = "";
if (reblogsOnly)
sql = "SELECT NoteBlogName as blogName, count(*) from notes inner join blogs on blogs.BlogName = notes.NoteBlogName where type IN ('reblog', 'reply', 'posted') and HasBeenOutput = 0 group by NoteBlogName ORDER BY count(*) desc, BlogName LIMIT " + top;
sql = "SELECT NoteBlogName as blogName, count(*) FROM notes INNER JOIN blogs ON blogs.BlogName = notes.NoteBlogName WHERE blogs.IsActive = @isActive AND type IN ('reblog', 'reply', 'posted') AND HasBeenOutput = 0 GROUP BY NoteBlogName ORDER BY count(*) DESC, BlogName LIMIT @top";
else
sql = "SELECT NoteBlogName as blogName, count(*) from notes inner join blogs on blogs.BlogName = notes.NoteBlogName where type NOT IN ('reblog', 'reply', 'posted') and HasBeenOutput = 0 group by NoteBlogName ORDER BY count(*) desc, BlogName LIMIT " + top;
sql = "SELECT NoteBlogName as blogName, count(*) FROM notes INNER JOIN blogs ON blogs.BlogName = notes.NoteBlogName WHERE blogs.IsActive = @isActive AND type NOT IN ('reblog', 'reply', 'posted') AND HasBeenOutput = 0 GROUP BY NoteBlogName ORDER BY count(*) DESC, BlogName LIMIT @top";
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
command.Parameters.AddWithValue("@isActive", 1);
command.Parameters.AddWithValue("@top", top);
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
@@ -456,11 +465,15 @@ namespace URLNotesGrabberCORE
connection.Open();
string sql = "";
if (reblogsOnly)
sql = "SELECT NoteBlogName as blogName, count(*) from notes inner join blogs on blogs.BlogName = notes.NoteBlogName where type IN ('reblog', 'reply', 'posted') and HasBeenOutput = 0 group by NoteBlogName ORDER BY count(*) desc, BlogName LIMIT " + top;
sql = "SELECT NoteBlogName as blogName, count(*) FROM notes INNER JOIN blogs ON blogs.BlogName = notes.NoteBlogName WHERE blogs.IsActive = @isActive AND type IN ('reblog', 'reply', 'posted') AND HasBeenOutput = 0 GROUP BY NoteBlogName ORDER BY count(*) DESC, BlogName LIMIT @top";
else
sql = "SELECT NoteBlogName as blogName, count(*) from notes inner join blogs on blogs.BlogName = notes.NoteBlogName where HasBeenOutput = 0 group by NoteBlogName ORDER BY count(*) desc, BlogName LIMIT " + top;
sql = "SELECT NoteBlogName as blogName, count(*) FROM notes INNER JOIN blogs ON blogs.BlogName = notes.NoteBlogName WHERE blogs.IsActive = @isActive AND HasBeenOutput = 0 GROUP BY NoteBlogName ORDER BY count(*) DESC, BlogName LIMIT @top";
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
command.Parameters.AddWithValue("@isActive", 1);
command.Parameters.AddWithValue("@top", top);
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
@@ -497,10 +510,14 @@ namespace URLNotesGrabberCORE
{
connection.Open();
string sql = "UPDATE Posts SET HasNotesGathered = 1, NotesGatheredDateTime = " + DateTimeOffset.UtcNow.ToUnixTimeSeconds() + " WHERE BlogName = '" + blogName + "' AND PostID = " + postID;
SQLiteCommand command = new SQLiteCommand(sql, connection);
command.ExecuteNonQuery();
string sql = "UPDATE Posts SET HasNotesGathered = 1, NotesGatheredDateTime = @notesGathered WHERE BlogName = @BlogName AND PostID = @PostID";
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
command.Parameters.AddWithValue("@notesGathered", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
command.Parameters.AddWithValue("@BlogName", blogName);
command.Parameters.AddWithValue("@PostID", postID);
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
@@ -521,10 +538,13 @@ namespace URLNotesGrabberCORE
{
connection.Open();
string sql = "UPDATE Posts SET NotFound = 1 WHERE BlogName = '" + blogName + "' AND PostID = " + postID;
SQLiteCommand command = new SQLiteCommand(sql, connection);
command.ExecuteNonQuery();
string sql = "UPDATE Posts SET NotFound = 1 WHERE BlogName = @BlogName AND PostID = @PostID";
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
command.Parameters.AddWithValue("@BlogName", blogName);
command.Parameters.AddWithValue("@PostID", postID);
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
@@ -545,10 +565,14 @@ namespace URLNotesGrabberCORE
{
connection.Open();
string sql = "UPDATE Posts SET postDate = '" + postDate + "' WHERE BlogName = '" + blogName + "' AND PostID = " + postID;
SQLiteCommand command = new SQLiteCommand(sql, connection);
command.ExecuteNonQuery();
string sql = "UPDATE Posts SET postDate = @postDate WHERE BlogName = @BlogName AND PostID = @PostID";
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
command.Parameters.AddWithValue("@postDate", postDate ?? string.Empty);
command.Parameters.AddWithValue("@BlogName", blogName);
command.Parameters.AddWithValue("@PostID", postID);
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
@@ -574,10 +598,15 @@ namespace URLNotesGrabberCORE
{
connection.Open();
string sql = string.Format("UPDATE Notes SET timestamp = '{0}' WHERE rootBlogName = '{1}' AND noteBlogName = '{2}' AND PostID = '{3}'", timestamp, rootBlogName, noteBlogName, postID);
SQLiteCommand command = new SQLiteCommand(sql, connection);
command.ExecuteNonQuery();
string sql = "UPDATE Notes SET timestamp = @timestamp WHERE rootBlogName = @rootBlogName AND noteBlogName = @noteBlogName AND PostID = @postID";
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
command.Parameters.AddWithValue("@timestamp", timestamp);
command.Parameters.AddWithValue("@rootBlogName", rootBlogName);
command.Parameters.AddWithValue("@noteBlogName", noteBlogName);
command.Parameters.AddWithValue("@postID", postID);
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
@@ -603,30 +632,54 @@ namespace URLNotesGrabberCORE
{
connection.Open();
string sql = "UPDATE Posts ";
sql += "SET postDate = " + Q(postDate) + ", ";
sql += "reblogURL = " + Q(reblogURL) + ", ";
sql += " postURL = " + Q(postURL) + ", ";
sql += " slug = " + Q(slug) + ", ";
sql += " reblogKey = " + Q(reblogKey) + ", ";
sql += " reblogName = " + Q(reblogName) + ", ";
sql += " summary = " + Q(summary) + ", ";
sql += " quote = " + Q(quote) + ", ";
sql += " body = " + Q(body) + ", ";
sql += " tags = " + Q(tags) + ", ";
sql += " link = " + Q(link) + ", ";
sql += " photoURL = " + Q(photoURL) + ", ";
sql += " photoCaption = " + Q(photoCaption) + ", ";
sql += " downloadedFiles = " + Q(downloadedFiles) + ", ";
sql += " audioCaption = " + Q(audioCaption) + ", ";
sql += " question = " + Q(question) + ", ";
sql += " answer = " + Q(answer) + ", ";
sql += " title = " + Q(title) + ", ";
sql += " hasImage = " + hasImage;
sql += " WHERE BlogName = " + Q(blogName) + " AND PostID = " + postID;
SQLiteCommand command = new SQLiteCommand(sql, connection);
string sql = "UPDATE Posts SET ";
sql += "postDate = @postDate, ";
sql += "reblogURL = @reblogURL, ";
sql += "postURL = @postURL, ";
sql += "slug = @slug, ";
sql += "reblogKey = @reblogKey, ";
sql += "reblogName = @reblogName, ";
sql += "summary = @summary, ";
sql += "quote = @quote, ";
sql += "body = @body, ";
sql += "tags = @tags, ";
sql += "link = @link, ";
sql += "photoURL = @photoURL, ";
sql += "photoCaption = @photoCaption, ";
sql += "downloadedFiles = @downloadedFiles, ";
sql += "audioCaption = @audioCaption, ";
sql += "question = @question, ";
sql += "answer = @answer, ";
sql += "title = @title, ";
sql += "hasImage = @hasImage ";
sql += " WHERE BlogName = @BlogName AND PostID = @PostID";
command.ExecuteNonQuery();
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
command.Parameters.AddWithValue("@postDate", postDate ?? string.Empty);
command.Parameters.AddWithValue("@reblogURL", reblogURL ?? string.Empty);
command.Parameters.AddWithValue("@postURL", postURL ?? string.Empty);
command.Parameters.AddWithValue("@slug", slug ?? string.Empty);
command.Parameters.AddWithValue("@reblogKey", reblogKey ?? string.Empty);
command.Parameters.AddWithValue("@reblogName", reblogName ?? string.Empty);
command.Parameters.AddWithValue("@summary", summary ?? string.Empty);
command.Parameters.AddWithValue("@quote", quote ?? string.Empty);
command.Parameters.AddWithValue("@body", body ?? string.Empty);
command.Parameters.AddWithValue("@tags", tags ?? string.Empty);
command.Parameters.AddWithValue("@link", link ?? string.Empty);
command.Parameters.AddWithValue("@photoURL", photoURL ?? string.Empty);
command.Parameters.AddWithValue("@photoCaption", photoCaption ?? string.Empty);
command.Parameters.AddWithValue("@downloadedFiles", downloadedFiles ?? string.Empty);
command.Parameters.AddWithValue("@audioCaption", audioCaption ?? string.Empty);
command.Parameters.AddWithValue("@question", question ?? string.Empty);
command.Parameters.AddWithValue("@answer", answer ?? string.Empty);
command.Parameters.AddWithValue("@title", title ?? string.Empty);
command.Parameters.AddWithValue("@hasImage", hasImage ? 1 : 0);
command.Parameters.AddWithValue("@BlogName", blogName);
command.Parameters.AddWithValue("@PostID", postID);
command.ExecuteNonQuery();
}
}
catch (Exception ex)
@@ -648,10 +701,12 @@ namespace URLNotesGrabberCORE
{
connection.Open();
string sql = "UPDATE Blogs SET HasBeenOutput = 1 WHERE BlogName = '" + blogName + "'";
SQLiteCommand command = new SQLiteCommand(sql, connection);
command.ExecuteNonQuery();
string sql = "UPDATE Blogs SET HasBeenOutput = 1 WHERE BlogName = @BlogName";
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
command.Parameters.AddWithValue("@BlogName", blogName);
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
@@ -675,12 +730,13 @@ namespace URLNotesGrabberCORE
{
connection.Open();
string sql = "UPDATE DailyAPICount SET APICount = " + APICount +
" WHERE [Date] = '" +
DateTime.Today.ToShortDateString() + "'";
SQLiteCommand command = new SQLiteCommand(sql, connection);
command.ExecuteNonQuery();
string sql = "UPDATE DailyAPICount SET APICount = @APICount WHERE [Date] = @date";
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
command.Parameters.AddWithValue("@APICount", APICount);
command.Parameters.AddWithValue("@date", DateTime.Today.ToShortDateString());
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
@@ -753,6 +809,7 @@ namespace URLNotesGrabberCORE
if (deserializedResult != null)
{
myDeserializedClass = deserializedResult;
myDeserializedClass.rawJson = myJsonResponse;
}
}
catch (Exception ex)
+2 -7
View File
@@ -364,6 +364,7 @@ namespace URLNotesGrabberCORE
else if (response.response == null)
{
Console.WriteLine("##### Response Null - API Failure? ###");
Console.WriteLine($"Raw JSON: {response.rawJson}");
return "FAILURE";
}
else if (response.response.notes == null)
@@ -373,13 +374,7 @@ namespace URLNotesGrabberCORE
}
else
{
if (response.response == null)
{
Console.WriteLine("response.response == null");
return "FAILURE";
}
while (response.response != null
while (response.response != null
&& response.response._links != null
&& long.Parse(response.response._links.next.query_params.before_timestamp) >= post.Item3)
{
@@ -2,7 +2,7 @@
"profiles": {
"URLNotesGrabberCORE": {
"commandName": "Project",
"commandLineArgs": "-collect 1"
"commandLineArgs": "-collect 0"
}
}
}
+2
View File
@@ -80,5 +80,7 @@ namespace URLNotesGrabberCORE
public string statusCode { get; set; }
public int retryInSeconds { get; set; }
public string rawJson { get; set; }
}
}
Binary file not shown.