COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libsqlite/include/coopy/SqliteTextBook.h
Go to the documentation of this file.
00001 #ifndef COOPY_SQLITETEXTBOOK
00002 #define COOPY_SQLITETEXTBOOK
00003 
00004 #include <coopy/TextBook.h>
00005 #include <coopy/TextBookFactory.h>
00006 #include <coopy/Sha1Generator.h>
00007 
00008 namespace coopy {
00009   namespace store {
00010 
00016     namespace sqlite {
00017       class SqliteTextBook;
00018       class SqliteTextBookFactory;
00019     }
00020   }
00021 }
00022 
00023 class coopy::store::sqlite::SqliteTextBook : public TextBook {
00024 public:
00025   SqliteTextBook(bool textual = false);
00026   virtual ~SqliteTextBook();
00027 
00028   void clear();
00029 
00030   virtual bool read(const char *fname, bool can_create,
00031                     const Property& config,
00032                     SqliteTextBook *base = 0/*NULL*/);
00033 
00034   virtual bool save(const char *fname, const char *format, 
00035                     bool itextual = false);
00036 
00037   virtual bool open(const Property& config, SqliteTextBook *base = 0/*NULL*/);
00038   
00039   std::vector<std::string> getNames() {
00040     return names;
00041   }
00042 
00043   PolySheet readSheet(const std::string& name);
00044 
00045   virtual bool inplace() const {
00046     return (!textual)&&(!memory);
00047   }
00048 
00049   virtual bool addSheet(const SheetSchema& schema);
00050 
00051 
00052   virtual bool writtenToFuture() const {
00053     return prewrite;
00054   }
00055 
00056   virtual std::string desc() const {
00057     return "SqliteBook";
00058   }
00059 
00060 private:
00061   void *implementation;
00062   Poly<RefCount> implementation_count;
00063   bool textual;
00064   bool memory;
00065   bool prewrite;
00066   bool postwrite;
00067   std::string hold_temp;
00068   std::string prefix;
00069   std::string prefix_dot;
00070   coopy::store::Sha1Generator hasher;
00071   
00072   std::vector<std::string> names;
00073 
00074   std::vector<std::string> getNamesSql();
00075 
00076 };
00077 
00078 
00079 class coopy::store::sqlite::SqliteTextBookFactory : public TextBookFactory {
00080 private:
00081   bool textual;
00082 public:
00083  SqliteTextBookFactory(bool textual=false) : textual(textual) {
00084   }
00085 
00086   virtual std::string getName() {
00087     return textual?"sqlitext":"sqlite";
00088   }
00089 
00090 
00091   virtual TextBook *open(AttachConfig& config, AttachReport& report) {
00092     if (!textual) return openNormal(config,report);
00093     return openTextual(config,report);
00094   }
00095         
00096   TextBook *openNormal(AttachConfig& config, AttachReport& report) {
00097     //printf("Hello, opening regular factory for: %s\n",
00098     //config.options.toString().c_str());
00099     SqliteTextBook *book = new SqliteTextBook(textual);
00100     if (book==NULL) return NULL;
00101 
00102     SqliteTextBook *base = NULL;
00103     if (config.baseBook!=NULL) {
00104       base = dynamic_cast<SqliteTextBook *>(&(config.baseBook->tail()));
00105     }
00106 
00107     if (!book->open(config.options,base)) {
00108       delete book;
00109       book = NULL;
00110     }
00111     if (config.shouldWrite) {
00112       if (config.prevBook!=NULL) {
00113         if (!config.prevBook->writtenToFuture()) {
00114           if (!book->copy(*config.prevBook,config.options)) {
00115             delete book;
00116             book = NULL;
00117             report.msg = "data transfer failed";
00118             return NULL;
00119           }
00120           if (!book->inplace()) {
00121             book->save("-",NULL,textual);
00122           }
00123         }
00124       }
00125     }
00126     if (book!=NULL) {
00127       report.success = true;
00128     }
00129     return book;
00130   }
00131 
00132   TextBook *openTextual(AttachConfig& config, AttachReport& report) {
00133     std::string ext = config.ext;
00134     report.applicable = true;
00135     if (config.shouldCreate) {
00136       report.errorCreateNotImplemented("sqlitext");
00137       return NULL;
00138     }
00139     if (config.shouldWrite && !config.shouldRead) {
00140       if (config.prevBook!=NULL) {
00141         SqliteTextBook *prev = dynamic_cast<SqliteTextBook *>(config.prevBook);
00142         if (prev!=NULL) {
00143           bool ok = prev->save(config.fname.c_str(),NULL,textual);
00144           if (!ok) {
00145             report.success = false;
00146             report.msg = "failed to save file";
00147             return NULL;
00148           }
00149           report.success = true;
00150           return prev;
00151         }
00152       }
00153     }
00154     SqliteTextBook *book = new SqliteTextBook(true);
00155     if (book==NULL) return book;
00156     bool ok = false;
00157     if (book->read(config.fname.c_str(),true,config.options)) {
00158       ok = true;
00159     }
00160     if (!ok) {
00161       delete book;
00162       book = NULL;
00163     } else {
00164       if (config.prevBook!=NULL) {
00165         book->copy(*config.prevBook,config.options);
00166         if (config.shouldWrite) {
00167           book->save(config.fname.c_str(),NULL,textual);
00168         }
00169       }
00170     }
00171     if (book!=NULL) {
00172       report.success = true;
00173     }
00174     return book;
00175   }
00176 };
00177 
00178 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines