Ver Mensaje Individual
  #11  
Antiguo 26-11-2012
Avatar de cesarsoftware
cesarsoftware cesarsoftware is offline
Miembro
 
Registrado: nov 2006
Posts: 241
Reputación: 18
cesarsoftware Va por buen camino
Bueno, creo que se lo que pasa, tema resuelto.

Segun se ve en la tabla de tipos, digamos, que no esta todo lo precisa que se necesita. He realizado una segunda pasada para incluir los nombres de los tipos y no queda como quisieramos, hay que hacer el CASE "en linea" como hace todo el mundo (o el primero que empezo, jejeje)
En esta imagen se ve la aplicacion que muestra los campos de las tablas usando la funcion que nos ocupa (osea, funciona)

En esta imagen se ven los tipos de datos en la tabla RDB$TYPES


He "Adecentado" o mejor dicho simplificado y adecuado el codigo para tener todos los campos de una tabla y sus caracteristicas.
Dejo toda la funcion completa ya que despues hay que sacar los indices y los campos que corresponden a los indices.
Este codigo es totalmente funcional, evidentemente el formulario y las grillas las pone cada usuario
Casimiro, puedes usarlo cuando quieras, jejeje
Código Delphi [-]
procedure TFormConfTablas.LeerCampos(tabla: string);
var
  sql, msg, nombreindice, campo, tipo: string;
  DataSetCampos, DataSetIndices, DataSetSegment: TSQLDataset;
  cuantos, i, j, indices, campos: word;
  longitud, precision, escala: integer;
begin
  SGcampos.OnClick := nil;
  SGindices.OnClick := nil;
  IniciaGrillas();
  sql := 'SELECT rela.RDB$RELATION_NAME as table_name,';
  sql := sql + ' rela.RDB$FIELD_POSITION as field_position,';
  sql := sql + ' rela.RDB$FIELD_NAME AS field_name,';
  sql := sql + ' fiel.RDB$FIELD_LENGTH AS field_length,';
  sql := sql + ' fiel.RDB$FIELD_PRECISION AS field_precision,';
  sql := sql + ' fiel.RDB$FIELD_SCALE AS field_scale,';
  sql := sql + ' CASE fiel.RDB$FIELD_TYPE';
  sql := sql + '   WHEN 7 THEN';// ''' + 'SHORT''';
  sql := sql + '   CASE fiel.RDB$FIELD_SUB_TYPE';
  sql := sql + '     WHEN 1 THEN ''' + 'NUMERIC''';
  sql := sql + '     WHEN 2 THEN ''' + 'DECIMAL''';
  sql := sql + '     ELSE ''' + 'SMALLINT''';
  sql := sql + '   END';
  sql := sql + '   WHEN 8 THEN';// ''' + 'LONG''';
  sql := sql + '   CASE fiel.RDB$FIELD_SUB_TYPE';
  sql := sql + '     WHEN 1 THEN ''' + 'NUMERIC''';
  sql := sql + '     WHEN 2 THEN ''' + 'DECIMAL''';
  sql := sql + '     ELSE ''' + 'INTEGER''';
  sql := sql + '   END';
  sql := sql + '   WHEN 9 THEN ''' + 'QUAD''';
  sql := sql + '   WHEN 10 THEN ''' + 'FLOAT''';
  sql := sql + '   WHEN 12 THEN ''' + 'DATE''';
  sql := sql + '   WHEN 13 THEN ''' + 'TIME''';
  sql := sql + '   WHEN 14 THEN ''' + 'TEXT''';
  sql := sql + '   WHEN 16 THEN';// ''' + 'INT64''';
  sql := sql + '   CASE fiel.RDB$FIELD_SUB_TYPE';
  sql := sql + '     WHEN 1 THEN ''' + 'NUMERIC''';
  sql := sql + '     WHEN 2 THEN ''' + 'DECIMAL''';
  sql := sql + '     ELSE ''' + 'BIGINT''';
  sql := sql + '   END';
  sql := sql + '   WHEN 27 THEN ''' + 'DOUBLE''';
  sql := sql + '   WHEN 35 THEN ''' + 'TIMESTAMP''';
  sql := sql + '   WHEN 37 THEN ''' + 'VARCHAR''';
  sql := sql + '   WHEN 40 THEN ''' + 'CSTRING''';
  sql := sql + '   WHEN 45 THEN ''' + 'BLOB_ID''';
  sql := sql + '   WHEN 261 THEN';// ''' + 'BLOB''';
  sql := sql + '   CASE fiel.RDB$FIELD_SUB_TYPE';
  sql := sql + '     WHEN 0 THEN ''' + 'UNSPECIFIED''';
  sql := sql + '     WHEN 1 THEN ''' + 'TEXT''';
  sql := sql + '     WHEN 2 THEN ''' + 'BLT''';
  sql := sql + '     WHEN 3 THEN ''' + 'ACL''';
  sql := sql + '     WHEN 4 THEN ''' + 'RANGES''';
  sql := sql + '     WHEN 5 THEN ''' + 'SUMMARY''';
  sql := sql + '     WHEN 6 THEN ''' + 'FORMAT''';
  sql := sql + '     WHEN 7 THEN ''' + 'TRANSACTION_DESCRIPTION''';
  sql := sql + '     WHEN 8 THEN ''' + 'EXTERNAL_FILE_DESCRIPCTION''';
  sql := sql + '   END';
  sql := sql + ' ELSE ''' + 'UNKNOWN''';
  sql := sql + ' END AS field_type';
  sql := sql + ' FROM RDB$RELATION_FIELDS rela';
  sql := sql + ' LEFT JOIN RDB$FIELDS fiel ON rela.RDB$FIELD_SOURCE = fiel.RDB$FIELD_NAME';
  sql := sql + ' WHERE RDB$RELATION_NAME = ''' + tabla + '''';
  sql := sql + ' ORDER BY rela.RDB$RELATION_NAME, rela.RDB$FIELD_POSITION;';
  try
    FormBBDD.SQLConnection.Execute(sql, nil, @DataSetCampos);
  except
    on E: Exception do
    begin
      msg := 'No se ha podido leer la tabla ' + tabla + #13 + #10 + E.Message;
      AvisoMsg(msg, True);
      DataSetCampos.Free;
      SGcampos.OnClick := SGcamposClick;
      SGindices.OnClick := SGindicesClick;
      Exit;
    end;
  end;
  cuantos := DataSetCampos.RecordCount;
  STnumCampos.Caption := IntToStr(cuantos);
  if cuantos = 0 then
  begin
    DataSetCampos.Free;
    SGcampos.OnClick := SGcamposClick;
    SGindices.OnClick := SGindicesClick;
    Exit;
  end;
  for i := 0 to cuantos - 1 do
  begin
    tipo := Trim(DataSetCampos.FieldByName('field_type').AsString);
    longitud := DataSetCampos.FieldByName('field_length').AsInteger;
    precision := DataSetCampos.FieldByName('field_precision').AsInteger;
    escala := DataSetCampos.FieldByName('field_scale').AsInteger;
    if escala < 0 then
      escala := escala * -1;
    if precision > 0 then
    begin
      if (tipo = 'SMALLINT')
      or (tipo = 'INTEGER')
      or (tipo = 'BIGINT') then
      begin
        tipo := 'NUMERIC';
        longitud := precision;
      end;
    end;
    SGcampos.Cells[0, i + 1] := Trim(DataSetCampos.FieldByName('field_position').AsString);
    SGcampos.Cells[1, i + 1] := Trim(DataSetCampos.FieldByName('field_name').AsString);
    SGcampos.Cells[2, i + 1] := tipo;
    SGcampos.Cells[3, i + 1] := IntToStr(longitud);
    SGcampos.Cells[4, i + 1] := IntToStr(escala);
    SGcampos.RowCount := SGcampos.RowCount + 1;
    SGcampos.Row := SGcampos.RowCount - 1;
    DataSetCampos.Next;
  end;
  SGcampos.RowCount := SGcampos.RowCount - 1;
  DataSetCampos.Free;
  // Leer Indices
  sql := 'SELECT RDB$INDEX_NAME, RDB$INDEX_ID, RDB$SEGMENT_COUNT FROM RDB$INDICES';
  sql := sql + ' WHERE RDB$RELATION_NAME = ''' + tabla + '''';
  try
    FormBBDD.SQLConnection.Execute(sql, nil, @DataSetIndices);
  except
    on E: Exception do
    begin
      msg := 'No se ha podido leer los indices de la tabla ' + tabla + #13 + #10 + E.Message;
      AvisoMsg(msg, True);
      DataSetIndices.Free;
      SGcampos.OnClick := SGcamposClick;
      SGindices.OnClick := SGindicesClick;
      Exit;
    end;
  end;
  cuantos := DataSetIndices.RecordCount;
  STnumIndices.Caption := IntToStr(cuantos);
  if cuantos = 0 then
  begin
    DataSetIndices.Free;
    SGcampos.OnClick := SGcamposClick;
    SGindices.OnClick := SGindicesClick;
    Exit;
  end;
  for i := 0 to cuantos - 1 do
  begin
    nombreindice := Trim(DataSetIndices.FieldByName('RDB$INDEX_NAME').AsString);
    SGindices.Cells[0, i + 1] := Trim(DataSetIndices.FieldByName('RDB$INDEX_ID').AsString);
    SGindices.Cells[1, i + 1] := nombreindice;
    indices := DataSetIndices.FieldByName('RDB$SEGMENT_COUNT').AsInteger;
    if indices = 0 then
      Continue;
    // Leer campos de indices
    sql := 'SELECT RDB$FIELD_NAME, RDB$FIELD_POSITION FROM RDB$INDEX_SEGMENTS';
    sql := sql + ' WHERE RDB$INDEX_NAME = ''' + nombreindice + '''';
    sql := sql + ' ORDER BY RDB$FIELD_POSITION ASC';
    try
      FormBBDD.SQLConnection.Execute(sql, nil, @DataSetSegment);
    except
      on E: Exception do
      begin
        msg := 'No se ha podido leer los campos de los indices de la tabla ' + tabla  + #13 + #10 + E.Message;
        AvisoMsg(msg, True);
        DataSetSegment.Free;
        Continue;
      end;
    end;
    campos := DataSetSegment.RecordCount;
    if campos = 0 then
    begin
      DataSetSegment.Free;
      Continue;
    end;
    for j := 0 to campos - 1 do
    begin
      if (2 + j) <= (SGindices.ColCount - 1) then
      begin
        campo := Trim(DataSetSegment.FieldByName('RDB$FIELD_NAME').AsString);
        SGindices.Cells[2 + j, i + 1] := campo;
      end;
      DataSetSegment.Next;
    end;
    SGindices.RowCount := SGindices.RowCount + 1;
    SGindices.Row := SGindices.RowCount - 1;
    DataSetSegment.Free;
    DataSetIndices.Next;
  end;
  SGindices.RowCount := SGindices.RowCount - 1;
  DataSetIndices.Free;
  SGcampos.OnClick := SGcamposClick;
  SGindices.OnClick := SGindicesClick;
  Application.ProcessMessages;
end;

Tema cerrado.

Última edición por cesarsoftware fecha: 26-11-2012 a las 20:29:46.
Responder Con Cita