using DryIoc.ImTools;
using Microsoft.EntityFrameworkCore;
using PLCSharp.Core.Common;
using PLCSharp.VVMs.Authority;
using PLCSharp.VVMs.Connects;
using PLCSharp.VVMs.GlobalVariables;
using PLCSharp.VVMs.Homepage;
using PLCSharp.VVMs.MotionController;
using PLCSharp.VVMs.Recipe;
using PLCSharp.VVMs.Robots;
using PLCSharp.VVMs.Vision;
using PLCSharp.VVMs.Vision.Camera;
using PLCSharp.VVMs.Workflows;
namespace PLCSharp.Models
{
///
/// DatasContext
///
public class DatasContext : DbContext
{
///
/// DatasContext
///
public DatasContext()
{
this.Database.EnsureCreated();
this.EnsureCreatingMissingTables();
}
///
/// Users
///
public DbSet Users { get; set; }
///
/// Recipes
///
public DbSet Recipes { get; set; }
///
/// 系统全局变量集合
///
public DbSet SystemVariables { get; set; }
///
/// 用户全局变量集合
///
public DbSet Variables { get; set; }
///
/// Workflows
///
public DbSet Workflows { get; set; }
///
/// VisionFunctions
///
public DbSet VisionFunctions { get; set; }
///
/// 全局图像列表
///
public DbSet ImageDatas { get; set; }
///
/// 连接模型
///
public DbSet Connects { get; set; }
///
/// Cameras
///
public DbSet Cameras { get; set; }
///
/// Controllers
///
public DbSet Controllers { get; set; }
///
/// Axes
///
public DbSet Axes { get; set; }
///
/// 轴Points
///
public DbSet AxisPoints { get; set; }
///
/// 轴Points
///
public DbSet Matrices { get; set; }
///
/// 插补Groups
///
public DbSet InterpolationGroups { get; set; }
///
/// DI
///
public DbSet DI { get; set; }
///
/// DQ
///
public DbSet DQ { get; set; }
///
/// Robots
///
public DbSet Robots { get; set; }
///
/// 机器人Points
///
public DbSet RobotPoints { get; set; }
///
/// 机器人矩阵
///
public DbSet RobotMatrices { get; set; }
///
/// 错误Logs
///
public DbSet ErrorLogs { get; set; }
///
/// CustomControls
///
public DbSet CustomControls { get; set; }
///
/// CanvasConfigs
///
public DbSet CanvasConfigs { get; set; }
///
/// CurrentRecipe
///
public Recipe CurrentRecipe { get; set; }
///
/// OnConfiguring
///
/// options
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite(@"Data Source=./Config/Datas.db");
private object _dbLock = new();
///
/// 保存
///
public void Save()
{
lock (_dbLock)
{
SaveChanges();
}
}
}
///
/// DbContextExtensions
///
internal static class DbContextExtensions
{
///
/// 创建缺失的表
///
///
///
internal static void EnsureCreatingMissingTables(this TDbContext dbContext) where TDbContext : DbContext
{
var type = typeof(TDbContext);
var dbSetType = typeof(DbSet<>);
var dbPropertyNames = type.GetProperties().Where(p => p.PropertyType.Name == dbSetType.Name)
.Select(p => p.Name).ToArray();
foreach (var entityName in dbPropertyNames)
{
CheckTableExistsAndCreateIfMissing(dbContext, entityName);
}
}
private static void CheckTableExistsAndCreateIfMissing(DbContext dbContext, string entityName)
{
var defaultSchema = dbContext.Model.GetDefaultSchema();
var tableName = string.IsNullOrWhiteSpace(defaultSchema) ? $"{entityName}" : $"{defaultSchema}.{entityName}";
try
{
var sqlstr = $"SELECT * FROM {tableName} LIMIT 1";
_ = dbContext.Database.ExecuteSqlRaw(sqlstr); //Throws on missing table
}
catch (Exception)
{
var scriptStart = $"CREATE TABLE \"{tableName}\"";
var script = dbContext.Database.GenerateCreateScript();
var tableScript = script.Split(scriptStart).Last().Split(";");
var first = $"{scriptStart} {tableScript.First()}";
if (first is not null)
dbContext.Database.ExecuteSqlRaw(first);
}
}
}
}