Skip to content

Commit 2a25116

Browse files
author
esseffe
committed
upgraded SpatiaLite provider:
- supporting SpatiaLite 2.4.0 [Release Candidate] - supporting SQLite 3.6.20 [introducing foreign key constraints] git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12163 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 6ac2cee commit 2a25116

13 files changed

+33773
-13211
lines changed

src/app/qgsspatialitesourceselect.cpp

+231-14
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ QgsSpatiaLiteSourceSelect::QgsSpatiaLiteSourceSelect( QgisApp * app, Qt::WFlags
3939
QDialog( app, fl ), qgisApp( app )
4040
{
4141
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 );
4843
populateConnectionList();
4944

5045
mSearchModeComboBox->addItem( tr( "Wildcard" ) );
@@ -84,15 +79,15 @@ void QgsSpatiaLiteSourceSelect::on_btnDelete_clicked()
8479
}
8580

8681
// Slot for performing action when the Add button is clicked
87-
void QgsSpatiaLiteSourceSelect::addClicked()
82+
void QgsSpatiaLiteSourceSelect::on_btnAdd_clicked()
8883
{
8984
addTables();
9085
}
9186

9287
// Slot for showing help
93-
void QgsSpatiaLiteSourceSelect::helpClicked()
88+
void QgsSpatiaLiteSourceSelect::on_btnHelp_clicked()
9489
{
95-
QgsContextHelp::run( context_id );
90+
showHelp();
9691
}
9792

9893
/** End Autoconnected SLOTS **/
@@ -480,19 +475,19 @@ void QgsSpatiaLiteSourceSelect::on_btnConnect_clicked()
480475

481476
// BEGIN CHANGES ECOS
482477
if ( cmbConnections->count() > 0 )
483-
mAddButton->setEnabled( true );
478+
btnAdd->setEnabled( true );
484479
// END CHANGES ECOS
485480

486481
mTablesTreeView->sortByColumn( 0, Qt::AscendingOrder );
487-
mTablesTreeView->header()->resizeSection( 1, 140 );
488-
mTablesTreeView->resizeColumnToContents( 0 );
489482

490483
//expand all the toplevel items
491484
int numTopLevelItems = mTableModel.invisibleRootItem()->rowCount();
492485
for ( int i = 0; i < numTopLevelItems; ++i )
493486
{
494487
mTablesTreeView->expand( mProxyModel.mapFromSource( mTableModel.indexFromItem( mTableModel.invisibleRootItem()->child( i ) ) ) );
495488
}
489+
mTablesTreeView->resizeColumnToContents( 0 );
490+
mTablesTreeView->resizeColumnToContents( 1 );
496491
}
497492

498493
QStringList QgsSpatiaLiteSourceSelect::selectedTables()
@@ -514,6 +509,7 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
514509
int columns;
515510
char *errMsg = NULL;
516511
bool ok = false;
512+
char sql[1024];
517513
QApplication::setOverrideCursor( Qt::WaitCursor );
518514

519515
// setting the SQLite DB name
@@ -522,8 +518,9 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
522518
mTableModel.setSqliteDb( myName );
523519

524520
// 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 );
527524
if ( ret != SQLITE_OK )
528525
goto error;
529526
if ( rows < 1 )
@@ -535,13 +532,70 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
535532
QString tableName = results[( i * columns ) + 0];
536533
QString column = results[( i * columns ) + 1];
537534
QString type = results[( i * columns ) + 2];
535+
if ( isDeclaredHidden( handle, tableName, column ))
536+
continue;
538537

539538
mTableModel.addTableEntry( type, tableName, column );
540539
}
541540
ok = true;
542541
}
543542
sqlite3_free_table( results );
544543

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+
545599
QApplication::restoreOverrideCursor();
546600
return ok;
547601

