36
36
#include " qgis.h"
37
37
#include " qgslogger.h"
38
38
39
+ #include " qgspgutil.h"
40
+
39
41
// for htonl
40
42
#ifdef WIN32
41
43
#include < winsock.h>
@@ -60,7 +62,7 @@ QgsShapeFile::QgsShapeFile(QString name, QString encoding){
60
62
// init the geometry types
61
63
geometries << " NULL" << " POINT" << " LINESTRING" << " POLYGON" << " MULTPOINT"
62
64
<< " MULTILINESTRING" << " MULTIPOLYGON" << " GEOMETRYCOLLECTION" ;
63
-
65
+
64
66
codec = QTextCodec::codecForName (encoding.toLocal8Bit ().data ());
65
67
if (!codec)
66
68
codec = QTextCodec::codecForLocale ();
@@ -219,7 +221,7 @@ QString QgsShapeFile::getFeatureClass(){
219
221
int numFields = OGR_F_GetFieldCount (feat);
220
222
for (int n=0 ; n<numFields; n++)
221
223
{
222
- QString s = codec->toUnicode (OGR_Fld_GetNameRef (OGR_F_GetFieldDefnRef (feat,n)));
224
+ QString s = codec->toUnicode (OGR_Fld_GetNameRef (OGR_F_GetFieldDefnRef (feat,n)));
223
225
column_names.push_back (s);
224
226
}
225
227
@@ -263,41 +265,30 @@ void QgsShapeFile::setColumnNames(QStringList columns)
263
265
}
264
266
}
265
267
266
- bool QgsShapeFile::insertLayer (QString dbname, QString schema, QString geom_col,
268
+ bool QgsShapeFile::insertLayer (QString dbname, QString schema, QString primary_key, QString geom_col,
267
269
QString srid, PGconn * conn, QProgressDialog& pro, bool &fin,
268
270
QString& errorText)
269
271
{
270
272
connect (&pro, SIGNAL (canceled ()), this , SLOT (cancelImport ()));
271
273
import_canceled = false ;
272
274
bool result = true ;
273
- // Mangle the table name to make it PG compliant by replacing spaces with
274
- // underscores
275
- table_name = table_name.replace (" " ," _" );
276
-
277
- QString query = " CREATE TABLE " +schema+" ." +table_name+" (gid int4 PRIMARY KEY, " ;
278
275
279
- for (uint n=0 ; n<column_names.size () && result; n++){
280
- if (!column_names[n][0 ].isLetter ())
281
- result = false ;
276
+ QString query = QString (" CREATE TABLE %1.%2(%3 int4 PRIMARY KEY" )
277
+ .arg ( QgsPgUtil::quotedIdentifier (schema) )
278
+ .arg ( QgsPgUtil::quotedIdentifier (table_name) )
279
+ .arg ( QgsPgUtil::quotedIdentifier (primary_key) );
282
280
283
- char * esc_str = new char [column_names[n].length ()*2 +1 ];
284
-
285
- PQescapeString (esc_str, (const char *)column_names[n].lower (), column_names[n].length ());
286
- QgsDebugMsg (" Escaped " + column_names[n] + " to " + QString (esc_str));
287
- query += esc_str;
288
- query += " " + column_types[n];
289
-
290
- if (n<column_names.size ()-1 )
291
- {
292
- query += " , " ;
293
- }
294
- delete[] esc_str;
281
+ for (uint n=0 ; n<column_names.size () && result; n++)
282
+ {
283
+ query += QString (" ,%1 %2" )
284
+ .arg ( QgsPgUtil::quotedIdentifier (column_names[n]) )
285
+ .arg ( column_types[n] );
295
286
}
296
287
query += " )" ;
297
288
298
289
QgsDebugMsg (" Query string is: " + query);
299
290
300
- PGresult *res = PQexec (conn, ( const char *)query );
291
+ PGresult *res = PQexec (conn, query. toUtf8 () );
301
292
302
293
if (PQresultStatus (res)!=PGRES_COMMAND_OK){
303
294
// flag error and send query and error message to stdout on debug
@@ -312,10 +303,14 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
312
303
PQclear (res);
313
304
}
314
305
315
- query = " SELECT AddGeometryColumn(\' " + schema + " \' , \' " + table_name + " \' , \' " +
316
- geom_col + " \' , " + srid + " , \' " + geom_type + " \' , 2)" ;
306
+ query = QString (" SELECT AddGeometryColumn(%1,%2,%3,%4,%5,2)" )
307
+ .arg ( QgsPgUtil::quotedValue ( schema ) )
308
+ .arg ( QgsPgUtil::quotedValue ( table_name ) )
309
+ .arg ( QgsPgUtil::quotedValue ( geom_col ) )
310
+ .arg ( srid )
311
+ .arg ( QgsPgUtil::quotedValue ( geom_type ) );
317
312
318
- res = PQexec (conn, ( const char *)query );
313
+ res = PQexec (conn, query. toUtf8 () );
319
314
320
315
if (PQresultStatus (res)!=PGRES_TUPLES_OK){
321
316
errorText += tr (" The database gave an error while executing this SQL:" ) + " \n " ;
@@ -332,15 +327,16 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
332
327
333
328
if (isMulti)
334
329
{
335
- query = QString (" select constraint_name from information_schema.table_constraints where table_schema='%1' and table_name='%2' and constraint_name in ('$2','enforce_geotype_the_geom')" )
336
- .arg ( schema ).arg ( table_name );
330
+ query = QString (" select constraint_name from information_schema.table_constraints where table_schema=%1 and table_name=%2 and constraint_name in ('$2','enforce_geotype_the_geom')" )
331
+ .arg ( QgsPgUtil::quotedValue (schema) )
332
+ .arg ( QgsPgUtil::quotedValue (table_name) );
337
333
338
334
QStringList constraints;
339
- res = PQexec ( conn, query );
335
+ res = PQexec (conn, query. toUtf8 () );
340
336
if ( PQresultStatus ( res ) == PGRES_TUPLES_OK )
341
337
{
342
338
for (int i=0 ; i<PQntuples (res); i++)
343
- constraints.append ( PQgetvalue (res, i, 0 ) );
339
+ constraints.append ( PQgetvalue (res, i, 0 ) );
344
340
}
345
341
PQclear (res);
346
342
@@ -350,9 +346,11 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
350
346
// convert the geometries to the same type or allow
351
347
// multiple types in the check constraint. For now, we
352
348
// just drop the constraint...
353
- query = " alter table " + table_name + " drop constraint \" " + constraints[0 ] + " \" " ;
349
+ query = QString (" alter table %1 drop constraint %2" )
350
+ .arg ( QgsPgUtil::quotedIdentifier (table_name) )
351
+ .arg ( QgsPgUtil::quotedIdentifier (constraints[0 ]) );
354
352
355
- res = PQexec (conn, ( const char *)query );
353
+ res = PQexec (conn, query. toUtf8 () );
356
354
if (PQresultStatus (res)!=PGRES_COMMAND_OK) {
357
355
errorText += tr (" The database gave an error while executing this SQL:" ) + " \n " ;
358
356
errorText += query + ' \n ' ;
@@ -368,8 +366,10 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
368
366
}
369
367
370
368
// adding the data into the table
371
- for (int m=0 ;m<features && result; m++){
372
- if (import_canceled){
369
+ for (int m=0 ; m<features && result; m++)
370
+ {
371
+ if (import_canceled)
372
+ {
373
373
fin = true ;
374
374
break ;
375
375
}
@@ -378,8 +378,10 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
378
378
if (feat){
379
379
OGRGeometryH geom = OGR_F_GetGeometryRef (feat);
380
380
if (geom){
381
- query = " INSERT INTO \" " + schema + " \" .\" " + table_name + " \" " +
382
- QString (" VALUES( %1, " ).arg (m);
381
+ query = QString (" INSERT INTO %1.%2 VALUES(%3" )
382
+ .arg ( QgsPgUtil::quotedIdentifier (schema) )
383
+ .arg ( QgsPgUtil::quotedIdentifier (table_name) )
384
+ .arg ( m );
383
385
384
386
int num = OGR_G_WkbSize (geom);
385
387
char * geo_temp = new char [num*3 ];
@@ -390,38 +392,28 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
390
392
OGR_G_ExportToWkt (geom,&geo_temp);
391
393
QString geometry (geo_temp);
392
394
393
- QString quotes;
394
- for (uint n=0 ; n<column_types.size (); n++){
395
- bool numericType (false );
396
- if (column_types[n] == " int" || column_types[n] == " float" )
397
- {
398
- quotes = " " ;
399
- numericType = true ;
400
- }
395
+ for (uint n=0 ; n<column_types.size (); n++) {
396
+ QString val;
397
+
398
+ // FIXME: OGR_F_GetFieldAsString returns junk when called with a 8.255 float field
399
+ if ( column_types[n] == " float" )
400
+ val = QString::number ( OGR_F_GetFieldAsDouble (feat,n) );
401
401
else
402
- quotes = " \' " ;
403
- query += quotes;
402
+ val = codec->toUnicode (OGR_F_GetFieldAsString (feat,n) );
404
403
405
- // escape the string value and cope with blank data
406
- QString val = codec->toUnicode (OGR_F_GetFieldAsString (feat,n));
407
- if (val.isEmpty () && numericType)
408
- {
404
+ if ( val.isEmpty () )
409
405
val = " NULL" ;
410
- }
411
- val.replace (" '" , " ''" );
412
- // char * esc_str = new char[val.length()*2+1];
413
- // PQescapeString(esc_str, (const char *)val.lower().utf8(), val.length());
414
-
415
- // add escaped value to the query
416
- query += val; // esc_str;
417
- query += QString (quotes + " , " );
406
+ else
407
+ val = QgsPgUtil::quotedValue ( val );
418
408
419
- // delete[] esc_str ;
409
+ query += " , " + val ;
420
410
}
421
- query += QString (" GeometryFromText(\' " )+geometry+QString (" \' , " )+srid+QString (" ))" );
411
+ query += QString (" ,GeometryFromText(%1,%2))" )
412
+ .arg ( QgsPgUtil::quotedValue ( geometry ) )
413
+ .arg ( srid );
422
414
423
415
if (result)
424
- res = PQexec (conn, ( const char *) query.utf8 ());
416
+ res = PQexec (conn, query.utf8 () );
425
417
426
418
if (PQresultStatus (res)!=PGRES_COMMAND_OK){
427
419
// flag error and send query and error message to stdout on debug
0 commit comments