// Get a catalog value by entry from database or catalog public TypedValue GetValue(CatalogEntry entry) { if (entry.IsDatabase) { if (_catalog.SqlFlag) { // Database sql comes from external table var table = DataTableSql.Create(entry.Name, entry.DataType.Heading); return(RelationValue.Create(table)); } else if (!entry.IsLoaded) // lazy load // Database non-sql lazy loaded from store on path, then in catalog { var value = Persist.Create(_catalog.DatabasePath, true).Load(entry.Name); if (entry.DataType != value.DataType) { throw ProgramError.Fatal("Catalog", "Type mismatch for variable {0}", entry.Name); } entry.IsLoaded = true; return(Persist.Create(_catalog.DatabasePath, true).Load(entry.Name)); } } // Non-database exists only in the catalog return(entry.Value); }
// Store new entry to catalog, perhaps removing old value first // Currently only for Sql -- Local does total dump internal void StoreEntry(CatalogEntry entry, CatalogEntry oldentry = null) { var ctm = CatalogTableMaker.Create(_catalogtableheading); if (SqlFlag) { var table = DataTableSql.Create(CatalogTableName, _catalogtableheading); if (oldentry != null) { var ctmx = CatalogTableMaker.Create(_catalogtableheading); ctmx.Table.AddRow(ctmx.MakeEntry(oldentry)); table.UpdateJoin(ctmx.Table, JoinOps.MINUS); } ctm.Table.AddRow(ctm.MakeEntry(entry)); table.UpdateJoin(ctm.Table, JoinOps.UNION); } }
// set entry to value, update as needed internal void SetValue(CatalogEntry entry, TypedValue value) { // Choose where to store and whether to convert TypedValue finalvalue; if (entry.IsDatabase && _catalog.SqlFlag) { // Database + sql => hand it to sql, to create table if needed // set a default value to carry the type DataTableSql.Create(entry.Name, value.AsTable()); finalvalue = value.DataType.DefaultValue(); } else { // everything else stored in catalog if (value.DataType is DataTypeRelation) { finalvalue = RelationValue.Create(DataTableLocal.Convert(value.AsTable())); } else { finalvalue = value; } // set flags for persisting if (entry.IsPersistent) { entry.IsUnsaved = true; entry.IsLoaded = true; } } // store value if changed if (finalvalue != entry.Value) { var oldvalue = (entry.Value == null) ? null : entry; entry.Set(finalvalue); if (entry.IsPersistent && _catalog.SaveFlag) { _catalog.StoreEntry(entry, oldvalue); } } }
//--- persistence Mk II // Store the persistent catalog and modified tables, local only // note: for Sql, only used to create new empty catalog public void StoreToTable() { Logger.WriteLine(2, "Save catalog for '{0}'", DatabaseName); var ctm = CatalogTableMaker.Create(_catalogtableheading); var table = ctm.AddEntries(PersistentVars.GetEntries()).Table; if (SqlFlag) { DataTableSql.Create(CatalogTableName, table); } else { Persist.Create(DatabasePath, true).Store(CatalogTableName, RelationValue.Create(table)); var savers = PersistentVars.GetEntries().Where(e => e.IsUnsaved); Logger.WriteLine(2, $"Persist {savers.Count()} entries"); foreach (var entry in savers) { Persist.Create(DatabasePath, true).Store(entry.Name, entry.Value); } } }
// Get the value of a relation from a database // Entry previously created by peeking public bool LinkRelvar(string name) { var entry = GlobalVars.FindEntry(name); Logger.Assert(entry != null && entry.IsDatabase); var heading = entry.DataType.Heading; if (SqlFlag) { var sqlheading = SqlTarget.Current.GetTableHeading(name); if (sqlheading == null) { throw ProgramError.Fatal("Catalog", "sql table not found: '{0}'", name); } // TODO: smarter test, but still may not match exactly //if (!heading.Equals(sqlheading)) if (heading.Degree != sqlheading.Degree) { throw ProgramError.Fatal("Catalog", "sql table schema mismatch: '{0}'", name); } var table = DataTableSql.Create(name, heading); GlobalVars.SetValue(entry, RelationValue.Create(table)); } else { var tablev = Persist.Create(DatabasePath, false).Load(name); if (tablev == null) { throw ProgramError.Fatal("Catalog", "local table not found: '{0}'", name); } if (!heading.Equals(tablev.Heading)) { throw ProgramError.Fatal("Catalog", "local table schema mismatch: '{0}'", name); } GlobalVars.SetValue(entry, RelationValue.Create(tablev.AsTable())); } return(true); }