Page 1 of 1

metadata for a table

Posted: 06.05.2009, 07:04
by yc1965
Hi,

I wonder if there is a way of getting the metadata (such as field descriptions) for a table?

Thanks in advance
Regards
Yusuf

Posted: 06.05.2009, 18:11
by seawolf
Take a look at the Zeos component called ZSQLMetadata, where you can find metadata info about tables, views

Posted: 07.05.2009, 11:42
by yc1965
I was aware of that component.
Just looking for something like similiar to
Connection.GetTableFields('TBL_NAME')

Thanks again
Yusuf

Posted: 07.05.2009, 12:35
by mdaems
In that case you could add these functions to the zeoslib component yourself or (even better) make a TMyZConnection=class(TZConnection) component adding these functions.

We can't just start to add this kind of functions for all properties just when someone asks it. But I can promise you one exception : if you write a generic procedure like GetMetadataList(Metadatatype, Column, [condition_field_array],[condition_value_array], List) and use this to re-implement the already existing metadata functions of TZConnection, I'll include this change into the official zeoslib library. (Implement in ZDbcMetadata.pas, add to interface and write a wrapper in zconnection.pas)

Just have a look at this sample:

Code: Select all

procedure TZConnection.GetTableNames(const tablePattern,schemaPattern: string; Types: TStringDynArray; List: TStrings);
var
  Metadata: IZDatabaseMetadata;
  ResultSet: IZResultSet;
begin
  CheckConnected;

  List.Clear;
  Metadata := DbcConnection.GetMetadata;
  ResultSet := Metadata.GetTables('', schemaPattern, tablePattern, types);
  while ResultSet.Next do
    List.Add(ResultSet.GetStringByName('TABLE_NAME'));
end;
and

Code: Select all

function TZSQLMetadata.CreateResultSet(const SQL: string; MaxRows: Integer):
  IZResultSet;
var
  Metadata: IZDatabaseMetadata;
begin
  Connection.ShowSQLHourGlass;
  try
    Metadata := Connection.DbcConnection.GetMetadata;

    case FMetadataType of
      mdProcedures:
        Result := Metadata.GetProcedures(FCatalog, FSchema, FProcedureName);
      mdProcedureColumns:
        Result := Metadata.GetProcedureColumns(FCatalog, FSchema,
          FProcedureName, FColumnName);
      mdTables:
        Result := Metadata.GetTables(FCatalog, FSchema, FTableName, nil);
      mdSchemas:
        Result := Metadata.GetSchemas;
      mdCatalogs:
        Result := Metadata.GetCatalogs;
      mdTableTypes:
        Result := Metadata.GetTableTypes;
      mdColumns:
        Result := Metadata.GetColumns(FCatalog, FSchema, FTableName,
          FColumnName);
      mdColumnPrivileges:
        Result := Metadata.GetColumnPrivileges(FCatalog, FSchema, FTableName,
          FColumnName);
      mdTablePrivileges:
        Result := Metadata.GetTablePrivileges(FCatalog, FSchema, FTableName);
      mdBestRowIdentifier:
        Result := Metadata.GetBestRowIdentifier(FCatalog, FSchema, FTableName,
          FScope, FNullable);
      mdVersionColumns:
        Result := Metadata.GetVersionColumns(FCatalog, FSchema, FTableName);
      mdPrimaryKeys:
        Result := Metadata.GetPrimaryKeys(FCatalog, FSchema, FTableName);
      mdImportedKeys:
        Result := Metadata.GetImportedKeys(FCatalog, FSchema, FTableName);
      mdExportedKeys:
        Result := Metadata.GetExportedKeys(FCatalog, FSchema, FTableName);
      mdCrossReference:
        Result := Metadata.GetCrossReference(FCatalog, FSchema, FTableName,
          FForeignCatalog, FForeignSchema, FForeignTableName);
      mdTypeInfo:
        Result := Metadata.GetTypeInfo;
      mdIndexInfo:
        Result := Metadata.GetIndexInfo(FCatalog, FSchema, FTableName, FUnique,
          FApproximate);
      mdSequences:
        Result := Metadata.GetSequences(FCatalog, FSchema, FSequenceName);
      mdUserDefinedTypes:
        Result := Metadata.GetUDTs(FCatalog, FSchema, FTypeName, nil);
     end;
  finally
    Connection.HideSQLHourGlass;
  end;
end;
I really hope you take the time to do this... It would be a nice feature for all the community.

Mark

Posted: 08.05.2009, 07:10
by yc1965
Thanks mdaems,
Yeah I will be happy to give you guys a hand.
I see what I can do.:D

Regards
Yusuf