Skip to content

Tiny Database

QQ edited this page Feb 17, 2022 · 4 revisions

if you want to have tiny database, registrykey maybe is choice
build comapny has many apps example

build mock

  1. build company data
Key Name1 Address
1 One Address_one
2 Two Address_two
3 Three Address_three
4 Four Address_four
public class Company
{
    [RegSubKeyName]
    public int Key { set; get; }
    [RegPropertyName(Name = "Name1")]
    public string Name { set; get; }
    public string Address { set; get; }
}
RegQuery<Company> regt_company = new RegQuery<Company>()
            .useSetting(x =>
            {
                x.Hive = RegistryHive.CurrentConfig;
                x.SubKey = @"UnitTest\Company";
                x.View = RegistryView.Registry64;
            });
regt_company.Insert(new List<Company>()
     {
         new Company(){ Key=1, Name = "One" ,Address="Address_one"},
         new Company(){ Key=2, Name = "Two" ,Address="Address_two"},
         new Company(){ Key=3, Name = "Three" ,Address="Address_three"},
         new Company(){ Key=4, Name = "Four" ,Address="Address_four"},
     });
  1. build app data
ID Name Version
1 Camera 1.1.1.1
2 Motion 2.2.2.2
3 VLC.exe 3.3.3.3
4 Joystick.cpl 4.4.4.4
5 IPBoroadcast.exe 5.5.5.5
5 PLC.exe 6.6.6.6
public class App
{
    public int ID { set; get; }
    [RegPropertyName(Name ="Name")]
    public string DisplayName { set; get; }
    public Version Version { set; get; }
    [RegIgnore]
    public int Size { set; get; }
}
RegQuery<App> regt_apps = new RegQuery<App>()
            .useSetting(x =>
            {
                x.Hive = RegistryHive.CurrentConfig;
                x.SubKey = @"UnitTest\Apps";
            });
regt_apps.Insert(new List<App>()
            {
                new App(){ID=1, DisplayName="Camera", Version=new Version(1,1,1,1), Size=123 },
                new App(){ID=2, DisplayName="Motion", Version=new Version(2,2,2,2), Size=123 },
                new App(){ID=3, DisplayName="VLC.exe", Version=new Version(3,3,3,3), Size=123 },
                new App(){ID=4, DisplayName="Joystick.cpl", Version=new Version(4,4,4,4), Size=123 },
                new App(){ID=5, DisplayName="IPBoroadcast.exe", Version=new Version(5,5,5,5), Size=123 },
                new App(){ID=6, DisplayName="PLC.exe", Version=new Version(6,6,6,6), Size=123 },
            });
  1. build mapping data
CompanyID AppID
1 1
1 2
2 3
2 4
public class AppMapping
{
    public int CompanyID { set; get; }
    public int AppID { set; get; }
}
RegQuery<AppMapping> regt_appmapping = new RegQuery<AppMapping>()
            .useSetting(x =>
            {
                x.Hive = RegistryHive.CurrentConfig;
                x.SubKey = @"UnitTest\AppMapping";
                x.View = RegistryView.Registry64;
            });
regt_appmapping.Insert(new List<AppMapping>()
            {
                new AppMapping(){AppID=1, CompanyID=1},
                new AppMapping(){AppID=2, CompanyID=1},
                new AppMapping(){AppID=3, CompanyID=2},
                new AppMapping(){AppID=4, CompanyID=2},
            });

Left join

var left = regt_apps.GroupJoin(regt_appmapping, app => app.ID, mapping => mapping.AppID, (app, mapping) => new { app, mapping })
                .SelectMany(x => x.mapping.DefaultIfEmpty(), (app, mapping) => new { app.app, mapping });
//get no mapping app
var nomapping = left.Where(x => x.mapping == null).Select(x => x.app);

below table is search result

ID Name Version
5 IPBoroadcast.exe 5.5.5.5
6 PLC.exe 6.6.6.6

Left Join three table

var left1 = regt_company.GroupJoin(regt_appmapping, company => company.Key, mapping => mapping.CompanyID, (company, mapping) => new { company, mapping })
                .SelectMany(x => x.mapping.DefaultIfEmpty(), (company, mapping) => new { company.company, mapping })
                .Where(x => x.mapping != null)
                .GroupJoin(regt_apps, mapping => mapping.mapping.AppID, app => app.ID, (x, y) => new { x.company, app = y })
                .SelectMany(x => x.app.DefaultIfEmpty(), (x, y) => new { Company = x.company.Name, App = y.DisplayName });

below table is search result

Company App
One Motion
One Camera
Two Joystick.cpl
Two VLC.exe

Update

regt_company.Update(x => new Company() { Address = $"{x.Name}_{x.Name}" });

blow table is update result

Key Name1 Address
1 One One_One
2 Two Two_Two
3 Three Three_Three
4 Four Four_Four
update use anyoumus type
regt_company.Where(x => x.Name == "One").Update(x => new { Address = "Test" });

blow table is update result

Key Name1 Address
1 One Test
2 Two Two_Two
3 Three Three_Three
4 Four Four_Four

Delete

//remove all data
regt_apps.RemoveAll();
regt_apps.Where(x => x.Version != new Version(1, 1, 1, 1)).RemoveAll();

blow table is update result

ID Name Version
1 Camera 1.1.1.1

truncate

RegistryKey has not auto increment key, so removeall is ok

regt_appmapping.RemoveAll();
Clone this wiki locally