COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libsqlite/include/coopy/SqliteSheet.h
Go to the documentation of this file.
00001 #ifndef COOPY_SQLITESHEET
00002 #define COOPY_SQLITESHEET
00003 
00004 #include <coopy/DataSheet.h>
00005 #include <coopy/SparseSheet.h>
00006 #include <coopy/EfficientMap.h>
00007 #include <coopy/ColumnInfo.h>
00008 #include <coopy/Compare.h>
00009 
00010 #include <vector>
00011 
00012 namespace coopy {
00013   namespace store {
00014     namespace sqlite {
00015       class SqliteSheet;
00016       class SqliteSheetSchema;
00017     }
00018   }
00019 }
00020 
00021 class coopy::store::sqlite::SqliteSheet : public DataSheet, 
00022   public coopy::cmp::Compare  {
00023 public:
00024   SqliteSheet(void *db, const char *name, const char *prefix);
00025 
00026   bool connect();
00027   bool create(const SheetSchema& schema);
00028 
00029   virtual ~SqliteSheet();
00030 
00031   virtual int width() const  { return w; }
00032   virtual int height() const { return h; }
00033 
00034   virtual std::string cellString(int x, int y) const;
00035 
00036   virtual std::string cellString(int x, int y, bool& escaped) const;
00037 
00038   virtual bool cellString(int x, int y, const std::string& str) {
00039     return cellString(x,y,str,false);
00040   }
00041 
00042   virtual bool cellString(int x, int y, const std::string& str, bool escaped);
00043 
00044   virtual ColumnRef moveColumn(const ColumnRef& src, const ColumnRef& base);
00045 
00046   virtual bool deleteColumn(const ColumnRef& column);
00047 
00048   virtual ColumnRef insertColumn(const ColumnRef& base);
00049 
00050   virtual ColumnRef insertColumn(const ColumnRef& base, const ColumnInfo& kind);
00051 
00052   virtual bool modifyColumn(const ColumnRef& base, const ColumnInfo& kind);
00053 
00054   virtual RowRef insertRow(const RowRef& base);
00055 
00056   virtual bool deleteRow(const RowRef& src);
00057 
00058   virtual ColumnInfo getColumnInfo(int x) {
00059     /*
00060     ColumnType t;
00061     t.primaryKey = col2pk[x];
00062     t.primaryKeySet = true;
00063     //printf(">> %d %s\n", x, col2sql[x].c_str());
00064     return ColumnInfo(col2sql[x],t);
00065     */
00066     return col2sql[x];
00067   }
00068 
00069   virtual int getColumnCount() const {
00070     return (int)col2sql.size();
00071   }
00072 
00073   virtual SheetSchema *getSchema() const;
00074 
00075   virtual bool applyRowCache(const RowCache& cache, int row, SheetCell *result);
00076 
00077   virtual bool deleteData(int offset);
00078 
00079   virtual bool hasExternalColumnNames() const {
00080     return true;
00081   }
00082 
00083   std::string getName() const {
00084     return name;
00085   }
00086 
00087   virtual bool clearCache() { 
00088     cache.clear();
00089     cacheFlag.clear();
00090     return true;
00091   }
00092 
00093  virtual bool isSequential() const {
00094    return false;
00095  }
00096 
00097  virtual void *getDatabase() const {
00098    return implementation;
00099  }
00100 
00101   virtual std::string getDescription() const {
00102     return "sqlite";
00103   }
00104 
00105  virtual coopy::cmp::Compare *getComparisonMethod() {
00106    return this;
00107  }
00108 
00109  virtual int compare(coopy::store::DataSheet& pivot, 
00110                      coopy::store::DataSheet& local, 
00111                      coopy::store::DataSheet& remote, 
00112                      coopy::cmp::Patcher& output, 
00113                      const coopy::cmp::CompareFlags& flags);
00114 
00115   virtual std::string getRawHash() const;
00116 
00117 private:
00118   SqliteSheetSchema *schema;
00119   void *implementation;
00120   std::string name;
00121   std::string quoted_name;
00122   std::string prefix;
00123   std::string prefix_dot;
00124   int w, h;
00125   bool pending_load;
00126   std::vector<int> row2sql;
00127   std::vector<ColumnInfo> col2sql;
00128 
00129   //std::vector<std::string> col2sql;
00130   std::vector<std::string> primaryKeys;
00131   //std::vector<bool> col2pk;
00132   SparseStringSheet cache;
00133   SparseByteSheet cacheFlag;
00134   void checkPrimaryKeys();
00135   void checkForeignKeys();
00136 
00137   void check();
00138 
00139 public:
00140   static bool isReserved(const std::string& name);
00141   static std::string _quoted(const std::string& x, char ch, bool force);
00142   static std::string _quoted_double(const std::string& x);
00143   static std::string _quoted_single(const std::string& x);
00144 };
00145 
00146 class coopy::store::sqlite::SqliteSheetSchema : public SheetSchema {
00147 public:
00148   SqliteSheet *sheet;
00149 
00150   virtual std::string getSheetName() const {
00151     return sheet->getName();
00152   }
00153 
00154   virtual ColumnInfo getColumnInfo(int x) const {
00155     return sheet->getColumnInfo(x);
00156   }
00157 
00158   virtual int getColumnCount() const {
00159     return sheet->getColumnCount();
00160   }
00161 
00162   virtual bool providesPrimaryKeys() const {
00163     return true;
00164   }
00165 
00166   virtual bool isShadow() const {
00167     return true;
00168   }
00169 };
00170 
00171 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines