private void AddColumnsForType(Schema.TableVar tableVar, Schema.ScalarType d4Type) { // Add columns for the base type, if any foreach (var parentType in d4Type.ParentTypes) { AddColumnsForType(tableVar, parentType); } // Add columns for the default representation var representation = Compiler.FindDefaultRepresentation(d4Type); if (representation != null) { // And columns for each property of the default representation foreach (var property in representation.Properties) { var column = new Schema.Column(property.Name, property.DataType); var tableVarColumn = new Schema.TableVarColumn(column); tableVar.DataType.Columns.Add(column); tableVar.Columns.Add(tableVarColumn); } } tableVar.AddDependency(d4Type); }
public static object Evaluate(Program program, string expression, Schema.TableVar tableVar, DataParams dataParams) { IServerExpressionPlan plan = ((IServerProcess)program.ServerProcess).PrepareExpression(expression, dataParams); try { plan.CheckCompiled(); IServerCursor cursor = plan.Open(dataParams); try { NativeTable nativeTable = new NativeTable(program.ValueManager, tableVar); while (cursor.Next()) { using (IRow row = cursor.Select()) { nativeTable.Insert(program.ValueManager, row); } } return(new TableValue(program.ValueManager, nativeTable.TableType, nativeTable)); } finally { plan.Close(cursor); } } finally { ((IServerProcess)program.ServerProcess).UnprepareExpression(plan); } }
public TableBuffer(ServerProcess AProcess, Schema.TableVar ATableVar, int AFanout, int ACapacity) { FTableVar = ATableVar; FFanout = AFanout; FCapacity = ACapacity; InternalInitialize(AProcess); }
public TableBuffer(ServerProcess AProcess, Schema.TableVar ATableVar) : base() { FTableVar = ATableVar; FFanout = CDefaultClusteredFanout; FCapacity = CDefaultClusteredCapacity; InternalInitialize(AProcess); }
public void Open() { if (_shouldOpen && (_cursor == null)) { if (_plan == null) { try { _cursor = _process.OpenCursor(_expression, _params); } catch (Exception exception) { CompilerMessages messages = new CompilerMessages(); messages.Add(exception); ErrorsOccurred(messages); throw exception; } _plan = _cursor.Plan; _tableVar = _plan.TableVar; ErrorsOccurred(_plan.Messages); } else { _cursor = _plan.Open(_params); } } }
public CatalogHeader(Schema.TableVar tableVar, NativeTable nativeTable, long timeStamp, CatalogCacheLevel cacheLevel) : base() { _tableVar = tableVar; _nativeTable = nativeTable; TimeStamp = timeStamp; _cacheLevel = cacheLevel; }
public void LoadTable(ServerProcess process, Schema.TableVar tableVar) { lock (_loadedTables) { if (!IsLoaded(tableVar)) { _loadedTables.Add(tableVar); if (File.Exists(GetTableVarFileName(tableVar))) { using (FileStream stream = new FileStream(GetTableVarFileName(tableVar), FileMode.Open, FileAccess.Read)) { NativeTable nativeTable = Tables[tableVar]; while (stream.Position < stream.Length) { int length = StreamUtility.ReadInteger(stream); byte[] rowValue = new byte[length]; stream.Read(rowValue, 0, rowValue.Length); IRow row = (IRow)DataValue.FromPhysical(process.ValueManager, nativeTable.RowType, rowValue, 0); try { nativeTable.Insert(process.ValueManager, row); } finally { row.Dispose(); } } } } } } }
public override DataVar InternalExecute(ServerProcess AProcess, DataVar[] AArguments) { if (AProcess.ServerSession.Server.Catalog.Generators == null) // if not set already { // check for existing table named Datphor.Generators Schema.TableVar LTableVar = (Schema.TableVar)Compiler.ResolveCatalogIdentifier(AProcess.Plan, "System.Generators", false); if (LTableVar == null) // if system.generators doesn't already exist { // get device Schema.Device LDevice = AArguments.Length > 0 ? (Schema.Device)Compiler.ResolveCatalogIdentifier(AProcess.Plan, AArguments[0].Value.AsString, true) : Language.D4.Compiler.GetDefaultDevice(AProcess.Plan, true); // make sure the device is started so that DeviceScalarTypes is filled AProcess.EnsureDeviceStarted(LDevice); // create table type Schema.TableType LTableType = new Schema.TableType(); // use System.String if available else System.IString if (LDevice.DeviceScalarTypes.Contains(AProcess.Plan.Catalog.DataTypes.SystemIString)) { LTableType.Columns.Add(new Schema.Column("ID", AProcess.Plan.Catalog.DataTypes.SystemIString)); } else { LTableType.Columns.Add(new Schema.Column("ID", AProcess.Plan.Catalog.DataTypes.SystemString)); } LTableType.Columns.Add(new Schema.Column("NextKey", AProcess.Plan.Catalog.DataTypes.SystemInteger)); // create table LTableVar = new Schema.BaseTableVar("System.Generators", LTableType, LDevice); MetaData LMetaData = new MetaData(); LMetaData.Tags.Add(new Tag("Storage.Length", "200", false, true)); LTableVar.Columns.Add(new Schema.TableVarColumn(LTableType.Columns["ID"], LMetaData)); LTableVar.Columns.Add(new Schema.TableVarColumn(LTableType.Columns["NextKey"])); LTableVar.Keys.Add(new Schema.Key(new Schema.TableVarColumn[] { LTableVar.Columns["ID"] })); LTableVar.Owner = AProcess.Plan.User; LTableVar.Library = AProcess.Plan.CurrentLibrary; LTableVar.AddDependency(LDevice); LTableVar.AddDependency((Schema.ScalarType)LTableVar.Columns[0].DataType); LTableVar.AddDependency((Schema.ScalarType)LTableVar.Columns[1].DataType); Compiler.Bind(AProcess.Plan, new CreateTableNode((Schema.BaseTableVar)LTableVar)).Execute(AProcess); if (AProcess.Plan.User.ID == Server.Server.CAdminUserID) { Schema.Group LGroup = AProcess.Plan.Catalog.Groups[Server.Server.CUserGroupName]; LGroup.GrantRight(LTableVar.Rights.Select, true); LGroup.GrantRight(LTableVar.Rights.Insert, true); LGroup.GrantRight(LTableVar.Rights.Update, true); } } // set generator table SystemSetGeneratorsNode.SetGenerator(AProcess, LTableVar); } return(null); }
protected virtual void Prepare() { Expression expression = _elaboratedExpression.Expression; _tableVar = _elaboratedExpression.MainElaboratedTableVar.TableVar; Schema.Order order = GetDefaultOrder(_tableVar); if (order != null) { BaseOrderExpression browseExpression; if (Convert.ToBoolean(DerivationUtility.GetTag(_tableVar.MetaData, "UseBrowse", _elaboratedExpression.PageType, "True"))) { browseExpression = new BrowseExpression(); } else { browseExpression = new OrderExpression(); } browseExpression.Expression = expression; foreach (Schema.OrderColumn column in order.Columns) { OrderColumnDefinition definition = column.EmitStatement(EmitMode.ForCopy) as OrderColumnDefinition; definition.ColumnName = Schema.Object.Qualify(column.Column.Name, _elaboratedExpression.MainElaboratedTableVar.ElaboratedName); browseExpression.Columns.Add(definition); } expression = browseExpression; } _expression = new D4TextEmitter().Emit ( new CursorDefinition ( expression, CursorCapability.Navigable | CursorCapability.BackwardsNavigable | CursorCapability.Bookmarkable | CursorCapability.Searchable | CursorCapability.Updateable, CursorIsolation.Browse, CursorType.Dynamic ) ); // Build the derivation info structure for use in structuring, layout and document production. _derivationInfo = new DerivationInfo(); _derivationInfo.Program = _program; _derivationInfo.Process = _process; _derivationInfo.PageType = _seed.PageType; _derivationInfo.Query = _seed.Query; _derivationInfo.Elaborate = _seed.Elaborate; _derivationInfo.MasterKeyNames = _seed.MasterKeyNames; _derivationInfo.DetailKeyNames = _seed.DetailKeyNames; _derivationInfo.KeyNames = GetKeyNames(_tableVar); _derivationInfo.Expression = _expression; _derivationInfo.ElaboratedExpression = _elaboratedExpression; _derivationInfo.TableVar = _tableVar; _derivationInfo.MainSourceName = DerivationUtility.MainSourceName; _derivationInfo.IsReadOnly = DerivationUtility.IsReadOnlyPageType(_seed.PageType); }
protected override void InternalInsertRow(Program program, Schema.TableVar table, IRow row, BitArray valueFlags) { base.InternalInsertRow(program, table, row, valueFlags); // TODO: Log the operation so it can be performed during the commit // NOTE: This will require that the operation log be considered a "cache" that will be read locally prior to returning a result (or as part of post-processing a result?) // Can use a PUT if we manage Id generation, assuming the server supports it // Must use a POST to allow the server to assign the Id, in that case the Location header of the response will contain the newly assigned Id }
public int IndexOf(Schema.TableVar ATableVar) { for (int LIndex = 0; LIndex < Count; LIndex++) { if (this[LIndex].TableVar == ATableVar) { return(LIndex); } } return(-1); }
public TableBuffer this[Schema.TableVar ATableVar] { get { int LIndex = IndexOf(ATableVar); if (LIndex < 0) { throw new RuntimeException(RuntimeException.Codes.TableBufferNotFound, ATableVar.Name); } return(this[LIndex]); } }
public static Schema.Key FindIdentifyingKey(Program program, Schema.TableVar tableVar) { foreach (var key in tableVar.Keys) { if (Convert.ToBoolean(MetaData.GetTag(key.MetaData, "REST.IsIdentity", "false"))) { return(key); } } return(program.FindClusteringKey(tableVar)); }
public void EndUpdate(ServerProcess process, Schema.TableVar tableVar) { SimpleDeviceHeader header = _headers[tableVar]; if (header.UpdateCount > 0) { header.UpdateCount--; if (header.UpdateCount == 0) { SaveTable(process, tableVar); } } }
public void TruncateTable(ServerProcess process, Schema.TableVar tableVar) { string tableVarFileName = GetTableVarFileName(tableVar); if (File.Exists(tableVarFileName)) { File.Delete(tableVarFileName); } Tables[tableVar].Truncate(process.ValueManager); SimpleDeviceHeader header = _headers[tableVar]; header.TimeStamp += 1; header.SaveTimeStamp = header.TimeStamp; }
public int IndexOf(Schema.TableVar tableVar) { lock (this) { for (int index = 0; index < Count; index++) { if (this[index].TableVar == tableVar) { return(index); } } return(-1); } }
protected Schema.Order GetDefaultOrder(Schema.TableVar tableVar) { foreach (Schema.Order order in tableVar.Orders) { if (Convert.ToBoolean(DerivationUtility.GetTag(order.MetaData, "IsDefault", "False"))) { return(new Schema.Order(order)); } } foreach (Schema.Key key in tableVar.Keys) { if (Convert.ToBoolean(DerivationUtility.GetTag(key.MetaData, "IsDefault", "False"))) { return(new Schema.Order(key)); } } if (tableVar.Keys.Count > 0) { Schema.Order order = new Schema.Order(_program.FindClusteringKey(tableVar)); if (IsOrderVisible(order)) { return(order); } foreach (Schema.Key key in tableVar.Keys) { order = new Schema.Order(key); if (IsOrderVisible(order)) { return(order); } } } if (tableVar.Orders.Count > 0) { foreach (Schema.Order order in tableVar.Orders) { if (IsOrderVisible(order)) { return(order); } } } return(null); }
protected string GetKeyNames(Schema.TableVar tableVar) { StringBuilder keyNames = new StringBuilder(); Schema.Key clusteringKey = _program.FindClusteringKey(tableVar); for (int index = 0; index < clusteringKey.Columns.Count; index++) { if (index > 0) { keyNames.Append(", "); } keyNames.Append(Schema.Object.Qualify(clusteringKey.Columns[index].Name, DerivationUtility.MainElaboratedTableName)); } return(keyNames.ToString()); }
public FastoreTable this[Schema.TableVar tableVar] { get { lock (this) { int index = IndexOf(tableVar); if (index < 0) { throw new RuntimeException(RuntimeException.Codes.NativeTableNotFound, tableVar.DisplayName); } return(this[index]); } } }
protected virtual SearchElement CreateSearchElement(SearchElement element, Schema.TableVar tableVar, string titleSeed, string pageType, bool isReadOnly) { MetaData metaData = null; if (tableVar.MetaData != null) { metaData = DerivationUtility.ExtractTags(tableVar.MetaData.Tags, "Search", pageType); } PrepareElement(element, metaData, tableVar.MetaData, titleSeed, pageType, isReadOnly); element.Hint = DerivationUtility.GetTag(metaData, "Hint", pageType, "Search for a specific row."); element.Properties.AddOrUpdate("Source", element.Source); return(element); }
public override object InternalExecute(Program program, object[] arguments) { SimpleDevice device = Compiler.ResolveCatalogIdentifier(program.Plan, (string)arguments[0], true) as SimpleDevice; if (device == null) { throw new SimpleDeviceException(SimpleDeviceException.Codes.SimpleDeviceExpected); } Schema.TableVar tableVar = Compiler.ResolveCatalogIdentifier(program.Plan, (string)arguments[1], true) as Schema.TableVar; if (tableVar == null) { throw new SimpleDeviceException(SimpleDeviceException.Codes.TableVarExpected); } device.BeginUpdate(program.ServerProcess, tableVar); return(null); }
protected virtual GridElement CreateGridElement(GridElement element, Schema.TableVar tableVar, string titleSeed, string pageType, bool isReadOnly) { MetaData metaData = null; if (tableVar.MetaData != null) { metaData = DerivationUtility.ExtractTags(tableVar.MetaData.Tags, "Grid", pageType); } PrepareElement(element, metaData, tableVar.MetaData, titleSeed, pageType, isReadOnly); //AElement.Title = ATitleSeed + DerivationUtility.GetTag(LMetaData, "Title", APageType, FDerivationInfo.ElaboratedExpression.MainElaboratedTableVar.TableTitle); // grid has no title element.Hint = DerivationUtility.GetTag(metaData, "Hint", pageType, DerivationUtility.GetTag(tableVar.MetaData, "Hint", pageType, _derivationInfo.ElaboratedExpression.MainElaboratedTableVar.TableTitle)); element.Properties.AddOrUpdate("Source", element.Source); //if (AIsReadOnly) // AElement.Properties.AddOrUpdate("ReadOnly", "True"); return(element); }
//TODO: FastoreTable? This function is used internally to make sure tables exist //In the case of Fastore, this means reading the tableVar, deciding what the columns will look like, and creating them. protected FastoreTable EnsureFastoreTable(Schema.TableVar tableVar) { if (!tableVar.IsSessionObject && !tableVar.IsATObject) { tableVar.Scope = (Schema.TableVarScope)Enum.Parse(typeof(Schema.TableVarScope), MetaData.GetTag(tableVar.MetaData, "Storage.Scope", "Database"), false); } FastoreTables tables = GetTables(tableVar.Scope); lock (tables) { int index = tables.IndexOf(tableVar); if (index < 0) { index = tables.Add(new FastoreTable(ServerProcess.ValueManager, tableVar, this.Device)); } return(tables[index]); } }
public static void SetGenerator(ServerProcess AProcess, Schema.TableVar ATableVar) { if ( !( (ATableVar.DataType.Columns.Count == 2) && ( (ATableVar.DataType.Columns[0].DataType.Is(AProcess.Plan.Catalog.DataTypes.SystemString)) || (ATableVar.DataType.Columns[0].DataType.Is(AProcess.Plan.Catalog.DataTypes.SystemIString)) ) && (Schema.Object.NamesEqual(ATableVar.DataType.Columns[0].Name, "ID")) && (ATableVar.DataType.Columns[1].DataType.Is(AProcess.Plan.Catalog.DataTypes.SystemInteger)) && (Schema.Object.NamesEqual(ATableVar.DataType.Columns[1].Name, "NextKey")) ) ) { throw new RuntimeException(RuntimeException.Codes.InvalidGeneratorsTable); } AProcess.ServerSession.Server.Catalog.Generators = ATableVar; }
protected override object InternalExecute(Program program, PlanNode planNode) { if ((planNode is BaseTableVarNode) || (planNode is OrderNode)) { Schema.TableVar tableVar = null; if (planNode is BaseTableVarNode) { tableVar = ((BaseTableVarNode)planNode).TableVar; } else if (planNode is OrderNode) { tableVar = ((BaseTableVarNode)planNode.Nodes[0]).TableVar; } if (tableVar != null) { Device.LoadTable(ServerProcess, tableVar); } } object result = base.InternalExecute(program, planNode); if (planNode is CreateTableNode) { Schema.TableVar tableVar = ((CreateTableNode)planNode).Table; SimpleDeviceHeader header = new SimpleDeviceHeader(tableVar, 0); Device.Headers.Add(header); if (!ServerProcess.IsLoading() && ((Device.ReconcileMode & ReconcileMode.Command) != 0)) { string fileName = Device.GetTableVarFileName(tableVar); if (File.Exists(fileName)) { File.Delete(fileName); } } } else if (planNode is DropTableNode) { Device.Headers.Remove(((DropTableNode)planNode).Table); } return(result); }
private void InternalPrepare() { if (_plan == null) { _plan = _process.PrepareExpression(_expression, _params); try { ErrorsOccurred(_plan.Messages); if (!(_plan.DataType is Schema.ITableType)) { throw new ClientException(ClientException.Codes.InvalidResultType, _plan.DataType == null ? "<invalid expression>" : _plan.DataType.Name); } _tableVar = _plan.TableVar; } catch { _plan = null; throw; } } }
//Tied Directly to device for time being... public FastoreTable(IValueManager manager, Schema.TableVar tableVar, FastoreDevice device) { TableVar = tableVar; Device = device; EnsureColumns(); }
public bool Contains(Schema.TableVar tableVar) { return(IndexOf(tableVar) >= 0); }
protected override void InternalDeleteRow(Program program, Schema.TableVar table, IRow row) { // TODO: Log the operation so it can be performed during the commit base.InternalDeleteRow(program, table, row); // DELETE, w/ ETag header equal to row.meta.version }
public override Schema.Catalog GetDeviceCatalog(ServerProcess process, Schema.Catalog serverCatalog, Schema.TableVar tableVar) { Schema.Catalog catalog = base.GetDeviceCatalog(process, serverCatalog, tableVar); using (Plan plan = new Plan(process)) { // Need to support reverse lookup to determine the scalar type for a given native type Type[] types = typeof(Base).Assembly.GetTypes(); foreach (Type type in types) { // create a table var for each DomainResource descendent if (type.IsSubclassOf(typeof(DomainResource))) { if (!type.IsGenericTypeDefinition) { string tableName = Schema.Object.Qualify(ToFHIRTableName(type.Name), plan.CurrentLibrary.Name); if (tableVar == null || Schema.Object.NamesEqual(tableName, tableVar.Name)) { Schema.BaseTableVar localTableVar = new Schema.BaseTableVar(tableName, null); localTableVar.Owner = plan.User; localTableVar.Library = plan.CurrentLibrary; localTableVar.Device = this; localTableVar.MetaData = new MetaData(); // with a FHIR.ResourceType tag localTableVar.MetaData.Tags.Add(new Tag("FHIR.ResourceType", type.Name)); localTableVar.AddDependency(this); localTableVar.DataType = new Schema.TableType(); var d4TypeName = Schema.Object.Qualify(GenerateTypesNode.GetD4TypeName(type.FullName), "FHIR.Core"); var d4Type = Compiler.ResolveCatalogIdentifier(plan, d4TypeName, false) as Schema.ScalarType; if (d4Type != null) { AddColumnsForType(localTableVar, d4Type); localTableVar.Keys.Add(new Schema.Key(new Schema.TableVarColumn[] { localTableVar.Columns["Id"] })); catalog.Add(localTableVar); } } } } } return(catalog); } }