34
34
#include " cpl_error.h"
35
35
#include " qgsshapefile.h"
36
36
#include " qgis.h"
37
+ #include " qgslogger.h"
37
38
38
39
// for htonl
39
40
#ifdef WIN32
@@ -159,11 +160,11 @@ QString QgsShapeFile::getFeatureClass(){
159
160
*/
160
161
// geom_type = QString(geom->getGeometryName());
161
162
// geom_type = "GEOMETRY";
162
- std::cerr << " Preparing to escape " << geom_type. toLocal8Bit (). data () << std::endl;
163
+ QgsDebugMsg ( " Preparing to escape " + geom_type);
163
164
char * esc_str = new char [geom_type.length ()*2 +1 ];
164
165
PQescapeString (esc_str, (const char *)geom_type, geom_type.length ());
165
166
geom_type = QString (esc_str);
166
- std::cerr << " After escaping, geom_type is : " << geom_type. toLocal8Bit (). data () << std::endl;
167
+ QgsDebugMsg ( " After escaping, geom_type is : " + geom_type);
167
168
delete[] esc_str;
168
169
169
170
QString file (filename);
@@ -258,59 +259,73 @@ void QgsShapeFile::setColumnNames(QStringList columns)
258
259
}
259
260
}
260
261
261
- bool QgsShapeFile::insertLayer (QString dbname, QString schema, QString geom_col, QString srid, PGconn * conn, QProgressDialog& pro, bool &fin){
262
+ bool QgsShapeFile::insertLayer (QString dbname, QString schema, QString geom_col,
263
+ QString srid, PGconn * conn, QProgressDialog& pro, bool &fin,
264
+ QString& errorText)
265
+ {
262
266
connect (&pro, SIGNAL (cancelled ()), this , SLOT (cancelImport ()));
263
267
import_cancelled = false ;
264
268
bool result = true ;
265
269
// Mangle the table name to make it PG compliant by replacing spaces with
266
270
// underscores
267
271
table_name = table_name.replace (" " ," _" );
272
+
268
273
QString query = " CREATE TABLE " +schema+" ." +table_name+" (gid int4 PRIMARY KEY, " ;
274
+
269
275
for (int n=0 ; n<column_names.size () && result; n++){
270
276
if (!column_names[n][0 ].isLetter ())
271
277
result = false ;
278
+
272
279
char * esc_str = new char [column_names[n].length ()*2 +1 ];
273
- std::cerr << " Escaping " << column_names[n]. toLocal8Bit (). data () << " to " ;
280
+
274
281
PQescapeString (esc_str, (const char *)column_names[n].lower (), column_names[n].length ());
275
- std::cerr << esc_str << std::endl ;
282
+ QgsDebugMsg ( " Escaped " + column_names[n] + " to " + esc_str) ;
276
283
query += esc_str;
277
- std::cerr << query.toLocal8Bit ().data () << std::endl;
278
- query += " " ;
279
- std::cerr << query.toLocal8Bit ().data () << std::endl;
280
- query += column_types[n];
281
- std::cerr << query.toLocal8Bit ().data () << std::endl;
284
+ query += " " + column_types[n];
285
+
282
286
if (n<column_names.size ()-1 )
283
287
{
284
288
query += " , " ;
285
- std::cerr << query.toLocal8Bit ().data () << std::endl;
286
289
}
287
290
delete[] esc_str;
288
291
}
289
292
query += " )" ;
290
- std::cerr << query.toLocal8Bit ().data () << std::endl;
293
+
294
+ QgsDebugMsg (" Query string is: " + query);
291
295
292
296
PGresult *res = PQexec (conn, (const char *)query);
293
- qWarning (query);
297
+
294
298
if (PQresultStatus (res)!=PGRES_COMMAND_OK){
295
299
// flag error and send query and error message to stdout on debug
296
- result = false ;
297
- qWarning (PQresultErrorMessage (res));
300
+ errorText += tr (" The database gave an error while executing this SQL:" ) + " \n " ;
301
+ errorText += query + ' \n ' ;
302
+ errorText += tr (" The error was:" ) + " \n " ;
303
+ errorText += PQresultErrorMessage (res) + ' \n ' ;
304
+ PQclear (res);
305
+ return false ;
298
306
}
299
307
else {
300
308
PQclear (res);
301
309
}
302
310
303
- query = " SELECT AddGeometryColumn(\' " + schema + " \' , \' " + table_name + " \' , \' " +geom_col+" \' , " + srid +
304
- " , \' " + geom_type + " \' , 2)" ;
305
- if (result) res = PQexec (conn, (const char *)query);
311
+ query = " SELECT AddGeometryColumn(\' " + schema + " \' , \' " + table_name + " \' , \' " +
312
+ geom_col + " \' , " + srid + " , \' " + geom_type + " \' , 2)" ;
313
+
314
+ res = PQexec (conn, (const char *)query);
315
+
306
316
if (PQresultStatus (res)!=PGRES_TUPLES_OK){
307
- result = false ;
317
+ errorText += tr (" The database gave an error while executing this SQL:" ) + " \n " ;
318
+
319
+ errorText += query + ' \n ' ;
320
+ errorText += tr (" The error was:" ) + " \n " ;
321
+ errorText += PQresultErrorMessage (res) + ' \n ' ;
322
+ PQclear (res);
323
+ return false ;
308
324
}
309
325
else {
310
- qWarning (query);
311
- qWarning (PQresultErrorMessage (res));
312
326
PQclear (res);
313
327
}
328
+
314
329
if (isMulti)
315
330
{
316
331
// drop the check constraint
@@ -319,8 +334,19 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
319
334
// multiple types in the check constraint. For now, we
320
335
// just drop the constraint...
321
336
query = " alter table " + table_name + " drop constraint \" $2\" " ;
322
- // XXX tacky - we don't even check the result...
323
- PQexec (conn, (const char *)query);
337
+
338
+ res = PQexec (conn, (const char *)query);
339
+ if (PQresultStatus (res)!=PGRES_TUPLES_OK){
340
+ errorText += tr (" The database gave an error while executing this SQL:" ) + " \n " ;
341
+ errorText += query + ' \n ' ;
342
+ errorText += tr (" The error was:" ) + " \n " ;
343
+ errorText += PQresultErrorMessage (res) + ' \n ' ;
344
+ PQclear (res);
345
+ return false ;
346
+ }
347
+ else {
348
+ PQclear (res);
349
+ }
324
350
}
325
351
326
352
// adding the data into the table
@@ -334,7 +360,8 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
334
360
if (feat){
335
361
OGRGeometry *geom = feat->GetGeometryRef ();
336
362
if (geom){
337
- query = " INSERT INTO " +schema+" ." +table_name+QString (" VALUES( %1, " ).arg (m);
363
+ query = " INSERT INTO \" " + schema + " \" .\" " + table_name + " \" " +
364
+ QString (" VALUES( %1, " ).arg (m);
338
365
339
366
int num = geom->WkbSize ();
340
367
char * geo_temp = new char [num*3 ];
@@ -343,14 +370,22 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
343
370
344
371
QString quotes;
345
372
for (int n=0 ; n<column_types.size (); n++){
373
+ bool numericType (false );
346
374
if (column_types[n] == " int" || column_types[n] == " float" )
375
+ {
347
376
quotes = " " ;
377
+ numericType = true ;
378
+ }
348
379
else
349
380
quotes = " \' " ;
350
381
query += quotes;
351
382
352
- // escape the string value
383
+ // escape the string value and cope with blank data
353
384
QString val = codec->toUnicode (feat->GetFieldAsString (n));
385
+ if (val.isEmpty () && numericType)
386
+ {
387
+ val = " NULL" ;
388
+ }
354
389
val.replace (" '" , " ''" );
355
390
// char * esc_str = new char[val.length()*2+1];
356
391
// PQescapeString(esc_str, (const char *)val.lower().utf8(), val.length());
@@ -362,14 +397,24 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
362
397
// delete[] esc_str;
363
398
}
364
399
query += QString (" GeometryFromText(\' " )+geometry+QString (" \' , " )+srid+QString (" ))" );
365
- // std::cerr << query << std::endl;
366
400
367
401
if (result)
368
402
res = PQexec (conn, (const char *)query.utf8 ());
403
+
369
404
if (PQresultStatus (res)!=PGRES_COMMAND_OK){
370
405
// flag error and send query and error message to stdout on debug
371
406
result = false ;
372
- qWarning (PQresultErrorMessage (res));
407
+ errorText += tr (" The database gave an error while executing this SQL:" ) + " \n " ;
408
+ // the query string can be quite long. Trim if necessary...
409
+ if (query.count () > 100 )
410
+ errorText += query.left (150 ) +
411
+ tr (" ... (rest of SQL trimmed)" , " is appended to a truncated SQL statement" ) +
412
+ " \n " ;
413
+ else
414
+ errorText += query + ' \n ' ;
415
+ errorText += tr (" The error was:" ) + " \n " ;
416
+ errorText += PQresultErrorMessage (res);
417
+ errorText += ' \n ' ;
373
418
}
374
419
else {
375
420
PQclear (res);
0 commit comments