Skip to content
Permalink
Browse files
An error message is displayed when an auxiliary layer cannot be created
  • Loading branch information
pblottiere authored and nyalldawson committed Jun 21, 2021
1 parent 9b34771 commit 16bc56fc2f9ea28bf7aaf4c20c1c64526c09a4f1
Showing with 39 additions and 11 deletions.
  1. +7 −8 src/core/qgsauxiliarystorage.cpp
  2. +3 −3 src/core/qgsauxiliarystorage.h
  3. +7 −0 src/gui/qgsnewauxiliarylayerdialog.cpp
  4. +22 −0 tests/src/python/test_qgsauxiliarystorage.py
@@ -587,7 +587,7 @@ QgsAuxiliaryLayer *QgsAuxiliaryStorage::createAuxiliaryLayer( const QgsField &fi

if ( !tableExists( table, database.get() ) )
{
if ( !createTable( field.typeName(), table, database.get() ) )
if ( !createTable( field.typeName(), table, database.get(), mErrorString ) )
{
return alayer;
}
@@ -702,25 +702,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;
}
@@ -413,18 +413,18 @@ 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 );

bool mValid = false;
QString mFileName; // original filename
QString mTmpFileName; // temporary filename used in copy mode
bool mCopy = false;
QString mErrorString;
mutable QString mErrorString;
};

#endif
@@ -54,6 +54,13 @@ void QgsNewAuxiliaryLayerDialog::accept()
mLayer->setAuxiliaryLayer( alayer );
}
}
else
{
QDialog::close();
const QString errMsg = QgsProject::instance()->auxiliaryStorage()->errorString();
QMessageBox::critical( this, tr( "New Auxiliary Layer" ), errMsg );
return;
}

QDialog::accept();
}
@@ -26,6 +26,7 @@
QgsPalLayerSettings,
QgsSymbolLayer,
QgsVectorLayerSimpleLabeling,
QgsField,
NULL)
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath, writeShape
@@ -477,6 +478,27 @@ def testQgdCreation(self):
qgd = newpath + '.qgd'
self.assertTrue(os.path.exists(qgd))

def testInvalidPrimaryKey(self):
# create layer
vl = QgsVectorLayer(
'Point?crs=epsg:4326&field=pk:integer&key=pk',
'test', 'memory')
assert (vl.isValid())

# add a field with an invalid typename
field = QgsField(name="invalid_pk", type=QVariant.Int, typeName="xsd:int")
vl.startEditing()
vl.addAttribute(field)
vl.commitChanges()

# create auxiliary storage based on the invalid field
s = QgsAuxiliaryStorage()
pkf = vl.fields().field(vl.fields().indexOf('invalid_pk'))
al = s.createAuxiliaryLayer(pkf, vl)

self.assertEqual(al, None)
self.assertTrue("CREATE TABLE IF NOT EXISTS" in s.errorString())


if __name__ == '__main__':
unittest.main()

0 comments on commit 16bc56f

Please sign in to comment.