Skip to content

Commit 890b092

Browse files
committed
[FEATURE] Improve SpatiaLite provider to manage non-spatial tables
1 parent 4d051b8 commit 890b092

File tree

4 files changed

+268
-264
lines changed

4 files changed

+268
-264
lines changed

src/app/spatialite/qgsspatialitesourceselect.cpp

+47-19
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ QgsSpatiaLiteSourceSelect::QgsSpatiaLiteSourceSelect( QgisApp * app, Qt::WFlags
9191
mSearchModeComboBox->setVisible( false );
9292
mSearchModeLabel->setVisible( false );
9393
mSearchTableEdit->setVisible( false );
94-
cbxAllowGeometrylessTables->setVisible( false );
94+
95+
cbxAllowGeometrylessTables->setDisabled( true );
9596
}
9697

9798
// Slot for performing action when the Add button is clicked
@@ -113,6 +114,11 @@ void QgsSpatiaLiteSourceSelect::buildQuery()
113114
setSql( mTablesTreeView->currentIndex() );
114115
}
115116

117+
void QgsSpatiaLiteSourceSelect::on_cbxAllowGeometrylessTables_stateChanged( int )
118+
{
119+
on_btnConnect_clicked();
120+
}
121+
116122
void QgsSpatiaLiteSourceSelect::on_mTablesTreeView_clicked( const QModelIndex &index )
117123
{
118124
mBuildQueryButton->setEnabled( index.parent().isValid() );
@@ -269,13 +275,8 @@ sqlite3 *QgsSpatiaLiteSourceSelect::openSpatiaLiteDb( QString path )
269275
if ( gcSpatiaLite && rsSpatiaLite )
270276
return handle;
271277

272-
// this one cannot be a valid SpatiaLite DB - no Spatial MetaData where found
273-
closeSpatiaLiteDb( handle );
274-
errCause = tr( "seems to be a valid SQLite DB, but not a SpatiaLite's one ..." );
275-
QMessageBox::critical( this, tr( "SpatiaLite DB Open Error" ),
276-
tr( "Failure while connecting to: %1\n\n%2" ).arg( mSqlitePath ).arg( errCause ) );
277-
mSqlitePath = "";
278-
return NULL;
278+
// this seems to be a valid SQLite DB, but not a SpatiaLite's one
279+
return handle;
279280

280281
error:
281282
// unexpected IO error
@@ -471,6 +472,8 @@ void QgsSpatiaLiteSourceSelect::on_btnConnect_clicked()
471472
{
472473
sqlite3 *handle;
473474

475+
cbxAllowGeometrylessTables->setEnabled( false );
476+
474477
QSettings settings;
475478
QString subKey = cmbConnections->currentText();
476479
int idx = subKey.indexOf( "@" );
@@ -517,6 +520,8 @@ void QgsSpatiaLiteSourceSelect::on_btnConnect_clicked()
517520
}
518521
mTablesTreeView->resizeColumnToContents( 0 );
519522
mTablesTreeView->resizeColumnToContents( 1 );
523+
524+
cbxAllowGeometrylessTables->setEnabled( true );
520525
}
521526

522527
QStringList QgsSpatiaLiteSourceSelect::selectedTables()
@@ -562,7 +567,7 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
562567
int columns;
563568
char *errMsg = NULL;
564569
bool ok = false;
565-
char sql[1024];
570+
QString sql;
566571
QApplication::setOverrideCursor( Qt::WaitCursor );
567572

568573
// setting the SQLite DB name
@@ -571,9 +576,9 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
571576
mTableModel.setSqliteDb( myName );
572577

573578
// the following query return the tables containing a Geometry column
574-
strcpy( sql, "SELECT f_table_name, f_geometry_column, type " );
575-
strcat( sql, "FROM geometry_columns" );
576-
ret = sqlite3_get_table( handle, sql, &results, &rows, &columns, &errMsg );
579+
sql = "SELECT f_table_name, f_geometry_column, type "
580+
"FROM geometry_columns";
581+
ret = sqlite3_get_table( handle, sql.toUtf8(), &results, &rows, &columns, &errMsg );
577582
if ( ret != SQLITE_OK )
578583
goto error;
579584
if ( rows < 1 )
@@ -599,10 +604,10 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
599604
if ( checkViewsGeometryColumns( handle ) )
600605
{
601606
// the following query return the views supporting a Geometry column
602-
strcpy( sql, "SELECT view_name, view_geometry, type " );
603-
strcat( sql, "FROM views_geometry_columns " );
604-
strcat( sql, "JOIN geometry_columns USING (f_table_name, f_geometry_column)" );
605-
ret = sqlite3_get_table( handle, sql, &results, &rows, &columns, &errMsg );
607+
sql = "SELECT view_name, view_geometry, type "
608+
"FROM views_geometry_columns "
609+
"JOIN geometry_columns USING (f_table_name, f_geometry_column)";
610+
ret = sqlite3_get_table( handle, sql.toUtf8(), &results, &rows, &columns, &errMsg );
606611
if ( ret != SQLITE_OK )
607612
goto error;
608613
if ( rows < 1 )
@@ -627,9 +632,9 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
627632
if ( checkVirtsGeometryColumns( handle ) )
628633
{
629634
// the following query return the VirtualShapefiles
630-
strcpy( sql, "SELECT virt_name, virt_geometry, type " );
631-
strcat( sql, "FROM virts_geometry_columns" );
632-
ret = sqlite3_get_table( handle, sql, &results, &rows, &columns, &errMsg );
635+
sql = "SELECT virt_name, virt_geometry, type "
636+
"FROM virts_geometry_columns";
637+
ret = sqlite3_get_table( handle, sql.toUtf8(), &results, &rows, &columns, &errMsg );
633638
if ( ret != SQLITE_OK )
634639
goto error;
635640
if ( rows < 1 )
@@ -651,6 +656,29 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
651656
sqlite3_free_table( results );
652657
}
653658

659+
if ( cbxAllowGeometrylessTables->isChecked() )
660+
{
661+
// get all tables
662+
sql = "SELECT name "
663+
"FROM sqlite_master "
664+
"WHERE type in ('table', 'view')";
665+
ret = sqlite3_get_table( handle, sql.toUtf8(), &results, &rows, &columns, &errMsg );
666+
if ( ret != SQLITE_OK )
667+
goto error;
668+
if ( rows < 1 )
669+
;
670+
else
671+
{
672+
for ( i = 1; i <= rows; i++ )
673+
{
674+
QString tableName = QString::fromUtf8( results[( i * columns ) + 0] );
675+
mTableModel.addTableEntry( tr( "No geometry" ), tableName, QString::null, "" );
676+
}
677+
ok = true;
678+
}
679+
sqlite3_free_table( results );
680+
}
681+
654682
QApplication::restoreOverrideCursor();
655683
return ok;
656684

src/app/spatialite/qgsspatialitesourceselect.h

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class QgsSpatiaLiteSourceSelect: public QDialog, private Ui::QgsDbSourceSelectBa
8585
void on_mSearchTableEdit_textChanged( const QString & text );
8686
void on_mSearchColumnComboBox_currentIndexChanged( const QString & text );
8787
void on_mSearchModeComboBox_currentIndexChanged( const QString & text );
88+
void on_cbxAllowGeometrylessTables_stateChanged( int );
8889
void setSql( const QModelIndex& index );
8990
void on_cmbConnections_activated( int );
9091
void setLayerType( QString table, QString column, QString type );

0 commit comments

Comments
 (0)