@@ -558,6 +612,169 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
558612
return false;
559613
}
560614

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+
561778
QString QgsSpatiaLiteSourceSelect::fullDescription( QString table, QString column, QString type )
562779
{
563780
QString full_desc = "";

src/app/qgsspatialitesourceselect.h

+19-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "qgisgui.h"
2222
#include "qgsspatialitefilterproxymodel.h"
2323
#include "qgsspatialitetablemodel.h"
24-
#include <QPushButton>
2524

2625
extern "C"
2726
{
@@ -81,14 +80,14 @@ class QgsSpatiaLiteSourceSelect: public QDialog, private Ui::QgsSpatiaLiteSource
8180
* Once connected, available layers are displayed.
8281
*/
8382
void on_btnConnect_clicked();
84-
void addClicked();
83+
void on_btnAdd_clicked();
8584
void on_btnNew_clicked();
8685
void on_btnDelete_clicked();
8786
void on_mSearchOptionsButton_clicked();
8887
void on_mSearchTableEdit_textChanged( const QString & text );
8988
void on_mSearchColumnComboBox_currentIndexChanged( const QString & text );
9089
void on_mSearchModeComboBox_currentIndexChanged( const QString & text );
91-
void helpClicked();
90+
void on_btnHelp_clicked();
9291
void on_cmbConnections_activated( int );
9392
void setLayerType( QString table, QString column, QString type );
9493
//!Sets a new regular expression to the model
@@ -106,6 +105,21 @@ class QgsSpatiaLiteSourceSelect: public QDialog, private Ui::QgsSpatiaLiteSource
106105
typedef std::pair < QString, QString > geomPair;
107106
typedef std::list < geomPair > geomCol;
108107

108+
/**Checks if geometry_columns_auth table exists*/
109+
bool checkGeometryColumnsAuth( sqlite3 * handle );
110+
111+
/**Checks if views_geometry_columns table exists*/
112+
bool checkViewsGeometryColumns( sqlite3 * handle );
113+
114+
/**Checks if virts_geometry_columns table exists*/
115+
bool checkVirtsGeometryColumns( sqlite3 * handle );
116+
117+
/**Checks if this layer has been declared HIDDEN*/
118+
bool isDeclaredHidden( sqlite3 * handle, QString table, QString geom );
119+
120+
/**cleaning well-formatted SQL strings*/
121+
QString quotedValue( QString value ) const;
122+
109123
/**Inserts information about the spatial tables into mTableModel*/
110124
bool getTableInfo( sqlite3 * handle );
111125

@@ -116,6 +130,8 @@ class QgsSpatiaLiteSourceSelect: public QDialog, private Ui::QgsSpatiaLiteSource
116130
// Set the position of the database connection list to the last
117131
// used one.
118132
void setConnectionListPosition();
133+
// Show the context help for the dialog
134+
void showHelp();
119135
// Combine the table and column data into a single string
120136
// useful for display to the user
121137
QString fullDescription( QString table, QString column, QString type );
@@ -131,7 +147,6 @@ class QgsSpatiaLiteSourceSelect: public QDialog, private Ui::QgsSpatiaLiteSource
131147
//! Model that acts as datasource for mTableTreeWidget
132148
QgsSpatiaLiteTableModel mTableModel;
133149
QgsSpatiaLiteFilterProxyModel mProxyModel;
134-
QPushButton * mAddButton;
135150
};
136151

137152
#endif // QGSSPATIALITESOURCESELECT_H

src/core/spatialite/headers/spatialite.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
spatialite.h -- Gaia spatial support for SQLite
33
4-
version 2.3, 2008 October 13
4+
version 2.4, 2009 September 17
55
66
Author: Sandro Furieri a.furieri@lqt.it
77

src/core/spatialite/headers/spatialite/gaiaaux.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
gaiaaux.h -- Gaia common utility functions
33
4-
version 2.3, 2008 October 13
4+
version 2.4, 2009 September 17
55
66
Author: Sandro Furieri a.furieri@lqt.it
77

0 commit comments

Comments
 (0)