Mirror

Params in a Web-Query at run time (Views: 100)


Problem/Question/Abstract:

Property editors are fine, but code is more readable and maintainable if we put the data out from TQuery's ressource direct in the implementation section. So the serialisation is important.

Answer:

1. It's important to assign the query first (SQL:= aQuery) and second assign the Params including definition of dataType:

procedure TModWebBroker.execWebQuerySQL;
var
  aQuery: TStringList;
begin
  try
    aQuery := TStringList.Create;
    aQuery.clear;
    Build_kontoSQLString(aQuery);
    with qryWebCust do
    begin
      SQL.Clear;
      SQL := aQuery;
      params[0].name := 'clientname';
      params[0].dataType := ftstring;
      OPEN;
    end; //with
  finally
    aQuery.Free;
  end; //try
end;

2. We build the query in a own procedure, so it's open for change (programming for change ;)):

procedure TModWebBroker.Build_kontoSQLString(var aQuery: TStringList);
begin
  with aQuery do
  begin
    Add('SELECT cust_no, customer, descript, amount');
    Add('FROM account, customer, acctype');
    Add('WHERE account.cust_no = customer.cust_no');
    Add('AND account.acc_type = acctype.acc_id');
    Add('AND UPPER(customer) LIKE UPPER(:clientname)||'#39'%'#39);
    Add('ORDER BY amount');
  end;
end;

3. We get the parameter from a browser through a request-object in a ISAPI-DLL:
(The example "webqueries" is available on the net,now)

procedure TModWebBroker.kontoStand1Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse;
  var Handled: Boolean);
var
  paraField: string;
begin
  with qrywebAccount do
  begin
    paraField := (Request.QueryFields.Values['clientname']);
    if Pos(uppercase(paraField), uppercase(Content)) > 1 then
      Response.Content := Content
    else
      Response.Content := Format('Client"%s" not found!'
        + '',
        [Request.QueryFields.Values['clientname']]);
  end; //qrywebAccount
end;

<< Back to main page