Ver Mensaje Individual
  #1  
Antiguo 23-11-2012
Avatar de cesarsoftware
cesarsoftware cesarsoftware is offline
Miembro
 
Registrado: nov 2006
Posts: 241
Reputación: 18
cesarsoftware Va por buen camino
Extrayendo metadatos

Hola compis, tengo una duda no existencial, uso un codigo que circula por la red para extraer los campos de una tabla (para tener integrado un visor de bbdd en las aplicaciones).

Es el siguiente
Código Delphi [-]
    sql := 'SELECT r.RDB$RELATION_NAME as table_name,';
    sql := sql + ' r.RDB$FIELD_POSITION as field_position,';
    sql := sql + ' r.RDB$FIELD_NAME AS field_name,';
    sql := sql + ' r.RDB$DESCRIPTION AS field_description,';
    sql := sql + ' r.RDB$DEFAULT_VALUE AS field_default_value,';
    sql := sql + ' r.RDB$NULL_FLAG AS field_not_null_constraint,';
    sql := sql + ' f.RDB$FIELD_LENGTH AS field_length,';
    sql := sql + ' f.RDB$FIELD_PRECISION AS field_precision,';
    sql := sql + ' f.RDB$FIELD_SCALE AS field_scale,';
    sql := sql + ' CASE f.RDB$FIELD_TYPE';
    sql := sql + '   WHEN 7 THEN ''' + 'SMALLINT''';
    sql := sql + '   WHEN 8 THEN ''' + 'INTEGER''';
    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 ''' + 'CHAR''';
    sql := sql + '   WHEN 16 THEN ''' + 'BIGINT''';
    sql := sql + '   WHEN 27 THEN ''' + 'DOUBLE PRECISION''';
    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 + ' ELSE ''' + 'UNKNOWN''';
    sql := sql + ' END AS field_type,';
    sql := sql + ' f.RDB$FIELD_SUB_TYPE AS field_subtype,';
    sql := sql + ' coll.RDB$COLLATION_NAME AS field_collation,';
    sql := sql + ' cset.RDB$CHARACTER_SET_NAME AS field_charset';
    sql := sql + ' FROM RDB$RELATION_FIELDS r';
    sql := sql + ' LEFT JOIN RDB$FIELDS f ON r.RDB$FIELD_SOURCE = f.RDB$FIELD_NAME';
    sql := sql + ' LEFT JOIN RDB$COLLATIONS coll ON r.RDB$COLLATION_ID = coll.RDB$COLLATION_ID';
    sql := sql + ' AND f.RDB$CHARACTER_SET_ID = coll.RDB$CHARACTER_SET_ID';
    sql := sql + ' LEFT JOIN RDB$CHARACTER_SETS cset ON f.RDB$CHARACTER_SET_ID = cset.RDB$CHARACTER_SET_ID';
    sql := sql + ' WHERE RDB$RELATION_NAME = ''' + tabla + '''';
    sql := sql + ' ORDER BY r.RDB$RELATION_NAME, r.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;
      Exit;
    end;
  end;
  cuantos := DataSetCampos.RecordCount;
  STnumCampos.Caption := IntToStr(cuantos);

En primer lugar deciros que no entiendo las funciones "coll" y "cset" ni poque ha veces usa "f" y otras "r" pero funciona, si me lo podeis explicar me haceis un favor.
Y lo segundo es que no entiendo porque hace un case para obtener el tipo de datos si estos estan en la tabla RDB$TYPES, ¿no se podria hacer un left join o algo para sacar directamente el tipo de datos, es decir si el tipo es 14 entonces "TEXT" ya que ese datos esta en la tabla de tipos?

Gracias de antemano.
Responder Con Cita