Database Schemas - How To View On Server At RunTime

Using a custom Delphi DataAbstract Server, how could we view the Schema tables and fields during runtime to make sure we are using current loaded schema? How do we call the Schema and then list the tables and fields from the server.

We are sometimes loading a new schema in the OnCreate of the DataService_Impl. We would like to check that schema loaded against the current database on that specific machine. Just to make sure we have everything in sync.

Thanks ahead of time.
Bill Brittain

Hi,

You can use Local Data Adapter on server-side or Remote Data Adapters on client-side.

this component has Schema property.

Thank you. We have been able to pull the schema tables and fields and get the information that we needed. Below is the code if anyone wishes to use it. Just use whatever you wish for the in memory datatables.

procedure TServerForm.bn_LoadServerSchemaClick(Sender: TObject);
var
  xDataSets : TDADataSetCollection;
  xFields   : TDAFieldCollection;
  xField    : TDAField;
  i         : Integer;
  j         : Integer;

  xTable_Name : string;

  xField_Name             : string;
  xField_Type             : string;
  xField_Size             : integer;
  xField_DecimalPrecision : integer;
  xField_DecimalScale     : integer;
  xField_PrimaryKey       : boolean;
  xField_LogChanges       : boolean;
  xField_DisplayFormat    : string;
  xField_Alignment        : string;
begin

  try

    ta_ServerSchema_Tables.DisableControls;
    ta_ServerSchema_Tables.Open;

    ta_ServerSchema_Fields.DisableControls;
    ta_ServerSchema_Fields.Open;
    ta_ServerSchema_Fields.Filtered := true;

    xDataSets := da_LocalDataAdapter.Schema.Datasets;

    for i := 0 to xDataSets.Count-1 do
    begin

      xTable_Name := xDataSets[i].Name;

      with ta_ServerSchema_Tables do
      begin
        Append;
        FieldByName( 'Table_Name' ).AsString  := xTable_Name;
        Post;
      end;

      xFields := xDataSets[i].Fields;

      for j := 0 to xFields.Count-1 do
      begin

        xField := xFields[j];

        xField_Name             := xFields[j].Name;
        xField_Type             := GetFieldType( xFields[j].DataType );
        xField_Size             := xFields[j].Size;
        xField_DecimalPrecision := xFields[j].DecimalPrecision;
        xField_DecimalScale     := xFields[j].DecimalScale;
        xField_PrimaryKey       := xFields[j].InPrimaryKey;
        xField_LogChanges       := xFields[j].LogChanges;
        xField_DisplayFormat    := xFields[j].DisplayFormat;
        xField_Alignment        := GetFieldAlignment( xFields[j].Alignment );

        with ta_ServerSchema_Fields do
        begin
          Append;
          FieldByName( 'Table_Name'             ).AsString   := xTable_Name;
          FieldByName( 'Field_Name'             ).AsString   := xField_Name;
          FieldByName( 'Field_Type'             ).AsString   := xField_Type;
          FieldByName( 'Field_Size'             ).AsInteger  := xField_Size;
          FieldByName( 'Field_DecimalPrecision' ).AsInteger  := xField_DecimalPrecision;
          FieldByName( 'Field_DecimalScale'     ).AsInteger  := xField_DecimalScale;
          FieldByName( 'Field_PrimaryKey'       ).AsBoolean  := xField_PrimaryKey;
          FieldByName( 'Field_LogChanges'       ).AsBoolean  := xField_LogChanges;
          FieldByName( 'Field_DisplayFormat'    ).AsString   := xField_DisplayFormat;
          FieldByName( 'Field_Alignment'        ).AsString   := xField_Alignment;
          Post;
        end;

      end;


    end;

  finally
    ta_ServerSchema_Tables.EnableControls;
    ta_ServerSchema_Fields.EnableControls;
  end;

end;


The next thing we are working on is updating the schema on the working server.
How do we add, edit, and delete fields from the schema while the server is in operation?

We would like to sometimes add a new field to an existing table, or maybe alter the Display Format.

We can certainly do this by altering and loading a completely new schema but we would like to modify on the existing schema just maybe one field. Can that be done? We have been able to pull an example of one field by finding the TDADataSet and then getting the TDAField. Then we have assigned the DisplayFormat to that Field on the Schema.

How do we make the change persist on the Schema? Are these values read only?

procedure TServerForm.bn_UpdateServerSchemaFieldClick(Sender: TObject);
var
  xDataSet    : TDADataSet;
  xField      : TDAField;

  xTable_Name : string;
  xField_Name : string;

  xField_DisplayFormat : string;

begin
  xTable_Name := ta_ServerSchema_Fields.FieldByName( 'Table_Name' ).AsString;
  xField_Name := ta_ServerSchema_Fields.FieldByName( 'Field_Name' ).AsString;

  xDataSet := da_LocalDataAdapter.Schema.FindDataset( xTable_Name );
  xField   := xDataSet.FieldByName( xField_Name );

  xField.DisplayFormat := 'mm/dd/yyyy';   // xField_DisplayFormat;

end;


Hi,

You can do this in DataService.OnActivate event.

if you want to change server-side schema, you should save changed schema and load in this event.

I can suggest to put TDASchema to server’s data module and copy it’s content like

procedure TDataService.DataAbstractServiceActivate(const aClientID: TGUID;
  aSession: TROSession; const aMessage: IROMessage);
begin  
  Self.ServiceSchema.Assign(ServerDataModule.DASchema1);
end;

You can update this schema (ServerDataModule.DASchema1) and updated schema will be available for all next calls.

in general, implementation may depend on used Class Factories (.NET & Delphi) and other factors.