unit uDAKDBInfo; {----------------------------------------------------------------------------} { Data Abstract Library - Core 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. } {----------------------------------------------------------------------------} {$I DataAbstract.inc} interface uses Classes, kdbInfo, kdbstruc, uDAInterfaces, uDAClasses; type TDAKDBConnectionInfo = class(TKDBInfo) private fConnection: IDAConnection; fDatabaseName: string; procedure AddGroupWithFields(aStructure: TKDBStructure; aTable: TDSTable); protected procedure TablesByDatabase(TablesList : TStrings); override; procedure FieldsByTable(const TableName : string; FieldsList : TDBFieldList); override; procedure AvailableDatabases(DBList : TStrings); override; //NEW: replacement for GetDatabaseNames public property Connection: IDAConnection read fConnection write fConnection; property DatabaseName: string read fDatabaseName write fDatabaseName; procedure GetSQLValues(SQL : String; AValues,AItems : TStrings); override; procedure FillStructure(aStructure: TKDBStructure); end; TDAKDBSchemaInfo = class(TKDBInfo) private fSchema: TDASchema; fDatabaseName: string; procedure AddGroupWithFields(aStructure: TKDBStructure; aTable: TDSTable); procedure SetSchema(const Value: TDASchema); protected procedure TablesByDatabase(TablesList : TStrings); override; procedure FieldsByTable(const TableName : string; FieldsList : TDBFieldList); override; procedure AvailableDatabases(DBList : TStrings); override; //NEW: replacement for GetDatabaseNames procedure Notification(AComponent: TComponent; Operation: TOperation); override; public property Schema: TDASchema read fSchema write SetSchema; property DatabaseName: string read fDatabaseName write fDatabaseName; procedure GetSQLValues(SQL : String; AValues,AItems : TStrings); override; procedure FillStructure(aStructure: TKDBStructure); end; implementation uses uROClasses, sqglobal, SysUtils, Math; { TDAKDBConnectionInfo } procedure TDAKDBConnectionInfo.FieldsByTable(const TableName: string; FieldsList: TDBFieldList); var lNewField: TDBFieldInfo; i: Integer; lFields: TDAFieldCollection; begin inherited; fConnection.GetTableFields(TableName, lFields); FieldsList.Clear(); for i := 0 to lFields.Count-1 do begin lNewField := TDBFieldInfo.Create(); lNewField.FieldName := lFields[i].Name; lNewField.FieldSize := lFields[i].Size; lNewField.FieldType := DATypeToVCLType(lFields[i].DataType); FieldsList.Add(lNewField); end; { for } end; procedure TDAKDBConnectionInfo.AvailableDatabases(DBList : TStrings); //NEW: previously realization of GetDatabaseNames, name Alist replaced with DBList begin inherited; DBList.Clear(); DBList.Add(DatabaseName); end; procedure TDAKDBConnectionInfo.GetSQLValues(SQL: String; AValues, AItems: TStrings); begin inherited; end; procedure TDAKDBConnectionInfo.TablesByDatabase(TablesList: TStrings); var lROStrings: IROStrings; begin inherited; fConnection.GetTableNames(lROStrings); TablesList.Clear(); TablesList.Text := lROStrings.Text; end; procedure TDAKDBConnectionInfo.AddGroupWithFields(aStructure: TKDBStructure; aTable: TDSTable); var G : TDSFieldGroup; //LastRootNode, GroupNode : TTreeNode; I,Idx : integer; TblFields : TStrings; begin TblFields := TStringList.Create; try G := TDSFieldGroup.Create(aStructure); G.GroupName := ATable.TableName; aStructure.FieldGroups.Add(G); //FillGroupsCombo; {LastRootNode := nil; if not chbRootFirst.Checked then begin for I := tvFields.Items.Count - 1 downto 0 do if tvFields.Items[I].Parent <> nil then begin if I <> tvFields.Items.Count - 1 then LastRootNode := tvFields.Items[I + 1]; break; end; end;} {if LastRootNode = nil then GroupNode := tvFields.Items.AddObject(nil, G.GroupName, G) else GroupNode := tvFields.Items.InsertObject(LastRootNode, G.GroupName, G);} // ShowField(tvFields); aStructure.DatabaseInfo.GetFieldNamesByTable(aTable.TableName, TblFields); for I := 0 to TblFields.Count - 1 do begin Idx := aStructure.AddField(kfkData, TblFields[I], aTable.TableName+'.'+TblFields[I], ATable); aStructure.Fields[Idx].AddAppliedOperators; aStructure.Fields[Idx].Group := G; //AddFieldNodeTo(GroupNode, aStructure.Fields[Idx]); end; finally TblFields.Free; end; end; procedure TDAKDBConnectionInfo.FillStructure(aStructure: TKDBStructure); var lLink: TDSLink; lFKs: TDADriverForeignKeyCollection; i: Integer; sl: TStringList; lTable: integer; begin sl := TStringList.Create(); try aStructure.ClearStructure(); aStructure.AddDefOperators(true); GetTableNames(sl); sl.Sort; for i := 0 to (sl.Count-1) do begin lTable := aStructure.AddTable(sl[i],sl[i]); AddGroupWithFields(aStructure, aStructure.Tables[lTable]); end; Connection.GetForeignKeys(lFKs); try for i := 0 to lFKs.Count-1 do begin lLink := TDSLink.Create(); lLink.Table1 := aStructure.TableByName(0, lFKs[i].FKTable); lLink.Table2 := aStructure.TableByName(0, lFKs[i].PKTable); lLink.Fields1.Text := lFKs[i].FKField; lLink.Fields2.Text := lFKs[i].PKField; aStructure.AddLinkByRef(lLink) end; finally FreeAndNil(lFKs); end; finally sl.Free(); end; end; { TDAKDBSchemaInfo } procedure TDAKDBSchemaInfo.AddGroupWithFields(aStructure: TKDBStructure; aTable: TDSTable); begin end; procedure TDAKDBSchemaInfo.FieldsByTable(const TableName: string; FieldsList: TDBFieldList); var lDataSet: TDADataSet; lNewField: TDBFieldInfo; i: Integer; begin inherited; lDataSet := Schema.Datasets.FindItem(TableName) as TDADataSet; if not Assigned(lDataSet) then RaiseError('DataSet %s not found in Schema',[TableName]); FieldsList.Clear(); for i := 0 to lDataSet.Fields.Count-1 do begin lNewField := TDBFieldInfo.Create(); lNewField.FieldName := lDataSet.Fields[i].Name; lNewField.FieldSize := lDataSet.Fields[i].Size; lNewField.FieldType := DATypeToVCLType(lDataSet.Fields[i].DataType); FieldsList.Add(lNewField); end; { for } end; procedure TDAKDBSchemaInfo.FillStructure(aStructure: TKDBStructure); var lLink: TDSLink; lFK: TDADatasetRelationship; i: Integer; sl: TStringList; lTable: integer; begin aStructure.ClearStructure(); aStructure.AddDefOperators(true); sl := TStringList.Create(); try GetTableNames(sl); sl.Sort; for i := 0 to (sl.Count-1) do begin lTable := aStructure.AddTable(sl[i],sl[i]); AddGroupWithFields(aStructure, aStructure.Tables[lTable]); end; finally sl.Free(); end; for i := 0 to Schema.Relationships.Count-1 do begin lLink := TDSLink.Create(); lFK := Schema.Relationships[i]; lLink.Table1 := aStructure.TableByName(0, lFK.DetailDatasetName); lLink.Table2 := aStructure.TableByName(0, lFK.MasterDatasetName); //ToDo: add list //lLink.Fields1.Text := lFK.DetailFields; //lLink.Fields2.Text := lFK.MasterFields; aStructure.AddLinkByRef(lLink) end; end; procedure TDAKDBSchemaInfo.AvailableDatabases(DBList : TStrings); //NEW: previously realization of GetDatabaseNames, name Alist replaced with DBList begin inherited; DBList.Clear(); DBList.Add(DatabaseName); end; procedure TDAKDBSchemaInfo.TablesByDatabase(TablesList: TStrings); var i: Integer; begin inherited; TablesList.BeginUpdate(); try TablesList.Clear(); for i := 0 to Schema.Datasets.Count-1 do begin TablesList.Add(Schema.Datasets[i].Name); end; { for } finally TablesList.EndUpdate(); end; end; procedure TDAKDBSchemaInfo.GetSQLValues(SQL: String; AValues, AItems: TStrings); begin inherited; end; procedure TDAKDBSchemaInfo.Notification(AComponent: TComponent; Operation: TOperation); begin inherited; if Operation <> opRemove then exit; if AComponent = Schema then Schema := nil; end; procedure TDAKDBSchemaInfo.SetSchema(const Value: TDASchema); begin if (fSchema <> Value) then begin fSchema := Value; if Assigned(fSchema) then fSchema.FreeNotification(self); end; end; end.