Recent

Author Topic: [SOLVED]TField alias in TSQLQuery  (Read 2567 times)

larsal007

  • Newbie
  • Posts: 3
[SOLVED]TField alias in TSQLQuery
« on: May 23, 2013, 02:52:05 am »
Hello everybody,
This is my first post. I use a TSQLQuery to get data from postgresql server. I use alias field in SQL string and can get alias fieldname to generate components on the form and show the data but I am not able to get original DB field to do SQL updates.

Code: [Select]
var
  MainForm: TMainForm;
  FCon  : TPQConnection;
  FTrs  : TSQLTransaction;
  Libelles: array [0..100] of TLabel;
  Champs  : array [0..100] of TWinControl;
  sqlSRPList: string = 'select srpnum as "ID", srpnam as "Designation" from salesrep order by "ID";';
  sqlSRPForm: String ='select srpnum as "&Identifiant", srpnam as "&Nom",'+
                     'datein as "Date entrée", dateout as "Date de sortie",'+
                     'isactive as "Actif" from salesrep;';

procedure TMainForm.FormCreate(Sender: TObject);
begin
  FCon := TPQConnection.Create(self);
  FCon.DatabaseName:= '*******';
  FCon.UserName    := 'postgres';
  FCon.HostName    := '*********';
  FCon.Password    := '*********';

  FTrs := TSQLTransaction.Create(MainForm);
  FTrs.DataBase := FCon;
end;

function TMainForm.GetQuery : TSQLQuery;
  var AQuery : TSQLQuery;
begin
  AQuery := TSQLQuery.Create(MainForm);
  AQuery.Database    := FCon;
  AQuery.Transaction := FTrs;
  Result             := AQuery;
end;

procedure TMainForm.BuildForm(panel: TGroupBox; sql: string);
var
  i, ColCount, LabelWidth : integer;
  dataset                 : TSQLQuery;
begin
  LabelWidth:=0;
  if panel.ComponentCount>0 then exit;;
  //Initialiser la requete
  dataset     := GetQuery;
  dataset.SQL.Text := sql;
  dataset.Open;

  colcount :=dataset.Fields.Count;     //Le nombre de colonnes

  //Loop 1:
  //Calculer la largeur des libélles
  For i:=0 to colcount-1 do begin
    if LabelWidth < Canvas.TextWidth(dataset.Fields[i].FieldName) then
       LabelWidth:= Canvas.TextWidth(dataset.Fields[i].FieldName)+10;
  end;

  //Loop 2:
  for i:=0 to colcount-1 do begin
    //Création des champs
    case dataset.FieldByName(dataset.Fields[i].FieldName).DataType of
      ftString:begin
          Champs[i] := TmzEdit.Create(panel);            //Crée champ
          Champs[i].width := dataset.Fields[i].Size*11;  //Fixe largeur
          TmzEdit(Champs[i]).MaxLength:=dataset.Fields[i].Size;//Fixe taille saisie
        end;
      ftBoolean     :Champs[i] := TCheckBox.Create(panel);//Crée champ à cocher
      ftdate        :Champs[i] := TmzDateTime.Create(panel);//Crée champ date
    end;

    with Champs[i] do
    begin
      Parent := panel;
      Top    := 5+(20*i);
      Left   := LabelWidth;
    end;

    //Création des libéllés
    Libelles[i] := TLabel.Create(panel);//Créer les libellés
    with Libelles[i] do
    begin
      Parent := panel;
      Top    := 5+(20*i);
      Left   := 5;
      Width  := LabelWidth;
      Caption:=dataset.Fields[i].FieldName;
      FocusControl:=Champs[i];      //Affecte raccourci clavier au champ adequat
    end;
  end;
  panel.Height:=((panel.ComponentCount div 2)+1)*20;

  dataset.Close;
  dataset.Free;
end;

I need an instruction like dataset.Fields.FieldOriginalName wich replies srpnam or srpnum (fields names in database and not alias names).
Any help will be welcome.
« Last Edit: May 23, 2013, 05:00:12 pm by larsal007 »

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: TField alias in TSQLQuery
« Reply #1 on: May 23, 2013, 07:24:40 am »
I think it's more useful to use the original names of the fields in the table and the property DisplayLabel for aliases.

larsal007

  • Newbie
  • Posts: 3
Re: TField alias in TSQLQuery
« Reply #2 on: May 23, 2013, 04:59:29 pm »
After checking, it looks like the origin property is filled when the TSQLQuery is created in design time.

Unfortunately, it not my case. Anyway, I have to use a bad trick but it is ok for me : I parse the displaylabel property from the SQL query strings which includes both origin and displaylabel like this :
Code: [Select]
select srpnum as "srpnum:ID", srpnam as "srpnam:Designation" from salesrep order by "srpnum:ID"
I know this not the best but it works.

Thanks for your reply.