From c899f5591e2942e2a48f1c382e5f6a215bbb767d Mon Sep 17 00:00:00 2001 From: Litfal Date: Tue, 1 Oct 2024 15:27:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AA=BF=E6=95=B4=20db=20=E4=B8=A6=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=20db=20=E5=8D=87=E7=B4=9A=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EHDownloader/Book.cs | 35 +++++++---- EHDownloader/DbContext.cs | 102 ++++++++++++++++++++++++++----- EHDownloader/EHDownloader.csproj | 6 ++ EHDownloader/Form1.cs | 14 ++--- 4 files changed, 121 insertions(+), 36 deletions(-) diff --git a/EHDownloader/Book.cs b/EHDownloader/Book.cs index 952975f..69c2c03 100644 --- a/EHDownloader/Book.cs +++ b/EHDownloader/Book.cs @@ -6,6 +6,7 @@ using System.ComponentModel; using System.Linq; using System.Net; using System.Net.Http; +using System.Runtime.InteropServices.WindowsRuntime; using System.Text; using System.Threading.Tasks; @@ -75,7 +76,7 @@ namespace EHDownloader public int PageCount => Pages.Count; - public BookStates BookState { get; set; } = BookStates.Wait; + public BookStates State { get; set; } = BookStates.Wait; public Book(string url, string parentFolder) @@ -105,7 +106,7 @@ namespace EHDownloader } catch (Exception ex) { - BookState = BookStates.LoadFailed; + State = BookStates.LoadFailed; return FetchBookResults.LoadPageFailed; } @@ -115,7 +116,7 @@ namespace EHDownloader } catch { - BookState = BookStates.FetchTitleFailed; + State = BookStates.FetchTitleFailed; return FetchBookResults.FetchTitleFailed; } @@ -125,7 +126,7 @@ namespace EHDownloader var htmlPageSets = doc.DocumentNode.SelectNodes(PageSetXPath); if (htmlPageSets == null) { - BookState = BookStates.FetchPageSetFailed; + State = BookStates.FetchPageSetFailed; return FetchBookResults.FetchPageSetFailed; } foreach (var htmlPageSet in htmlPageSets) @@ -142,7 +143,7 @@ namespace EHDownloader } catch { - BookState = BookStates.FetchPageSetFailed; + State = BookStates.FetchPageSetFailed; return FetchBookResults.FetchTitleFailed; } @@ -170,7 +171,7 @@ namespace EHDownloader } catch { - BookState = BookStates.FetchPageLinkFailed; + State = BookStates.FetchPageLinkFailed; return FetchBookResults.FetchPageLinkFailed; } @@ -180,7 +181,7 @@ namespace EHDownloader Pages[i].PageNumber = i + 1; } - BookState = BookStates.FetchCompleted; + State = BookStates.FetchCompleted; return FetchBookResults.Completed; } @@ -218,15 +219,16 @@ namespace EHDownloader if (Pages.All(p => p.Result == DownloadPageResults.Completed)) { - BookState = BookStates.DownloadCompleted; + State = BookStates.DownloadCompleted; return; } } - BookState = BookStates.DownloadFinishedButSomePageError; + State = BookStates.DownloadFinishedButSomePageError; } - private string GetFolderName() + private string getFolderName() { + if (Title == null) return null; string folderName = Title; foreach (var ch in System.IO.Path.GetInvalidFileNameChars()) folderName = folderName.Replace(ch, ' '); @@ -251,7 +253,7 @@ namespace EHDownloader public async Task CompressAsync() { string folderPath = getFolderPath(); - if (BookState != BookStates.DownloadCompleted) + if (State != BookStates.DownloadCompleted) return; if (!System.IO.Directory.Exists(ParentFolder)) return; @@ -271,12 +273,19 @@ namespace EHDownloader System.IO.Directory.Delete(folderPath, true); } - BookState = BookStates.Compressed; + State = BookStates.Compressed; } private string getFolderPath() { - return System.IO.Path.Combine(ParentFolder, GetFolderName()); + string nane = getFolderName(); + if (nane == null) return null; + return System.IO.Path.Combine(ParentFolder, nane); + } + + public string Path + { + get => getFolderPath(); } } diff --git a/EHDownloader/DbContext.cs b/EHDownloader/DbContext.cs index cd750b7..945ef00 100644 --- a/EHDownloader/DbContext.cs +++ b/EHDownloader/DbContext.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Data.SQLite; using System.IO; using System.Linq; +using System.Security.Policy; using System.Text; using System.Threading.Tasks; @@ -13,23 +14,82 @@ namespace EHDownloader { public DbContext() { - if (File.Exists(Settings.GetDbPath())) return; - - using (var cn = new SQLiteConnection(Settings.GetConnectString())) + if (File.Exists(Settings.GetDbPath())) { - cn.Execute(@" - CREATE TABLE Book ( - Url VARCHAR(256), - Title VARNCHAR(256), - PageCount smallint, - DownloadDateTime datetime, - CONSTRAINT Book_PK PRIMARY KEY (Url) - )"); - + checkDB(); + } + else + { + createNewDB(); } } - public bool PageIsExists(string url) + private void checkDB() + { + int DBVer = getDBSchemaVersion(); + if(DBVer == 0) + { + updateDBSchema(DBVer); + } + } + + private void updateDBSchema(int fromVer) + { + using (var cn = new SQLiteConnection(Settings.GetConnectString())) + { + switch (fromVer) + { + case 0: + { + // add App table and ver=1 + cn.Execute(@" + CREATE TABLE App (DBSchemaVersion INTEGER NOT NULL); + INSERT INTO App (DBSchemaVersion) VALUES (1); + ALTER TABLE Book ADD COLUMN State INTEGER NOT NULL DEFAULT 3; + ALTER TABLE Book ADD COLUMN Path TEXT; + "); + } + break; + } + } + } + + private static void createNewDB() + { + using (var cn = new SQLiteConnection(Settings.GetConnectString())) + { + cn.Execute(@" + CREATE TABLE ""Book"" ( + ""Url"" VARCHAR(256), + ""Title"" VARNCHAR(256), + ""PageCount"" smallint, + ""DownloadDateTime"" datetime, + ""State"" INTEGER NOT NULL, + ""Path"" TEXT, + CONSTRAINT ""Book_PK"" PRIMARY KEY(""Url"") + ); + CREATE TABLE ""App"" ( + ""DBSchemaVersion"" INTEGER NOT NULL + ); + INSERT INTO App (DBSchemaVersion) VALUES (1); + "); + } + } + + private int getDBSchemaVersion() + { + using (var cn = new SQLiteConnection(Settings.GetConnectString())) + { + // check App table is exists + var query_table_exists = "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'App'"; + bool app_table_is_exists = cn.Query(query_table_exists).Any(); + if (!app_table_is_exists) return 0; + var query = "select DBSchemaVersion from App"; + return cn.Query(query).FirstOrDefault(); + } + } + + public bool BookIsExists(string url) { using (var cn = new SQLiteConnection(Settings.GetConnectString())) { @@ -39,14 +99,24 @@ namespace EHDownloader } } - public void InsertPage(Book book) + public void InsertBook(Book book) { using (var cn = new SQLiteConnection(Settings.GetConnectString())) - { + { var insertScript = - "INSERT INTO Book VALUES (@Url,@Title,@PageCount,@DownloadDateTime)"; + "INSERT INTO Book VALUES (@Url,@Title,@PageCount,@DownloadDateTime,@State,@Path)"; cn.Execute(insertScript, book); } } + + public void updateBookStatus(Book book) + { + using (var cn = new SQLiteConnection(Settings.GetConnectString())) + { + var updateScript = + "UPDATE Book SET (State=@State) WHERE Url=@Url"; + cn.Execute(updateScript, book); + } + } } } diff --git a/EHDownloader/EHDownloader.csproj b/EHDownloader/EHDownloader.csproj index ea06cd0..41b2194 100644 --- a/EHDownloader/EHDownloader.csproj +++ b/EHDownloader/EHDownloader.csproj @@ -114,4 +114,10 @@ + + true + false + false + false + \ No newline at end of file diff --git a/EHDownloader/Form1.cs b/EHDownloader/Form1.cs index 1799651..8ecb9ca 100644 --- a/EHDownloader/Form1.cs +++ b/EHDownloader/Form1.cs @@ -106,7 +106,7 @@ namespace EHDownloader { if (!isEhUrl(url)) return; if (lv_Tasks.Items.ContainsKey(url)) return; - if (_dbContext.PageIsExists(url)) return; + if (_dbContext.BookIsExists(url)) return; ListViewItem lvi = new ListViewItem(url) { Name = url }; lvi.SubItems.Add("Wait"); @@ -135,8 +135,8 @@ namespace EHDownloader while (running) { - lvi.SubItems[1].Text = book.BookState.GetDescription(); - switch (book.BookState) + lvi.SubItems[1].Text = book.State.GetDescription(); + switch (book.State) { case BookStates.Wait: await book.FetchAsync(); @@ -147,7 +147,7 @@ namespace EHDownloader break; case BookStates.DownloadCompleted: await book.CompressAsync(); - _dbContext.InsertPage(book); + _dbContext.InsertBook(book); goto default; case BookStates.Compressed: case BookStates.LoadFailed: @@ -178,7 +178,7 @@ namespace EHDownloader { var lvi = lv_Tasks.Items[i]; var book = (Book)lvi.Tag; - switch (book.BookState) + switch (book.State) { case BookStates.Wait: case BookStates.FetchCompleted: @@ -190,10 +190,10 @@ namespace EHDownloader case BookStates.FetchTitleFailed: case BookStates.FetchPageSetFailed: case BookStates.FetchPageLinkFailed: - book.BookState = BookStates.Wait; + book.State = BookStates.Wait; continue; case BookStates.DownloadFinishedButSomePageError: - book.BookState = BookStates.FetchCompleted; + book.State = BookStates.FetchCompleted; continue; } }