Skip to content

Commit

Permalink
Tiny spatialite-related improvement & fix (#5857)
Browse files Browse the repository at this point in the history
* [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 authored Dec 13, 2017
1 parent b579414 commit 353ca63
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/providers/spatialite/qgsspatialiteconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) ) );
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/providers/spatialite/qgsspatialiteprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 )
{
Expand Down

0 comments on commit 353ca63

Please sign in to comment.