Skip to content
Permalink
Browse files

Tiny spatialite-related improvement & fix (#5857)

* [spatialite provider] when creating an empty layer, primary key should be autoincrement
* [processing] fix import into spatialite's primary key parameter
* [spatialite provider] case insensitive search for table names
  • Loading branch information
nirvn committed Dec 13, 2017
1 parent b579414 commit 353ca633b078ee1e1116b595240a594931ec2c81
@@ -64,7 +64,7 @@ def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT, self.tr('Layer to import')))
self.addParameter(QgsProcessingParameterVectorLayer(self.DATABASE, self.tr('File database'), [], False, False))
self.addParameter(QgsProcessingParameterString(self.TABLENAME, self.tr('Table to import to (leave blank to use layer name)'), optional=True))
self.addParameter(QgsProcessingParameterField(self.PRIMARY_KEY, self.tr('Primary key field'), self.INPUT, optional=True))
self.addParameter(QgsProcessingParameterField(self.PRIMARY_KEY, self.tr('Primary key field'), None, self.INPUT, QgsProcessingParameterField.Any, False, True))
self.addParameter(QgsProcessingParameterString(self.GEOMETRY_COLUMN, self.tr('Geometry column'), 'geom'))
self.addParameter(QgsProcessingParameterString(self.ENCODING, self.tr('Encoding'), 'UTF-8', optional=True))
self.addParameter(QgsProcessingParameterBoolean(self.OVERWRITE, self.tr('Overwrite'), True))
@@ -330,7 +330,7 @@ bool QgsSpatiaLiteConnection::getTableInfoAbstractInterface( sqlite3 *handle, bo
for ( i = 1; i <= rows; i++ )
{
QString tableName = QString::fromUtf8( results[( i * columns ) + 0] );
if ( !ignoreTableNames.contains( tableName ) )
if ( !ignoreTableNames.contains( tableName, Qt::CaseInsensitive ) )
mTables.append( TableEntry( tableName, QString(), QStringLiteral( "qgis_table" ) ) );
}
}
@@ -236,10 +236,11 @@ QgsSpatiaLiteProvider::createEmptyLayer( const QString &uri,
throw SLException( errMsg );
}

sql = QStringLiteral( "CREATE TABLE %1 (%2 %3 PRIMARY KEY)" )
sql = QStringLiteral( "CREATE TABLE %1 (%2 %3 PRIMARY KEY%4)" )
.arg( quotedIdentifier( tableName ),
quotedIdentifier( primaryKey ),
primaryKeyType );
primaryKeyType,
primaryKeyType == QLatin1String( "INTEGER" ) ? QStringLiteral( " AUTOINCREMENT" ) : QString() );

ret = sqlite3_exec( sqliteHandle, sql.toUtf8().constData(), nullptr, nullptr, &errMsg );
if ( ret != SQLITE_OK )
@@ -867,7 +868,7 @@ void QgsSpatiaLiteProvider::fetchConstraints()

if ( mAttributeFields[ fieldIdx ].name() == mPrimaryKey )
{
QString sql = QStringLiteral( "SELECT sql FROM sqlite_master WHERE tbl_name=%1" ).arg( quotedIdentifier( mTableName ) );
QString sql = QStringLiteral( "SELECT sql FROM sqlite_master WHERE type = 'table' AND tbl_name like %1" ).arg( quotedIdentifier( mTableName ) );
int ret = sqlite3_get_table( mSqliteHandle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );
if ( ret != SQLITE_OK )
{

0 comments on commit 353ca63

Please sign in to comment.
You can’t perform that action at this time.