COOPY » Guide
version 0.6.5
|
00001 #include <coopy/PoolImpl.h> 00002 #include <coopy/Dbg.h> 00003 00004 00005 using namespace coopy::store; 00006 using namespace std; 00007 00008 00009 bool PoolImpl::create(const std::string& key, 00010 const std::string& table_name, 00011 const std::string& column_name, 00012 bool invent) { 00013 dbg_printf("CREATE Pool: %s %s:%s %s\n", 00014 key.c_str(), table_name.c_str(), column_name.c_str(), 00015 invent?"(create)":""); 00016 std::map<std::string,PoolSlice>::iterator p = pool.find(key); 00017 if (p==pool.end()) { 00018 pool[key] = PoolSlice(); 00019 p = pool.find(key); 00020 p->second.pool_name = key; 00021 } 00022 PoolLinkImpl link; 00023 link.name = key; 00024 link.invent = invent; 00025 link.table_name = table_name; 00026 link.column_name = column_name; 00027 pool_link[getKey(table_name,column_name)] = link; 00028 if (pool_link_root.find(key)==pool_link_root.end() || invent) { 00029 pool_link_root[key] = &pool_link[getKey(table_name,column_name)]; 00030 } 00031 return true; 00032 } 00033 00034 00035 PoolRecord& PoolImpl::lookup(const std::string& table_name, 00036 const std::string& column_name, 00037 const SheetCell& val, 00038 bool& match) { 00039 PoolColumnLink col = lookup(table_name,column_name); 00040 if (!col.isValid()) { 00041 match = false; 00042 return dud; 00043 } 00044 match = true; 00045 return col.getColumn().lookupMod(val,match); 00046 } 00047 00048 00049 00050 PoolColumnLink PoolImpl::lookup(const std::string& table_name, 00051 const std::string& column_name) { 00052 std::string key = getKey(table_name,column_name); 00053 std::map<std::string,PoolLinkImpl>::iterator link = pool_link.find(key); 00054 if (link==pool_link.end()) { 00055 return PoolColumnLink(null_column,false); 00056 } 00057 key = link->second.name; 00058 std::map<std::string,PoolSlice>::iterator p = pool.find(key); 00059 if (p==pool.end()) { 00060 return PoolColumnLink(null_column,false); 00061 } 00062 return PoolColumnLink(p->second,link->second.invent, 00063 table_name,column_name,link->second.name); 00064 } 00065 00066 00067 PoolColumnLink PoolImpl::trace(const PoolColumnLink& src) { 00068 PoolLinkImpl *root = pool_link_root[src.getPoolName()]; 00069 return PoolColumnLink(pool[src.getPoolName()],root->invent, 00070 root->table_name,root->column_name,root->name); 00071 } 00072 00073 bool PoolImpl::load() { 00074 dbg_printf("Load pool\n"); 00075 vector<string> names = book.getNames(); 00076 for (int k=0; k<(int)names.size(); k++) { 00077 string name = names[k]; 00078 PoolSlice& slice = pool[name] = PoolSlice(); 00079 slice.pool_name = name; 00080 PolySheet sheet = book.readSheet(name); 00081 for (int y=0; y<sheet.height(); y++) { 00082 PoolRecord rec = {sheet.cellSummary(1,y), false, false, true}; 00083 slice.item[sheet.cellSummary(0,y).toString()] = rec; 00084 } 00085 } 00086 return true; 00087 } 00088 00089 bool PoolImpl::save() { 00090 dbg_printf("Save pool\n"); 00091 for (map<string,PoolSlice>::iterator it=pool.begin(); it!=pool.end(); it++) { 00092 string name = it->first; 00093 PoolSlice& pool = it->second; 00094 SimpleSheetSchema schema; 00095 schema.setSheetName(name.c_str()); 00096 schema.addColumn("local"); 00097 schema.addColumn("remote"); 00098 PolySheet sheet = book.provideSheet(schema); 00099 sheet.deleteData(); 00100 for (map<string,PoolRecord>::iterator it2 = pool.item.begin(); 00101 it2 != pool.item.end(); it2++) { 00102 string from = it2->first; 00103 SheetCell to = it2->second.cell; 00104 Poly<SheetRow> row = sheet.insertRow(); 00105 row->setCell(0,SheetCell(from,false)); // fix for escaped cells 00106 row->setCell(1,to); 00107 row->flush(); 00108 } 00109 } 00110 00111 return true; 00112 } 00113 00114 00115 const PoolRecord& PoolSlice::lookup(const SheetCell& val, bool& match) const { 00116 std::map<std::string,PoolRecord>::const_iterator it = item.find(val.toString()); 00117 if (it==item.end()) { 00118 match = false; 00119 return dud; 00120 } 00121 match = true; 00122 return it->second; 00123 } 00124 00125 PoolRecord& PoolSlice::lookupMod(const SheetCell& val, bool& match) { 00126 std::map<std::string,PoolRecord>::iterator it = item.find(val.toString()); 00127 if (it==item.end()) { 00128 match = false; 00129 return dud; 00130 } 00131 match = true; 00132 return it->second; 00133 }