COOPY » Guide
version 0.6.5
|
00001 #include <coopy/SchemaSniffer.h> 00002 #include <coopy/NameSniffer.h> 00003 #include <coopy/IndexSniffer.h> 00004 #include <coopy/Dbg.h> 00005 #include <coopy/CompareFlags.h> 00006 #include <coopy/Stringer.h> 00007 00008 #include <stdlib.h> 00009 00010 using namespace coopy::store; 00011 using namespace coopy::cmp; 00012 00013 using namespace std; 00014 00015 void SchemaSniffer::sniff(bool force) { 00016 dbg_printf("Sniff schema\n"); 00017 fallback = SimpleSheetSchema(); 00018 COOPY_ASSERT(sheet!=NULL); 00019 if (!force) { 00020 if (sheet->getSchema()!=NULL) { 00021 if (sheet->getSchema()->getColumnCount()!=0) { 00022 schema = sheet->getSchema(); 00023 dbg_printf(" - Schema built in\n"); 00024 return; 00025 } 00026 } 00027 } 00028 CompareFlags flags; 00029 if (!ns) { 00030 ns = new NameSniffer(*sheet,flags); 00031 COOPY_ASSERT(ns); 00032 } 00033 NameSniffer& nameSniffer = *ns; 00034 vector<string> names = nameSniffer.suggestNames(); 00035 vector<ColumnType> ct = nameSniffer.suggestTypes(); 00036 IndexSniffer indexSniffer(*sheet,flags,nameSniffer); 00037 vector<int> indexes = indexSniffer.suggestIndexes(); 00038 bool haveKey = false; 00039 bool shouldSetKey = true; 00040 while (ct.size()<indexes.size()) { 00041 ct.push_back(ColumnType()); 00042 } 00043 for (int i=0; i<(int)indexes.size(); i++) { 00044 if (indexes[i]>0) { 00045 haveKey = true; 00046 } 00047 } 00048 for (int i=0; i<(int)ct.size(); i++) { 00049 if (ct[i].primaryKeySet) { 00050 shouldSetKey = false; 00051 break; 00052 } 00053 } 00054 if (haveKey&&shouldSetKey) { 00055 for (int i=0; i<(int)indexes.size(); i++) { 00056 ct[i].primaryKeySet = true; 00057 ct[i].primaryKey = (indexes[i]>0); 00058 } 00059 } 00060 bool force_guess = true; 00061 for (int i=0; i<(int)names.size(); i++) { 00062 ColumnType base = ct[i]; 00063 fallback.addColumn(names[i].c_str(),ct[i]); 00064 force_guess = false; 00065 } 00066 int at = (int)names.size(); 00067 string col = Stringer::getSpreadsheetColumnName(at); 00068 //char col[3] = "A2"; 00069 while ((int)fallback.getColumnCount()<sheet->width()) { 00070 fallback.addColumn(col.c_str(),ct[at]); 00071 col = Stringer::nextSpreadsheetColumnName(col); 00072 at++; 00073 } 00074 fallback.setSheetName(name.c_str()); 00075 fallback.setHasSheetName(sheet->hasSheetName()); 00076 fallback.setHeaderHeight(nameSniffer.getHeaderHeight()); 00077 schema = &fallback; 00078 fallback.setGuess(nameSniffer.isFake()||force_guess); 00079 } 00080 00081 SheetSchema *SchemaSniffer::suggestSchema() { 00082 return schema; 00083 } 00084 00085 void SchemaSniffer::resniff(coopy::store::SchemaSniffer& alt) { 00086 if (ns && alt.ns) { 00087 if (ns->resniff(*alt.ns)) { 00088 sniff(); 00089 } 00090 } 00091 }