using CommonBase.Properties;
|
using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Data;
|
using System.Data.SQLite;
|
using System.IO;
|
|
namespace CommonBase
|
{
|
public class Db1
|
{
|
static string path=null;
|
static Stack<string> stack = new Stack<string>();
|
static Db1()
|
{
|
|
}
|
public static void Init(string path0=null)
|
{
|
path = path0;
|
if (path == null)
|
path = AppDomain.CurrentDomain.BaseDirectory + "config.wdb";
|
|
//检查数据库是否存在,不在则新建
|
if (!File.Exists(path))
|
File.WriteAllBytes(path, Resources.Db);
|
|
//初始化数据库连接串
|
Connection = new SQLiteConnection($"Data Source={path};Pooling=true;FailIfMissing=false");
|
Connection.Open();
|
}
|
|
public static IDbConnection Connection;
|
public static void Save()
|
{
|
if (!Directory.Exists("backup"))
|
{
|
Directory.CreateDirectory("backup");
|
}
|
|
string backupPath = $"backup/{Path.GetFileName(path)}.{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.bak";
|
if (File.Exists(path))
|
{
|
File.Copy(path, backupPath);
|
stack.Push(backupPath);
|
}
|
}
|
|
public static void Undo()
|
{
|
if (CanUndo())
|
{
|
Save();
|
string lastBackupPath = stack.Pop();
|
if (File.Exists(lastBackupPath))
|
{
|
File.Copy(lastBackupPath, path, true);
|
}
|
}
|
}
|
|
public static void Redo()
|
{
|
if (CanRedo())
|
{
|
Save();
|
string nextBackupPath = stack.Pop();
|
if (File.Exists(nextBackupPath))
|
{
|
File.Copy(nextBackupPath, path, true);
|
}
|
}
|
}
|
|
public static bool CanUndo()
|
{
|
return stack.Count > 0;
|
}
|
|
public static bool CanRedo()
|
{
|
return stack.Count > 1;
|
}
|
|
public static void ClearBackup()
|
{
|
if (Directory.Exists("backup"))
|
{
|
Directory.Delete("backup", true);
|
}
|
|
stack.Clear();
|
}
|
}
|
|
|
|
public class Db
|
{
|
public static Db Instance;
|
|
string path = null;
|
Stack<string> stack = new Stack<string>();
|
public Db()
|
{
|
|
}
|
public void Init(string path0 = null)
|
{
|
path = path0;
|
if (path == null)
|
path = AppDomain.CurrentDomain.BaseDirectory + "config.wdb";
|
if (GlobalPath.Path==null )
|
{
|
FileInfo fileInfo = new FileInfo(path);
|
GlobalPath.Path = fileInfo.DirectoryName;
|
|
}
|
|
|
//检查数据库是否存在,不在则新建
|
if (!File.Exists(path))
|
File.WriteAllBytes(path, Resources.Db);
|
|
// 移除只读属性
|
FileAttributes attributes = File.GetAttributes(path);
|
attributes &= ~FileAttributes.ReadOnly;
|
File.SetAttributes(path, attributes);
|
|
//初始化数据库连接串
|
Connection = new SQLiteConnection($"Data Source={path};Pooling=true;FailIfMissing=false");
|
Connection.Open();
|
}
|
|
public void Close()
|
{
|
if (Connection!=null)
|
this.Connection.Close();
|
}
|
public IDbConnection Connection;
|
public void Save()
|
{
|
if (!Directory.Exists("backup"))
|
{
|
Directory.CreateDirectory("backup");
|
}
|
|
string backupPath = $"backup/{Path.GetFileName(path)}.{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.bak";
|
if (File.Exists(path))
|
{
|
File.Copy(path, backupPath);
|
stack.Push(backupPath);
|
}
|
}
|
|
public void Undo()
|
{
|
if (CanUndo())
|
{
|
Save();
|
string lastBackupPath = stack.Pop();
|
if (File.Exists(lastBackupPath))
|
{
|
File.Copy(lastBackupPath, path, true);
|
}
|
}
|
}
|
|
public void Redo()
|
{
|
if (CanRedo())
|
{
|
Save();
|
string nextBackupPath = stack.Pop();
|
if (File.Exists(nextBackupPath))
|
{
|
File.Copy(nextBackupPath, path, true);
|
}
|
}
|
}
|
|
public bool CanUndo()
|
{
|
return stack.Count > 0;
|
}
|
|
public bool CanRedo()
|
{
|
return stack.Count > 1;
|
}
|
|
public void ClearBackup()
|
{
|
if (Directory.Exists("backup"))
|
{
|
Directory.Delete("backup", true);
|
}
|
|
stack.Clear();
|
}
|
}
|
}
|