@@ -181,7 +181,7 @@ QString QgsDbSourceSelect::makeGeomQuery(QString schema,
181
181
" when geometrytype(%1) IN ('LINESTRING','MULTILINESTRING') THEN 'LINESTRING'"
182
182
" when geometrytype(%1) IN ('POLYGON','MULTIPOLYGON') THEN 'POLYGON'"
183
183
" end "
184
- " from \" %2\" .\" %3\" " ).arg (column).arg (schema).arg (table);
184
+ " from \" %2\" .\" %3\" " ).arg (" \" " + column+ " \" " ).arg (schema).arg (table);
185
185
}
186
186
187
187
QgsDbSourceSelect::~QgsDbSourceSelect ()
@@ -361,11 +361,11 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
361
361
QString username = settings.readEntry (key + " /username" );
362
362
QString password = settings.readEntry (key + " /password" );
363
363
364
- if (password == QString::null )
364
+ if ( password. isEmpty () )
365
365
{
366
366
// get password from user
367
367
makeConnection = false ;
368
- QString password = QInputDialog::getText (tr (" Password for " ) + username,
368
+ password = QInputDialog::getText (tr (" Password for " ) + username,
369
369
tr (" Please enter your password:" ),
370
370
QLineEdit::Password, QString::null, &makeConnection, this );
371
371
// allow null password entry in case its valid for the database
@@ -393,13 +393,13 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
393
393
if (pd != 0 )
394
394
PQfinish (pd);
395
395
396
- pd = PQconnectdb (m_connInfo.toLocal8Bit (). data ());
396
+ pd = PQconnectdb (m_connInfo.toLocal8Bit ()); // use what is set based on locale; after connecting, use Utf8
397
397
// std::cout << pd->ErrorMessage();
398
398
if (PQstatus (pd) == CONNECTION_OK)
399
399
{
400
400
// qDebug("Connection succeeded");
401
401
// tell the DB that we want text encoded in UTF8
402
- PQsetClientEncoding (pd, " UNICODE" );
402
+ PQsetClientEncoding (pd, QString ( " UNICODE" ). toLocal8Bit () );
403
403
404
404
// get the list of suitable tables and columns and populate the UI
405
405
geomCol details;
@@ -434,7 +434,7 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
434
434
QMessageBox::warning (this , tr (" Connection failed" ),
435
435
tr
436
436
(" Connection to %1 on %2 failed. Either the database is down or your settings are incorrect.%3Check your username and password and try again.%4The database said:%5%6" ).
437
- arg (settings.readEntry (key + " /database" )).arg (settings.readEntry (key + " /host" )).arg (" \n\n " ).arg (" \n\n " ).arg (" \n " ).arg (PQerrorMessage (pd)));
437
+ arg (settings.readEntry (key + " /database" )).arg (settings.readEntry (key + " /host" )).arg (" \n\n " ).arg (" \n\n " ).arg (" \n " ).arg (QString::fromLocal8Bit ( PQerrorMessage (pd) )));
438
438
}
439
439
}
440
440
@@ -526,43 +526,33 @@ bool QgsDbSourceSelect::getTableInfo(PGconn *pg, bool searchGeometryColumnsOnly,
526
526
bool ok = false ;
527
527
QApplication::setOverrideCursor (Qt::waitCursor);
528
528
529
- QString sql = " select * from geometry_columns" ;
530
- sql += " order by f_table_schema,f_table_name" ;
531
-
532
- PGresult *result = PQexec (pg, sql.toLocal8Bit ().data ());
529
+ // The following query returns only tables that exist and the user has SELECT privilege on.
530
+ // Can't use regclass here because table must exist, else error occurs.
531
+ QString sql = " select * from geometry_columns,pg_class,pg_namespace "
532
+ " where relname=f_table_name and f_table_schema=nspname "
533
+ " and pg_namespace.oid = pg_class.relnamespace "
534
+ " and has_table_privilege('\" '||pg_namespace.nspname||'\" .\" '||pg_class.relname||'\" ','select')" // user has select privilege
535
+ " order by f_table_schema,f_table_name" ;
536
+
537
+ PGresult *result = PQexec (pg, sql.toUtf8 ());
533
538
if (result)
534
539
{
535
540
for (int idx = 0 ; idx < PQntuples (result); idx++)
536
541
{
537
- // Be a bit paranoid and check that the table actually
538
- // exists. This is not done as a subquery in the query above
539
- // because I can't get it to work correctly when there are tables
540
- // with capital letters in the name.
542
+ QString tableName = QString::fromUtf8 (PQgetvalue (result, idx, PQfnumber (result, QString (" f_table_name" ).toUtf8 ())));
543
+ QString schemaName = QString::fromUtf8 (PQgetvalue (result, idx, PQfnumber (result, QString (" f_table_schema" ).toUtf8 ())));
541
544
542
- // Take care to deal with tables with the same name but in different schema.
543
- QString tableName = PQgetvalue (result, idx, PQfnumber (result, " f_table_name" ));
544
- QString schemaName = PQgetvalue (result, idx, PQfnumber (result, " f_table_schema" ));
545
- sql = " select oid from pg_class where relname = '" + tableName + " '" ;
546
- if (schemaName.length () > 0 )
547
- sql +=" and relnamespace = (select oid from pg_namespace where nspname = '" +
548
- schemaName + " ')" ;
545
+ QString column = QString::fromUtf8 (PQgetvalue (result, idx, PQfnumber (result, QString (" f_geometry_column" ).toUtf8 ())));
546
+ QString type = QString::fromUtf8 (PQgetvalue (result, idx, PQfnumber (result, QString (" type" ).toUtf8 ())));
549
547
550
- PGresult* exists = PQexec (pg, sql. toLocal8Bit (). data ()) ;
551
- if ( PQntuples (exists) == 1 )
548
+ QString as = " " ;
549
+ if (type== " GEOMETRY " && !searchGeometryColumnsOnly)
552
550
{
553
- QString column = PQgetvalue (result, idx, PQfnumber (result, " f_geometry_column" ));
554
- QString type = PQgetvalue (result, idx, PQfnumber (result, " type" ));
555
-
556
- QString as = " " ;
557
- if (type==" GEOMETRY" && !searchGeometryColumnsOnly)
558
- {
559
- addSearchGeometryColumn (schemaName, tableName, column);
560
- as=type=" WAITING" ;
561
- }
562
-
563
- mTableModel .addTableEntry (type, schemaName, tableName, column, " " );
551
+ addSearchGeometryColumn (schemaName, tableName, column);
552
+ as=type=" WAITING" ;
564
553
}
565
- PQclear (exists);
554
+
555
+ mTableModel .addTableEntry (type, schemaName, tableName, column, " " );
566
556
}
567
557
ok = true ;
568
558
}
@@ -581,20 +571,21 @@ bool QgsDbSourceSelect::getTableInfo(PGconn *pg, bool searchGeometryColumnsOnly,
581
571
// geometry_columns table. This code is specific to postgresql,
582
572
// but an equivalent query should be possible in other
583
573
// databases.
584
- sql = " select pg_class.relname, pg_namespace.nspname, pg_attribute.attname, "
585
- " pg_class.relkind from "
586
- " pg_attribute, pg_class, pg_type, pg_namespace where pg_type.typname = 'geometry' and "
587
- " pg_attribute.atttypid = pg_type.oid and pg_attribute.attrelid = pg_class.oid " ;
588
-
574
+ sql = " select pg_class.relname, pg_namespace.nspname, pg_attribute.attname, pg_class.relkind "
575
+ " from pg_attribute, pg_class, pg_namespace "
576
+ " where pg_namespace.oid = pg_class.relnamespace "
577
+ " and pg_attribute.atttypid = regtype('geometry') "
578
+ " and pg_attribute.attrelid = pg_class.oid "
579
+ " and has_table_privilege('\" '||pg_namespace.nspname||'\" .\" '||pg_class.relname||'\" ','select') " ;
580
+ // user has select privilege
589
581
if (searchPublicOnly)
590
582
sql += " and pg_namespace.nspname = 'public' " ;
591
583
592
- sql += " and cast(pg_class.relname as character varying) not in "
593
- " (select f_table_name from geometry_columns) "
594
- " and pg_namespace.oid = pg_class.relnamespace "
584
+ sql += " and pg_namespace.nspname||'.'||pg_class.relname not in " // needs to be table and schema
585
+ " (select f_table_schema||'.'||f_table_name from geometry_columns) "
595
586
" and pg_class.relkind in ('v', 'r')" ; // only from views and relations (tables)
596
587
597
- result = PQexec (pg, sql.toLocal8Bit (). data ());
588
+ result = PQexec (pg, sql.toUtf8 ());
598
589
599
590
for (int i = 0 ; i < PQntuples (result); i++)
600
591
{
@@ -605,10 +596,10 @@ bool QgsDbSourceSelect::getTableInfo(PGconn *pg, bool searchGeometryColumnsOnly,
605
596
// Make the assumption that the geometry type for the first
606
597
// row is the same as for all other rows.
607
598
608
- QString table = PQgetvalue (result, i, 0 ); // relname
609
- QString schema = PQgetvalue (result, i, 1 ); // nspname
610
- QString column = PQgetvalue (result, i, 2 ); // attname
611
- QString relkind = PQgetvalue (result, i, 3 ); // relation kind
599
+ QString table = QString::fromUtf8 ( PQgetvalue (result, i, 0 ) ); // relname
600
+ QString schema = QString::fromUtf8 ( PQgetvalue (result, i, 1 ) ); // nspname
601
+ QString column = QString::fromUtf8 ( PQgetvalue (result, i, 2 ) ); // attname
602
+ QString relkind = QString::fromUtf8 ( PQgetvalue (result, i, 3 ) ); // relation kind
612
603
613
604
addSearchGeometryColumn (schema, table, column);
614
605
// details.push_back(geomPair(fullDescription(schema, table, column, "WAITING"), "WAITING"));
@@ -620,6 +611,7 @@ bool QgsDbSourceSelect::getTableInfo(PGconn *pg, bool searchGeometryColumnsOnly,
620
611
return ok;
621
612
}
622
613
614
+ #if 0 // this function is never called - smizuno
623
615
bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
624
616
geomCol& details, bool searchGeometryColumnsOnly,
625
617
bool searchPublicOnly)
@@ -632,7 +624,7 @@ bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
632
624
// where f_table_schema ='" + settings.readEntry(key + "/database") + "'";
633
625
sql += " order by f_table_schema,f_table_name";
634
626
//qDebug("Fetching tables using: " + sql);
635
- PGresult *result = PQexec (pg, sql.toLocal8Bit (). data ());
627
+ PGresult *result = PQexec(pg, sql.toUtf8 ());
636
628
if (result)
637
629
{
638
630
QString msg;
@@ -646,18 +638,18 @@ bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
646
638
// with capital letters in the name.
647
639
648
640
// Take care to deal with tables with the same name but in different schema.
649
- QString tableName = PQgetvalue (result, idx, PQfnumber (result, " f_table_name" ));
650
- QString schemaName = PQgetvalue (result, idx, PQfnumber (result, " f_table_schema" ));
641
+ QString tableName = QString::fromUtf8( PQgetvalue(result, idx, PQfnumber(result, "f_table_name") ));
642
+ QString schemaName = QString::fromUtf8( PQgetvalue(result, idx, PQfnumber(result, "f_table_schema") ));
651
643
sql = "select oid from pg_class where relname = '" + tableName + "'";
652
644
if (schemaName.length() > 0)
653
645
sql +=" and relnamespace = (select oid from pg_namespace where nspname = '" +
654
646
schemaName + "')";
655
647
656
- PGresult* exists = PQexec (pg, sql.toLocal8Bit (). data ());
648
+ PGresult* exists = PQexec(pg, sql.toUtf8 ());
657
649
if (PQntuples(exists) == 1)
658
650
{
659
- QString column = PQgetvalue (result, idx, PQfnumber (result, " f_geometry_column" ));
660
- QString type = PQgetvalue (result, idx, PQfnumber (result, " type" ));
651
+ QString column = QString::fromUtf8( PQgetvalue(result, idx, PQfnumber(result, "f_geometry_column") ));
652
+ QString type = QString::fromUtf8( PQgetvalue(result, idx, PQfnumber(result, "type") ));
661
653
662
654
QString as = "";
663
655
if(type=="GEOMETRY" && !searchGeometryColumnsOnly) {
@@ -695,7 +687,7 @@ bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
695
687
"and pg_namespace.oid = pg_class.relnamespace "
696
688
"and pg_class.relkind in ('v', 'r')"; // only from views and relations (tables)
697
689
698
- result = PQexec (pg, sql.toLocal8Bit (). data ());
690
+ result = PQexec(pg, sql.toUtf8 ());
699
691
700
692
for (int i = 0; i < PQntuples(result); i++)
701
693
{
@@ -706,10 +698,10 @@ bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
706
698
// Make the assumption that the geometry type for the first
707
699
// row is the same as for all other rows.
708
700
709
- QString table = PQgetvalue (result, i, 0 ); // relname
710
- QString schema = PQgetvalue (result, i, 1 ); // nspname
711
- QString column = PQgetvalue (result, i, 2 ); // attname
712
- QString relkind = PQgetvalue (result, i, 3 ); // relation kind
701
+ QString table = QString::fromUtf8( PQgetvalue(result, i, 0) ); // relname
702
+ QString schema = QString::fromUtf8( PQgetvalue(result, i, 1) ); // nspname
703
+ QString column = QString::fromUtf8( PQgetvalue(result, i, 2) ); // attname
704
+ QString relkind = QString::fromUtf8( PQgetvalue(result, i, 3) ); // relation kind
713
705
714
706
addSearchGeometryColumn(schema, table, column);
715
707
details.push_back(geomPair(fullDescription(schema, table, column, "WAITING"), "WAITING"));
@@ -720,6 +712,7 @@ bool QgsDbSourceSelect::getGeometryColumnInfo(PGconn *pg,
720
712
721
713
return ok;
722
714
}
715
+ #endif
723
716
724
717
void QgsDbSourceSelect::showHelp ()
725
718
{
@@ -800,23 +793,23 @@ void QgsGeomColumnTypeThread::getLayerTypes()
800
793
{
801
794
mStopped =false ;
802
795
803
- PGconn *pd = PQconnectdb (mConnInfo .toLocal8Bit (). data () );
796
+ PGconn *pd = PQconnectdb (mConnInfo .toLocal8Bit ());
804
797
if (PQstatus (pd) == CONNECTION_OK)
805
798
{
806
- PQsetClientEncoding (pd, " UNICODE" );
799
+ PQsetClientEncoding (pd, QString ( " UNICODE" ). toLocal8Bit () );
807
800
808
801
for (uint i = 0 ; i<schemas.size (); i++)
809
802
{
810
803
QString query = QgsDbSourceSelect::makeGeomQuery (schemas[i],
811
804
tables[i],
812
805
columns[i]);
813
- PGresult* gresult = PQexec (pd, query.toLocal8Bit (). data ());
806
+ PGresult* gresult = PQexec (pd, query.toUtf8 ());
814
807
QString type;
815
808
if (PQresultStatus (gresult) == PGRES_TUPLES_OK) {
816
809
QStringList types;
817
810
818
811
for (int j=0 ; j<PQntuples (gresult); j++) {
819
- QString type = PQgetvalue (gresult, j, 0 );
812
+ QString type = QString::fromUtf8 ( PQgetvalue (gresult, j, 0 ) );
820
813
if (type!=" " )
821
814
types += type;
822
815
}
0 commit comments