Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bump minimum spatialite version to 4.2
  • Loading branch information
m-kuhn committed Nov 16, 2017
1 parent 4420eba commit bb6fda6
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 119 deletions.
10 changes: 0 additions & 10 deletions CMakeLists.txt
Expand Up @@ -250,16 +250,6 @@ IF(WITH_CORE)

FIND_PACKAGE(SpatiaLite REQUIRED)

IF(SPATIALITE_VERSION_GE_4_0_0)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSPATIALITE_VERSION_GE_4_0_0")
ENDIF(SPATIALITE_VERSION_GE_4_0_0)
IF(SPATIALITE_VERSION_G_4_1_1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSPATIALITE_VERSION_G_4_1_1")
ENDIF(SPATIALITE_VERSION_G_4_1_1)
IF(SPATIALITE_HAS_INIT_EX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSPATIALITE_HAS_INIT_EX")
ENDIF(SPATIALITE_HAS_INIT_EX)

IF (NOT PROJ_FOUND OR NOT GEOS_FOUND OR NOT GDAL_FOUND)
MESSAGE (SEND_ERROR "Some dependencies were not found! Proj: ${PROJ_FOUND}, Geos: ${GEOS_FOUND}, GDAL: ${GDAL_FOUND}")
ENDIF (NOT PROJ_FOUND OR NOT GEOS_FOUND OR NOT GDAL_FOUND)
Expand Down
9 changes: 5 additions & 4 deletions cmake/FindSpatiaLite.cmake
Expand Up @@ -65,14 +65,15 @@ IF (SPATIALITE_FOUND)
MESSAGE(STATUS "Found SpatiaLite: ${SPATIALITE_LIBRARY}")
ENDIF (NOT SPATIALITE_FIND_QUIETLY)

# Check for symbol gaiaDropTable
IF(APPLE)
# no extra LDFLAGS used in link test, may fail in OS X SDK
SET(CMAKE_REQUIRED_LIBRARIES "-F/Library/Frameworks" ${CMAKE_REQUIRED_LIBRARIES})
ENDIF(APPLE)
check_library_exists("${SPATIALITE_LIBRARY}" gaiaDropTable "" SPATIALITE_VERSION_GE_4_0_0)
check_library_exists("${SPATIALITE_LIBRARY}" gaiaStatisticsInvalidate "" SPATIALITE_VERSION_G_4_1_1)
check_library_exists("${SPATIALITE_LIBRARY}" spatialite_init_ex "" SPATIALITE_HAS_INIT_EX)

check_library_exists("${SPATIALITE_LIBRARY}" gaiaStatisticsInvalidate "" SPATIALITE_VERSION_GE_4_2_0)
IF (NOT SPATIALITE_VERSION_GE_4_2_0)
MESSAGE(FATAL_ERROR "Found SpatiaLite, but version is too old. Requires at least version 4.2.0")
ENDIF (NOT SPATIALITE_VERSION_GE_4_2_0)

ELSE (SPATIALITE_FOUND)

Expand Down
2 changes: 1 addition & 1 deletion doc/overview.t2t
Expand Up @@ -19,7 +19,7 @@ Required build dependencies:
- Proj >= 4.4.x
- GEOS >= 3.4
- Sqlite3 >= 3.0.0
- SpatiaLite
- SpatiaLite >= 4.2.0
- libspatialindex
- GDAL/OGR >= 2.1
- Qwt >= 5.0 & (< 6.1 with internal QwtPolar)
Expand Down
16 changes: 1 addition & 15 deletions src/core/qgsspatialiteutils.cpp
Expand Up @@ -24,21 +24,15 @@

int spatialite_database_unique_ptr::open( const QString &path )
{
#if defined(SPATIALITE_HAS_INIT_EX)
auto &deleter = get_deleter();
deleter.mSpatialiteContext = spatialite_alloc_connection();
#else
spatialite_init( 0 );
#endif

sqlite3 *database = nullptr;
int result = sqlite3_open( path.toUtf8(), &database );
std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset( database );

#if defined(SPATIALITE_HAS_INIT_EX)
if ( result == SQLITE_OK )
spatialite_init_ex( database, deleter.mSpatialiteContext, 0 );
#endif

return result;
}
Expand All @@ -50,21 +44,15 @@ void spatialite_database_unique_ptr::reset()

int spatialite_database_unique_ptr::open_v2( const QString &path, int flags, const char *zVfs )
{
#if defined(SPATIALITE_HAS_INIT_EX)
auto &deleter = get_deleter();
deleter.mSpatialiteContext = spatialite_alloc_connection();
#else
spatialite_init( 0 );
#endif

sqlite3 *database = nullptr;
int result = sqlite3_open_v2( path.toUtf8(), &database, flags, zVfs );
std::unique_ptr< sqlite3, QgsSpatialiteCloser>::reset( database );

#if defined(SPATIALITE_HAS_INIT_EX)
if ( result == SQLITE_OK )
spatialite_init_ex( database, deleter.mSpatialiteContext, 0 );
#endif

return result;
}
Expand Down Expand Up @@ -92,9 +80,7 @@ void QgsSpatialiteCloser::operator()( sqlite3 *handle )
{
QgsDebugMsg( QStringLiteral( "sqlite3_close() failed: %1" ).arg( res ) );
}
#if defined(SPATIALITE_HAS_INIT_EX)

spatialite_cleanup_ex( mSpatialiteContext );
mSpatialiteContext = nullptr;
#endif

}
27 changes: 0 additions & 27 deletions src/providers/spatialite/qgsspatialiteconnection.cpp
Expand Up @@ -77,27 +77,7 @@ QgsSpatiaLiteConnection::Error QgsSpatiaLiteConnection::fetchTables( bool loadGe
return FailedToCheckMetadata;
}

bool recentVersion = false;
#ifdef SPATIALITE_VERSION_GE_4_0_0
// only if libspatialite version is >= 4.0.0
recentVersion = true;
#endif

if ( ret == LayoutCurrent && !recentVersion )
{
// obsolete library version
mErrorMsg = tr( "obsolete libspatialite: connecting to this DB requires using v.4.0 (or any subsequent)" );
return FailedToCheckMetadata;
}

#ifdef SPATIALITE_VERSION_GE_4_0_0
// only if libspatialite version is >= 4.0.0
// using v.4.0 Abstract Interface
if ( !getTableInfoAbstractInterface( database.get(), loadGeometrylessTables ) )
#else
// obsolete library: still using the traditional approach
if ( !getTableInfo( database.get(), loadGeometrylessTables ) )
#endif
{
return FailedToGetTables;
}
Expand All @@ -107,7 +87,6 @@ QgsSpatiaLiteConnection::Error QgsSpatiaLiteConnection::fetchTables( bool loadGe

bool QgsSpatiaLiteConnection::updateStatistics()
{
#ifdef SPATIALITE_VERSION_GE_4_0_0
QFileInfo fi( mPath );
if ( !fi.exists() )
return false;
Expand All @@ -120,9 +99,6 @@ bool QgsSpatiaLiteConnection::updateStatistics()
ret = update_layer_statistics( database.get(), nullptr, nullptr );

return ret;
#else
return false;
#endif
}

int QgsSpatiaLiteConnection::checkHasMetadataTables( sqlite3 *handle )
Expand Down Expand Up @@ -242,8 +218,6 @@ int QgsSpatiaLiteConnection::checkHasMetadataTables( sqlite3 *handle )
return false;
}

#ifdef SPATIALITE_VERSION_GE_4_0_0
// only if libspatialite version is >= 4.0.0
bool QgsSpatiaLiteConnection::getTableInfoAbstractInterface( sqlite3 *handle, bool loadGeometrylessTables )
{
int ret;
Expand Down Expand Up @@ -353,7 +327,6 @@ bool QgsSpatiaLiteConnection::getTableInfoAbstractInterface( sqlite3 *handle, bo
}
return false;
}
#endif

bool QgsSpatiaLiteConnection::getTableInfo( sqlite3 *handle, bool loadGeometrylessTables )
{
Expand Down
4 changes: 0 additions & 4 deletions src/providers/spatialite/qgsspatialiteconnection.h
Expand Up @@ -92,9 +92,6 @@ class QgsSpatiaLiteConnection : public QObject
\returns true if querying of tables was successful, false on error */
bool getTableInfo( sqlite3 *handle, bool loadGeometrylessTables );

#ifdef SPATIALITE_VERSION_GE_4_0_0
// only if libspatialite version is >= 4.0.0

/**
* Inserts information about the spatial tables into mTables
* please note: this method is fully based on the Abstract Interface
Expand All @@ -105,7 +102,6 @@ class QgsSpatiaLiteConnection : public QObject
* thus completely freeing the client application to take care of them.
*/
bool getTableInfoAbstractInterface( sqlite3 *handle, bool loadGeometrylessTables );
#endif

//! Cleaning well-formatted SQL strings
QString quotedValue( QString value ) const;
Expand Down
57 changes: 2 additions & 55 deletions src/providers/spatialite/qgsspatialiteprovider.cpp
Expand Up @@ -475,8 +475,6 @@ QgsSpatiaLiteProvider::QgsSpatiaLiteProvider( QString const &uri )
bool alreadyDone = false;
bool ret = false;

#ifdef SPATIALITE_VERSION_GE_4_0_0
// only if libspatialite version is >= 4.0.0
gaiaVectorLayersListPtr list = nullptr;
gaiaVectorLayerPtr lyr = nullptr;
bool specialCase = false;
Expand All @@ -500,7 +498,6 @@ QgsSpatiaLiteProvider::QgsSpatiaLiteProvider( QString const &uri )
QgsDebugMsg( "Using checkLayerTypeAbstractInterface" );
alreadyDone = true;
}
#endif

if ( !alreadyDone )
{
Expand Down Expand Up @@ -533,7 +530,6 @@ QgsSpatiaLiteProvider::QgsSpatiaLiteProvider( QString const &uri )

alreadyDone = false;

#ifdef SPATIALITE_VERSION_GE_4_0_0
if ( lyr )
{
// using the v.4.0 AbstractInterface
Expand All @@ -559,7 +555,6 @@ QgsSpatiaLiteProvider::QgsSpatiaLiteProvider( QString const &uri )
gaiaFreeVectorLayersList( list );
alreadyDone = true;
}
#endif

if ( !alreadyDone )
{
Expand Down Expand Up @@ -665,9 +660,6 @@ static TypeSubType getVariantType( const QString &type )
return TypeSubType( QVariant::String, QVariant::Invalid );
}

#ifdef SPATIALITE_VERSION_GE_4_0_0
// only if libspatialite version is >= 4.0.0

void QgsSpatiaLiteProvider::loadFieldsAbstractInterface( gaiaVectorLayerPtr lyr )
{
if ( !lyr )
Expand Down Expand Up @@ -774,7 +766,6 @@ void QgsSpatiaLiteProvider::loadFieldsAbstractInterface( gaiaVectorLayerPtr lyr

sqlite3_free_table( results );
}
#endif

QString QgsSpatiaLiteProvider::spatialiteVersion()
{
Expand Down Expand Up @@ -4185,16 +4176,9 @@ bool QgsSpatiaLiteProvider::addAttributes( const QList<QgsField> &attributes )
handleError( sql, errMsg, true );
return false;
}
#ifdef SPATIALITE_VERSION_GE_4_0_0
sql = QStringLiteral( "UPDATE geometry_columns_statistics set last_verified = 0 WHERE f_table_name=\"%1\" AND f_geometry_column=\"%2\";" )
.arg( mTableName,
mGeometryColumn );
ret = sqlite3_exec( mSqliteHandle, sql.toUtf8().constData(), nullptr, nullptr, &errMsg );
update_layer_statistics( mSqliteHandle, mTableName.toUtf8().constData(), mGeometryColumn.toUtf8().constData() );
#elif SPATIALITE_VERSION_G_4_1_1
gaiaStatisticsInvalidate( mSqliteHandle, tableName.toUtf8().constData(), mGeometryColumn.toUtf8().constData() );

gaiaStatisticsInvalidate( mSqliteHandle, mTableName.toUtf8().constData(), mGeometryColumn.toUtf8().constData() );
update_layer_statistics( mSqliteHandle, mTableName.toUtf8().constData(), mGeometryColumn.toUtf8().constData() );
#endif

// reload columns
loadFields();
Expand Down Expand Up @@ -4401,8 +4385,6 @@ QString QgsSpatiaLiteProvider::quotedValue( QString value )
return value.prepend( '\'' ).append( '\'' );
}

#ifdef SPATIALITE_VERSION_GE_4_0_0
// only if libspatialite version is >= 4.0.0
bool QgsSpatiaLiteProvider::checkLayerTypeAbstractInterface( gaiaVectorLayerPtr lyr )
{
if ( !lyr )
Expand Down Expand Up @@ -4444,7 +4426,6 @@ bool QgsSpatiaLiteProvider::checkLayerTypeAbstractInterface( gaiaVectorLayerPtr

return true;
}
#endif

bool QgsSpatiaLiteProvider::checkLayerType()
{
Expand Down Expand Up @@ -4626,8 +4607,6 @@ bool QgsSpatiaLiteProvider::checkLayerType()
return count == 1;
}

#ifdef SPATIALITE_VERSION_GE_4_0_0
// only if libspatialite version is >= 4.0.0
bool QgsSpatiaLiteProvider::getGeometryDetailsAbstractInterface( gaiaVectorLayerPtr lyr )
{
if ( !lyr )
Expand Down Expand Up @@ -4728,7 +4707,6 @@ void QgsSpatiaLiteProvider::getViewSpatialIndexName()
}
sqlite3_free_table( results );
}
#endif

bool QgsSpatiaLiteProvider::getGeometryDetails()
{
Expand Down Expand Up @@ -5143,8 +5121,6 @@ bool QgsSpatiaLiteProvider::getSridDetails()
return true;
}

#ifdef SPATIALITE_VERSION_GE_4_0_0
// only if libspatialite version is >= 4.0.0
bool QgsSpatiaLiteProvider::getTableSummaryAbstractInterface( gaiaVectorLayerPtr lyr )
{
if ( !lyr )
Expand All @@ -5164,7 +5140,6 @@ bool QgsSpatiaLiteProvider::getTableSummaryAbstractInterface( gaiaVectorLayerPtr

return true;
}
#endif

bool QgsSpatiaLiteProvider::getTableSummary()
{
Expand Down Expand Up @@ -5389,41 +5364,13 @@ QGISEXTERN bool deleteLayer( const QString &dbPath, const QString &tableName, QS
}
sqlite3 *sqlite_handle = hndl->handle();
int ret;
#ifdef SPATIALITE_VERSION_GE_4_0_0
// only if libspatialite version is >= 4.0.0
// if libspatialite is v.4.0 (or higher) using the internal library
// method is highly recommended
if ( !gaiaDropTable( sqlite_handle, tableName.toUtf8().constData() ) )
{
// unexpected error
errCause = QObject::tr( "Unable to delete table %1\n" ).arg( tableName );
QgsSqliteHandle::closeDb( hndl );
return false;
}
#else
// drop the table
QString sql = QString( "DROP TABLE " ) + QgsSpatiaLiteProvider::quotedIdentifier( tableName );
QgsDebugMsg( sql );
char *errMsg = nullptr;
ret = sqlite3_exec( sqlite_handle, sql.toUtf8().constData(), nullptr, nullptr, &errMsg );
if ( ret != SQLITE_OK )
{
errCause = QObject::tr( "Unable to delete table %1:\n" ).arg( tableName );
errCause += QString::fromUtf8( errMsg );
sqlite3_free( errMsg );
QgsSqliteHandle::closeDb( hndl );
return false;
}

// remove table from geometry columns
sql = QString( "DELETE FROM geometry_columns WHERE upper(f_table_name) = upper(%1)" )
.arg( QgsSpatiaLiteProvider::quotedValue( tableName ) );
ret = sqlite3_exec( sqlite_handle, sql.toUtf8().constData(), nullptr, nullptr, nullptr );
if ( ret != SQLITE_OK )
{
QgsDebugMsg( "sqlite error: " + QString::fromUtf8( sqlite3_errmsg( sqlite_handle ) ) );
}
#endif

// TODO: remove spatial indexes?
// run VACUUM to free unused space and compact the database
Expand Down
3 changes: 0 additions & 3 deletions src/providers/spatialite/qgsspatialiteprovider.h
Expand Up @@ -325,14 +325,11 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
bool getQueryGeometryDetails();
bool getSridDetails();
bool getTableSummary();
#ifdef SPATIALITE_VERSION_GE_4_0_0
// only if libspatialite version is >= 4.0.0
bool checkLayerTypeAbstractInterface( gaiaVectorLayerPtr lyr );
bool getGeometryDetailsAbstractInterface( gaiaVectorLayerPtr lyr );
bool getTableSummaryAbstractInterface( gaiaVectorLayerPtr lyr );
void loadFieldsAbstractInterface( gaiaVectorLayerPtr lyr );
void getViewSpatialIndexName();
#endif
bool prepareStatement( sqlite3_stmt *&stmt,
const QgsAttributeList &fetchAttributes,
bool fetchGeometry,
Expand Down

0 comments on commit bb6fda6

Please sign in to comment.