COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libcoopy_sqldiff/include/coopy/DbiSqlWrapper.h
Go to the documentation of this file.
00001 #ifndef COOPY_DBISQLWRAPPER_INC
00002 #define COOPY_DBISQLWRAPPER_INC
00003 
00004 namespace coopy {
00005   namespace store {
00006     class SqlTableName;
00007     class SqlTable;
00008     class DbiSqlWrapper;
00009   }
00010 }
00011 
00012 #include <string>
00013 #include <vector>
00014 
00015 #include <coopy/ColumnInfo.h>
00016 #include <coopy/SheetCell.h>
00017 
00018 class coopy::store::SqlTableName {
00019 public:
00020   std::string prefix;
00021   std::string name;
00022 };
00023 
00024 
00025 class coopy::store::DbiSqlWrapper {
00026 public:
00027   virtual ~DbiSqlWrapper() {}
00028 
00029   virtual std::vector<ColumnInfo> getColumns(const SqlTableName& name) = 0;
00030   virtual std::string getQuotedTableName(const SqlTableName& name) = 0;
00031   virtual std::string getQuotedColumnName(const std::string& name) = 0;
00032 
00033   virtual bool begin(const std::string& query) = 0;
00034   virtual bool read() = 0;
00035   virtual SheetCell get(int index) = 0;
00036   virtual bool end() = 0;
00037   virtual int width() = 0;
00038 };
00039 
00040 class coopy::store::SqlTable {
00041 private:
00042   DbiSqlWrapper *db;
00043   std::vector<ColumnInfo> columns;
00044   SqlTableName name;
00045   std::string quotedTableName;
00046 
00047   void getColumns() {
00048     if (columns.size()>0) return;
00049     if (!db) return;
00050     columns = db->getColumns(name);
00051   }
00052 
00053 public:
00054   SqlTable(DbiSqlWrapper *db, const SqlTableName& name) {
00055     this->db = db;
00056     this->name = name;
00057   }
00058 
00059   std::vector<std::string> getPrimaryKey() {
00060     getColumns();
00061     std::vector<std::string> result;
00062     for (int i=0; i<(int)columns.size(); i++) {
00063       ColumnInfo& col = columns[i];
00064       if (!col.isPrimaryKey()) continue;
00065       result.push_back(col.getName());
00066     }
00067     return result;
00068   }
00069 
00070   std::vector<std::string> getAllButPrimaryKey() {
00071     getColumns();
00072     std::vector<std::string> result;
00073     for (int i=0; i<(int)columns.size(); i++) {
00074       ColumnInfo& col = columns[i];
00075       if (col.isPrimaryKey()) continue;
00076       result.push_back(col.getName());
00077     }
00078     return result;
00079   }
00080 
00081   std::vector<std::string> getColumnNames() {
00082     getColumns();
00083     std::vector<std::string> result;
00084     for (int i=0; i<(int)columns.size(); i++) {
00085       ColumnInfo& col = columns[i];
00086       result.push_back(col.getName());
00087     }
00088     return result;
00089   }
00090 
00091 
00092   std::string getQuotedTableName() {
00093     if (quotedTableName!="") return quotedTableName;
00094     quotedTableName = db->getQuotedTableName(name);
00095     return quotedTableName;
00096   }
00097 
00098 
00099   std::string getQuotedColumnName(const std::string& name) {
00100     return db->getQuotedColumnName(name);
00101   }
00102 
00103 };
00104 
00105 
00106 
00107 
00108 #endif
00109 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines