Board index » delphi » Getting underlying field type programaticly

Getting underlying field type programaticly

I want to find the underlying field type of all columns in a table. The
Database is Oracle 8. In the database explorer I can see the underlying
type, but how do I get to by going through TSession and TTable.

John.

 

Re:Getting underlying field type programaticly


I use this to fill in required fields after calling an insert into a
dataset, check out the Field.datatype.

Var
  i : Integer;
  Field : TField;
begin
  inherited;
  // loop through all fields and find out if they are required, if so then
autofill them.
  for i := 0 to ADataset.FieldCount -1 do begin
    if ADataset.Fields[i].Required then begin
      if ADataset.Fields[i].IsNull then begin
        Field := ADataset.FieldByName(ADataset.Fields[i].FieldName);

          case Field.DataType of    //
            ftSmallInt, ftInteger: ADataset.Fields[i].Value := 0;
            ftDateTime: ADataset.Fields[i].Value := now;
           else begin
             if Field.Size > 1 then
               ADataset.Fields[i].Value := ' '
             else
               ADataset.Fields[i].Value := 'N';
           end;
          end; // case
      end;
    end;
  end;

Re:Getting underlying field type programaticly


John,

What do you mean by field type? The BDE translates the physical data
type of the field to a logical data type which is "universal". The
logical field type is given by the DataType property of TField. If you
need the physical data type then you need to probe into the Dbi calls.

Bob
---
Sent using Virtual Access 5.01 - download your freeware copy now
http://www.atlantic-coast.com/downloads/vasetup.exe

Re:Getting underlying field type programaticly


I am trying to write a tool to use with ORACLE Pro*C. For this I need to
know if the field is a CHAR or VARCHAR or NUMBER or DATE or whatever.
Which Dbi functions to look at, and where to start to find them?

John.

Quote
Bob Villiers wrote:

> John,

> What do you mean by field type? The BDE translates the physical data
> type of the field to a logical data type which is "universal". The
> logical field type is given by the DataType property of TField. If you
> need the physical data type then you need to probe into the Dbi calls.

> Bob
> ---
> Sent using Virtual Access 5.01 - download your freeware copy now
> http://www.atlantic-coast.com/downloads/vasetup.exe

Re:Getting underlying field type programaticly


John,

Below is some code for displaying in a stringGrid the logical and
physical field types of a paradox table. The constants are declared in
BDE.pas for Paradox, dBase, Access and ASCII tables. I do not know
where you would get the equivalent for Oracle but maybe this will get
you started.

Bob
---

procedure TForm1.ShowFields(T: TTable);
var
   curProp: CURProps;
   pfldDes, pCurFld: pFLDDesc;
   i: integer;          // counter
   MemSize: integer;
   s: string;
