Compilata da CSiCoAlm il 2021/11/07 - Corretto bug rilevato (ISSUE 01) - Inserita solita icona per i tools. - Sistemato la classe cTools inserendo tutti i tools trovati finora
284 lines
14 KiB
C#
284 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Data.SQLite;
|
|
|
|
|
|
namespace A69WD {
|
|
|
|
|
|
public class DBTools {
|
|
/*
|
|
|
|
Database: A69WD:
|
|
|
|
Tabella per storicizzare i link downloadati:
|
|
--------------------------------------------
|
|
CREATE TABLE [LinksLog] (
|
|
[URL] TEXT UNIQUE NOT NULL PRIMARY KEY,
|
|
[Tipo] TEXT NOT NULL,
|
|
[Status] INTEGER NOT NULL,
|
|
[LastUp] TIMESTAMP NOT NULL
|
|
)
|
|
|
|
*/
|
|
|
|
|
|
#region Class Fields & Property:
|
|
|
|
public System.Data.SQLite.SQLiteConnection cn { get; set; }
|
|
public bool DB_ok { get; set; }
|
|
|
|
#endregion
|
|
|
|
#region Costruttore e distruttore:
|
|
|
|
public DBTools() {
|
|
// Copio il DB dal file in memoria e mantengo la connessione per utilizzarla durante l'applicazione
|
|
object obj = new Object();
|
|
lock (obj) {
|
|
System.Data.SQLite.SQLiteConnection conn = null;
|
|
#region MyRegion
|
|
|
|
#endregion
|
|
// Check esistenza del DB dell'applicazione, altrimenti ne creo uno nuovo:
|
|
try {
|
|
if (!System.IO.File.Exists(System.IO.Path.Combine(Cfg.Settings.PathDB, Cfg.Settings.DBName))) {
|
|
// Creo un nuovo file di database e definisco le tabelle in uso.
|
|
conn = new SQLiteConnection(Cfg.Settings.DBConnectionString);
|
|
conn.Open();
|
|
SQLiteCommand cmd = new SQLiteCommand(CreaA69DB(), conn);
|
|
int res = cmd.ExecuteNonQuery();
|
|
if (res > 1) Logger.Log(new object[] { "Error Code: ER01012", string.Format("Possibile errore in DBTools() - cmd.ExecuteNonQuery(CREATE TABLE [CreaA69DB])={0}", res) });
|
|
conn.Close();
|
|
conn.Dispose();
|
|
}
|
|
} catch (Exception ex) {
|
|
Logger.Log(new object[] { "Error Code: ER01010", "Errore in ATLib.Data.DBtools() durante il Check DB", ex });
|
|
DB_ok = false;
|
|
return;
|
|
} finally {
|
|
if ((conn != null) && (conn.State != System.Data.ConnectionState.Closed)) conn.Close();
|
|
}
|
|
// Riporto in memoria il DB per poter lavorare ad alta velocità.
|
|
try {
|
|
// Apro DB in memoria
|
|
if (cn == null) cn = new System.Data.SQLite.SQLiteConnection("data source=:memory:");
|
|
cn.Open();
|
|
// Creo tabelle in uso
|
|
SQLiteCommand cmd = new SQLiteCommand(CreaA69DB(), cn);
|
|
int res = cmd.ExecuteNonQuery();
|
|
if (res > 1) Logger.Log(new object[] { "Error Code: ER01022", string.Format("Possibile errore in DBTools() - cmd.ExecuteNonQuery(CreaA69DB())={0}", res) });
|
|
// Attach DB A69DB su File
|
|
cmd = new SQLiteCommand(string.Format("ATTACH '{0}' AS A69DBfile", System.IO.Path.Combine(Cfg.Settings.PathDB, Cfg.Settings.DBName)), cn);
|
|
res = cmd.ExecuteNonQuery();
|
|
if (res > 1) Logger.Log(new object[] { "Error Code: ER01026b", string.Format("Possibile errore in DBTools() - cmd.ExecuteNonQuery(ATTACH {0} AS DBfile)", res) });
|
|
// Copio i dati
|
|
cmd = new SQLiteCommand("INSERT INTO [LinksLog] SELECT * FROM A69DBfile.LinksLog", cn);
|
|
res = cmd.ExecuteNonQuery();
|
|
if (res < 1) Logger.Information(string.Format("Info Code: IN01001 - Nessun LinksLog riportato sul DB in memoria.", res));
|
|
// Detach del DB su File
|
|
} catch (Exception ex) {
|
|
Logger.Log(new object[] { "Error Code: ER01020", "Errore in ATLib.Data.DBtools() durante il trasferimento in memoria del DB", ex });
|
|
DB_ok = false;
|
|
return;
|
|
}
|
|
// Memorizzo che il DB è stato correttamente caricato
|
|
DB_ok = true;
|
|
return;
|
|
}
|
|
}
|
|
|
|
public bool Dispose() {
|
|
object obj = new Object();
|
|
lock (obj) {
|
|
bool Res = false;
|
|
// Copio i valori creati: solo se il DB è OK:
|
|
if (!DB_ok) return Res;
|
|
try {
|
|
// Copio i dati saltando i doppioni
|
|
// Elimino i dati vecchi in A69DB:
|
|
SQLiteCommand cmd = new SQLiteCommand("DELETE FROM A69DBfile.LinksLog", cn);
|
|
int res = cmd.ExecuteNonQuery();
|
|
if (res < 1) Logger.Information(string.Format("Info Code: IN03001 - Nessun LinksLog cancellato dal file del DB.", res));
|
|
|
|
// Inserisco i nuovi dati in A69DB:
|
|
cmd = new SQLiteCommand("INSERT INTO A69DBfile.LinksLog SELECT * FROM LinksLog", cn);
|
|
res = cmd.ExecuteNonQuery();
|
|
if (res < 1) Logger.Information(string.Format("Info Code: IN03002 - Nessun LinksLog riportato sul file del DB.", res));
|
|
|
|
Res = true;
|
|
} catch (Exception ex) {
|
|
Logger.Log(new object[] { "Error Code: ER03000", "Errore in ATLib.Data.Dispose() durante il trasferimento in memoria del DB", ex });
|
|
} finally {
|
|
if ((cn != null) && (cn.State != System.Data.ConnectionState.Closed)) cn.Close();
|
|
}
|
|
// Eseguo la compressione del DB su file per evitare di usare troppa memoria:
|
|
System.Data.SQLite.SQLiteConnection conn = null;
|
|
// Check esistenza del DB dell'applicazione, altrimenti ne creo uno nuovo:
|
|
try {
|
|
conn = new SQLiteConnection(Cfg.Settings.DBConnectionString);
|
|
conn.Open();
|
|
SQLiteCommand cmd = new SQLiteCommand("VACUUM", conn);
|
|
int res = cmd.ExecuteNonQuery();
|
|
if (res > 1) Logger.Log(new object[] { "Error Code: IN03027", string.Format("Possibile errore in ATLib.Data.Dispose() - cmd.ExecuteNonQuery(VACUUM)={0}", res) });
|
|
conn.Close();
|
|
conn.Dispose();
|
|
Res = Res && true;
|
|
} catch (Exception ex) {
|
|
Logger.Log(new object[] { "Error Code: ER03003", "Errore in ATLib.Data.Dispose() durante la compressione del DB", ex });
|
|
Res = false;
|
|
} finally {
|
|
try { if ((conn != null) && (conn.State != System.Data.ConnectionState.Closed)) conn.Close(); } catch { }
|
|
}
|
|
return Res;
|
|
}
|
|
}
|
|
|
|
private string CreaA69DB() {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
#region Definisco il DB dalla struttura sopra riportata
|
|
sb.AppendLine("------------------------------------------------------------------------------");
|
|
sb.AppendLine("-- ");
|
|
sb.AppendLine("-- Creazione delle tabelle principali:");
|
|
sb.AppendLine("-- ");
|
|
sb.AppendLine("-- ");
|
|
sb.AppendLine("CREATE TABLE [LinksLog] ( -- COMMENT 'Tabella per la gestione di tutti i link'");
|
|
sb.AppendLine(" [URL] TEXT UNIQUE NOT NULL PRIMARY KEY, -- COMMENT 'URL del link'");
|
|
sb.AppendLine(" [Tipo] TEXT NOT NULL, -- COMMENT 'Tipologia del link: Idx=Link a un Indice, Rac=Link a un Racconto, Unk=Non Definito'");
|
|
sb.AppendLine(" [Status] INTEGER NOT NULL, -- COMMENT 'Stato del link: 1=ToCheck, 2=ToDownload, 3=OnDownload, 4=Downloaded'");
|
|
sb.AppendLine(" [LastUp] TIMESTAMP NOT NULL -- COMMENT 'Timestamp del record'");
|
|
sb.AppendLine(");");
|
|
sb.AppendLine("-- ");
|
|
sb.AppendLine("-- ");
|
|
sb.AppendLine("------------------------------------------------------------------------------");
|
|
#endregion
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
public int LoadData(List<string> allLinks, List<string> toDownload, List<string> downloaded, List<string> racLinks) {
|
|
object obj = new Object();
|
|
lock (obj) {
|
|
int Res = 0;
|
|
try {
|
|
|
|
// Carico tutti i racconti non ancora downloadati completamente:
|
|
System.Data.SQLite.SQLiteCommand cmd = new SQLiteCommand("SELECT [URL], [Tipo], [Status], [LastUp] FROM [LinksLog] WHERE [Tipo]='Rac' AND [Status]<>4", cn); // Donwloaded = 4
|
|
SQLiteDataReader rdr = cmd.ExecuteReader();
|
|
while (rdr.Read()) {
|
|
string url = rdr["URL"] as string;
|
|
allLinks.Add(url);
|
|
toDownload.Add(url);
|
|
racLinks.Add(url);
|
|
Res++;
|
|
}
|
|
|
|
// Carico ora tutti i racconti downloadati completamente per evitare doppioni:
|
|
cmd = new SQLiteCommand("SELECT [URL], [Tipo], [Status], [LastUp] FROM [LinksLog] WHERE [Tipo]='Rac' AND [Status]=4", cn); // Donwloaded = 4
|
|
rdr = cmd.ExecuteReader();
|
|
while (rdr.Read()) {
|
|
string url = rdr["URL"] as string;
|
|
allLinks.Add(url);
|
|
racLinks.Add(url);
|
|
Res++;
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
Logger.Log(new object[] { "Error Code: DB07003", "Errore in Data.DBTools.LoadData()", ex });
|
|
if (Res > 0) Res = -1 * Res;
|
|
}
|
|
return Res;
|
|
}
|
|
}
|
|
|
|
public int SaveData(List<string> allLinks, List<string> toDownload, List<string> downloaded, List<string> racLinks) {
|
|
object obj = new Object();
|
|
lock (obj) {
|
|
int Res = 0;
|
|
try {
|
|
foreach (string link in racLinks) { LinkLogUpdtate(link, cA69Engine.LinkType(link), eLinkState.Donwloaded); }
|
|
foreach (string link in toDownload) { LinkLogUpdtate(link, cA69Engine.LinkType(link), eLinkState.ToDownload); }
|
|
foreach (string link in downloaded) { LinkLogUpdtate(link, cA69Engine.LinkType(link), eLinkState.Donwloaded); }
|
|
} catch (Exception ex) {
|
|
Logger.Log(new object[] { "Error Code: DB07003", "Errore in Data.DBTools.LoadData()", ex });
|
|
if (Res > 0) Res = -1 * Res;
|
|
}
|
|
return Res;
|
|
}
|
|
}
|
|
private bool LinkLogUpdtate(string url, string tip, eLinkState st) {
|
|
// object obj = new Object();
|
|
// lock (obj) {
|
|
// Cancello riga (se non presente, non fa nulla)
|
|
// Inserisco riga
|
|
bool Res = false;
|
|
try {
|
|
// Cancello il link anche se non presente
|
|
System.Data.SQLite.SQLiteCommand cmd = new SQLiteCommand(string.Format("DELETE FROM [LinksLog] WHERE [URL]='{0}'", url), cn);
|
|
int res = cmd.ExecuteNonQuery();
|
|
cmd = null;
|
|
// Inserisco il link come se fosse nuovo
|
|
cmd = new System.Data.SQLite.SQLiteCommand();
|
|
cmd.Connection = cn;
|
|
cmd.CommandText = "INSERT INTO LinksLog (URL,Tipo,Status,LastUp)VALUES (@URL,@Tipo,@Status,@LastUp)";
|
|
cmd.CommandType = System.Data.CommandType.Text;
|
|
System.Data.SQLite.SQLiteParameter param = new System.Data.SQLite.SQLiteParameter();
|
|
param.ParameterName = "@URL";
|
|
param.DbType = global::System.Data.DbType.String;
|
|
param.SourceColumn = "URL";
|
|
cmd.Parameters.Add(param);
|
|
param = new System.Data.SQLite.SQLiteParameter();
|
|
param.ParameterName = "@Tipo";
|
|
param.DbType = global::System.Data.DbType.String;
|
|
param.SourceColumn = "Tipo";
|
|
cmd.Parameters.Add(param);
|
|
param = new System.Data.SQLite.SQLiteParameter();
|
|
param.ParameterName = "@Status";
|
|
param.DbType = global::System.Data.DbType.Int32;
|
|
param.SourceColumn = "Status";
|
|
cmd.Parameters.Add(param);
|
|
param = new System.Data.SQLite.SQLiteParameter();
|
|
param.ParameterName = "@LastUp";
|
|
param.DbType = global::System.Data.DbType.DateTime;
|
|
param.SourceColumn = "LastUp";
|
|
cmd.Parameters.Add(param);
|
|
cmd.Parameters[0].Value = url;
|
|
cmd.Parameters[1].Value = tip;
|
|
cmd.Parameters[2].Value = st;
|
|
//if (InsertDate) { cmd.Parameters[3].Value = DateTime.Now; } else { cmd.Parameters[3].Value = DateTime.MaxValue; }
|
|
cmd.Parameters[3].Value = DateTime.Now;
|
|
res = cmd.ExecuteNonQuery();
|
|
if (res != 1) {
|
|
Logger.Log(new object[] { "Error Code: DB06010", string.Format("DBTools.LinkLogUpdtate({0})={1}", url, res) });
|
|
Res = false;
|
|
} else {
|
|
Res = true;
|
|
}
|
|
} catch (Exception ex) {
|
|
Logger.Log(new object[] { "Error Code: DB06001", string.Format("DBTools.LinkLogUpdtate({0})", url), ex });
|
|
return false;
|
|
} finally {
|
|
//if ((cn != null) && (cn.State != System.Data.ConnectionState.Closed)) cn.Close();
|
|
}
|
|
return Res;
|
|
// } // lock (obj)
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // class DBTools
|
|
|
|
|
|
}//namespace A69WD
|