Skip to content
Permalink
Browse files
Add a new parameter to retrieve underlying error when creating auxili…
…ary layer
  • Loading branch information
pblottiere committed Jun 7, 2021
1 parent cc0d9b1 commit d290df292f49d3accaf2e16296d83928d92c1c49
@@ -338,7 +338,7 @@ Saves the current database.
:return: ``True`` if everything is saved, ``False`` otherwise
%End

QgsAuxiliaryLayer *createAuxiliaryLayer( const QgsField &field, QgsVectorLayer *layer ) const /Factory/;
QgsAuxiliaryLayer *createAuxiliaryLayer( const QgsField &field, QgsVectorLayer *layer, QString *errorMessage /Out/ = 0 ) const /Factory/;
%Docstring
Creates an auxiliary layer for a vector layer. A new table is created if
necessary. The primary key to use to construct the auxiliary layer is
@@ -611,7 +611,7 @@ bool QgsAuxiliaryStorage::save() const
}
}

QgsAuxiliaryLayer *QgsAuxiliaryStorage::createAuxiliaryLayer( const QgsField &field, QgsVectorLayer *layer ) const
QgsAuxiliaryLayer *QgsAuxiliaryStorage::createAuxiliaryLayer( const QgsField &field, QgsVectorLayer *layer, QString *errorMessage ) const
{
QgsAuxiliaryLayer *alayer = nullptr;

@@ -623,8 +623,13 @@ QgsAuxiliaryLayer *QgsAuxiliaryStorage::createAuxiliaryLayer( const QgsField &fi

if ( !tableExists( table, database.get() ) )
{
if ( !createTable( field.typeName(), table, database.get() ) )
QString msg;
if ( !createTable( field.typeName(), table, database.get(), msg ) )
{
if ( errorMessage )
{
*errorMessage = msg;
}
return alayer;
}
}
@@ -738,25 +743,24 @@ bool QgsAuxiliaryStorage::exec( const QString &sql, sqlite3 *handler )
return rc;
}

void QgsAuxiliaryStorage::debugMsg( const QString &sql, sqlite3 *handler )
QString QgsAuxiliaryStorage::debugMsg( const QString &sql, sqlite3 *handler )
{
#ifdef QGISDEBUG
const QString err = QString::fromUtf8( sqlite3_errmsg( handler ) );
const QString msg = QObject::tr( "Unable to execute" );
const QString errMsg = QObject::tr( "%1 '%2': %3" ).arg( msg, sql, err );
QgsDebugMsg( errMsg );
#else
Q_UNUSED( sql )
Q_UNUSED( handler )
#endif
return errMsg;
}

bool QgsAuxiliaryStorage::createTable( const QString &type, const QString &table, sqlite3 *handler )
bool QgsAuxiliaryStorage::createTable( const QString &type, const QString &table, sqlite3 *handler, QString &errorMsg )
{
const QString sql = QStringLiteral( "CREATE TABLE IF NOT EXISTS '%1' ( '%2' %3 )" ).arg( table, AS_JOINFIELD, type );

if ( !exec( sql, handler ) )
{
errorMsg = QgsAuxiliaryStorage::debugMsg( sql, handler );
return false;
}

return true;
}
@@ -381,7 +381,7 @@ class CORE_EXPORT QgsAuxiliaryStorage
*
* \returns A new auxiliary layer or NULLPTR if an error happened.
*/
QgsAuxiliaryLayer *createAuxiliaryLayer( const QgsField &field, QgsVectorLayer *layer ) const SIP_FACTORY;
QgsAuxiliaryLayer *createAuxiliaryLayer( const QgsField &field, QgsVectorLayer *layer, QString *errorMessage SIP_OUT = nullptr ) const SIP_FACTORY;

/**
* Removes a table from the auxiliary storage.
@@ -426,10 +426,10 @@ class CORE_EXPORT QgsAuxiliaryStorage
static spatialite_database_unique_ptr createDB( const QString &filename );
static spatialite_database_unique_ptr openDB( const QString &filename );
static bool tableExists( const QString &table, sqlite3 *handler );
static bool createTable( const QString &type, const QString &table, sqlite3 *handler );
static bool createTable( const QString &type, const QString &table, sqlite3 *handler, QString &errorMsg );

static bool exec( const QString &sql, sqlite3 *handler );
static void debugMsg( const QString &sql, sqlite3 *handler );
static QString debugMsg( const QString &sql, sqlite3 *handler );

static QgsDataSourceUri parseOgrUri( const QgsDataSourceUri &uri );

0 comments on commit d290df2

Please sign in to comment.