| 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 | } |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | TmDmProcessor::~TmDmProcessor() { |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | int TmDmProcessor::FindBlock(const wxString & blockname) { |
|---|
| 33 | wxFileInputStream input(m_FileSrc.GetFullPath()); |
|---|
| 34 | wxTextInputStream text(input); |
|---|
| 35 | long myLineIndex = 0; |
|---|
| 36 | while(input.IsOk() && !input.Eof() ){ |
|---|
| 37 | wxString myLine = text.ReadLine(); |
|---|
| 38 | if (myLine.StartsWith(blockname)==true){ |
|---|
| 39 | return myLineIndex; |
|---|
| 40 | } |
|---|
| 41 | myLineIndex++; |
|---|
| 42 | } |
|---|
| 43 | return wxNOT_FOUND; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | TmDmProcessorSimple::TmDmProcessorSimple(const wxFileName & src, const wxFileName & dest) : TmDmProcessor(src,dest) { |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | TmDmProcessorSimple::~TmDmProcessorSimple() { |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | bool TmDmProcessorSimple::ProcessBlock(int blockstart, const wxString & tablename) { |
|---|
| 64 | wxArrayString mySQLCols; |
|---|
| 65 | TmDmCopier myCopier(m_FileDst); |
|---|
| 66 | myCopier.CopyFrom(wxString::Format(_T("\n-- %s --\n"), tablename)); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | wxFileInputStream input(m_FileSrc.GetFullPath()); |
|---|
| 70 | wxTextInputStream text(input); |
|---|
| 71 | long myLineIndex = 0; |
|---|
| 72 | while(input.IsOk() && !input.Eof() ){ |
|---|
| 73 | wxString myRow = text.ReadLine(); |
|---|
| 74 | if (myLineIndex <= blockstart) { |
|---|
| 75 | myLineIndex++; |
|---|
| 76 | continue; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | if (myLineIndex == blockstart+1) { |
|---|
| 80 | mySQLCols = wxStringTokenize(myRow, _T("\t"), wxTOKEN_RET_EMPTY); |
|---|
| 81 | myLineIndex++; |
|---|
| 82 | continue; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | wxArrayString myValues = wxStringTokenize(myRow, _T("\t"), wxTOKEN_RET_EMPTY_ALL); |
|---|
| 86 | bool bEmpty = true; |
|---|
| 87 | for (unsigned int i = 0; i< myValues.GetCount(); i++) { |
|---|
| 88 | if (myValues[i] != wxEmptyString) { |
|---|
| 89 | bEmpty = false; |
|---|
| 90 | break; |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | if (bEmpty == true) { |
|---|
| 94 | // ok empty line found |
|---|
| 95 | return true; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | // write insert sentence |
|---|
| 99 | wxString myInsert = wxString::Format(_T("INSERT INTO `%s` ("), tablename); |
|---|
| 100 | for (unsigned int i = 0; i< mySQLCols.GetCount(); i++) { |
|---|
| 101 | myInsert.Append(wxString::Format(_T("%s,"), mySQLCols.Item(i))); |
|---|
| 102 | } |
|---|
| 103 | myInsert.RemoveLast(); |
|---|
| 104 | myInsert.Append(_T(") VALUES (")); |
|---|
| 105 | for (unsigned int i = 0; i< mySQLCols.GetCount(); i++) { |
|---|
| 106 | myInsert.Append(wxString::Format(_T("\"%s\","), myValues.Item(i))); |
|---|
| 107 | } |
|---|
| 108 | myInsert.RemoveLast(); |
|---|
| 109 | myInsert.Append(_T(");\n")); |
|---|
| 110 | myCopier.CopyFrom(myInsert); |
|---|
| 111 | myLineIndex++; |
|---|
| 112 | } |
|---|
| 113 | return false; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | TmDmProcessorAttributs::TmDmProcessorAttributs(const wxFileName & src, const wxFileName & dest) : TmDmProcessor(src,dest) { |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | TmDmProcessorAttributs::~TmDmProcessorAttributs() { |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | bool TmDmProcessorAttributs::_ProcessAttributesName(int blockstart) { |
|---|
| 135 | wxArrayString mySQLCols; |
|---|
| 136 | TmDmCopier myCopier(m_FileDst); |
|---|
| 137 | myCopier.CopyFrom(wxString::Format(_T("\n-- %s --\n"), _T("dmn_layer_attribut"))); |
|---|
| 138 | int NUM_COLS = 3; |
|---|
| 139 | wxArrayString myPreviousRow; |
|---|
| 140 | |
|---|
| 141 | wxFileInputStream input(m_FileSrc.GetFullPath()); |
|---|
| 142 | wxTextInputStream text(input); |
|---|
| 143 | long myLineIndex = 0; |
|---|
| 144 | while(input.IsOk() && !input.Eof() ){ |
|---|
| 145 | wxString myRow = text.ReadLine(); |
|---|
| 146 | if (myLineIndex <= blockstart) { |
|---|
| 147 | myLineIndex++; |
|---|
| 148 | continue; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | if (myLineIndex == blockstart+1) { |
|---|
| 152 | mySQLCols = wxStringTokenize(myRow, _T("\t"), wxTOKEN_RET_EMPTY); |
|---|
| 153 | myLineIndex++; |
|---|
| 154 | continue; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | wxArrayString myValues = wxStringTokenize(myRow, _T("\t"), wxTOKEN_RET_EMPTY_ALL); |
|---|
| 158 | bool bEmpty = true; |
|---|
| 159 | for (unsigned int i = 0; i< myValues.GetCount(); i++) { |
|---|
| 160 | if (myValues[i] != wxEmptyString) { |
|---|
| 161 | bEmpty = false; |
|---|
| 162 | break; |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | if (bEmpty == true) { |
|---|
| 166 | // ok empty line found |
|---|
| 167 | return true; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | // check that this row differs from previous |
|---|
| 171 | wxArrayString myAttributRow; |
|---|
| 172 | for (unsigned int i = 0; i< NUM_COLS; i++) { |
|---|
| 173 | myAttributRow.Add(myValues.Item(i)); |
|---|
| 174 | } |
|---|
| 175 | if (myAttributRow == myPreviousRow) { |
|---|
| 176 | continue; |
|---|
| 177 | } |
|---|
| 178 | myPreviousRow = myAttributRow; |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | wxString myInsert = _T("INSERT INTO `dmn_layer_attribut` VALUES ("); |
|---|
| 182 | for (unsigned int i = 0; i< myAttributRow.GetCount(); i++) { |
|---|
| 183 | myInsert.Append(wxString::Format(_T("\"%s\","), myAttributRow.Item(i))); |
|---|
| 184 | } |
|---|
| 185 | myInsert.RemoveLast(); |
|---|
| 186 | myInsert.Append(_T(");\n")); |
|---|
| 187 | myCopier.CopyFrom(myInsert); |
|---|
| 188 | myLineIndex++; |
|---|
| 189 | } |
|---|
| 190 | return true; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | bool TmDmProcessorAttributs::_ProcessAttributesValues(int blockstart) { |
|---|
| 196 | wxArrayString mySQLCols; |
|---|
| 197 | TmDmCopier myCopier(m_FileDst); |
|---|
| 198 | myCopier.CopyFrom(wxString::Format(_T("\n-- %s --\n"), _T("attribut values"))); |
|---|
| 199 | int START_COL = 3; |
|---|
| 200 | |
|---|
| 201 | wxFileInputStream input(m_FileSrc.GetFullPath()); |
|---|
| 202 | wxTextInputStream text(input); |
|---|
| 203 | long myLineIndex = 0; |
|---|
| 204 | while(input.IsOk() && !input.Eof() ){ |
|---|
| 205 | wxString myRow = text.ReadLine(); |
|---|
| 206 | if (myLineIndex <= blockstart) { |
|---|
| 207 | myLineIndex++; |
|---|
| 208 | continue; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | if (myLineIndex == blockstart+1) { |
|---|
| 212 | mySQLCols = wxStringTokenize(myRow, _T("\t"), wxTOKEN_RET_EMPTY); |
|---|
| 213 | myLineIndex++; |
|---|
| 214 | continue; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | wxArrayString myValues = wxStringTokenize(myRow, _T("\t"), wxTOKEN_RET_EMPTY_ALL); |
|---|
| 219 | bool bEmpty = true; |
|---|
| 220 | for (unsigned int i = 0; i< myValues.GetCount(); i++) { |
|---|
| 221 | if (myValues[i] != wxEmptyString) { |
|---|
| 222 | bEmpty = false; |
|---|
| 223 | break; |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | if (bEmpty == true) { |
|---|
| 227 | // ok empty line found |
|---|
| 228 | return true; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | // query to dmn_catalog |
|---|
| 232 | wxString myInsert = _T("INSERT INTO `dmn_catalog` VALUES ("); |
|---|
| 233 | for (unsigned int i = START_COL; i< mySQLCols.GetCount(); i++) { |
|---|
| 234 | myInsert.Append(wxString::Format(_T("\"%s\","), myValues.Item(i))); |
|---|
| 235 | } |
|---|
| 236 | myInsert.RemoveLast(); |
|---|
| 237 | myInsert.Append(_T(");\n")); |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | // query to dmn_attribut_value |
|---|
| 241 | myInsert.Append(wxString::Format(_T("INSERT INTO `dmn_attribut_value` VALUES (%s, %s);\n"), |
|---|
| 242 | myValues.Item(0), myValues.Item(START_COL))); |
|---|
| 243 | |
|---|
| 244 | myCopier.CopyFrom(myInsert); |
|---|
| 245 | myLineIndex++; |
|---|
| 246 | } |
|---|
| 247 | return true; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | bool TmDmProcessorAttributs::ProcessBlock(int blockstart, const wxString & tablename) { |
|---|
| 253 | if (_ProcessAttributesName(blockstart) == false) { |
|---|
| 254 | return false; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | if (_ProcessAttributesValues(blockstart) == false) { |
|---|
| 258 | return false; |
|---|
| 259 | } |
|---|
| 260 | return true; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|