Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
esseffe committed Nov 17, 2009
1 parent 6ac2cee commit 2a25116
Show file tree
Hide file tree
Showing 13 changed files with 33,773 additions and 13,211 deletions.
245 changes: 231 additions & 14 deletions src/app/qgsspatialitesourceselect.cpp
Expand Up @@ -39,12 +39,7 @@ QgsSpatiaLiteSourceSelect::QgsSpatiaLiteSourceSelect( QgisApp * app, Qt::WFlags
QDialog( app, fl ), qgisApp( app )
{
setupUi( this );
mAddButton = new QPushButton( tr( "&Add" ) );
buttonBox->addButton( mAddButton, QDialogButtonBox::ActionRole );
connect( mAddButton, SIGNAL( clicked() ), this, SLOT( addClicked() ) );
connect( buttonBox, SIGNAL( helpRequested() ), this, SLOT( helpClicked() ) );

mAddButton->setEnabled( false );
btnAdd->setEnabled( false );
populateConnectionList();

mSearchModeComboBox->addItem( tr( "Wildcard" ) );
Expand Down Expand Up @@ -84,15 +79,15 @@ void QgsSpatiaLiteSourceSelect::on_btnDelete_clicked()
}

// Slot for performing action when the Add button is clicked
void QgsSpatiaLiteSourceSelect::addClicked()
void QgsSpatiaLiteSourceSelect::on_btnAdd_clicked()
{
addTables();
}

// Slot for showing help
void QgsSpatiaLiteSourceSelect::helpClicked()
void QgsSpatiaLiteSourceSelect::on_btnHelp_clicked()
{
QgsContextHelp::run( context_id );
showHelp();
}

/** End Autoconnected SLOTS **/
Expand Down Expand Up @@ -480,19 +475,19 @@ void QgsSpatiaLiteSourceSelect::on_btnConnect_clicked()

// BEGIN CHANGES ECOS
if ( cmbConnections->count() > 0 )
mAddButton->setEnabled( true );
btnAdd->setEnabled( true );
// END CHANGES ECOS

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

//expand all the toplevel items
int numTopLevelItems = mTableModel.invisibleRootItem()->rowCount();
for ( int i = 0; i < numTopLevelItems; ++i )
{
mTablesTreeView->expand( mProxyModel.mapFromSource( mTableModel.indexFromItem( mTableModel.invisibleRootItem()->child( i ) ) ) );
}
mTablesTreeView->resizeColumnToContents( 0 );
mTablesTreeView->resizeColumnToContents( 1 );
}

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

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

// the following query return the tables containing a Geometry column
ret = sqlite3_get_table( handle,
"SELECT f_table_name, f_geometry_column, type FROM geometry_columns", &results, &rows, &columns, &errMsg );
strcpy( sql, "SELECT f_table_name, f_geometry_column, type ");
strcat( sql, "FROM geometry_columns");
ret = sqlite3_get_table( handle, sql, &results, &rows, &columns, &errMsg );
if ( ret != SQLITE_OK )
goto error;
if ( rows < 1 )
Expand All @@ -535,13 +532,70 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
QString tableName = results[( i * columns ) + 0];
QString column = results[( i * columns ) + 1];
QString type = results[( i * columns ) + 2];
if ( isDeclaredHidden( handle, tableName, column ))
continue;

mTableModel.addTableEntry( type, tableName, column );
}
ok = true;
}
sqlite3_free_table( results );

if ( checkViewsGeometryColumns( handle ) )
{
// the following query return the views supporting a Geometry column
strcpy( sql, "SELECT view_name, view_geometry, type ");
strcat( sql, "FROM views_geometry_columns ");
strcat( sql, "JOIN geometry_columns USING (f_table_name, f_geometry_column)");
ret = sqlite3_get_table( handle, sql, &results, &rows, &columns, &errMsg );
if ( ret != SQLITE_OK )
goto error;
if ( rows < 1 )
;
else
{
for ( i = 1; i <= rows; i++ )
{
QString tableName = results[( i * columns ) + 0];
QString column = results[( i * columns ) + 1];
QString type = results[( i * columns ) + 2];
if ( isDeclaredHidden( handle, tableName, column ))
continue;

mTableModel.addTableEntry( type, tableName, column );
}
ok = true;
}
sqlite3_free_table( results );
}

