@@ -39,12 +39,7 @@ QgsSpatiaLiteSourceSelect::QgsSpatiaLiteSourceSelect( QgisApp * app, Qt::WFlags
39
39
QDialog( app, fl ), qgisApp( app )
40
40
{
41
41
setupUi ( this );
42
- mAddButton = new QPushButton ( tr ( " &Add" ) );
43
- buttonBox->addButton ( mAddButton , QDialogButtonBox::ActionRole );
44
- connect ( mAddButton , SIGNAL ( clicked () ), this , SLOT ( addClicked () ) );
45
- connect ( buttonBox, SIGNAL ( helpRequested () ), this , SLOT ( helpClicked () ) );
46
-
47
- mAddButton ->setEnabled ( false );
42
+ btnAdd->setEnabled ( false );
48
43
populateConnectionList ();
49
44
50
45
mSearchModeComboBox ->addItem ( tr ( " Wildcard" ) );
@@ -84,15 +79,15 @@ void QgsSpatiaLiteSourceSelect::on_btnDelete_clicked()
84
79
}
85
80
86
81
// Slot for performing action when the Add button is clicked
87
- void QgsSpatiaLiteSourceSelect::addClicked ()
82
+ void QgsSpatiaLiteSourceSelect::on_btnAdd_clicked ()
88
83
{
89
84
addTables ();
90
85
}
91
86
92
87
// Slot for showing help
93
- void QgsSpatiaLiteSourceSelect::helpClicked ()
88
+ void QgsSpatiaLiteSourceSelect::on_btnHelp_clicked ()
94
89
{
95
- QgsContextHelp::run ( context_id );
90
+ showHelp ( );
96
91
}
97
92
98
93
/* * End Autoconnected SLOTS **/
@@ -480,19 +475,19 @@ void QgsSpatiaLiteSourceSelect::on_btnConnect_clicked()
480
475
481
476
// BEGIN CHANGES ECOS
482
477
if ( cmbConnections->count () > 0 )
483
- mAddButton ->setEnabled ( true );
478
+ btnAdd ->setEnabled ( true );
484
479
// END CHANGES ECOS
485
480
486
481
mTablesTreeView ->sortByColumn ( 0 , Qt::AscendingOrder );
487
- mTablesTreeView ->header ()->resizeSection ( 1 , 140 );
488
- mTablesTreeView ->resizeColumnToContents ( 0 );
489
482
490
483
// expand all the toplevel items
491
484
int numTopLevelItems = mTableModel .invisibleRootItem ()->rowCount ();
492
485
for ( int i = 0 ; i < numTopLevelItems; ++i )
493
486
{
494
487
mTablesTreeView ->expand ( mProxyModel .mapFromSource ( mTableModel .indexFromItem ( mTableModel .invisibleRootItem ()->child ( i ) ) ) );
495
488
}
489
+ mTablesTreeView ->resizeColumnToContents ( 0 );
490
+ mTablesTreeView ->resizeColumnToContents ( 1 );
496
491
}
497
492
498
493
QStringList QgsSpatiaLiteSourceSelect::selectedTables ()
@@ -514,6 +509,7 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
514
509
int columns;
515
510
char *errMsg = NULL ;
516
511
bool ok = false ;
512
+ char sql[1024 ];
517
513
QApplication::setOverrideCursor ( Qt::WaitCursor );
518
514
519
515
// setting the SQLite DB name
@@ -522,8 +518,9 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
522
518
mTableModel .setSqliteDb ( myName );
523
519
524
520
// the following query return the tables containing a Geometry column
525
- ret = sqlite3_get_table ( handle,
526
- " SELECT f_table_name, f_geometry_column, type FROM geometry_columns" , &results, &rows, &columns, &errMsg );
521
+ strcpy ( sql, " SELECT f_table_name, f_geometry_column, type " );
522
+ strcat ( sql, " FROM geometry_columns" );
523
+ ret = sqlite3_get_table ( handle, sql, &results, &rows, &columns, &errMsg );
527
524
if ( ret != SQLITE_OK )
528
525
goto error;
529
526
if ( rows < 1 )
@@ -535,13 +532,70 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
535
532
QString tableName = results[( i * columns ) + 0 ];
536
533
QString column = results[( i * columns ) + 1 ];
537
534
QString type = results[( i * columns ) + 2 ];
535
+ if ( isDeclaredHidden ( handle, tableName, column ))
536
+ continue ;
538
537
539
538
mTableModel .addTableEntry ( type, tableName, column );
540
539
}
541
540
ok = true ;
542
541
}
543
542
sqlite3_free_table ( results );
544
543
544
+ if ( checkViewsGeometryColumns ( handle ) )
545
+ {
546
+ // the following query return the views supporting a Geometry column
547
+ strcpy ( sql, " SELECT view_name, view_geometry, type " );
548
+ strcat ( sql, " FROM views_geometry_columns " );
549
+ strcat ( sql, " JOIN geometry_columns USING (f_table_name, f_geometry_column)" );
550
+ ret = sqlite3_get_table ( handle, sql, &results, &rows, &columns, &errMsg );
551
+ if ( ret != SQLITE_OK )
552
+ goto error;
553
+ if ( rows < 1 )
554
+ ;
555
+ else
556
+ {
557
+ for ( i = 1 ; i <= rows; i++ )
558
+ {
559
+ QString tableName = results[( i * columns ) + 0 ];
560
+ QString column = results[( i * columns ) + 1 ];
561
+ QString type = results[( i * columns ) + 2 ];
562
+ if ( isDeclaredHidden ( handle, tableName, column ))
563
+ continue ;
564
+
565
+ mTableModel .addTableEntry ( type, tableName, column );
566
+ }
567
+ ok = true ;
568
+ }
569
+ sqlite3_free_table ( results );
570
+ }
571
+
572
+ if ( checkVirtsGeometryColumns ( handle ) )
573
+ {
574
+ // the following query return the VirtualShapefiles
575
+ strcpy ( sql, " SELECT virt_name, virt_geometry, type " );
576
+ strcat ( sql, " FROM virts_geometry_columns" );
577
+ ret = sqlite3_get_table ( handle, sql, &results, &rows, &columns, &errMsg );
578
+ if ( ret != SQLITE_OK )
579
+ goto error;
580
+ if ( rows < 1 )
581
+ ;
582
+ else
583
+ {
584
+ for ( i = 1 ; i <= rows; i++ )
585
+ {
586
+ QString tableName = results[( i * columns ) + 0 ];
587
+ QString column = results[( i * columns ) + 1 ];
588
+ QString type = results[( i * columns ) + 2 ];
589
+ if ( isDeclaredHidden ( handle, tableName, column ))
590
+ continue ;
591
+
592
+ mTableModel .addTableEntry ( type, tableName, column );
593
+ }
594
+ ok = true ;
595
+ }
596
+ sqlite3_free_table ( results );
597
+ }
598
+
545
599
QApplication::restoreOverrideCursor ();
546
600
return ok;
547
601
@@ -558,6 +612,169 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
558
612
return false ;
559
613
}
560
614
615
+ QString QgsSpatiaLiteSourceSelect::quotedValue ( QString value ) const
616
+ {
617
+ if ( value.isNull () )
618
+ return " NULL" ;
619
+
620
+ value.replace ( " '" , " ''" );
621
+ return value.prepend ( " '" ).append ( " '" );
622
+ }
623
+
624
+ bool QgsSpatiaLiteSourceSelect::checkGeometryColumnsAuth ( sqlite3 * handle )
625
+ {
626
+ int ret;
627
+ int i;
628
+ char **results;
629
+ int rows;
630
+ int columns;
631
+ bool exists = false ;
632
+
633
+ // checking the metadata tables
634
+ QString sql = QString ( " SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'geometry_columns_auth'" );
635
+
636
+ ret = sqlite3_get_table ( handle, sql.toUtf8 ().constData (), &results, &rows, &columns, NULL );
637
+ if ( ret != SQLITE_OK )
638
+ return false ;
639
+ if ( rows < 1 )
640
+ ;
641
+ else
642
+ {
643
+ for ( i = 1 ; i <= rows; i++ )
644
+ {
645
+ if ( results[( i * columns ) + 0 ] != NULL )
646
+ {
647
+ const char *name = results[( i * columns ) + 0 ];
648
+ if (name)
649
+ exists = true ;
650
+ }
651
+ }
652
+ }
653
+ sqlite3_free_table ( results );
654
+ return exists;
655
+ }
656
+
657
+ bool QgsSpatiaLiteSourceSelect::checkViewsGeometryColumns ( sqlite3 * handle )
658
+ {
659
+ int ret;
660
+ int i;
661
+ char **results;
662
+ int rows;
663
+ int columns;
664
+ bool exists = false ;
665
+
666
+ // checking the metadata tables
667
+ QString sql = QString ( " SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'views_geometry_columns'" );
668
+
669
+ ret = sqlite3_get_table ( handle, sql.toUtf8 ().constData (), &results, &rows, &columns, NULL );
670
+ if ( ret != SQLITE_OK )
671
+ return false ;
672
+ if ( rows < 1 )
673
+ ;
674
+ else
675
+ {
676
+ for ( i = 1 ; i <= rows; i++ )
677
+ {
678
+ if ( results[( i * columns ) + 0 ] != NULL )
679
+ {
680
+ const char *name = results[( i * columns ) + 0 ];
681
+ if (name)
682
+ exists = true ;
683
+ }
684
+ }
685
+ }
686
+ sqlite3_free_table ( results );
687
+ return exists;
688
+ }
689
+
690
+ bool QgsSpatiaLiteSourceSelect::checkVirtsGeometryColumns ( sqlite3 * handle )
691
+ {
692
+ int ret;
693
+ int i;
694
+ char **results;
695
+ int rows;
696
+ int columns;
697
+ bool exists = false ;
698
+
699
+ // checking the metadata tables
700
+ QString sql = QString ( " SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'virts_geometry_columns'" );
701
+
702
+ ret = sqlite3_get_table ( handle, sql.toUtf8 ().constData (), &results, &rows, &columns, NULL );
703
+ if ( ret != SQLITE_OK )
704
+ return false ;
705
+ if ( rows < 1 )
706
+ ;
707
+ else
708
+ {
709
+ for ( i = 1 ; i <= rows; i++ )
710
+ {
711
+ if ( results[( i * columns ) + 0 ] != NULL )
712
+ {
713
+ const char *name = results[( i * columns ) + 0 ];
714
+ if (name)
715
+ exists = true ;
716
+ }
717
+ }
718
+ }
719
+ sqlite3_free_table ( results );
720
+ return exists;
721
+ }
722
+
723
+ bool QgsSpatiaLiteSourceSelect::isDeclaredHidden ( sqlite3 * handle, QString table, QString geom )
724
+ {
725
+ int ret;
726
+ int i;
727
+ char **results;
728
+ int rows;
729
+ int columns;
730
+ char *errMsg = NULL ;
731
+ bool isHidden = false ;
732
+
733
+ if ( checkGeometryColumnsAuth ( handle ) == false )
734
+ return false ;
735
+ // checking if some Layer has been declared as HIDDEN
736
+ QString sql = QString ( " SELECT hidden FROM geometry_columns_auth"
737
+ " WHERE f_table_name=%1 and f_geometry_column=%2" ).arg ( quotedValue ( table ) ).
738
+ arg ( quotedValue ( geom ) );
739
+
740
+ ret = sqlite3_get_table ( handle, sql.toUtf8 ().constData (), &results, &rows, &columns, &errMsg );
741
+ if ( ret != SQLITE_OK )
742
+ goto error;
743
+ if ( rows < 1 )
744
+ ;
745
+ else
746
+ {
747
+ for ( i = 1 ; i <= rows; i++ )
748
+ {
749
+ if ( results[( i * columns ) + 0 ] != NULL )
750
+ {
751
+ if (atoi ( results[( i * columns ) + 0 ] ) != 0 )
752
+ isHidden = true ;
753
+ }
754
+ }
755
+ }
756
+ sqlite3_free_table ( results );
757
+
758
+ return isHidden;
759
+
760
+ error:
761
+ // unexpected IO error
762
+ QString errCause = tr ( " unknown error cause" );
763
+ if ( errMsg != NULL )
764
+ {
765
+ errCause = errMsg;
766
+ sqlite3_free ( errMsg );
767
+ }
768
+ QMessageBox::critical ( this , tr ( " SpatiaLite getTableInfo Error" ),
769
+ tr ( " Failure exploring tables from: %1\n\n %2" ).arg ( mSqlitePath ).arg ( errCause ) );
770
+ return false ;
771
+ }
772
+
773
+ void QgsSpatiaLiteSourceSelect::showHelp ()
774
+ {
775
+ QgsContextHelp::run ( context_id );
776
+ }
777
+
561
778
QString QgsSpatiaLiteSourceSelect::fullDescription ( QString table, QString column, QString type )
562
779
{
563
780
QString full_desc = " " ;
0 commit comments