| 1 | /*************************************************************************** |
|---|
| 2 | tmdmprocessor.cpp |
|---|
| 3 | ------------------- |
|---|
| 4 | copyright : (C) 2013 CREALP Lucien Schreiber |
|---|
| 5 | email : lucien.schreiber at crealp dot vs dot ch |
|---|
| 6 | ***************************************************************************/ |
|---|
| 7 | |
|---|
| 8 | /*************************************************************************** |
|---|
| 9 | * * |
|---|
| 10 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 11 | * it under the terms of the GNU General Public License as published by * |
|---|
| 12 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 13 | * (at your option) any later version. * |
|---|
| 14 | * * |
|---|
| 15 | ***************************************************************************/ |
|---|
| 16 | |
|---|
| 17 | #include "tmdmprocessor.h" |
|---|
| 18 | #include "tmdmcopier.h" |
|---|
| 19 | |
|---|
| 20 | TmDmProcessor::TmDmProcessor(const wxFileName & src, const wxFileName & dest) { |
|---|
| 21 | m_FileSrc = src; |
|---|
| 22 | m_FileDst = dest; |
|---|
| 23 | m_LanguageCol = 0; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | TmDmProcessor::~TmDmProcessor() { |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | int TmDmProcessor::FindBlock(const wxString & blockname) { |
|---|
| 34 | wxFileInputStream input(m_FileSrc.GetFullPath()); |
|---|
| 35 | wxTextInputStream text(input); |
|---|
| 36 | long myLineIndex = 0; |
|---|
| 37 | while(input.IsOk() && !input.Eof() ){ |
|---|
| 38 | wxString myLine = text.ReadLine(); |
|---|
| 39 | if (myLine.StartsWith(blockname)==true){ |
|---|
| 40 | return myLineIndex; |
|---|
| 41 | } |
|---|
| 42 | myLineIndex++; |
|---|
| 43 | } |
|---|
| 44 | return wxNOT_FOUND; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | wxString TmDmProcessor::SwitchCols(wxArrayString * cols, wxArrayString * values, int item) { |
|---|
| 50 | if (m_LanguageCol == 0) { |
|---|
| 51 | return values->Item(item); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | if(cols->Item(item).Contains(_T("_0")) == true) { |
|---|
| 56 | return values->Item(item + m_LanguageCol); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | wxString myColName = wxString::Format(_T("_%d"), m_LanguageCol); |
|---|
| 60 | if (cols->Item(item).Contains(myColName) == true) { |
|---|
| 61 | return values->Item(item - m_LanguageCol); |
|---|
| 62 | } |
|---|
| 63 | return values->Item(item); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | void TmDmProcessor::SetLanguageColumn(int value) { |
|---|
| 69 | m_LanguageCol = value; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | TmDmProcessorSimple::TmDmProcessorSimple(const wxFileName & src, const wxFileName & dest) : TmDmProcessor(src,dest) { |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | TmDmProcessorSimple::~TmDmProcessorSimple() { |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | bool TmDmProcessorSimple::ProcessBlock(int blockstart, const wxString & tablename) { |
|---|
| 90 | wxArrayString mySQLCols; |
|---|
| 91 | TmDmCopier myCopier(m_FileDst); |
|---|
| 92 | myCopier.CopyFrom(wxString::Format(_T("\n-- %s --\n"), tablename)); |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | wxFileInputStream input(m_FileSrc.GetFullPath()); |
|---|
| 96 | wxTextInputStream text(input); |
|---|
| 97 | long myLineIndex = 0; |
|---|
| 98 | while(input.IsOk() && !input.Eof() ){ |
|---|
| 99 | wxString myRow = text.ReadLine(); |
|---|
| 100 | if (myLineIndex <= blockstart) { |
|---|
| 101 | myLineIndex++; |
|---|
| 102 | continue; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | if (myLineIndex == blockstart+1) { |
|---|
| 106 | mySQLCols = wxStringTokenize(myRow, _T("\t"), wxTOKEN_RET_EMPTY); |
|---|
| 107 | myLineIndex++; |
|---|
| 108 | continue; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | wxArrayString myValues = wxStringTokenize(myRow, _T("\t"), wxTOKEN_RET_EMPTY_ALL); |
|---|
| 112 | bool bEmpty = true; |
|---|
| 113 | for (unsigned int i = 0; i< myValues.GetCount(); i++) { |
|---|
| 114 | if (myValues[i] != wxEmptyString) { |
|---|
| 115 | bEmpty = false; |
|---|
| 116 | break; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | if (bEmpty == true) { |
|---|
| 120 | // ok empty line found |
|---|
| 121 | return true; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | // write insert sentence |
|---|
| 125 | wxString myInsert = wxString::Format(_T("INSERT INTO `%s` ("), tablename); |
|---|
| 126 | for (unsigned int i = 0; i< mySQLCols.GetCount(); i++) { |
|---|
| 127 | myInsert.Append(wxString::Format(_T("%s,"), mySQLCols.Item(i))); |
|---|
| 128 | } |
|---|
| 129 | myInsert.RemoveLast(); |
|---|
| 130 | myInsert.Append(_T(") VALUES (")); |
|---|
| 131 | for (unsigned int i = 0; i< mySQLCols.GetCount(); i++) { |
|---|
| 132 | myInsert.Append(wxString::Format(_T("\"%s\","), SwitchCols(&mySQLCols, &myValues, i))); |
|---|
| 133 | } |
|---|
| 134 | myInsert.RemoveLast(); |
|---|
| 135 | myInsert.Append(_T(");\n")); |
|---|
| 136 | myCopier.CopyFrom(myInsert); |
|---|
| 137 | myLineIndex++; |
|---|
| 138 | } |
|---|
| 139 | return false; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | TmDmProcessorAttributs::TmDmProcessorAttributs(const wxFileName & src, const wxFileName & dest) : TmDmProcessor(src,dest) { |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | TmDmProcessorAttributs::~TmDmProcessorAttributs() { |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | bool TmDmProcessorAttributs::_ProcessAttributesName(int blockstart) { |
|---|
| 161 | wxArrayString mySQLCols; |
|---|
| 162 | TmDmCopier myCopier(m_FileDst); |
|---|
| 163 | myCopier.CopyFrom(wxString::Format(_T("\n-- %s --\n"), _T("dmn_layer_attribut"))); |
|---|
| 164 | int NUM_COLS = 3; |
|---|
| 165 | wxArrayString myPreviousRow; |
|---|
| 166 | |
|---|
| 167 | wxFileInputStream input(m_FileSrc.GetFullPath()); |
|---|
| 168 | wxTextInputStream text(input); |
|---|
| 169 | long myLineIndex = 0; |
|---|
| 170 | while(input.IsOk() && !input.Eof() ){ |
|---|
| 171 | wxString myRow = text.ReadLine(); |
|---|
| 172 | if (myLineIndex <= blockstart) { |
|---|
| 173 | myLineIndex++; |
|---|
| 174 | continue; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | if (myLineIndex == blockstart+1) { |
|---|
| 178 | mySQLCols = wxStringTokenize(myRow, _T("\t"), wxTOKEN_RET_EMPTY); |
|---|
| 179 | myLineIndex++; |
|---|
| 180 | continue; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | wxArrayString myValues = wxStringTokenize(myRow, _T("\t"), wxTOKEN_RET_EMPTY_ALL); |
|---|
| 184 | bool bEmpty = true; |
|---|
| 185 | for (unsigned int i = 0; i< myValues.GetCount(); i++) { |
|---|
| 186 | if (myValues[i] != wxEmptyString) { |
|---|
| 187 | bEmpty = false; |
|---|
| 188 | break; |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | if (bEmpty == true) { |
|---|
| 192 | // ok empty line found |
|---|
| 193 | return true; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | // check that this row differs from previous |
|---|
| 197 | wxArrayString myAttributRow; |
|---|
| 198 | for (unsigned int i = 0; i< NUM_COLS; i++) { |
|---|
| 199 | myAttributRow.Add(myValues.Item(i)); |
|---|
| 200 | } |
|---|
| 201 | if (myAttributRow == myPreviousRow) { |
|---|
| 202 | continue; |
|---|
| 203 | } |
|---|
| 204 | myPreviousRow = myAttributRow; |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | wxString myInsert = _T("INSERT INTO `dmn_layer_attribut` VALUES ("); |
|---|
| 208 | for (unsigned int i = 0; i< myAttributRow.GetCount(); i++) { |
|---|
| 209 | myInsert.Append(wxString::Format(_T("\"%s\","), myAttributRow.Item(i))); |
|---|
| 210 | } |
|---|
| 211 | myInsert.RemoveLast(); |
|---|
| 212 | myInsert.Append(_T(");\n")); |
|---|
| 213 | myCopier.CopyFrom(myInsert); |
|---|
| 214 | myLineIndex++; |
|---|
| 215 | } |
|---|
| 216 | return true; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | bool TmDmProcessorAttributs::_ProcessAttributesValues(int blockstart) { |
|---|
| 222 | wxArrayString mySQLCols; |
|---|
| 223 | TmDmCopier myCopier(m_FileDst); |
|---|
| 224 | myCopier.CopyFrom(wxString::Format(_T("\n-- %s --\n"), _T("attribut values"))); |
|---|
| 225 | int START_COL = 3; |
|---|
| 226 | |
|---|
| 227 | wxFileInputStream input(m_FileSrc.GetFullPath()); |
|---|
| 228 | wxTextInputStream text(input); |
|---|
| 229 | long myLineIndex = 0; |
|---|
| 230 | while(input.IsOk() && !input.Eof() ){ |
|---|
| 231 | wxString myRow = text.ReadLine(); |
|---|
| 232 | if (myLineIndex <= blockstart) { |
|---|
| 233 | myLineIndex++; |
|---|
| 234 | continue; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | if (myLineIndex == blockstart+1) { |
|---|
| 238 | mySQLCols = wxStringTokenize(myRow, _T("\t"), wxTOKEN_RET_EMPTY); |
|---|
| 239 | myLineIndex++; |
|---|
| 240 | continue; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | wxArrayString myValues = wxStringTokenize(myRow, _T("\t"), wxTOKEN_RET_EMPTY_ALL); |
|---|
| 245 | bool bEmpty = true; |
|---|
| 246 | for (unsigned int i = 0; i< myValues.GetCount(); i++) { |
|---|
| 247 | if (myValues[i] != wxEmptyString) { |
|---|
| 248 | bEmpty = false; |
|---|
| 249 | break; |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | if (bEmpty == true) { |
|---|
| 253 | // ok empty line found |
|---|
| 254 | return true; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | // query to dmn_catalog |
|---|
| 258 | wxString myInsert = _T("INSERT INTO `dmn_catalog` VALUES ("); |
|---|
| 259 | for (unsigned int i = START_COL; i< mySQLCols.GetCount(); i++) { |
|---|
| 260 | myInsert.Append(wxString::Format(_T("\"%s\","), SwitchCols(&mySQLCols, &myValues, i))); |
|---|
| 261 | } |
|---|
| 262 | myInsert.RemoveLast(); |
|---|
| 263 | myInsert.Append(_T(");\n")); |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | // query to dmn_attribut_value |
|---|
| 267 | myInsert.Append(wxString::Format(_T("INSERT INTO `dmn_attribut_value` VALUES (%s, %s);\n"), |
|---|
| 268 | myValues.Item(0), myValues.Item(START_COL))); |
|---|
| 269 | |
|---|
| 270 | myCopier.CopyFrom(myInsert); |
|---|
| 271 | myLineIndex++; |
|---|
| 272 | } |
|---|
| 273 | return true; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | bool TmDmProcessorAttributs::ProcessBlock(int blockstart, const wxString & tablename) { |
|---|
| 279 | if (_ProcessAttributesName(blockstart) == false) { |
|---|
| 280 | return false; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | if (_ProcessAttributesValues(blockstart) == false) { |
|---|
| 284 | return false; |
|---|
| 285 | } |
|---|
| 286 | return true; |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|