@@ -436,7 +436,7 @@ QgsFeature *QgsPostgresProvider::getNextFeature(bool fetchAttributes)
436
436
437
437
// get the value of the primary key based on type
438
438
439
- int oid = *(int *)PQgetvalue (queryResult, row, PQfnumber (queryResult,primaryKey));
439
+ int oid = *(int *)PQgetvalue (queryResult, row, PQfnumber (queryResult," \" " + primaryKey+ " \" " ));
440
440
#ifdef QGISDEBUG
441
441
// std::cerr << "OID from database: " << oid << std::endl;
442
442
#endif
@@ -508,7 +508,7 @@ QgsFeature* QgsPostgresProvider::getNextFeature(std::list<int> const & attlist,
508
508
queryResult = PQexec (connection, (const char *)fetch);
509
509
510
510
int rows = PQntuples (queryResult);
511
-
511
+
512
512
if (rows == 0 )
513
513
{
514
514
#ifdef QGISDEBUG
@@ -521,7 +521,7 @@ QgsFeature* QgsPostgresProvider::getNextFeature(std::list<int> const & attlist,
521
521
522
522
for (int row = 0 ; row < rows; row++)
523
523
{
524
- int oid = *(int *)PQgetvalue (queryResult, row, PQfnumber (queryResult,primaryKey));
524
+ int oid = *(int *)PQgetvalue (queryResult, row, PQfnumber (queryResult," \" " + primaryKey+ " \" " ));
525
525
#ifdef QGISDEBUG
526
526
// std::cerr << "Primary key type is " << primaryKeyType << std::endl;
527
527
#endif
@@ -601,9 +601,9 @@ void QgsPostgresProvider::select(QgsRect * rect, bool useIntersect)
601
601
std::cerr << " Selection polygon is " << rect->asPolygon ().toLocal8Bit ().data () << std::endl;
602
602
#endif
603
603
604
- QString declare = QString (" declare qgisf binary cursor for select "
604
+ QString declare = QString (" declare qgisf binary cursor for select \" "
605
605
+ primaryKey
606
- + " ,asbinary(%1 ,'%2') as qgs_feature_geometry from %3" ).arg (geometryColumn).arg (endianString ()).arg (mSchemaTableName );
606
+ + " \" ,asbinary(\" %1 \" ,'%2') as qgs_feature_geometry from %3" ).arg (geometryColumn).arg (endianString ()).arg (mSchemaTableName );
607
607
#ifdef QGISDEBUG
608
608
std::cout << " Binary cursor: " << declare.toLocal8Bit ().data () << std::endl;
609
609
#endif
@@ -727,7 +727,7 @@ int QgsPostgresProvider::fieldCount() const
727
727
*/
728
728
void QgsPostgresProvider::getFeatureAttributes (int key, int &row, QgsFeature *f) {
729
729
730
- QString sql = QString (" select * from %1 where %2 = %3" ).arg (mSchemaTableName ).arg (primaryKey).arg (key);
730
+ QString sql = QString (" select * from %1 where \" %2 \" = %3" ).arg (mSchemaTableName ).arg (primaryKey).arg (key);
731
731
732
732
#ifdef QGISDEBUG
733
733
std::cerr << " QgsPostgresProvider::getFeatureAttributes using: " << sql.toLocal8Bit ().data () << std::endl;
@@ -756,7 +756,7 @@ void QgsPostgresProvider::getFeatureAttributes(int key, int &row,
756
756
std::list<int >::const_iterator iter;
757
757
for (iter=attlist.begin ();iter!=attlist.end ();++iter)
758
758
{
759
- QString sql = QString (" select %1 from %2 where %3 = %4" )
759
+ QString sql = QString (" select \" %1 \" from %2 where \" %3 \" = %4" )
760
760
.arg (fields ()[*iter].name ())
761
761
.arg (mSchemaTableName )
762
762
.arg (primaryKey)
@@ -786,7 +786,7 @@ void QgsPostgresProvider::getFeatureGeometry(int key, QgsFeature *f)
786
786
}
787
787
788
788
QString cursor = QString (" declare qgisf binary cursor for "
789
- " select asbinary(%1 ,'%2') from %3 where %4 = %5" )
789
+ " select asbinary(\" %1 \" ,'%2') from %3 where \" %4 \" = %5" )
790
790
.arg (geometryColumn)
791
791
.arg (endianString ())
792
792
.arg (mSchemaTableName )
@@ -842,9 +842,9 @@ void QgsPostgresProvider::reset()
842
842
{
843
843
// reset the cursor to the first record
844
844
// --std::cout << "Resetting the cursor to the first record " << std::endl;
845
- QString declare = QString (" declare qgisf binary cursor for select " +
845
+ QString declare = QString (" declare qgisf binary cursor for select \" " +
846
846
primaryKey +
847
- " ,asbinary(%1 ,'%2') as qgs_feature_geometry from %3" ).arg (geometryColumn)
847
+ " \" ,asbinary(\" %1 \" ,'%2') as qgs_feature_geometry from %3" ).arg (geometryColumn)
848
848
.arg (endianString ()).arg (mSchemaTableName );
849
849
if (sqlWhereClause.length () > 0 )
850
850
{
@@ -1028,7 +1028,6 @@ QString QgsPostgresProvider::getPrimaryKey()
1028
1028
}
1029
1029
else
1030
1030
{
1031
- std::cerr << col.local8Bit ().data () << ' \n ' ;
1032
1031
sql = " select attname from pg_attribute, pg_type where "
1033
1032
" atttypid = pg_type.oid and attnum in (" +
1034
1033
col.replace (" " , " ," )
@@ -1284,7 +1283,7 @@ QString QgsPostgresProvider::chooseViewColumn(const tableCols& cols)
1284
1283
}
1285
1284
else
1286
1285
{
1287
- log << QString (tr (" Note: " ) + " '" + i->first + " '"
1286
+ log << QString (tr (" Note: " ) + " '" + i->first + " ' "
1288
1287
+ tr (" initially appeared suitable but does not "
1289
1288
" contain unique data, so is not suitable.\n " ));
1290
1289
}
@@ -1320,11 +1319,12 @@ bool QgsPostgresProvider::uniqueData(QString schemaName,
1320
1319
1321
1320
bool isUnique = false ;
1322
1321
1323
- QString sql = " select count(distinct " + colName + " ) = count(" +
1324
- colName + " ) from \" " + schemaName + " \" .\" " + tableName + " \" " ;
1322
+ QString sql = " select count(distinct \" " + colName + " \" ) = count(\" " +
1323
+ colName + " \" ) from \" " + schemaName + " \" .\" " + tableName + " \" " ;
1325
1324
1326
1325
PGresult* unique = PQexec (connection, (const char *) (sql.utf8 ()));
1327
- if (PQntuples (unique) == 1 )
1326
+
1327
+ if (PQntuples (unique) == 1 )
1328
1328
if (strncmp (PQgetvalue (unique, 0 , 0 )," t" , 1 ) == 0 )
1329
1329
isUnique = true ;
1330
1330
@@ -1342,7 +1342,47 @@ void QgsPostgresProvider::findColumns(tableCols& cols)
1342
1342
// This sql is derived from the one that defines the view
1343
1343
// 'information_schema.view_column_usage' in PostgreSQL, with a few
1344
1344
// mods to suit our purposes.
1345
- QString sql = " SELECT DISTINCT nv.nspname AS view_schema, v.relname AS view_name, a.attname AS view_column_name, nt.nspname AS table_schema, t.relname AS table_name, a.attname AS column_name, t.relkind as table_type, typ.typname as column_type FROM pg_namespace nv, pg_class v, pg_depend dv, pg_depend dt, pg_class t, pg_namespace nt, pg_attribute a, pg_user u, pg_type typ WHERE nv.oid = v.relnamespace AND v.relkind = 'v'::\" char\" AND v.oid = dv.refobjid AND dv.refclassid = 'pg_class'::regclass::oid AND dv.classid = 'pg_rewrite'::regclass::oid AND dv.deptype = 'i'::\" char\" AND dv.objid = dt.objid AND dv.refobjid <> dt.refobjid AND dt.classid = 'pg_rewrite'::regclass::oid AND dt.refclassid = 'pg_class'::regclass::oid AND dt.refobjid = t.oid AND t.relnamespace = nt.oid AND (t.relkind = 'r'::\" char\" OR t.relkind = 'v'::\" char\" ) AND t.oid = a.attrelid AND dt.refobjsubid = a.attnum AND nv.nspname NOT IN ('pg_catalog', 'information_schema' ) AND a.atttypid = typ.oid" ;
1345
+ QString sql = " "
1346
+ " SELECT DISTINCT "
1347
+ " nv.nspname AS view_schema, "
1348
+ " v.relname AS view_name, "
1349
+ " va.attname AS view_column_name, "
1350
+ " nt.nspname AS table_schema, "
1351
+ " t.relname AS table_name, "
1352
+ " a.attname AS column_name, "
1353
+ " t.relkind as table_type, "
1354
+ " typ.typname as column_type "
1355
+ " FROM "
1356
+ " pg_namespace nv, "
1357
+ " pg_class v, "
1358
+ " pg_depend dv,"
1359
+ " pg_depend dt, "
1360
+ " pg_class t, "
1361
+ " pg_namespace nt, "
1362
+ " pg_attribute a,"
1363
+ " pg_attribute va,"
1364
+ " pg_user u, "
1365
+ " pg_type typ "
1366
+ " WHERE "
1367
+ " nv.oid = v.relnamespace AND "
1368
+ " v.relkind = 'v'::\" char\" AND "
1369
+ " v.oid = dv.refobjid AND "
1370
+ " dv.refclassid = 'pg_class'::regclass::oid AND "
1371
+ " dv.classid = 'pg_rewrite'::regclass::oid AND "
1372
+ " dv.deptype = 'i'::\" char\" AND "
1373
+ " dv.objid = dt.objid AND "
1374
+ " dv.refobjid <> dt.refobjid AND "
1375
+ " dt.classid = 'pg_rewrite'::regclass::oid AND "
1376
+ " dt.refclassid = 'pg_class'::regclass::oid AND "
1377
+ " dt.refobjid = t.oid AND "
1378
+ " t.relnamespace = nt.oid AND "
1379
+ " (t.relkind = 'r'::\" char\" OR t.relkind = 'v'::\" char\" ) AND "
1380
+ " t.oid = a.attrelid AND "
1381
+ " dt.refobjsubid = a.attnum AND "
1382
+ " nv.nspname NOT IN ('pg_catalog', 'information_schema' ) AND "
1383
+ " a.atttypid = typ.oid AND "
1384
+ " v.oid = va.attrelid AND "
1385
+ " va.attnum = dt.refobjsubid" ;
1346
1386
1347
1387
// A structure to store the results of the above sql.
1348
1388
typedef std::map<QString, TT> columnRelationsType;
@@ -1396,6 +1436,7 @@ void QgsPostgresProvider::findColumns(tableCols& cols)
1396
1436
{
1397
1437
columnRelationsType::const_iterator
1398
1438
ii = columnRelations.find (PQgetvalue (result, i, 0 ));
1439
+ columnRelationsType::const_iterator start_iter = ii;
1399
1440
1400
1441
if (ii == columnRelations.end ())
1401
1442
continue ;
@@ -1439,11 +1480,12 @@ void QgsPostgresProvider::findColumns(tableCols& cols)
1439
1480
<< ii->second .table_name .local8Bit ().data () << ' .'
1440
1481
<< ii->second .column_name .local8Bit ().data () << " failed: exceeded maximum "
1441
1482
<< " interation limit (" << max_loops << " ).\n " ;
1483
+
1442
1484
cols[ii->second .view_column_name ] = SRC (" " ," " ," " ," " );
1443
1485
}
1444
1486
else if (ii != columnRelations.end ())
1445
1487
{
1446
- cols[ii ->second .view_column_name ] =
1488
+ cols[start_iter ->second .view_column_name ] =
1447
1489
SRC (ii->second .table_schema ,
1448
1490
ii->second .table_name ,
1449
1491
ii->second .column_name ,
@@ -1467,11 +1509,11 @@ QString QgsPostgresProvider::minValue(int position){
1467
1509
QString sql;
1468
1510
if (sqlWhereClause.isEmpty ())
1469
1511
{
1470
- sql = QString (" select min(%1 ) from %2" ).arg (fld.name ()).arg (mSchemaTableName );
1512
+ sql = QString (" select min(\" %1 \" ) from %2" ).arg (fld.name ()).arg (mSchemaTableName );
1471
1513
}
1472
1514
else
1473
1515
{
1474
- sql = QString (" select min(%1 ) from %2" ).arg (fld.name ()).arg (mSchemaTableName )+" where " +sqlWhereClause;
1516
+ sql = QString (" select min(\" %1 \" ) from %2" ).arg (fld.name ()).arg (mSchemaTableName )+" where " +sqlWhereClause;
1475
1517
}
1476
1518
PGresult *rmin = PQexec (connection,(const char *)(sql.utf8 ()));
1477
1519
QString minValue = PQgetvalue (rmin,0 ,0 );
@@ -1487,11 +1529,11 @@ QString QgsPostgresProvider::maxValue(int position){
1487
1529
QString sql;
1488
1530
if (sqlWhereClause.isEmpty ())
1489
1531
{
1490
- sql = QString (" select max(%1 ) from %2" ).arg (fld.name ()).arg (mSchemaTableName );
1532
+ sql = QString (" select max(\" %1 \" ) from %2" ).arg (fld.name ()).arg (mSchemaTableName );
1491
1533
}
1492
1534
else
1493
1535
{
1494
- sql = QString (" select max(%1 ) from %2" ).arg (fld.name ()).arg (mSchemaTableName )+" where " +sqlWhereClause;
1536
+ sql = QString (" select max(\" %1 \" ) from %2" ).arg (fld.name ()).arg (mSchemaTableName )+" where " +sqlWhereClause;
1495
1537
}
1496
1538
PGresult *rmax = PQexec (connection,(const char *)(sql.utf8 ()));
1497
1539
QString maxValue = PQgetvalue (rmax,0 ,0 );
@@ -1504,7 +1546,7 @@ int QgsPostgresProvider::maxPrimaryKeyValue()
1504
1546
{
1505
1547
QString sql;
1506
1548
1507
- sql = QString (" select max(%1 ) from %2" )
1549
+ sql = QString (" select max(\" %1 \" ) from %2" )
1508
1550
.arg (primaryKey)
1509
1551
.arg (mSchemaTableName );
1510
1552
@@ -1557,11 +1599,11 @@ bool QgsPostgresProvider::addFeature(QgsFeature* f, int primaryKeyHighWater)
1557
1599
insert+=" (" ;
1558
1600
1559
1601
// add the name of the geometry column to the insert statement
1560
- insert += geometryColumn;
1602
+ insert += " \" " + geometryColumn;
1561
1603
1562
1604
// add the name of the primary key column to the insert statement
1563
- insert += " , " ;
1564
- insert += primaryKey;
1605
+ insert += " \" , \" " ;
1606
+ insert += primaryKey + " \" " ;
1565
1607
1566
1608
#ifdef QGISDEBUG
1567
1609
std::cout << " QgsPostgresProvider::addFeature: Constructing insert SQL, currently at: " << insert.toLocal8Bit ().data ()
@@ -1611,8 +1653,8 @@ bool QgsPostgresProvider::addFeature(QgsFeature* f, int primaryKeyHighWater)
1611
1653
(fieldInLayer)
1612
1654
)
1613
1655
{
1614
- insert+=" ," ;
1615
- insert+=fieldname;
1656
+ insert+=" ,\" " ;
1657
+ insert+=fieldname + " \" " ;
1616
1658
}
1617
1659
}
1618
1660
@@ -1819,7 +1861,7 @@ QString QgsPostgresProvider::getDefaultValue(const QString& attr, QgsFeature* f)
1819
1861
1820
1862
bool QgsPostgresProvider::deleteFeature (int id)
1821
1863
{
1822
- QString sql (" DELETE FROM " +mSchemaTableName +" WHERE " +primaryKey+" = " +QString::number (id));
1864
+ QString sql (" DELETE FROM " +mSchemaTableName +" WHERE \" " +primaryKey+" \ " = " +QString::number (id));
1823
1865
#ifdef QGISDEBUG
1824
1866
qWarning (" delete sql: " +sql);
1825
1867
#endif
@@ -2005,7 +2047,7 @@ bool QgsPostgresProvider::changeAttributeValues(std::map<int,std::map<QString,QS
2005
2047
{
2006
2048
for (std::map<QString,QString>::const_iterator siter=(*iter).second .begin ();siter!=(*iter).second .end ();++siter)
2007
2049
{
2008
- QString sql=" UPDATE " +mSchemaTableName +" SET " +(*siter).first +" ='" +(*siter).second +" ' WHERE " +primaryKey+" =" +QString::number ((*iter).first );
2050
+ QString sql=" UPDATE " +mSchemaTableName +" SET " +(*siter).first +" ='" +(*siter).second +" ' WHERE \" " +primaryKey+" \ " =" +QString::number ((*iter).first );
2009
2051
#ifdef QGISDEBUG
2010
2052
qWarning (sql);
2011
2053
#endif
@@ -2095,8 +2137,8 @@ bool QgsPostgresProvider::changeGeometryValues(std::map<int, QgsGeometry> & geom
2095
2137
<< std::endl;
2096
2138
#endif
2097
2139
2098
- QString sql = " UPDATE " + mSchemaTableName +" SET " +
2099
- geometryColumn + " =" ;
2140
+ QString sql = " UPDATE " + mSchemaTableName +" SET \" " +
2141
+ geometryColumn + " \" =" ;
2100
2142
2101
2143
sql += " GeomFromWKB('" ;
2102
2144
@@ -2147,7 +2189,7 @@ bool QgsPostgresProvider::changeGeometryValues(std::map<int, QgsGeometry> & geom
2147
2189
sql += " ::bytea'," +srid+" )" ;
2148
2190
}
2149
2191
2150
- sql += " WHERE " +primaryKey+" =" +QString::number (iter->first );
2192
+ sql += " WHERE \" " +primaryKey+" \ " =" +QString::number (iter->first );
2151
2193
2152
2194
#ifdef QGISDEBUG
2153
2195
std::cerr << " QgsPostgresProvider::changeGeometryValues: Updating with '"
@@ -2389,7 +2431,7 @@ long QgsPostgresProvider::getFeatureCount()
2389
2431
std::cerr << " QgsPostgresProvider: Running SQL: " <<
2390
2432
sql << std::endl;
2391
2433
#else
2392
- QString sql = " select count(*) from " + mSchemaTableName ;
2434
+ QString sql = " select count(*) from " + mSchemaTableName + " " ;
2393
2435
2394
2436
if (sqlWhereClause.length () > 0 )
2395
2437
{
@@ -2435,11 +2477,11 @@ void QgsPostgresProvider::calculateExtents()
2435
2477
2436
2478
2437
2479
#if WASTE_TIME
2438
- sql = " select xmax(extent(" + geometryColumn + " )) as xmax,"
2439
- " xmin(extent(" + geometryColumn + " )) as xmin,"
2440
- " ymax(extent(" + geometryColumn + " )) as ymax,"
2441
- " ymin(extent(" + geometryColumn + " )) as ymin"
2442
- " from " + mSchemaTableName + " " ;
2480
+ sql = " select xmax(extent(\" " + geometryColumn + " \ " )) as xmax,"
2481
+ " xmin(extent(\" " + geometryColumn + " \ " )) as xmin,"
2482
+ " ymax(extent(\" " + geometryColumn + " \ " )) as ymax,"
2483
+ " ymin(extent(\" " + geometryColumn + " \ " )) as ymin"
2484
+ " from " + mSchemaTableName ;
2443
2485
#endif
2444
2486
2445
2487
#ifdef QGISDEBUG
@@ -2492,18 +2534,18 @@ void QgsPostgresProvider::calculateExtents()
2492
2534
2493
2535
// get the extents
2494
2536
2495
- QString sql = " select extent(" + geometryColumn + " ) from " +
2537
+ QString sql = " select extent(\" " + geometryColumn + " \ " ) from " +
2496
2538
mSchemaTableName ;
2497
2539
if (sqlWhereClause.length () > 0 )
2498
2540
{
2499
2541
sql += " where " + sqlWhereClause;
2500
2542
}
2501
2543
2502
2544
#if WASTE_TIME
2503
- sql = " select xmax(extent(" + geometryColumn + " )) as xmax,"
2504
- " xmin(extent(" + geometryColumn + " )) as xmin,"
2505
- " ymax(extent(" + geometryColumn + " )) as ymax,"
2506
- " ymin(extent(" + geometryColumn + " )) as ymin"
2545
+ sql = " select xmax(extent(\" " + geometryColumn + " \ " )) as xmax,"
2546
+ " xmin(extent(\" " + geometryColumn + " \ " )) as xmin,"
2547
+ " ymax(extent(\" " + geometryColumn + " \ " )) as ymax,"
2548
+ " ymin(extent(\" " + geometryColumn + " \ " )) as ymin"
2507
2549
" from " + mSchemaTableName ;
2508
2550
#endif
2509
2551
@@ -2698,8 +2740,8 @@ bool QgsPostgresProvider::getGeometryDetails()
2698
2740
// fail if there is no data in the relevant table.
2699
2741
PQclear (result); // for the query just before the if() statement
2700
2742
sql = " select "
2701
- " srid(" + geometryColumn + " ), "
2702
- " geometrytype(" + geometryColumn + " ) from " +
2743
+ " srid(\" " + geometryColumn + " \ " ), "
2744
+ " geometrytype(\" " + geometryColumn + " \ " ) from " +
2703
2745
mSchemaTableName + " limit 1" ;
2704
2746
2705
2747
result = executeDbCommand (connection, sql);
0 commit comments