unit uDADataTableWizards; {----------------------------------------------------------------------------} { Data Abstract Library - IDE Library { { compiler: Delphi 6 and up, Kylix 3 and up { platform: Win32, Linux { { (c)opyright RemObjects Software. all rights reserved. { { Using this code requires a valid license of the Data Abstract { which can be obtained at http://www.remobjects.com. {----------------------------------------------------------------------------} interface uses Classes, Types, ToolsAPI, DesignIntf, uDAClasses, uDARemoteDataAdapter; type TDataTableWizards = class private class procedure GetDataTables(const aName: string); public class function CreateDataTables(aDesigner: IDesigner; aAdapter: TDARemoteDataAdapter; aSchema: TDASchema; aCoordinates: TPoint): boolean; overload; class function CreateDataTables(aForm: IOTAFormEditor; aAdapter: TDARemoteDataAdapter; aSchema: TDASchema; aCoordinates: TPoint): boolean; overload; end; implementation uses Forms, Windows, SysUtils, Dialogs, Registry, TypInfo, uROClasses, uDADataTable, uDAInterfaces, uDAPleaseWaitForm, uDASelectDataTablesForm, uDAMemDataTable, uDACDSDataTable; function ComponentExists(aDesigner: IDesigner; aName: string): boolean; var i: integer; begin for i := 0 to aDesigner.Root.ComponentCount-1 do if aDesigner.Root.Components[i].Name = aName then begin result := true; exit; end; result := false; end; procedure CreateDataTable(aDesigner: IDesigner; aClass:TDADataTableClass; aAdapter: TDARemoteDataAdapter; aSchema: TDASchema; const aDataTableName: String; X,Y: integer; aCreateDataSource: boolean=true); overload; var lDataTable:TDADataTable; lDataSource:TDADataSource; lBaseName:string; lDataTablePrefix,lDataSourcePrefix:string; lDataset: TDADataSet; lField: TDAField; begin with TRegistry.Create do try RootKey := HKEY_CURRENT_USER; OpenKey('Software\RemObjects\Data Abstract\IDE\ComponentPrefixes',false); lDataTablePrefix := ReadString('TDADataTable'); lDataSourcePrefix := ReadString('TDADataSource'); finally Free(); end; if lDataTablePrefix = '' then lDataTablePrefix := 'tbl_'; if lDataSourcePrefix = '' then lDataSourcePrefix := 'ds_'; lDataTable := aDesigner.CreateComponent(aClass, nil, X, Y, 24, 24) as TDADataTable; lDataset := aSchema.FindDataset(aDataTableName); if Assigned(lDataset) then begin lDataTable.LogicalName := lDataset.Name; lDataTable.CustomAttributes.Assign(lDataset.CustomAttributes); lDataTable.Fields.AssignFieldCollection(lDataset.Fields); lDataTable.Params.AssignParamCollection(lDataset.Params); lBaseName := lDataset.Name; // ToDo: the code below is shared with RDA.FillSchema. Refactor. if lDataset is TDAUnionDataTable then begin if not Assigned(lDataset.Fields.FindField(def_SourceTableFieldName) as TDAField) then begin lField := lDataTable.Fields.Add(); lField.Name := def_SourceTableFieldName; lField.DataType := datInteger; lField.InPrimaryKey := True; lField.ServerAutoRefresh := True; end; end; end; lDataTable.RemoteDataAdapter := aAdapter; RemoveExcept(lBaseName,['a'..'z','A'..'Z','0'..'9','_']); if ComponentExists(aDesigner, lDataTablePrefix+lBaseName) then lDataTable.Name := aDesigner.UniqueName(lDataTablePrefix+lBaseName) else lDataTable.Name := lDataTablePrefix+lBaseName; if aCreateDataSource then begin lDataSource := aDesigner.CreateComponent(TDADataSource, nil , X, Y+44, 24, 24) as TDADataSource; lDataSource.DataTable := lDataTable; if ComponentExists(aDesigner, lDataSourcePrefix+lBaseName) then lDataSource.Name := aDesigner.UniqueName(lDataSourcePrefix+lBaseName) else lDataSource.Name := lDataSourcePrefix+lBaseName; end; end; class function TDataTableWizards.CreateDataTables(aForm: IOTAFormEditor; aAdapter: TDARemoteDataAdapter; aSchema: TDASchema; aCoordinates: TPoint): boolean; begin result := CreateDataTables((aForm as INTAFormEditor).FormDesigner, aAdapter, aSchema, aCoordinates); end; var gDataTables: TStringList; gDesigner: IDesigner; class procedure TDataTableWizards.GetDataTables(const aName: string); var lComponent: TComponent; begin lComponent := gDesigner.GetComponent(aName); if assigned(lComponent) then gDataTables.Add((lComponent as TDADataTable).LogicalName); end; class function TDataTableWizards.CreateDataTables(aDesigner: IDesigner; aAdapter: TDARemoteDataAdapter; aSchema: TDASchema; aCoordinates: TPoint): boolean; var lForm: TDASelectDataTablesForm; lDataTables: TStringList; i: integer; Y: integer; begin lDataTables := TStringList.Create; try lDataTables.Sorted := true; for i := 0 to aSchema.Datasets.Count-1 do if aSchema.Datasets[i].IsPublic then lDataTables.AddObject(aSchema.Datasets[i].Name, aSchema.Datasets[i]); for i := 0 to aSchema.UnionDataTables.Count-1 do if aSchema.UnionDataTables[i].IsPublic then lDataTables.AddObject(aSchema.UnionDataTables[i].Name, aSchema.UnionDataTables[i]); for i := 0 to aSchema.JoinDataTables.Count-1 do if aSchema.JoinDataTables[i].IsPublic then lDataTables.AddObject(aSchema.JoinDataTables[i].Name, aSchema.JoinDataTables[i]); if lDataTables.Count > 0 then begin gDesigner := aDesigner; gDataTables := TStringList.Create(); try aDesigner.GetComponentNames(GetTypeData(TypeInfo(TDADataTable)), GetDataTables); lForm := TDASelectDataTablesForm.Create(nil); try for i := 0 to lDataTables.Count-1 do begin lForm.lb_DataTables.Items.AddObject(lDataTables[i], lDataTables.Objects[i]); lForm.lb_DataTables.Checked[lForm.lb_DataTables.Items.Count-1] := gDataTables.IndexOf(lDataTables[i]) = -1; end; lForm.UpdateCheckBoxState(); lForm.OkButtonCaption := '&Create'; result := (lForm.ShowModal() = idOk); if result then begin Y := aCoordinates.Y; for i := 0 to lForm.lb_DataTables.Items.Count - 1 do if lForm.lb_DataTables.Checked[i] then begin CreateDataTable(aDesigner, TDAMemDataTable, aAdapter, aSchema, lForm.lb_DataTables.Items[i], aCoordinates.X, Y, true); inc(y, 88); // doesn't seem to work :( end; end; finally FreeAndNil(lForm); end; finally FreeAndNil(gDataTables); gDesigner := nil; end; end else begin ShowMessage('No data tables were found in schema.'); result := false; end; finally FreeAndNil(lDataTables); end; end; end.