COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libjackcess/JackSheet.cpp
Go to the documentation of this file.
00001 #include <coopy/Dbg.h>
00002 #include <coopy/JackSheet.h>
00003 
00004 #include <gcj/cni.h>
00005 #include <JackBox.h>
00006 
00007 using namespace coopy::store;
00008 using namespace coopy::store::mdb;
00009 using namespace std;
00010 using namespace java::lang;
00011 using namespace java::util;
00012 using namespace com::healthmarketscience::jackcess;
00013 
00014 #define Savepoint Cursor$Savepoint 
00015 
00016 #define DB(x) ((Database *)(x))
00017 #define TABLE(x) ((Table *)(x))
00018 #define BOX(x) ((JackBox *)(x))
00019 #define CURSOR(x) ((Cursor *)(x))
00020 
00021 #define JS(x) JvNewStringLatin1(x)
00022 
00023 JackSheet::JackSheet(void *db1, void *box,
00024                      const char *name, const Property& config) {
00025   this->box_implementation = box;
00026   implementation = db1;
00027   table_implementation = NULL;
00028   cursor_implementation = NULL;
00029   this->name = name;
00030   this->config = config;
00031   w = h = 0;
00032 
00033   schema = new JackSheetSchema;
00034   COOPY_MEMORY(schema);
00035   schema->sheet = this;
00036 }
00037 
00038 bool JackSheet::connect() {
00039 
00040   Database *mdb = DB(implementation);
00041   if (!mdb) return false;
00042 
00043   JackBox *box = BOX(box_implementation);
00044   if (!box) return false;
00045 
00046   Table *tbl = box->getTable(mdb,JS(name.c_str()));
00047   if (!tbl) return false;
00048 
00049   table_implementation = tbl;
00050 
00052   // Check column names
00053 
00054   List *cols = box->getColumns(tbl);
00055   if (!cols) return false;
00056 
00057   w = box->getCount(cols);
00058 
00059   for (int j=0; j<w; j++) {
00060     String *str = box->getElement(cols,j);
00061     if (!str) return false;
00062     int slen = JvGetStringUTFLength(str);
00063     string str2(slen,'\0');
00064     JvGetStringUTFRegion(str,0,slen,(char*)str2.c_str());
00065     //printf("COL %s\n", str2.c_str());
00066     col2sql.push_back(str2);
00067     col2pk.push_back(false);
00068     col2type.push_back("TEXT");
00069   }
00070 
00072   // Cache data - haven't figured out random access yet
00073 
00074   Cursor *cursor = box->createCursor(tbl);
00075   if (!cursor) return false;
00076   cursor_implementation = cursor;
00077 
00078   List *row = NULL;
00079   h = 0;
00080   cache.resize(w,h,"");
00081   cacheFlag.resize(w,h,0);
00082   do {
00083     row = box->getNextRow(cursor);
00084     if (row) {
00085       //printf("row found\n");
00086       h++;
00087       cache.resize(w,h,"");
00088       cacheFlag.resize(w,h,0);
00089       for (int i=0; i<w; i++) {
00090         String *str = box->getElement(row,i);
00091         if (!str) return false;
00092         int slen = JvGetStringUTFLength(str);
00093         string str2(slen,'\0');
00094         JvGetStringUTFRegion(str,0,slen,(char*)str2.c_str());
00095         cache.cell(i,h-1) = str2;
00096         //printf("Value %d,%d [%s]\n", i, h-1, str2.c_str());
00097         savepoint.push_back((void*)box->getSavepoint(cursor));
00098       }
00099     }
00100   } while (row);
00101 
00102   //printf("Final dimensions: %d %d\n", w, h);
00103 
00104   return true;
00105 }
00106 
00107 JackSheet::~JackSheet() {
00108   /*
00109   if (table_implementation!=NULL) {
00110     mdb_free_tabledef(TABLE(table_implementation));
00111     table_implementation = NULL;
00112   }
00113   */
00114   if (schema!=NULL) delete schema;
00115   schema = NULL;
00116 }
00117 
00118 SheetSchema *JackSheet::getSchema() const {
00119   return schema;
00120 }
00121 
00122 
00123 std::string JackSheet::cellString(int x, int y) const {
00124   bool tmp;
00125   return cellString(x,y,tmp);
00126 }
00127 
00128 std::string JackSheet::cellString(int x, int y, bool& escaped) const {
00129   escaped = false;
00130   const unsigned char *f = cacheFlag.pcell_const(x,y);
00131   if (f!=NULL) {
00132     escaped = true;
00133     return "NULL";
00134   }
00135   const string *c = cache.pcell_const(x,y);
00136   if (c!=NULL) {
00137     return *c;
00138   }
00139 
00140   escaped = true;
00141   return "";
00142 }
00143 
00144 bool JackSheet::cellString(int x, int y, const std::string& str, 
00145                            bool escaped) {
00146   if (escaped) {
00147     fprintf(stderr,"JackSheet cannot deal with nulls yet\n");
00148   }
00149   printf("SHOULD WRITE!\n");
00150   JackBox *box = BOX(box_implementation);
00151   Table *tbl = TABLE(table_implementation);
00152   Cursor *cursor = CURSOR(cursor_implementation);
00153   if (!(box&&cursor&&tbl)) return false;
00154   box->restoreSavepoint(cursor,(Savepoint *)savepoint[y]);
00155   printf("Found row!\n");
00156   box->setCurrentRowValue(tbl,cursor,JS(col2sql[x].c_str()),JS(str.c_str()));
00157 
00158   return false;
00159 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines