COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libcoopy_core/include/coopy/SheetSchema.h
Go to the documentation of this file.
00001 #ifndef COOPY_SHEETSCHEMA
00002 #define COOPY_SHEETSCHEMA
00003 
00004 #include <coopy/ColumnInfo.h>
00005 #include <coopy/ColumnRef.h>
00006 #include <coopy/RefCount.h>
00007 #include <coopy/Dbg.h>
00008 
00009 #include <vector>
00010 #include <string>
00011 
00012 namespace coopy {
00013   namespace store {
00014     class SheetSchema;
00015     class SimpleSheetSchema;
00016   }
00017 }
00018 
00019 class coopy::store::SheetSchema : public RefCount {
00020 public:
00021   virtual ~SheetSchema() {
00022   }
00023 
00024   virtual ColumnInfo getColumnInfo(int x) const {
00025     return ColumnInfo();
00026   }
00027 
00028   virtual int headerHeight() const {
00029     return 0;
00030   }
00031 
00032   virtual int getColumnCount() const {
00033     return -1;
00034   }
00035 
00036   virtual int getColumnIndexByName(const char *name) const {
00037     for (int i=0; i<getColumnCount(); i++) {
00038       ColumnInfo info = getColumnInfo(i);
00039       if (info.getName()==name) {
00040         return i;
00041       }
00042     }
00043     return -1;
00044   }
00045 
00046   virtual bool providesPrimaryKeys() const {
00047     return false;
00048   }
00049 
00050   virtual std::string getSheetName() const {
00051     return "";
00052   }
00053 
00054   virtual bool hasSheetName() const {
00055     return true;
00056   }
00057 
00058   virtual SheetSchema *clone() const;
00059 
00060   virtual bool isGuess() const {
00061     return false;
00062   }
00063 
00064   virtual bool addedHeader() {
00065     fprintf(stderr, "Don't know what to do with header\n");
00066     COOPY_ASSERT(1==0);
00067     return false;
00068   }
00069 
00070   virtual bool deleteColumn(const ColumnRef& column) {
00071     return false;
00072   }
00073 
00074   virtual ColumnRef insertColumn(const ColumnRef& column, 
00075                                  const ColumnInfo& info) {
00076     fprintf(stderr, "Don't know how to insert column in schema\n");
00077     return ColumnRef();
00078   }
00079 
00080   virtual ColumnRef moveColumn(const ColumnRef& src, 
00081                                const ColumnRef& base) {
00082     fprintf(stderr, "Don't know how to insert column in schema\n");
00083     return ColumnRef();
00084   }
00085 
00086   virtual bool modifyColumn(const ColumnRef& column, 
00087                             const ColumnInfo& info) {
00088     fprintf(stderr, "Don't know how to modify column in schema\n");
00089     return false;
00090   }
00091 
00092   virtual bool setHeaderHeight(int hh) {
00093     return false;
00094   }
00095 
00096   virtual std::string toString() const {
00097     return "unknown-schema";
00098   }
00099 
00100   virtual bool isShadow() const {
00101     return false;
00102   }
00103 
00104   virtual bool copy(const SheetSchema& ss) {
00105     return false;
00106   }
00107 
00108 };
00109 
00110 class coopy::store::SimpleSheetSchema : public SheetSchema {
00111 private:
00112   std::string sheetName;
00113   std::vector<std::string> columns;
00114   std::vector<ColumnType> kinds;
00115   int hh;
00116   bool guessed;
00117   bool hasName;
00118 public:
00119   SimpleSheetSchema() {
00120     hh = 0;
00121     guessed = false;
00122     hasName = true;
00123   }
00124 
00125   virtual bool copy(const SheetSchema& ss) {
00126     sheetName = ss.getSheetName();
00127     hh = ss.headerHeight();
00128     int ct = ss.getColumnCount();
00129     columns.clear();
00130     kinds.clear();
00131     for (int i=0; i<ct; i++) {
00132       ColumnInfo c = ss.getColumnInfo(i);
00133       columns.push_back(c.getName());
00134       kinds.push_back(c.getColumnType());
00135     }
00136     guessed = ss.isGuess();
00137     return true;
00138   }
00139 
00140   virtual bool addedHeader() {
00141     hh++;
00142     return true;
00143   }  
00144 
00145   virtual bool setHeaderHeight(int hh) {
00146     this->hh = hh;
00147     return true;
00148   }
00149 
00150   virtual int headerHeight() const {
00151     return hh;
00152   }
00153 
00154   void setSheetName(const char *name) {
00155     sheetName = name;
00156   }
00157 
00158   bool addColumn(const char *name) {
00159     columns.push_back(name);
00160     kinds.push_back(ColumnType());
00161     return true;
00162   }
00163 
00164   bool addColumn(const char *name, const ColumnType& kind) {
00165     columns.push_back(name);
00166     kinds.push_back(kind);
00167     return true;
00168   }
00169 
00170   virtual ColumnInfo getColumnInfo(int x) const {
00171     return ColumnInfo(columns[x],kinds[x]);
00172   }
00173 
00174   virtual ColumnType& modifyType(int x) {
00175     return kinds[x];
00176   }
00177 
00178   virtual int getColumnCount() const {
00179     return columns.size();
00180   }
00181 
00182   virtual bool providesPrimaryKeys() const {
00183     return false;
00184   }
00185 
00186   virtual std::string getSheetName() const {
00187     return sheetName;
00188   }
00189 
00190   virtual bool isGuess() const {
00191     return guessed;
00192   }
00193 
00194   void setGuess(bool flag) {
00195     guessed = flag;
00196   }
00197 
00198   void setHasSheetName(bool flag) {
00199     hasName = flag;
00200   }
00201 
00202   virtual bool hasSheetName() const {
00203     return hasName;
00204   }
00205 
00206   virtual bool deleteColumn(const ColumnRef& column) {
00207     columns.erase(columns.begin()+column.getIndex());
00208     kinds.erase(kinds.begin()+column.getIndex());
00209     return true;
00210   }
00211 
00212   virtual ColumnRef moveColumn(const ColumnRef& src, 
00213                                const ColumnRef& base) {
00214     int w = columns.size();
00215     int offset = base.getIndex();
00216     if (offset>=(int)w) return ColumnRef();
00217     int offset_src = src.getIndex();
00218     int offset_del = offset_src;
00219     if (offset_del<0||offset_del>=w) return ColumnRef();
00220     int final = offset;
00221     if (offset<=offset_del&&offset!=-1) {
00222       offset_del++;
00223     } else {
00224       final--;
00225     }
00226     if (offset>=0) {
00227       columns.insert(columns.begin()+offset,columns[offset_src]);
00228       kinds.insert(kinds.begin()+offset,kinds[offset_src]);
00229     } else {
00230       columns.push_back(columns[offset_src]);
00231       kinds.push_back(kinds[offset_src]);
00232     }
00233     columns.erase(columns.begin()+offset_del);
00234     kinds.erase(kinds.begin()+offset_del);
00235 
00236     if (offset<0) {
00237       return ColumnRef(w-1);
00238     }
00239     return ColumnRef(final);
00240   }
00241 
00242   virtual ColumnRef insertColumn(const ColumnRef& column, 
00243                                  const ColumnInfo& info) {
00244     int index = column.getIndex();
00245     if (index==-1) {
00246       bool ok = addColumn(info.getName().c_str());
00247       if (!ok) return ColumnRef();
00248       return ColumnRef(getColumnCount()-1);
00249     }
00250     columns.insert(columns.begin()+column.getIndex(),info.getName().c_str());
00251     kinds.insert(kinds.begin()+column.getIndex(),info.getColumnType());
00252     return column;
00253   }
00254 
00255   virtual bool modifyColumn(const ColumnRef& column, 
00256                             const ColumnInfo& info) {
00257     
00258     columns[column.getIndex()] = info.getName();
00259     return true;
00260   }
00261 
00262   virtual std::string toString() const;
00263 };
00264 
00265 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines