|
@@ -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" ) ); |
|
@@ -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 **/ |
|
@@ -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() |
|
@@ -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 |
|
@@ -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 ) |
|
@@ -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; |
|
|
|
|
@@ -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 = ""; |
|
|