1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/*
/ TextCsv.cpp
/ methods related to CSV/TXT loading
/
/ version 1.7, 2013 May 8
/
/ Author: Sandro Furieri a-furieri@lqt.it
/
/ Copyright (C) 2008-2013 Alessandro Furieri
/
/ This program is free software: you can redistribute it and/or modify
/ it under the terms of the GNU General Public License as published by
/ the Free Software Foundation, either version 3 of the License, or
/ (at your option) any later version.
/
/ This program is distributed in the hope that it will be useful,
/ but WITHOUT ANY WARRANTY; without even the implied warranty of
/ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/ GNU General Public License for more details.
/
/ You should have received a copy of the GNU General Public License
/ along with this program. If not, see <http://www.gnu.org/licenses/>.
/
*/
#include "Classdef.h"
#if defined(_WIN32) && !defined(__MINGW32__)
#define strcasecmp _stricmp
#endif
static void text_clean_integer(char *value)
{
/* cleaning an integer value */
char last;
char buffer[35536];
int len = strlen(value);
last = value[len - 1];
if (last == '-' || last == '+')
{
/* trailing sign; transforming into a leading sign */
*buffer = last;
strcpy(buffer + 1, value);
buffer[len - 1] = '\0';
strcpy(value, buffer);
}
}
static void text_clean_double(char *value)
{
/* cleaning an integer value */
char *p;
char last;
char buffer[35536];
int len = strlen(value);
last = value[len - 1];
if (last == '-' || last == '+')
{
/* trailing sign; transforming into a leading sign */
*buffer = last;
strcpy(buffer + 1, value);
buffer[len - 1] = '\0';
strcpy(value, buffer);
}
p = value;
while (*p != '\0')
{
/* transforming COMMAs into POINTs */
if (*p == ',')
*p = '.';
p++;
}
}
void
MyFrame::LoadText(wxString & path, wxString & table, wxString & charset,
bool first_titles, const char decimal_separator,
const char separator, const char text_separator)
{
//
// loading a CSV/TXT as a new DB table
//
gaiaTextReaderPtr text = NULL;
int seed;
int dup;
int idup;
char dummy[65536];
char dummyName[4096];
char **col_name = NULL;
int i;
char sql[65536];
int len;
int ret;
int rows = 0;
char *errMsg = NULL;
bool sqlError = false;
char xname[1024];
int current_row;
wxString msg;
char buf[4096];
int type;
const char *value;
//
// performing some checks before starting
//
if (TableAlreadyExists(table) == true)
{
wxMessageBox(wxT("a table named '") + table + wxT("' already exists"),
wxT("spatialite_gui"), wxOK | wxICON_ERROR, this);
return;
}
text = gaiaTextReaderAlloc(path.ToUTF8(), separator,
text_separator, decimal_separator,
first_titles, charset.ToUTF8());
if (text)
{
if (gaiaTextReaderParse(text) == 0)
{
gaiaTextReaderDestroy(text);
text = NULL;
}
}
if (!text)
return;
::wxBeginBusyCursor();
//
// checking for duplicate / illegal column names and antialising them
//
col_name = (char **) malloc(sizeof(char *) * text->max_fields);
seed = 0;
for (i = 0; i < text->max_fields; i++)
{
strcpy(dummyName, text->columns[i].name);
dup = 0;
for (idup = 0; idup < i; idup++)
{
if (strcasecmp(dummyName, *(col_name + idup)) == 0)
dup = 1;
}
if (strcasecmp(dummyName, "PK_UID") == 0)
dup = 1;
if (dup)
sprintf(dummyName, "DUPCOL_%d", seed++);
len = strlen(dummyName);
*(col_name + i) = (char *) malloc(len + 1);
strcpy(*(col_name + i), dummyName);
}
//
// starting a transaction
//
ret = sqlite3_exec(SqliteHandle, "BEGIN", NULL, 0, &errMsg);
if (ret != SQLITE_OK)
{
wxMessageBox(wxT("load CSV/TXT error:") + wxString::FromUTF8(errMsg),
wxT("spatialite_gui"), wxOK | wxICON_ERROR, this);
sqlite3_free(errMsg);
sqlError = true;
goto clean_up;
}
//
// creating the Table
//
strcpy(xname, table.ToUTF8());
DoubleQuotedSql(xname);
sprintf(sql, "CREATE TABLE %s", xname);
strcat(sql, " (\nPK_UID INTEGER PRIMARY KEY AUTOINCREMENT");
for (i = 0; i < text->max_fields; i++)
{
strcat(sql, ",\n");
strcpy(xname, *(col_name + i));
DoubleQuotedSql(xname);
strcat(sql, xname);
if (text->columns[i].type == VRTTXT_INTEGER)
strcat(sql, " INTEGER");
else if (text->columns[i].type == VRTTXT_DOUBLE)
strcat(sql, " DOUBLE");
else
strcat(sql, " TEXT");
}
strcat(sql, ")");
ret = sqlite3_exec(SqliteHandle, sql, NULL, 0, &errMsg);
if (ret != SQLITE_OK)
{
wxMessageBox(wxT("load text error:") + wxString::FromUTF8(errMsg),
wxT("spatialite_gui"), wxOK | wxICON_ERROR, this);
sqlite3_free(errMsg);
sqlError = true;
goto clean_up;
}
current_row = 0;
while (current_row < text->num_rows)
{
//
// inserting rows from CSV/TXT
//
if (!gaiaTextReaderGetRow(text, current_row))
break;
strcpy(xname, table.ToUTF8());
DoubleQuotedSql(xname);
sprintf(sql, "INSERT INTO %s (\nPK_UID", xname);
for (i = 0; i < text->max_fields; i++)
{
// columns corresponding to some CSV/TXT column
strcat(sql, ",");
strcpy(xname, *(col_name + i));
DoubleQuotedSql(xname);
strcat(sql, xname);
}
strcat(sql, ")\nVALUES (");
sprintf(dummy, "%d", current_row);
strcat(sql, dummy);
for (i = 0; i < text->max_fields; i++)
{
// column values
strcat(sql, ",");
if (!gaiaTextReaderFetchField(text, i, &type, &value))
strcat(sql, "NULL");
else
{
if (type == VRTTXT_INTEGER)
{
strcpy(buf, value);
text_clean_integer(buf);
#if defined(_WIN32) || defined(__MINGW32__)
/* CAVEAT - M$ runtime has non-standard functions for 64 bits */
sprintf(dummy, "%I64d", _atoi64(buf));
#else
sprintf(dummy, "%lld", atoll(buf));
#endif
strcat(sql, dummy);
} else if (type == VRTTXT_DOUBLE)
{
strcpy(buf, value);
text_clean_double(buf);
sprintf(dummy, "%1.6f", atof(buf));
strcat(sql, dummy);
} else if (type == VRTTXT_TEXT)
{
void *ptr = (void *) value;
strcpy(dummy, value);
free(ptr);
CleanSqlString(dummy);
strcat(sql, "'");
strcat(sql, dummy);
strcat(sql, "'");
} else
strcat(sql, "NULL");
}
}
strcat(sql, ")");
ret = sqlite3_exec(SqliteHandle, sql, NULL, 0, &errMsg);
if (ret != SQLITE_OK)
{
wxMessageBox(wxT("load text error:") + wxString::FromUTF8(errMsg),
wxT("spatialite_gui"), wxOK | wxICON_ERROR, this);
sqlite3_free(errMsg);
sqlError = true;
goto clean_up;
}
rows++;
current_row++;
}
clean_up:
if (col_name)
{
// releasing memory allocation for column names
for (i = 0; i < text->max_fields; i++)
free(*(col_name + i));
free(col_name);
}
gaiaTextReaderDestroy(text);
if (sqlError == true)
{
// some error occurred - ROLLBACK
ret = sqlite3_exec(SqliteHandle, "ROLLBACK", NULL, 0, &errMsg);
if (ret != SQLITE_OK)
{
wxMessageBox(wxT("load text error:") + wxString::FromUTF8(errMsg),
wxT("spatialite_gui"), wxOK | wxICON_ERROR, this);
sqlite3_free(errMsg);
}
::wxEndBusyCursor();
msg =
wxT("CSV/TXT not loaded\n\n\na ROLLBACK was automatically performed");
wxMessageBox(msg, wxT("spatialite_gui"), wxOK | wxICON_WARNING, this);
} else
{
// ok - confirming pending transaction - COMMIT
ret = sqlite3_exec(SqliteHandle, "COMMIT", NULL, 0, &errMsg);
if (ret != SQLITE_OK)
{
wxMessageBox(wxT("load text error:") + wxString::FromUTF8(errMsg),
wxT("spatialite_gui"), wxOK | wxICON_ERROR, this);
sqlite3_free(errMsg);
return;
}
::wxEndBusyCursor();
sprintf(dummy, "CSV/TXT loaded\n\n%d inserted rows", rows);
msg = wxString::FromUTF8(dummy);
wxMessageBox(msg, wxT("spatialite_gui"), wxOK | wxICON_INFORMATION, this);
InitTableTree();
}
}
|