我想写个类,用来描述并保一个任意的Oracle数据库的表(包括表结构和所有纪录的字段值),请问对它的实现有何高见?

解决方案 »

  1.   

    delphi提供的组件好像取不出所有的元数据,比如数据长度的定义,用java可以
      

  2.   

    将字段作为属性published,将数据库的操作封装,直接读出后写入私有的数据字段成员,写入时类似
      

  3.   

    不同的表的字段是不同的,所以只是简单地将字段作为属性published是肯定不行的!!
      

  4.   

    我写了一个类: 看一下,满意请给分!//    Description: Retrieve database properities when application started.
    unit DBDefU;interfaceuses
      Forms, SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
      DB, DBTables, dialogs, ListUtilU;type                        // which database?
                            // The table naming is different for each database.
                            // For Paradox table, we need remove the file extension.
                            // For SQL Server table, we need remove the prefix of the database name.
      TDBType = (eParadox,eSQLServer,eOracle);  TDBFieldDef = class
        DataType : TFieldType;
        Size : integer;
      end;  TDBDef = class
      private
        { Private declarations }
        m_pTableList : TCustStringList;
        m_pQuery : TQuery;
        m_pDM : TDataModule;
        m_eDBType : TDBType;    procedure SetFieldList(sTbName : string);
      protected
        procedure SetDBType( eType : TDBType );
        function  sfnChopTableName(sTBName : string; sDBOwner : string) : string;
      public
        { Public declarations }
        constructor Create(pDM : TDataModule);
        destructor Destroy; override;    procedure GetDBNameList(pList : TStringList);
        procedure GetTbPropertyList(sTbName : string; pList : TStringList);    function pfTbNameList : TCustStringList;
        function pfFieldList(sTbName : string) : TCustStringList;
        function ifFieldSize(sTBName, sFldName : string) : integer;
        function pfFieldType(sTBName, sFldName : string) : TFieldType;
        function pfFieldDef(sTBName, sFldName : string) : TDBFieldDef;    procedure InitDBDef(sDBOwner : string);    property DatabaseType : TDBType read m_eDBType write SetDBType;
      end;var
      G_pDBDef : TDBDef;implementationuses DBiTypes,
         DbiProcs,
         GlobalConstU,
         DBConstU,
         StrUtilU,
         SQLBuilderU,
         ABSDM,
         FileUtilU;const
                            // delimiter for the table name.
      C_sTableNameDelimiter = '.';
      

  5.   

    还不给分阿?
    constructor TDBDef.Create(pDM : TDataModule);
    begin
      inherited Create;
      m_pDM := pDM;
      m_pTableList := TCustStringList.Create;
      m_pQuery := TQuery.Create(nil);
                            // default is for PTWin
      //SetDBType( eSQLServer );
      //DatabaseType := TdmCDS(pDM).DBType;
      DatabaseType := TdmABS(m_pDM).DBType
    end;destructor TDBDef.Destroy;
    begin
      m_pTableList.Free;
      m_pQuery.Active := False;
      m_pQuery.Free;
      inherited;
    end;procedure TDBDef.GetDBNameList(pList : TStringList);
    begin
      Session.GetDatabaseNames(pList);
    end;procedure TDBDef.InitDBDef(sDBOwner : string);
    var
      pList : TStringList;
      sTbl : string;
      i : integer;begin
      pList := TStringList.Create;
      m_pTableList.Clear;
      try
         //Session.GetFieldNames();
         //Session.GetTableNames('dbVWH','', True, False,Plist);
         //TdmABS(m_pDM).Database.GetTableNames(pList,False);
         if DatabaseType = eOracle then
           TdmABS(m_pDM).bfDBFieldForStringList('CAT','TABLE_NAME','TABLE_TYPE=''TABLE''',pList)
         else
           TdmABS(m_pDM).Database.Session.GetTableNames(TdmABS(m_pDM).Database.DatabaseName,
             '', True, False,pList);     for i := 0 to pList.Count-1 do
         begin
           sTbl := sfnChopTableName(pList.Strings[i], sDBOwner);
           if sTbl<>EmptyStr then
             SetFieldList(sTbl);
         end;
      finally
        pList.Free;
      end;
    end;function TDBDef.pfTbNameList : TCustStringList;
    begin
      result := m_pTableList;
    end;procedure TDBDef.GetTbPropertyList(sTbName : string; pList : TStringList);
    var
      pSelectedTbProp: CURProps;
      sDbiName: DBINAME;
      sDbiNameLen: Word;
      sSQL : string;
    begin
      try         
        sSQL := sfSQLFormatSelectFromWhere(C_sDBAll, sTbName, C_sDBFalseCondition);
        if TdmABS(m_pDM).bfOpenQuery(m_pQuery, sSQL) then
        with m_pQuery do
        begin
          Check(DbiGetProp(hDBIObj(Handle), dbDATABASENAME,@(sDbiName),sizeOf( sDbiName ),sDbiNameLen));
          Check(DbiGetCursorProps(Handle, pSelectedTbProp));
        end;    with pList, pSelectedTbProp do
        begin
          Clear;
          Add( 'Database Name: ' + StrPas( sDbiName));
          { table name (no extension, if it can be derived) }
          Add( 'Table Name: ' + StrPas( szName ));
          { Full file name size }
          Add( 'File Name Size: ' + IntToStr( iFNameSize ));
          { Driver type }
          Add( 'Table Type: ' + StrPas( szTableType ));
          { No of fields in Table }
          Add( 'Number of Fields: ' + IntToStr( iFields ));
          { Record size (logical record) }
          Add( 'Logical Record Size: ' + IntToStr( iRecSize ));
          { Record size (physical record) }
          Add( 'Physical Record Size: ' + IntToStr( iRecBufSize ));
          { Key size }
          Add( 'Key Size: ' + IntToStr( iKeySize ));
          { Number of indexes }
          Add( 'Number of Indexes: ' + IntToStr( iIndexes ));
          { Number of val checks }
          Add( 'Number of Validity Checks: ' + IntToStr( iValChecks ));
          { Number of Referential Integrity constraints }
          Add( 'Number of RIs: ' + IntToStr( iRefIntChecks ));
          { Book size }
          Add( 'Book Size: ' + IntToStr( iBookMarkSize ));
          { Stable book s }
          Add( 'Stable Book Marks: ' +
            IntToStr( Integer( bBookMarkStable) ));
          { ReadOnly / RW }
          Add( 'Read Only: ' + IntToStr( Integer( eOpenMode )));
          { Excl / Share }
          Add( 'Share Mode: ' + IntToStr( Integer( eShareMode )));
          { Index is in use }
          Add( 'Index in Use: ' + IntToStr( Integer( bIndexed )));
          { 1: Has Seqnums; 0: Has Record# }
          Add( 'Seq/Rec Num: ' + IntToStr( iSeqNums ));
          { Supports soft deletes }
          Add( 'Soft Deletes: ' + IntToStr( Integer( bSoftDeletes )));
          { If above, deleted recs seen }
          Add( 'Deleted On: ' + IntToStr( Integer( bDeletedOn )));
          { Translate Mode }
          Add( 'Translate Mode: ' + IntToStr( Integer( exltMode )));
          { Restructure version number }
          Add( 'Restructure Version: ' + IntToStr( iRestrVersion ));
          { Cursor is uni-directional }
          Add( 'Unidirectional: ' +
            IntToStr( Integer( bUniDirectional )));
          { Table  rights }
          Add( 'Table Rights: ' + IntToStr( Integer( eprvRights )));
          { Family rights      }
          Add( 'Family Rights: ' + IntToStr( iFmlRights ));
          { Number of Aux passwords }
          Add( 'Auxliliary Passwords: ' + IntToStr( iPasswords ));
          { Codepage (0 if unknown) }
          Add( 'Codepage: ' + IntToStr( iCodePage ));
          { Table is protected by password }
          Add( 'Protected: ' + IntToStr( Integer( bProtected )));
          { Driver dependent table level }
          Add( 'Driver Table Level: ' + IntToStr( iTblLevel ));
          { Language driver name }
          Add( 'Language Driver: ' + StrPas( szLangDriver ));
          { Field map active }
          Add( 'Field Map On: ' + IntToStr( Integer( bFieldMap )));
          { Physical file blocksize in K }
          Add( 'Block Size (K): ' + IntToStr( iBlockSize ));
          { Strict referential integrity }
          Add( 'Strict RI: ' + IntToStr( Integer( bStrictRefInt )));
          { Number of filters }
          Add( 'Number of Filters: ' + IntToStr( iFilters ));
          { Table is a temporary table }
          Add( 'Temporary: ' + IntToStr( Integer( bTempTable )));
        end;
      finally
        m_pQuery.Close;
      end;
    end;//-- ...