begin
  if Checkbox1.Checked then
    // Set the translation mode of cursor to xltNONE...
    Check(DbiSetProp(hDBIObj(T.Handle), curxltMODE, integer(xltNONE)));

  Check(DbiGetCursorProps(T.Handle, curProp));
  StringGrid1.Colcount:= T.FieldCount +1;
  for i:= 0 to 11 do begin
    Case i of
      0: StringGrid1.Cells[0,0]:=  '  iFldNum';
      1: StringGrid1.Cells[0,1]:=  '  szName';
      2: StringGrid1.Cells[0,2]:=  '  iFldType';
      3: StringGrid1.Cells[0,3]:=  '  iSubType';
      4: StringGrid1.Cells[0,4]:=  '  iUnits1';
      5: StringGrid1.Cells[0,5]:=  '  iUnits2';
      6: StringGrid1.Cells[0,6]:=  '  iOffset';
      7: StringGrid1.Cells[0,7]:=  '  iLen';
      8: StringGrid1.Cells[0,8]:=  '  iNullOffset';
      9: StringGrid1.Cells[0,9]:=  '  efldvVchk ';
      10: StringGrid1.Cells[0,10]:= '  efldrRights';
      11: StringGrid1.Cells[0,11]:= '  bCalcField ';
    end;
  end;
  MemSize := curProp.iFields * SizeOf(FLDDesc);
  pfldDes := AllocMem(MemSize);
  try
    pCurFld := pfldDes;
    Check(DbiGetFieldDescs(T.Handle, pfldDes));
    i:= 0;
    while (i < T.FieldCount) do begin
      StringGrid1.Cells[i+1,0]:=  IntToStr(pCurFld^.iFldNum);
      StringGrid1.Cells[i+1,1]:=  pCurFld^.szName;
      s:= '';
      if Checkbox1.Checked then
      begin
      case pCurFld^.iFldType of
        $101: s:= 'fldPDXCHAR';
        $102: s:= 'fldPDXNUM';
        $103: s:= 'fldPDXMONEY';
        $104: s:= 'fldPDXDATE';
        $105: s:= 'fldPDXSHORT';
        $106: s:= 'fldPDXMEMO';
        $107: s:= 'fldPDXBINARYBLOB';
        $108: s:= 'fldPDXFMTMEMO';
        $109: s:= 'fldPDXOLEBLOB';
        $10A: s:= 'fldPDXGRAPHIC';
        $10B: s:= 'fldPDXLONG';
        $10C: s:= 'fldPDXTIME';
        $10D: s:= 'fldPDXDATETIME';
        $10E: s:= 'fldPDXBOOL';
        $10F: s:= 'fldPDXAUTOINC';
        $110: s:= 'fldPDXBYTES';
        $111: s:= 'fldPDXBCD';
      end;

      end
      else
      begin
        case pCurFld^.iFldType of
          0: s:= 'fldUNKNOWN';
          1: s:= 'fldZSTRING';        { Null terminated string }
          2: s:= 'fldDATE';           { Date     (32 bit) }
          3: s:= 'fldBLOB';           { Blob }
          4: s:= 'fldBOOL';           { Boolean  (16 bit) }
          5: s:= 'fldINT16';          { 16 bit signed number }
          6: s:= 'fldINT32';          { 32 bit signed number }
          7: s:= 'fldFLOAT';          { 64 bit floating point }
          8: s:= 'fldBCD';            { BCD }
          9: s:= 'fldBYTES';          { Fixed number of bytes }
         10: s:= 'fldTIME';           { Time        (32 bit) }
         11: s:= 'fldTIMESTAMP';      { Time-stamp  (64 bit) }
         12: s:= 'fldUINT16';         { Unsigned 16 bit integer }
         13: s:= 'fldUINT32';         { Unsigned 32 bit integer }
         14: s:= 'fldFLOATIEEE';      { 80-bit IEEE float }
         15: s:= 'fldVARBYTES';       { Length prefixed var bytes }
         16: s:= 'fldLOCKINFO';       { Look for LOCKINFO typedef }
         17: s:= 'fldCURSOR';         { For Oracle Cursor type }
        end;
      end;
      StringGrid1.Cells[i+1,2]:= s;
      s:= '';
      case pCurFld^.iSubType of
         1: s:=  'fldstPASSWORD';
        21: s:=  'fldstMONEY';
        22: s:=  'fldstMEMO';
        23: s:=  'fldstBINARY';
        24: s:=  'fldstFMTMEMO';
        25: s:=  'fldstOLEOBJ';
        26: s:=  'fldstGRAPHIC';
        27: s:=  'fldstDBSOLEOBJ';    { dBASE OLE object }
        28: s:=  'fldstTYPEDBINARY';
        29: s:=  'fldstAUTOINC';
        30: s:=  'fldstACCOLEOBJ';    { Access OLE object }
        31: s:=  'fldstFIXED';        { CHAR type }
      end;
      StringGrid1.Cells[i+1,3]:=  s;
      StringGrid1.Cells[i+1,4]:=  IntToStr(pCurFld^.iUnits1);
      StringGrid1.Cells[i+1,5]:=  IntToStr(pCurFld^.iUnits2);
      StringGrid1.Cells[i+1,6]:=  IntToStr(pCurFld^.iOffset);
      StringGrid1.Cells[i+1,7]:=  IntToStr(pCurFld^.iLen);
      StringGrid1.Cells[i+1,8]:=  IntToStr(pCurFld^.iNullOffset);
    //efldvVchk       : FLDVchk;          { Field Has vcheck (computed)

Quote
}

    //efldrRights     : FLDRights;        { Field Rights (computed) }
      StringGrid1.Cells[i+1,11]:=  BoolToStr(pCurFld^.bCalcField);  {
Is Calculated field (computed)}
      // increment pointer to the next record
      inc(pCurFld);
      inc(i);
    end;
  finally
    FreeMem(pfldDes, MemSize);
  end;
  if Checkbox1.Checked then
    // Set the translation mode of cursor back to xltFIELD...
    Check(DbiSetProp(hDBIObj(T.Handle), curxltMODE,
integer(xltFIELD)));
end;

Other Threads