CifSchemaMap.h

Go to the documentation of this file.
00001 /*$$FILE$$*/
00002 /*$$VERSION$$*/
00003 /*$$DATE$$*/
00004 /*$$LICENSE$$*/
00005 
00006 
00014 #ifndef CIFSCHEMAMAP_H
00015 #define CIFSCHEMAMAP_H
00016 
00017 
00018 #include <string>
00019 
00020 #include "CifFile.h"
00021 
00022 
00023 typedef struct _attribInfo
00024 {
00025     int iIndex;
00026     int iNull;
00027     int iWidth;
00028     int iPrecision;
00029     unsigned int iMaxWidth; 
00030     string dType;
00031     int iTypeCode;
00032 } ATTRIBINFO;
00033 
00034 
00035 typedef struct
00036 {
00037     string attribName;
00038     string dataType;
00039     string indexFlag;
00040     string nullFlag;
00041     string width;
00042     string precision;
00043     string populated;   // Not taken from the file, but constructed afterwards
00044 } AttrInfo;
00045 
00046 
00061 class SchemaMapping
00062 {
00063 
00064   public:
00065     // TODO - All these constants can be made
00066     // private, except _TYPE_CODE_TEXT that is used outside of this
00067     // class. Try to fix that and move all these constants to private
00068     static const int _TYPE_CODE_INT        = 1;
00069     static const int _TYPE_CODE_FLOAT      = 2;
00070     static const int _TYPE_CODE_STRING     = 3;
00071     static const int _TYPE_CODE_TEXT       = 4;
00072     static const int _TYPE_CODE_DATETIME   = 5;
00073     static const int _TYPE_CODE_BIGINT     = 6;
00074 
00075     static const int _MAX_LINE_LENGTH = 255;
00076 
00104     SchemaMapping(const string& schemaFile = String::Empty,
00105       const string& schemaFileOdb = String::Empty, bool verbose = false);
00106 
00120     virtual ~SchemaMapping();
00121 
00141     void SetReviseSchemaMode(bool mode = true);
00142 
00157     bool GetReviseSchemaMode();
00158 
00174     void ReviseSchemaMap(const string& revisedSchemaFile);
00175 
00176 
00191     void updateSchemaMapDetails(SchemaMapping& revSchMap);
00192 
00209     void CreateTables(CifFile& cifFile, const string& blockName);
00210 
00225     void GetTableNames(vector<string>& tableNames);
00226 
00227     ISTable* CreateTableInfo();
00228     ISTable* CreateColumnInfo();
00229 
00230     void GetAttributeNames(vector<string>& attributes,
00231       const string& tableName);
00232     void GetAttributesInfo(vector<AttrInfo>& attrInfo,
00233       const string& tableName);
00234     void GetMappedAttributesInfo(vector<vector<string> >& mappedAttrInfo,
00235       const string& tableName);
00236 
00237     void GetMappedConditions(vector<vector<string> >& mappedConditions,
00238       const string& tableName);
00239 
00240     void GetTableNameAbbrev(string &abbrev, string target);
00241     void GetAttributeNameAbbrev(string &abbrev, string targetTable,
00242       string targetAttribute);
00243 
00244     void getSchemaMapDetails(const string& tableName,
00245       const string& attribName, string& dataType,
00246       string& indexFlag, string& nullFlag,
00247       string& width, string& precision, string& populated);
00248 
00249     void UpdateAttributeDef(const string& tableName, const string& columnName,
00250       int type, int iWidth, int newWidth);
00251 
00252     void GetAttributeInfo(vector<ATTRIBINFO>& aI, ISTable *tIn);
00253     void GetAllAttributeInfo(vector<ATTRIBINFO>& aI, string tableName);
00254 
00255     static bool AreAttributesMandatory(const vector<string>& row,
00256       const vector<ATTRIBINFO>& aI);
00257     static int CheckIndexAttributes(const vector<string>& row,
00258       const vector<ATTRIBINFO>& aI);
00259 
00260   private:
00261     bool _verbose;
00262 
00263     string _schemaFile;    // Schema definition file name (mmCIF)
00264     string _schemaFileOdb; // Schema definition file name (odb)
00265 
00266     CifFile* _fobjS;
00267 
00268     ISTable* _tableSchema;
00269     ISTable* _attribSchema;
00270     ISTable* _schemaMap;
00271     ISTable* _mapConditions;
00272     ISTable* _tableAbbrev;    
00273     ISTable* _attributeAbbrev;
00274 
00275     bool _reviseSchemaMode;
00276     int  _compatibilityMode;
00277 
00278     void _AssignAttribIndices(void);
00279     void GetGroupNames(vector<string>& groupNames);
00280 
00281     void Clear();
00282 };
00283 
00284 
00294 class Db
00295 {
00296   public:
00297     SchemaMapping& _schemaMapping;
00298 
00299     Db(SchemaMapping& schemaMapping);
00300     virtual ~Db();
00301 
00302     void SetUseOnlyPopulated(bool mode=true);
00303     bool GetUseOnlyPopulated();
00304 
00305     void SetAppendFlag(const bool appendFlag);
00306     bool GetAppendFlag();
00307 
00308     virtual void SetDatabaseName(const string& dbName);
00309     void SetDatabaseUser(const string& dbUser);
00310 
00311     void SetFieldSeparator(const string& fieldSeparator);
00312     void SetRowSeparator(const string& rowSeparator);
00313 
00314     string GetCommandTerm();
00315 
00316     string GetFieldSeparator();
00317     string GetRowSeparator();
00318 
00319     virtual void GetStart(string& start);
00320 
00321     virtual void WriteSchemaStart(ostream& io);
00322     virtual void WriteDeleteTable(ostream& io, const string& fromTable,
00323       const string& tableNameDb);
00324     virtual void WriteThirdDeleteTable(ostream& io, const string& tableNameDb);
00325 
00326     virtual void DropTableSql(ostream& io, const string& tableName);
00327 
00328     virtual const string& GetExec();
00329     virtual const string& GetExecOption();
00330     virtual const string& GetUserOption();
00331     virtual const string& GetPassOption();
00332 
00333     virtual const string& GetEnvDbUser();
00334     virtual const string& GetEnvDbPass();
00335 
00336     virtual const string& GetConnect();
00337     virtual const string& GetTerminate();
00338     virtual const string& GetScriptCommand();
00339 
00340     virtual void WriteLoad(ostream& io);
00341 
00342     const string& GetLoadingFileName();
00343 
00344     virtual void WriteLoadingStart(ostream& io);
00345     virtual void WriteLoadingEnd(ostream& io);
00346     virtual void WriteLoadingTable(ostream& io, const string& tableName,
00347       const string& path);
00348 
00349     virtual void WritePrint(ostream& io, const string& tableNameDb);
00350 
00351     virtual void GetChar(string& dType, const unsigned int width);
00352     virtual void GetFloat(string& dType);
00353     virtual void GetText(string& dType, const unsigned int width);
00354     virtual void GetDate(string& dType);
00355     virtual void WriteNull(ostream& io, const int iNull,
00356       const unsigned int curr, const unsigned int attSize);
00357     virtual void WriteTableIndex(ostream& io, const string& tableNameDb,
00358       const vector<string>& indexList);
00359 
00360     const string& GetBcpStringDelimiter();
00361     virtual void WriteBcpDoubleQuotes(ostream& io);
00362 
00363     virtual void WriteNewLine(ostream& io, bool special = false);
00364 
00365     bool IsFirstTextNewLineSpecial();
00366 
00367   protected:
00368     bool _useOnlyPopulated;
00369 
00370     bool _appendFlag;
00371 
00372     // Field and row separators for compact output (eg. BCP)
00373     string _fieldSeparator; 
00374     string _rowSeparator;   
00375 
00376     string _cmdTerm;            // SQL command terminator.
00377 
00378     // Used in assigning permissions when exporting schema in SQL. 
00379     string _dbName; // Target database name 
00380     string _dbUser; // Valid user of target database.  
00381 
00382     string _exec;
00383     string _execOption;
00384     string _userOption;
00385     string _passOption;
00386 
00387     string _connect;
00388     string _terminate;
00389     string _scriptCommand;
00390     string _envDbUser;
00391     string _envDbPass;
00392 
00393     string _loadFileName;
00394 
00395     string _bcpStringDelimiter;
00396 
00397     bool _firstTextNewLineSpecial;
00398 
00399   private:
00400     static const string _LOAD_COMMAND_FILE;
00401 
00402 };
00403 
00404 
00412 class DbOracle : public Db
00413 {
00414 
00415   // Oracle 8.1.6 SQLLDR
00416 
00417   public:
00418     DbOracle(SchemaMapping& schemaMapping);
00419     ~DbOracle();
00420 
00421     void WriteSchemaStart(ostream& io);
00422 
00423     void WriteLoadingStart(ostream& io);
00424     void WriteLoadingTable(ostream& io, const string& tableName,
00425       const string& path);
00426 
00427     void GetDate(string& dType);
00428     void GetText(string& dType, const unsigned int width);
00429 
00430     void WriteNull(ostream& io, const int iNull,
00431       const unsigned int curr, const unsigned int attSize);
00432     void WriteTableIndex(ostream& io, const string& tableNameDb,
00433       const vector<string>& indexList);
00434 
00435     void WriteNewLine(ostream& io, bool special = false);
00436 };
00437 
00438 
00446 class DbDb2 : public Db
00447 {
00448 
00449   public:
00450     DbDb2(SchemaMapping& schemaMapping);
00451     ~DbDb2();
00452 
00453 
00454     void GetStart(string& start);
00455 
00456     void WriteSchemaStart(ostream& io);
00457     void WriteDeleteTable(ostream& io, const string& fromTable,
00458       const string& tableNameDb);
00459     void WriteThirdDeleteTable(ostream& io, const string& tableNameDb);
00460 
00461     void SetDatabaseName(const string& dbName);
00462 
00463     void WriteLoadingStart(ostream& io);
00464     void WriteLoadingEnd(ostream& io);
00465     void WriteLoadingTable(ostream& io, const string& tableName,
00466       const string& path);
00467 
00468     void GetFloat(string& dType);
00469     void GetDate(string& dType);
00470     void GetText(string& dType, const unsigned int width);
00471     void WriteNull(ostream& io, const int iNull,
00472       const unsigned int curr, const unsigned int attSize);
00473 
00474     void WriteTableIndex(ostream& io, const string& tableNameDb,
00475       const vector<string>& indexList);
00476 
00477     void WriteBcpDoubleQuotes(ostream& io);
00478 };
00479 
00480 
00488 class DbMySql : public Db
00489 {
00490 
00491   public:
00492     DbMySql(SchemaMapping& schemaMapping);
00493     ~DbMySql();
00494 
00495     void GetStart(string& start);
00496 
00497     void DropTableSql(ostream& io, const string& tableName);
00498 
00499     void WriteLoad(ostream& io);
00500     void WriteLoadingStart(ostream& io);
00501     void WriteLoadingTable(ostream& io, const string& tableName,
00502       const string& path);
00503 
00504     void WriteTableIndex(ostream& io, const string& tableNameDb,
00505       const vector<string>& indexList);
00506 
00507     void WriteNull(ostream& io, const int iNull,
00508       const unsigned int curr, const unsigned int attSize);
00509 
00510     void WriteNewLine(ostream& io, bool special = false);
00511 
00512   private:
00513     static const string _SQL_FILE_LOAD;
00514 
00515 };
00516 
00517 
00525 class DbSybase : public Db
00526 {
00527 
00528   public:
00529     DbSybase(SchemaMapping& schemaMapping);
00530     ~DbSybase();
00531 
00532     void WriteLoadingStart(ostream& io);
00533     void WriteLoadingTable(ostream& io, const string& tableName,
00534       const string& path);
00535 
00536     void WritePrint(ostream& io, const string& tableNameDb);
00537     void WriteNull(ostream& io, const int iNull,
00538       const unsigned int curr, const unsigned int attSize);
00539 
00540     void WriteTableIndex(ostream& io, const string& tableNameDb,
00541       const vector<string>& indexList);
00542 };
00543 
00544 
00554 class DbOutput
00555 {
00556 
00557   public:
00558     Db& _db;
00559 
00560     DbOutput(Db& db);
00561     virtual ~DbOutput();
00562 
00563     virtual void WriteSchema(const string& path = String::Empty);
00564     virtual void WriteData(Block& block, const string& path = String::Empty);
00565     virtual void WriteDataLoadingScripts(const string& path = String::Empty);
00566 
00567     void SetInputFile(const string& inpFile);
00568 
00569     virtual void _WriteTable(ostream& io, ISTable* tIn, bool noRevise = false);
00570 
00571     const string& GetCommandScriptName();
00572 
00573   protected:
00574     static const string _COMMAND_FILE;
00575 
00576     string _INPUT_FILE;
00577 
00578     string _stringDelimiter;
00579     vector<char> _specialChars;
00580 
00581     string _dateDelimiter;
00582     vector<char> _specialDateChars;
00583 
00584     string _itemSeparator; 
00585     string _rowSeparator; 
00586 
00587     void WriteDbExec(ostream& io, const string& fileName,
00588       const unsigned int indentLevel = 0);
00589     void WriteDbExecOnly(ostream& io, const string& fileName,
00590       const unsigned int indentLevel = 1);
00591 
00592     void WriteHeader(ostream& io);
00593 
00594     void _FormatNumericData(ostream &io, const string &cs);
00595     void _FormatStringData(ostream &io, const string &cs,
00596       unsigned int maxWidth);
00597     void _FormatTextData(ostream &io, const string &cs);
00598     void _FormatDateData(ostream &io, const string &cs, unsigned int maxWidth);
00599 
00600     void _FormatData(ostream &io, const string &cs,
00601       ATTRIBINFO& attribInfo, const bool noRevise);
00602 
00603     bool IsSpecialChar(const char& character);
00604     bool IsSpecialDateChar(const char& character);
00605 
00606     virtual void WriteEmptyNumeric(ostream& io);
00607     virtual void WriteEmptyString(ostream& io);
00608     virtual void WriteSpecialChar(ostream& io, const char& specChar);
00609 
00610     virtual void WriteEmptyDate(ostream& io);
00611     virtual void WriteSpecialDateChar(ostream& io, const char& specDateChar);
00612 
00613     void _DeleteRows(ostream& io, ISTable* tIn);
00614 
00615     virtual bool IsFirstTextNewLineSpecial();
00616     virtual void WriteNewLine(ostream& io, bool special = false);
00617 
00618     virtual void GetTableStart(string& tableStart, const string& tableName);
00619     virtual void GetTableEnd(string& tableEnd);
00620     const string& GetItemSeparator();
00621     const string& GetRowSeparator();
00622 
00623   private:
00624     static void _FormatStringDataSql(ostream &io, const string &cs,
00625       unsigned int maxWidth);
00626 
00627 };
00628 
00629 
00638 class BcpOutput : public DbOutput
00639 {
00640   public:
00641     BcpOutput(Db& db);
00642     virtual ~BcpOutput();
00643 
00644     void WriteData(Block& block, const string& path = String::Empty);
00645     void WriteDataLoadingScripts(const string& path = String::Empty);
00646 
00647   private:
00648     static const string _SQL_FILE_DELETE;
00649 
00650     void WriteCommandScript(const string& path);
00651     void WriteLoadingFiles(const string& path = String::Empty);
00652 
00653     void WriteDelete(ostream& io);
00654 
00655     void WriteEmptyString(ostream& io);
00656 
00657     void WriteSpecialDateChar(ostream& io, const char& specDateChar);
00658 };
00659  
00660 
00669 class SqlOutput : public DbOutput
00670 {
00671 
00672   public:
00673     SqlOutput(Db& db);
00674     virtual ~SqlOutput();
00675 
00676     void WriteSchema(const string& path = String::Empty);
00677     void WriteData(Block& block, const string& path = String::Empty);
00678     void WriteDataLoadingScripts(const string& path = String::Empty);
00679 
00680   protected:
00681     void WriteEmptyNumeric(ostream& io);
00682     bool IsFirstTextNewLineSpecial();
00683     void WriteNewLine(ostream& io, bool special = false);
00684     void GetTableStart(string& tableStart, const string& tableName);
00685     void GetTableEnd(string& tableEnd);
00686 
00687   private:
00688     static const unsigned int _MAX_SQL_NAME_LENGTH = 60;
00689     static const string _SCHEMA_COMMAND_FILE;
00690     static const string _SCHEMA_SQL_DROP_FILE;
00691     static const string _SCHEMA_SQL_FILE;
00692     static const string _SQL_FILE;
00693 
00694     void WriteSqlScriptSchemaInfo(ostream& io);
00695     void WriteCommandScript(const string& path);
00696 
00697     void CreateTableSql(ostream& io, const string& tableName);
00698 
00699     void WriteAuxTables(ostream& io, ISTable* infoP,
00700       const vector<string>& tableNames);
00701 
00702     void WriteEmptyString(ostream& io);
00703     void WriteEmptyDate(ostream& io);
00704 };
00705 
00706 
00715 class XmlOutput : public DbOutput
00716 {
00717   public:
00718     XmlOutput(Db& db);
00719     virtual ~XmlOutput();
00720 
00721     void WriteSchema(const string& path = String::Empty);
00722     void WriteData(Block& block, const string& path = String::Empty);
00723 
00724   private:
00725     static const int _MAX_NAME_LENGTH = 80;
00726     static const string _SCHEMA_XML_FILE;
00727     string _NS_DEFAULT;
00728 
00729     void _CreateComplexTypeXML(ostream &io, const string& tableName);
00730     void _SetNamespaceDefaultXML(const string& name); 
00731     void _GetDefaultNamespaceXML(ostream &io); 
00732     void _WriteTable(ostream& io, ISTable* tIn, bool noRevise = false);
00733 
00734     void _GetAttributeNameAbbrevXML(string &abbrev,
00735       const string& targetAttribute);
00736     void _QualifyNameXML(ostream &io,  const string& name); 
00737 
00738     bool _isElementNullXML(const string& val);
00739 
00740     void _GetTableNameAbbrevXML(string &abbrev, string target);
00741 
00742     void WriteSpecialChar(ostream& io, const char& specChar);
00743 };
00744 
00745 
00761 class DbLoader
00762 {
00763   public:
00764     enum eConvOpt
00765     {
00766         // Generate only data loading files
00767         eDATA_ONLY = 0,
00768 
00769         // Generate both data loading files and loading shell scripts
00770         eDATA_WITH_SCRIPTS,
00771 
00772         // Generate only loading shell scripts
00773         eSCRIPTS_ONLY
00774     };
00775 
00796     DbLoader(SchemaMapping& schemaMapping, DbOutput& dbOutput,
00797       bool verbose = false, const string& workDir = String::Empty);
00798 
00812     virtual ~DbLoader();
00813 
00828     void SetWorkDir(const string& workDir);
00829 
00848     void AsciiFileToDb(const string& asciiFile, const eConvOpt convOpt);
00849 
00869     void SerFileToDb(const string& serFile, const eConvOpt convOpt);
00870 
00889     void FileObjToDb(CifFile& cifFile, const eConvOpt convOpt);
00890 
00894     static unsigned int GetTableColumnIndex(const ISTable& isTable,
00895       const string& colName);
00896 
00897 #ifdef DB_HASH_ID
00898     void SetHashMode(int mode);
00899 #endif
00900 
00901   private:
00902     static const string _LOG_FILE;
00903 
00904     string _workDir; // Working directory for all generated files.
00905     string _INPUT_FILE;
00906 
00907 #ifdef DB_HASH_ID
00908     string _HASH_ID;
00909     int _hashMode;
00910 #endif
00911 
00912     // Block name of loadable data in mmCIF format
00913     string _blockName;
00914 
00915     bool _verbose;
00916 
00917     ofstream _log;
00918 
00919     SchemaMapping& _schemaMapping;
00920     DbOutput& _dbOutput;
00921 
00922     void _LoadBlock(Block& rBlock, Block& wBlock);
00923     bool _Search(vector<vector<string> >& dMap, const unsigned int iAttr,
00924       ISTable& isTable, const string& blockName,
00925       const vector<string>& cNameMap, const string& sItem,
00926       const string& sCnd, const string& sFnct);
00927  
00928     void _DoFunc(vector<string>& s, const vector<string>& r,
00929       const string& sFnct);
00930 
00931     void _OpenLog(const string& logName);
00932 
00933     int _GetMapColumnIndex(const vector<string>& cNameMap, const string& vOf);
00934     void _GetMapColumnValue(string& p, vector<vector<string> >& dMap,
00935       int iCol, int irow);
00936     int _GetMapColumnLength(vector<vector<string> >& dMap,   int iCol);
00937 
00938     void _dformat_1(const char *date, char *odate);
00939     void _dformat_2(const char *date, char *odate);
00940     void _dformat_3(const char *date, char *odate, int shortFlag);
00941     void _dformat_4(const char *date, char *odate);
00942     void _dformat_5(const char *date, char *odate);
00943 
00944     void _ReorderName(string& res, char *string, int mode);
00945     void _ToUpperString(string& aString);
00946     void _StripString(string& aString, int mode);
00947 
00948     int _CheckNullRow(const vector<string>& row, const vector<ATTRIBINFO>& aI);
00949 
00950 #ifdef DB_HASH_ID
00951     long long pdbIdHash(const string& id);
00952 #endif
00953 
00954     static void CleanString(string& aString);
00955 
00956     void Clear();
00957 };
00958 
00959 
00960 #endif

Generated on Wed Oct 31 11:05:34 2007 for db-loader-v4.0 by  doxygen 1.5.1