if ( checkVirtsGeometryColumns( handle ) )
{
// the following query return the VirtualShapefiles
strcpy( sql, "SELECT virt_name, virt_geometry, type ");
strcat( sql, "FROM virts_geometry_columns");
ret = sqlite3_get_table( handle, sql, &results, &rows, &columns, &errMsg );
if ( ret != SQLITE_OK )
goto error;
if ( rows < 1 )
;
else
{
for ( i = 1; i <= rows; i++ )
{
QString tableName = results[( i * columns ) + 0];
QString column = results[( i * columns ) + 1];
QString type = results[( i * columns ) + 2];
if ( isDeclaredHidden( handle, tableName, column ))
continue;

mTableModel.addTableEntry( type, tableName, column );
}
ok = true;
}
sqlite3_free_table( results );
}

QApplication::restoreOverrideCursor();
return ok;

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

QString QgsSpatiaLiteSourceSelect::quotedValue( QString value ) const
{
if ( value.isNull() )
return "NULL";

value.replace( "'", "''" );
return value.prepend( "'" ).append( "'" );
}

bool QgsSpatiaLiteSourceSelect::checkGeometryColumnsAuth( sqlite3 * handle )
{
int ret;
int i;
char **results;
int rows;
int columns;
bool exists = false;

// checking the metadata tables
QString sql = QString( "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'geometry_columns_auth'" );

ret = sqlite3_get_table( handle, sql.toUtf8().constData(), &results, &rows, &columns, NULL );
if ( ret != SQLITE_OK )
return false;
if ( rows < 1 )
;
else
{
for ( i = 1; i <= rows; i++ )
{
if ( results[( i * columns ) + 0] != NULL)
{
const char *name = results[( i * columns ) + 0];
if (name)
exists = true;
}
}
}
sqlite3_free_table( results );
return exists;
}

bool QgsSpatiaLiteSourceSelect::checkViewsGeometryColumns( sqlite3 * handle )
{
int ret;
int i;
char **results;
int rows;
int columns;
bool exists = false;

// checking the metadata tables
QString sql = QString( "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'views_geometry_columns'" );

ret = sqlite3_get_table( handle, sql.toUtf8().constData(), &results, &rows, &columns, NULL );
if ( ret != SQLITE_OK )
return false;
if ( rows < 1 )
;
else
{
for ( i = 1; i <= rows; i++ )
{
if ( results[( i * columns ) + 0] != NULL)
{
const char *name = results[( i * columns ) + 0];
if (name)
exists = true;
}
}
}
sqlite3_free_table( results );
return exists;
}

bool QgsSpatiaLiteSourceSelect::checkVirtsGeometryColumns( sqlite3 * handle )
{
int ret;
int i;
char **results;
int rows;
int columns;
bool exists = false;

// checking the metadata tables
QString sql = QString( "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'virts_geometry_columns'" );

ret = sqlite3_get_table( handle, sql.toUtf8().constData(), &results, &rows, &columns, NULL );
if ( ret != SQLITE_OK )
return false;
if ( rows < 1 )
;
else
{
for ( i = 1; i <= rows; i++ )
{
if ( results[( i * columns ) + 0] != NULL)
{
const char *name = results[( i * columns ) + 0];
if (name)
exists = true;
}
}
}
sqlite3_free_table( results );
return exists;
}

bool QgsSpatiaLiteSourceSelect::isDeclaredHidden( sqlite3 * handle, QString table, QString geom )
{
int ret;
int i;
char **results;
int rows;
int columns;
char *errMsg = NULL;
bool isHidden = false;

if ( checkGeometryColumnsAuth( handle ) == false )
return false;
// checking if some Layer has been declared as HIDDEN
QString sql = QString( "SELECT hidden FROM geometry_columns_auth"
" WHERE f_table_name=%1 and f_geometry_column=%2" ).arg( quotedValue( table ) ).
arg( quotedValue( geom ) );

ret = sqlite3_get_table( handle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );
if ( ret != SQLITE_OK )
goto error;
if ( rows < 1 )
;
else
{
for ( i = 1; i <= rows; i++ )
{
if ( results[( i * columns ) + 0] != NULL)
{
if (atoi( results[( i * columns ) + 0] ) != 0)
isHidden = true;
}
}
}
sqlite3_free_table( results );

return isHidden;

error:
// unexpected IO error
QString errCause = tr( "unknown error cause" );
if ( errMsg != NULL )
{
errCause = errMsg;
sqlite3_free( errMsg );
}
QMessageBox::critical( this, tr( "SpatiaLite getTableInfo Error" ),
tr( "Failure exploring tables from: %1\n\n%2" ).arg( mSqlitePath ).arg( errCause ) );
return false;
}

void QgsSpatiaLiteSourceSelect::showHelp()
{
QgsContextHelp::run( context_id );
}

QString QgsSpatiaLiteSourceSelect::fullDescription( QString table, QString column, QString type )
{
QString full_desc = "";
Expand Down
23 changes: 19 additions & 4 deletions src/app/qgsspatialitesourceselect.h
Expand Up @@ -21,7 +21,6 @@
#include "qgisgui.h"
#include "qgsspatialitefilterproxymodel.h"
#include "qgsspatialitetablemodel.h"
#include <QPushButton>

extern "C"
{
Expand Down Expand Up @@ -81,14 +80,14 @@ class QgsSpatiaLiteSourceSelect: public QDialog, private Ui::QgsSpatiaLiteSource
* Once connected, available layers are displayed.
*/
void on_btnConnect_clicked();
void addClicked();
void on_btnAdd_clicked();
void on_btnNew_clicked();
void on_btnDelete_clicked();
void on_mSearchOptionsButton_clicked();
void on_mSearchTableEdit_textChanged( const QString & text );
void on_mSearchColumnComboBox_currentIndexChanged( const QString & text );
void on_mSearchModeComboBox_currentIndexChanged( const QString & text );
void helpClicked();
void on_btnHelp_clicked();
void on_cmbConnections_activated( int );
void setLayerType( QString table, QString column, QString type );
//!Sets a new regular expression to the model
Expand All @@ -106,6 +105,21 @@ class QgsSpatiaLiteSourceSelect: public QDialog, private Ui::QgsSpatiaLiteSource
typedef std::pair < QString, QString > geomPair;
typedef std::list < geomPair > geomCol;

/**Checks if geometry_columns_auth table exists*/
bool checkGeometryColumnsAuth( sqlite3 * handle );

/**Checks if views_geometry_columns table exists*/
bool checkViewsGeometryColumns( sqlite3 * handle );

/**Checks if virts_geometry_columns table exists*/
bool checkVirtsGeometryColumns( sqlite3 * handle );

/**Checks if this layer has been declared HIDDEN*/
bool isDeclaredHidden( sqlite3 * handle, QString table, QString geom );

/**cleaning well-formatted SQL strings*/
QString quotedValue( QString value ) const;

/**Inserts information about the spatial tables into mTableModel*/
bool getTableInfo( sqlite3 * handle );

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

#endif // QGSSPATIALITESOURCESELECT_H
2 changes: 1 addition & 1 deletion src/core/spatialite/headers/spatialite.h
@@ -1,7 +1,7 @@
/*
spatialite.h -- Gaia spatial support for SQLite
version 2.3, 2008 October 13
version 2.4, 2009 September 17
Author: Sandro Furieri a.furieri@lqt.it
Expand Down
2 changes: 1 addition & 1 deletion src/core/spatialite/headers/spatialite/gaiaaux.h
@@ -1,7 +1,7 @@
/*
gaiaaux.h -- Gaia common utility functions
version 2.3, 2008 October 13
version 2.4, 2009 September 17
Author: Sandro Furieri a.furieri@lqt.it
Expand Down

0 comments on commit 2a25116

Please sign in to comment.