Skip to content

Commit

Permalink
do not use st_multi when SL < 2.4 (follow 2469c5aa17)
Browse files Browse the repository at this point in the history
  • Loading branch information
brushtyler committed Dec 5, 2012
1 parent 1bde6c5 commit c99fdd4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
55 changes: 53 additions & 2 deletions src/providers/spatialite/qgsspatialiteprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ QgsSpatiaLiteProvider::createEmptyLayer(
QgsSpatiaLiteProvider::QgsSpatiaLiteProvider( QString const &uri )
: QgsVectorDataProvider( uri )
, geomType( QGis::WKBUnknown )
, mGotSpatialiteVersion( false )
, sqliteHandle( NULL )
, sqliteStatement( NULL )
, mSrid( -1 )
Expand Down Expand Up @@ -557,6 +558,10 @@ QgsSpatiaLiteProvider::QgsSpatiaLiteProvider( QString const &uri )
QgsDebugMsg( "Invalid SpatiaLite layer" );
return;
}

// retrieve version information
spatialiteVersion();

//fill type names into sets
mNativeTypes
<< QgsVectorDataProvider::NativeType( tr( "Binary object (BLOB)" ), "BLOB", QVariant::ByteArray )
Expand Down Expand Up @@ -618,6 +623,48 @@ void QgsSpatiaLiteProvider::loadFieldsAbstractInterface( gaiaVectorLayerPtr lyr
}
#endif

QString QgsSpatiaLiteProvider::spatialiteVersion()
{
if ( mGotSpatialiteVersion )
return mSpatialiteVersionInfo;

int ret;
char **results;
int rows;
int columns;
char *errMsg = NULL;
QString sql;

sql = "SELECT spatialite_version()";
ret = sqlite3_get_table( sqliteHandle, sql.toUtf8(), &results, &rows, &columns, &errMsg );
if ( ret != SQLITE_OK || rows != 1 )
{
QgsMessageLog::logMessage( tr( "Retrieval of spatialite version failed" ), tr( "SpatiaLite" ) );
return QString::null;
}

mSpatialiteVersionInfo = QString::fromUtf8( results[( 1 * columns ) + 0] );
sqlite3_free_table( results );

QgsDebugMsg( "SpatiaLite version info: " + mSpatialiteVersionInfo );

QStringList spatialiteParts = mSpatialiteVersionInfo.split( " ", QString::SkipEmptyParts );

// Get major and minor version
QStringList spatialiteVersionParts = spatialiteParts[0].split( ".", QString::SkipEmptyParts );
if ( spatialiteVersionParts.size() < 2 )
{
QgsMessageLog::logMessage( tr( "Could not parse spatialite version string '%1'" ).arg( mSpatialiteVersionInfo ), tr( "SpatiaLite" ) );
return QString::null;
}

mSpatialiteVersionMajor = spatialiteVersionParts[0].toInt();
mSpatialiteVersionMinor = spatialiteVersionParts[1].toInt();

mGotSpatialiteVersion = true;
return mSpatialiteVersionInfo;
}

void QgsSpatiaLiteProvider::loadFields()
{
int ret;
Expand Down Expand Up @@ -3749,14 +3796,18 @@ QString QgsSpatiaLiteProvider::geomParam() const
break;
}

if ( forceMulti )
// ST_Multi function is available from QGIS >= 2.4
bool hasMultiFunction = mSpatialiteVersionMajor > 2 ||
( mSpatialiteVersionMajor == 2 && mSpatialiteVersionMinor >= 4 );

if ( forceMulti && hasMultiFunction )
{
geometry += "ST_Multi(";
}

geometry += QString( "GeomFromWKB(?, %2)" ).arg( mSrid );

if ( forceMulti )
if ( forceMulti && hasMultiFunction )
{
geometry += ")";
}
Expand Down
15 changes: 15 additions & 0 deletions src/providers/spatialite/qgsspatialiteprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider

QString geomParam() const;

//! get SpatiaLite version string
QString spatialiteVersion();

QgsFieldMap attributeFields;
/**
* Flag indicating if the layer data source is a valid SpatiaLite layer
Expand Down Expand Up @@ -398,6 +401,18 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
/** geometry column index used when fetching geometry */
int mGeomColIdx;

//! SpatiaLite version string
QString mSpatialiteVersionInfo;

//! Are mSpatialiteVersionMajor, mSpatialiteVersionMinor valid?
bool mGotSpatialiteVersion;

//! SpatiaLite major version
int mSpatialiteVersionMajor;

//! SpatiaLite minor version
int mSpatialiteVersionMinor;

/**
* internal utility functions used to handle common SQLite tasks
*/
Expand Down

0 comments on commit c99fdd4

Please sign in to comment.