COOPY » Guide
version 0.6.5
|
00001 #include <coopy/AccessTextBook.h> 00002 #include <coopy/AccessSheet.h> 00003 00004 #include "mdbtools.h" 00005 #include <stdio.h> 00006 00007 #define WANT_VECTOR2STRING 00008 #include <coopy/Stringer.h> 00009 00010 #include <algorithm> 00011 00012 using namespace std; 00013 using namespace coopy::store; 00014 using namespace coopy::store::mdb; 00015 00016 int AccessTextBook::uses = 0; 00017 00018 #define DB(x) ((MdbHandle *)(x)) 00019 00020 AccessTextBook::AccessTextBook() { 00021 implementation = NULL; 00022 uses++; 00023 if (uses==1) { 00024 mdb_init(); 00025 } 00026 } 00027 00028 AccessTextBook::~AccessTextBook() { 00029 clear(); 00030 uses--; 00031 if (uses==0) { 00032 mdb_exit(); 00033 } 00034 } 00035 00036 void AccessTextBook::clear() { 00037 if (implementation!=NULL) { 00038 mdb_close(DB(implementation)); 00039 implementation = NULL; 00040 } 00041 } 00042 00043 bool AccessTextBook::read(const char *fname) { 00044 clear(); 00045 MdbHandle *mdb = mdb_open(fname,MDB_NOFLAGS); 00046 if (!mdb) { 00047 fprintf(stderr,"Failed to open database %s\n", fname); 00048 return false; 00049 } 00050 implementation = (void *)mdb; 00051 if (!mdb_read_catalog(mdb,MDB_ANY)) { 00052 fprintf(stderr,"Failed to read as Access file: %s\n",fname); 00053 clear(); 00054 return false; 00055 } 00056 00057 names = getNamesImpl(); 00058 return true; 00059 } 00060 00061 bool AccessTextBook::open(const Property& config) { 00062 clear(); 00063 if (!config.check("file")) { 00064 fprintf(stderr,"file parameter needed\n"); 00065 return false; 00066 } 00067 if (!read(config.get("file").asString().c_str())) { 00068 fprintf(stderr,"failed to read %s\n", config.get("file").asString().c_str()); 00069 return false; 00070 } 00071 this->config = config; 00072 if (!config.check("table")) { 00073 dbg_printf("Loaded access workbook\n"); 00074 return true; 00075 } 00076 names.clear(); 00077 names.push_back(config.get("table").asString().c_str()); 00078 dbg_printf("Loaded access workbook, table: %s\n", 00079 vector2string(names).c_str()); 00080 return true; 00081 } 00082 00083 00084 vector<string> AccessTextBook::getNamesImpl() { 00085 std::vector<std::string> tables; 00086 MdbHandle *mdb = DB(implementation); 00087 for (int i=0; i<mdb->num_catalog; i++) { 00088 MdbCatalogEntry *entry = (MdbCatalogEntry *)g_ptr_array_index(mdb->catalog, i); 00089 if (entry->object_type!=MDB_TABLE) continue; 00090 if (mdb_is_system_table(entry)) continue; 00091 //printf("Found table %s ...\n", entry->object_name); 00092 tables.push_back(entry->object_name); 00093 } 00094 return tables; 00095 } 00096 00097 PolySheet AccessTextBook::readSheet(const std::string& name) { 00098 getNames(); 00099 if (find(names.begin(),names.end(),name)==names.end()) { 00100 return PolySheet(); 00101 } 00102 AccessSheet *sheet = new AccessSheet(implementation,name.c_str(),config); 00103 if (sheet!=NULL) sheet->connect(); 00104 return PolySheet(sheet,true); 00105 } 00106 00107 bool AccessTextBook::addSheet(const SheetSchema& schema) { 00108 return false; 00109 } 00110