maz
maz

Reputation: 41

Determine a parameters data type in a parameterized query using Zeoslib

Is it possible to determine a parameters data type in a parameterized query using Zeoslib and Firebird?

I'm migrating from FibPlus to Zeoslib.

with a stament like 'select name, surname, age from members where registration > :since and nationality = :country'

With FibPlus I do this:

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

    t := Q1.Params[i].ServerSQLType;

    case t of
      SQL_TYPE_DATE, SQL_TYPE_DATE + 1:
        begin
          with TsDateEdit.Create(self) do
          begin
            Left := lblleft + MaxWidth + 10;
            Top := lbltop + (25 * i) - (height - LabelHeight);
            Parent := f;
            Tag := i + 1;
            TabOrder := i;
          end;
        end;
      sql_short, sql_long, sql_int64, sql_short + 1, sql_long + 1, sql_int64 + 1:
        begin
          with TsSpinEdit.Create(self) do
          begin
            Left := lblleft + MaxWidth + 10;
            Top := lbltop + (25 * i) - (height - LabelHeight);
            Parent := f;
            Tag := i + 1;
            TabOrder := i;
          end;
        end;
....

With Zeoslib I have tried this:

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

    if Q1.Params[i].DataType = ftDate then
        begin
          with TsDateEdit.Create(self) do
          begin
            Left := lblleft + MaxWidth + 10;
            Top := lbltop + (25 * i) - (height - LabelHeight);
            Parent := f;
            Tag := i + 1;
            TabOrder := i;
          end;
        end;

    if Q1.Params[i].DataType in [ftInteger, ftShortInt, ftLargeInt, ftLongWord] then
        begin
          with TsSpinEdit.Create(self) do
          begin
            Left := lblleft + MaxWidth + 10;
            Top := lbltop + (25 * i) - (height - LabelHeight);
            Parent := f;
            Tag := i + 1;
            TabOrder := i;
          end;
        end;
....

but Q1.Params[i].DataType is allways ftUnkown.

Upvotes: 4

Views: 1457

Answers (1)

Imants Gulbis
Imants Gulbis

Reputation: 87

You can't do that with prams because they are not set at this moment. I suggest you to use TZSQLMetadata;

  ZSQLMetadata1 := TZSQLMetadata.Create(Self);
  try
    ZSQLMetadata1.Connection := ZConnection1; //Your connection to db
    ZSQLMetadata1.MetadataType := mdColumns; //What kind of meta data get you want column types so we get columns metadata
    ZSQLMetadata1.TableName := 'MYTABLE'; //from which table metadata to get
    ZSQLMetadata1.Open;

    //After open ZSQLMetadata1 rows contains information about table columns
    while not ZSQLMetadata1.Eof do
    begin
      if ZSQLMetadata1.FieldByName('TYPE_NAME').AsString = 'char' then
      begin
        //Do something
      end;

      { if you do not like working with strings
      if ZSQLMetadata1.FieldByName('DATA_NAME').AsInteger = 9 then
      begin
        //Do something
      end;
      }   
      ZSQLMetadata1.Next;
    end;
  finally
    ZSQLMetadata1.Free;
  end;

ZSQLMetadata1 works like dataset you can assign it to datasource ad look at all columns and values in dbgrid

/edit/ Column names and field values is DB specifics my example is on MySQL database

Upvotes: 2

Related Questions