Skip to content

Commit df9ad67

Browse files
author
esseffe
committedOct 26, 2010
a) applied the patch suggested in ticket #3139
b) fixed the SourceSelect dialog so to avoid showing RasterLite-1 related tables git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14435 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

4 files changed

+365
-53
lines changed

4 files changed

+365
-53
lines changed
 

‎src/app/qgsspatialitesourceselect.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,8 @@ bool QgsSpatiaLiteSourceSelect::getTableInfo( sqlite3 * handle )
507507
{
508508
for ( i = 1; i <= rows; i++ )
509509
{
510+
if ( isRasterlite1Datasource( handle, results[( i * columns ) + 0] ) )
511+
continue;
510512
QString tableName = QString::fromUtf8( results[( i * columns ) + 0] );
511513
QString column = QString::fromUtf8( results[( i * columns ) + 1] );
512514
QString type = results[( i * columns ) + 2];
@@ -698,6 +700,52 @@ bool QgsSpatiaLiteSourceSelect::checkVirtsGeometryColumns( sqlite3 * handle )
698700
return exists;
699701
}
700702

703+
bool QgsSpatiaLiteSourceSelect::isRasterlite1Datasource (sqlite3 * handle, const char *table)
704+
{
705+
// testing for RasterLite-1 datasources
706+
int ret;
707+
int i;
708+
char **results;
709+
int rows;
710+
int columns;
711+
bool exists = false;
712+
int len;
713+
char table_raster[4192];
714+
char sql[4192];
715+
716+
strcpy ( table_raster, table );
717+
len = strlen( table_raster );
718+
if (strlen( table_raster ) < 9)
719+
return false;
720+
if (strcmp( table_raster + len - 9, "_metadata" ) != 0)
721+
return false;
722+
// ok, possible candidate
723+
strcpy( table_raster + len - 9, "_rasters" );
724+
725+
// checking if the related "_RASTERS table exists
726+
sprintf( sql, "SELECT name FROM sqlite_master WHERE type = 'table' AND name = '%s'", table_raster );
727+
728+
ret = sqlite3_get_table( handle, sql, &results, &rows, &columns, NULL );
729+
if ( ret != SQLITE_OK )
730+
return false;
731+
if ( rows < 1 )
732+
;
733+
else
734+
{
735+
for ( i = 1; i <= rows; i++ )
736+
{
737+
if ( results[( i * columns ) + 0] != NULL )
738+
{
739+
const char *name = results[( i * columns ) + 0];
740+
if ( name )
741+
exists = true;
742+
}
743+
}
744+
}
745+
sqlite3_free_table( results );
746+
return exists;
747+
}
748+
701749
bool QgsSpatiaLiteSourceSelect::isDeclaredHidden( sqlite3 * handle, QString table, QString geom )
702750
{
703751
int ret;

‎src/app/qgsspatialitesourceselect.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ class QgsSpatiaLiteSourceSelect: public QDialog, private Ui::QgsSpatiaLiteSource
117117
/**Checks if this layer has been declared HIDDEN*/
118118
bool isDeclaredHidden( sqlite3 * handle, QString table, QString geom );
119119

120+
/**Checks if this layer is a RasterLite-1 datasource*/
121+
bool isRasterlite1Datasource( sqlite3 * handle, const char * table );
122+
120123
/**cleaning well-formatted SQL strings*/
121124
QString quotedValue( QString value ) const;
122125

‎src/providers/spatialite/qgsspatialiteprovider.cpp

Lines changed: 304 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ QgsSpatiaLiteProvider::QgsSpatiaLiteProvider( QString const &uri )
5252
mGeometryColumn = anUri.geometryColumn();
5353
mSqlitePath = anUri.database();
5454
mSubsetString = anUri.sql();
55+
mPrimaryKey = anUri.keyColumn();
56+
mQuery = mTableName;
5557

5658
// trying to open the SQLite DB
5759
spatialite_init( 0 );
@@ -131,65 +133,137 @@ void QgsSpatiaLiteProvider::loadFields()
131133
{
132134
int ret;
133135
int i;
136+
sqlite3_stmt *stmt = NULL;
134137
char **results;
135138
int rows;
136139
int columns;
137140
char *errMsg = NULL;
138141
QString pkName;
139142
int pkCount = 0;
140143
int fldNo = 0;
144+
QString sql;
141145

142146
attributeFields.clear();
143-
mPrimaryKey.clear();
144147

145-
QString sql = QString( "PRAGMA table_info(\"%1\")" ).arg( mTableName );
146-
147-
ret = sqlite3_get_table( sqliteHandle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );
148-
if ( ret != SQLITE_OK )
149-
goto error;
150-
if ( rows < 1 )
151-
;
152-
else
148+
if ( !isQuery )
153149
{
154-
for ( i = 1; i <= rows; i++ )
155-
{
156-
QString name = QString::fromUtf8( results[( i * columns ) + 1] );
157-
const char *type = results[( i * columns ) + 2];
158-
QString pk = results[( i * columns ) + 5];
159-
if ( pk.toInt() != 0 )
160-
{
161-
// found a Primary Key column
162-
pkCount++;
163-
pkName = name;
164-
}
150+
mPrimaryKey.clear();
151+
152+
sql = QString( "PRAGMA table_info(%1)" ).arg( quotedIdentifier( mTableName ) );
165153

166-
if ( name != mGeometryColumn )
154+
ret = sqlite3_get_table( sqliteHandle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );
155+
if ( ret != SQLITE_OK )
156+
goto error;
157+
if ( rows < 1 )
158+
;
159+
else
160+
{
161+
for ( i = 1; i <= rows; i++ )
167162
{
168-
// for sure any SQLite value can be represented as SQLITE_TEXT
169-
QVariant::Type fieldType = QVariant::String;
170-
171-
// making some assumptions in order to guess a more realistic type
172-
if ( strcasecmp( type, "int" ) == 0 ||
173-
strcasecmp( type, "integer" ) == 0 ||
174-
strcasecmp( type, "bigint" ) == 0 ||
175-
strcasecmp( type, "smallint" ) == 0 ||
176-
strcasecmp( type, "tinyint" ) == 0 ||
177-
strcasecmp( type, "boolean" ) == 0 )
163+
QString name = QString::fromUtf8( results[( i * columns ) + 1] );
164+
const char *type = results[( i * columns ) + 2];
165+
QString pk = results[( i * columns ) + 5];
166+
if ( pk.toInt() != 0 )
178167
{
179-
fieldType = QVariant::Int;
168+
// found a Primary Key column
169+
pkCount++;
170+
pkName = name;
180171
}
181-
else if ( strcasecmp( type, "real" ) == 0 ||
182-
strcasecmp( type, "double" ) == 0 ||
183-
strcasecmp( type, "double precision" ) == 0 || strcasecmp( type, "float" ) == 0 )
172+
173+
if ( name != mGeometryColumn )
184174
{
185-
fieldType = QVariant::Double;
186-
}
175+
// for sure any SQLite value can be represented as SQLITE_TEXT
176+
QVariant::Type fieldType = QVariant::String;
177+
178+
// making some assumptions in order to guess a more realistic type
179+
if ( strcasecmp( type, "int" ) == 0 ||
180+
strcasecmp( type, "integer" ) == 0 ||
181+
strcasecmp( type, "bigint" ) == 0 ||
182+
strcasecmp( type, "smallint" ) == 0 ||
183+
strcasecmp( type, "tinyint" ) == 0 ||
184+
strcasecmp( type, "boolean" ) == 0 )
185+
{
186+
fieldType = QVariant::Int;
187+
}
188+
else if ( strcasecmp( type, "real" ) == 0 ||
189+
strcasecmp( type, "double" ) == 0 ||
190+
strcasecmp( type, "double precision" ) == 0 || strcasecmp( type, "float" ) == 0 )
191+
{
192+
fieldType = QVariant::Double;
193+
}
187194

188-
attributeFields.insert( fldNo++, QgsField( name, fieldType, type, 0, 0, "" ) );
195+
attributeFields.insert( fldNo++, QgsField( name, fieldType, type, 0, 0, "" ) );
196+
}
189197
}
190198
}
191-
}
192-
sqlite3_free_table( results );
199+
sqlite3_free_table( results );
200+
}
201+
else
202+
{
203+
sql = QString( "select * from %1 limit 1" ).arg( mQuery );
204+
205+
if ( sqlite3_prepare_v2( sqliteHandle, sql.toUtf8().constData(), -1, &stmt, NULL ) != SQLITE_OK )
206+
{
207+
// some error occurred
208+
QgsDebugMsg( QString( "SQLite error: %1\n\nSQL: %2" )
209+
.arg( sql )
210+
.arg( QString::fromUtf8( sqlite3_errmsg( sqliteHandle ) ) ) );
211+
return;
212+
}
213+
214+
ret = sqlite3_step( stmt );
215+
if ( ret == SQLITE_DONE )
216+
{
217+
// there are no rows to fetch
218+
sqlite3_finalize( stmt );
219+
return;
220+
}
221+
222+
if ( ret == SQLITE_ROW )
223+
{
224+
// one valid row has been fetched from the result set
225+
columns = sqlite3_column_count( stmt );
226+
for ( i = 0; i < columns; i++ )
227+
{
228+
QString name = QString::fromUtf8( sqlite3_column_name( stmt, i ) );
229+
const char *type = sqlite3_column_decltype( stmt, i );
230+
if ( type == NULL )
231+
type = "TEXT";
232+
233+
if ( name != mPrimaryKey )
234+
{
235+
pkCount++;
236+
pkName = name;
237+
}
238+
239+
if ( name != mGeometryColumn )
240+
{
241+
// for sure any SQLite value can be represented as SQLITE_TEXT
242+
QVariant::Type fieldType = QVariant::String;
243+
244+
// making some assumptions in order to guess a more realistic type
245+
if ( strcasecmp( type, "int" ) == 0 ||
246+
strcasecmp( type, "integer" ) == 0 ||
247+
strcasecmp( type, "bigint" ) == 0 ||
248+
strcasecmp( type, "smallint" ) == 0 ||
249+
strcasecmp( type, "tinyint" ) == 0 ||
250+
strcasecmp( type, "boolean" ) == 0 )
251+
{
252+
fieldType = QVariant::Int;
253+
}
254+
else if ( strcasecmp( type, "real" ) == 0 ||
255+
strcasecmp( type, "double" ) == 0 ||
256+
strcasecmp( type, "double precision" ) == 0 || strcasecmp( type, "float" ) == 0 )
257+
{
258+
fieldType = QVariant::Double;
259+
}
260+
261+
attributeFields.insert( fldNo++, QgsField( name, fieldType, type, 0, 0, "" ) );
262+
}
263+
}
264+
}
265+
sqlite3_finalize( stmt );
266+
}
193267

194268
if ( pkCount == 1 )
195269
{
@@ -221,7 +295,9 @@ bool QgsSpatiaLiteProvider::featureAtId( int featureId, QgsFeature & feature, bo
221295

222296
feature.setValid( false );
223297

224-
QString sql = "SELECT ROWID";
298+
QString primaryKey = !isQuery ? "ROWID" : quotedIdentifier( mPrimaryKey );
299+
300+
QString sql = QString( "SELECT %1" ).arg( primaryKey );
225301
for ( QgsAttributeList::const_iterator it = fetchAttributes.constBegin(); it != fetchAttributes.constEnd(); ++it )
226302
{
227303
const QgsField & fld = field( *it );
@@ -232,7 +308,10 @@ bool QgsSpatiaLiteProvider::featureAtId( int featureId, QgsFeature & feature, bo
232308
{
233309
sql += QString( ", AsBinary(%1)" ).arg( quotedIdentifier( mGeometryColumn ) );
234310
}
235-
sql += QString( " FROM %1 WHERE ROWID = %2" ).arg( quotedIdentifier( mTableName ) ).arg( featureId );
311+
sql += QString( " FROM %1 WHERE %2 = %3" )
312+
.arg( mQuery )
313+
.arg( primaryKey )
314+
.arg( featureId );
236315

237316
if ( sqlite3_prepare_v2( sqliteHandle, sql.toUtf8().constData(), -1, &stmt, NULL ) != SQLITE_OK )
238317
{
@@ -267,7 +346,7 @@ bool QgsSpatiaLiteProvider::featureAtId( int featureId, QgsFeature & feature, bo
267346
{
268347
if ( ic == 0 )
269348
{
270-
// first column always contains the ROWID
349+
// first column always contains the ROWID (or the primary key)
271350
feature.setFeatureId( sqlite3_column_int( stmt, ic ) );
272351
}
273352
else
@@ -391,7 +470,7 @@ bool QgsSpatiaLiteProvider::nextFeature( QgsFeature & feature )
391470
{
392471
if ( ic == 0 )
393472
{
394-
// first column always contains the ROWID
473+
// first column always contains the ROWID (or the primary key)
395474
feature.setFeatureId( sqlite3_column_int( sqliteStatement, ic ) );
396475
}
397476
else
@@ -522,7 +601,9 @@ void QgsSpatiaLiteProvider::select( QgsAttributeList fetchAttributes, QgsRectang
522601
sqliteStatement = NULL;
523602
}
524603

525-
QString sql = "SELECT ROWID";
604+
QString primaryKey = !isQuery ? "ROWID" : quotedIdentifier( mPrimaryKey );
605+
606+
QString sql = QString( "SELECT %1" ).arg( primaryKey );
526607
for ( QgsAttributeList::const_iterator it = fetchAttributes.constBegin(); it != fetchAttributes.constEnd(); ++it )
527608
{
528609
const QgsField & fld = field( *it );
@@ -533,7 +614,7 @@ void QgsSpatiaLiteProvider::select( QgsAttributeList fetchAttributes, QgsRectang
533614
{
534615
sql += QString( ", AsBinary(%1)" ).arg( quotedIdentifier( mGeometryColumn ) );
535616
}
536-
sql += QString( " FROM %1" ).arg( quotedIdentifier( mTableName ) );
617+
sql += QString( " FROM %1" ).arg( mQuery );
537618

538619
QString whereClause;
539620

@@ -569,7 +650,10 @@ void QgsSpatiaLiteProvider::select( QgsAttributeList fetchAttributes, QgsRectang
569650
mbrFilter += QString( "ymin <= %1 AND " ).arg( QString::number( rect.yMaximum(), 'f', 6 ) );
570651
mbrFilter += QString( "ymax >= %1" ).arg( QString::number( rect.yMinimum(), 'f', 6 ) );
571652
QString idxName = QString( "idx_%1_%2" ).arg( mIndexTable ).arg( mIndexGeometry );
572-
whereClause += QString( "ROWID IN (SELECT pkid FROM %1 WHERE %2)" ).arg( quotedIdentifier( idxName ) ).arg( mbrFilter );
653+
whereClause += QString( "%1 IN (SELECT pkid FROM %2 WHERE %3)" )
654+
.arg( quotedIdentifier( primaryKey ) )
655+
.arg( quotedIdentifier( idxName ) )
656+
.arg( mbrFilter );
573657
}
574658
else if ( spatialIndexMbrCache )
575659
{
@@ -579,7 +663,10 @@ void QgsSpatiaLiteProvider::select( QgsAttributeList fetchAttributes, QgsRectang
579663
arg( QString::number( rect.yMinimum(), 'f', 6 ) ).
580664
arg( QString::number( rect.xMaximum(), 'f', 6 ) ).arg( QString::number( rect.yMaximum(), 'f', 6 ) );
581665
QString idxName = QString( "cache_%1_%2" ).arg( mIndexTable ).arg( mIndexGeometry );
582-
whereClause += QString( "ROWID IN (SELECT rowid FROM %1 WHERE mbr = FilterMbrIntersects(%2))" ).arg( quotedIdentifier( idxName ) ).arg( mbr );
666+
whereClause += QString( "%1 IN (SELECT rowid FROM %2 WHERE mbr = FilterMbrIntersects(%3))" )
667+
.arg( quotedIdentifier( primaryKey ) )
668+
.arg( quotedIdentifier( idxName ) )
669+
.arg( mbr );
583670
}
584671
else
585672
{
@@ -711,7 +798,7 @@ QVariant QgsSpatiaLiteProvider::minimumValue( int index )
711798
// get the field name
712799
const QgsField & fld = field( index );
713800

714-
QString sql = QString( "SELECT Min(\"%1\") FROM \"%2\"" ).arg( fld.name() ).arg( mTableName );
801+
QString sql = QString( "SELECT Min(%1) FROM %2" ).arg( quotedIdentifier( fld.name() ) ).arg( mQuery );
715802

716803
if ( !mSubsetString.isEmpty() )
717804
{
@@ -766,7 +853,7 @@ QVariant QgsSpatiaLiteProvider::maximumValue( int index )
766853
// get the field name
767854
const QgsField & fld = field( index );
768855

769-
QString sql = QString( "SELECT Max(\"%1\") FROM \"%2\"" ).arg( fld.name() ).arg( mTableName );
856+
QString sql = QString( "SELECT Max(%1) FROM %2" ).arg( quotedIdentifier( fld.name() ) ).arg( mQuery );
770857

771858
if ( !mSubsetString.isEmpty() )
772859
{
@@ -819,7 +906,7 @@ void QgsSpatiaLiteProvider::uniqueValues( int index, QList < QVariant > &uniqueV
819906
// get the field name
820907
const QgsField & fld = field( index );
821908

822-
sql = QString( "SELECT DISTINCT \"%1\" FROM \"%2\" ORDER BY \"%1\"" ).arg( fld.name() ).arg( mTableName );
909+
sql = QString( "SELECT DISTINCT %1 FROM %2 ORDER BY %1" ).arg( quotedIdentifier( fld.name() ) ).arg( mQuery );
823910

824911
if ( !mSubsetString.isEmpty() )
825912
{
@@ -1511,6 +1598,7 @@ bool QgsSpatiaLiteProvider::checkLayerType()
15111598
mTableBased = false;
15121599
mViewBased = false;
15131600
mVShapeBased = false;
1601+
isQuery = false;
15141602

15151603
// checking if this one is a Table-based layer
15161604
QString sql = QString( "SELECT read_only FROM geometry_columns "
@@ -1594,6 +1682,49 @@ bool QgsSpatiaLiteProvider::checkLayerType()
15941682
}
15951683
sqlite3_free_table( results );
15961684

1685+
// checking if this one is a select query
1686+
if ( mQuery.startsWith( "(select", Qt::CaseInsensitive ) &&
1687+
mQuery.endsWith( ")" ) )
1688+
{
1689+
// get a new alias for the subquery
1690+
int index = 0;
1691+
QString alias;
1692+
QRegExp regex;
1693+
do
1694+
{
1695+
alias = QString( "subQuery_%1" ).arg( QString::number( index++ ) );
1696+
QString pattern = QString( "(\\\"?)%1\\1" ).arg( QRegExp::escape( alias ) );
1697+
regex.setPattern( pattern );
1698+
regex.setCaseSensitivity( Qt::CaseInsensitive );
1699+
}
1700+
while ( mQuery.contains( regex ) );
1701+
1702+
// convert the custom query into a subquery
1703+
mQuery = QString( "%1 as %2" )
1704+
.arg( mQuery )
1705+
.arg( quotedIdentifier( alias ) );
1706+
1707+
sql = QString( "SELECT 0 FROM %1 LIMIT 1" ).arg( mQuery );
1708+
ret = sqlite3_get_table( sqliteHandle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );
1709+
if ( ret == SQLITE_OK && rows == 1 )
1710+
{
1711+
isQuery = true;
1712+
mReadOnly = true;
1713+
count++;
1714+
}
1715+
if ( errMsg )
1716+
{
1717+
QgsDebugMsg( QString( "sqlite error %1 [%2]" ).arg( sql ).arg( errMsg ) );
1718+
sqlite3_free( errMsg );
1719+
errMsg = 0;
1720+
}
1721+
sqlite3_free_table( results );
1722+
}
1723+
else
1724+
{
1725+
mQuery = quotedIdentifier( mTableName );
1726+
}
1727+
15971728
// checking for validity
15981729
return count == 1;
15991730
}
@@ -1607,6 +1738,8 @@ bool QgsSpatiaLiteProvider::getGeometryDetails()
16071738
ret = getViewGeometryDetails();
16081739
if ( mVShapeBased )
16091740
ret = getVShapeGeometryDetails();
1741+
if ( isQuery )
1742+
ret = getQueryGeometryDetails();
16101743
return ret;
16111744
}
16121745

@@ -1845,6 +1978,122 @@ bool QgsSpatiaLiteProvider::getVShapeGeometryDetails()
18451978
return false;
18461979
}
18471980

1981+
bool QgsSpatiaLiteProvider::getQueryGeometryDetails()
1982+
{
1983+
int ret;
1984+
int i;
1985+
char **results;
1986+
int rows;
1987+
int columns;
1988+
char *errMsg = NULL;
1989+
1990+
QString fType( "" );
1991+
QString xSrid( "" );
1992+
1993+
// get stuff from the relevant column instead. This may (will?)
1994+
// fail if there is no data in the relevant table.
1995+
QString sql = QString( "select srid(%1), geometrytype(%1) from %2" )
1996+
.arg( quotedIdentifier( mGeometryColumn ) )
1997+
.arg( mQuery );
1998+
1999+
//it is possible that the where clause restricts the feature type
2000+
if ( !mSubsetString.isEmpty() )
2001+
{
2002+
sql += " WHERE " + mSubsetString;
2003+
}
2004+
2005+
sql += " limit 1";
2006+
2007+
ret = sqlite3_get_table( sqliteHandle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );
2008+
if ( ret != SQLITE_OK )
2009+
goto error;
2010+
if ( rows < 1 )
2011+
;
2012+
else
2013+
{
2014+
for ( i = 1; i <= rows; i++ )
2015+
{
2016+
xSrid = results[( i * columns ) + 0];
2017+
fType = results[( i * columns ) + 1];
2018+
}
2019+
}
2020+
sqlite3_free_table( results );
2021+
2022+
if ( !xSrid.isEmpty() && !fType.isEmpty() )
2023+
{
2024+
if ( fType == "GEOMETRY" )
2025+
{
2026+
// check to see if there is a unique geometry type
2027+
sql = QString( "select distinct "
2028+
"case"
2029+
" when geometrytype(%1) IN ('POINT','MULTIPOINT') THEN 'POINT'"
2030+
" when geometrytype(%1) IN ('LINESTRING','MULTILINESTRING') THEN 'LINESTRING'"
2031+
" when geometrytype(%1) IN ('POLYGON','MULTIPOLYGON') THEN 'POLYGON'"
2032+
" end "
2033+
"from %2" )
2034+
.arg( quotedIdentifier( mGeometryColumn ) )
2035+
.arg( mQuery );
2036+
2037+
if ( !mSubsetString.isEmpty() )
2038+
sql += " where " + mSubsetString;
2039+
2040+
ret = sqlite3_get_table( sqliteHandle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );
2041+
if ( ret != SQLITE_OK )
2042+
goto error;
2043+
if ( rows != 1 )
2044+
;
2045+
else
2046+
{
2047+
for ( i = 1; i <= rows; i++ )
2048+
{
2049+
fType = results[( 1 * columns ) + 0];
2050+
}
2051+
}
2052+
sqlite3_free_table( results );
2053+
}
2054+
2055+
if ( fType == "POINT" )
2056+
{
2057+
geomType = QGis::WKBPoint;
2058+
}
2059+
else if ( fType == "MULTIPOINT" )
2060+
{
2061+
geomType = QGis::WKBMultiPoint;
2062+
}
2063+
else if ( fType == "LINESTRING" )
2064+
{
2065+
geomType = QGis::WKBLineString;
2066+
}
2067+
else if ( fType == "MULTILINESTRING" )
2068+
{
2069+
geomType = QGis::WKBMultiLineString;
2070+
}
2071+
else if ( fType == "POLYGON" )
2072+
{
2073+
geomType = QGis::WKBPolygon;
2074+
}
2075+
else if ( fType == "MULTIPOLYGON" )
2076+
{
2077+
geomType = QGis::WKBMultiPolygon;
2078+
}
2079+
mSrid = xSrid.toInt();
2080+
}
2081+
2082+
if ( geomType == QGis::WKBUnknown || mSrid < 0 )
2083+
goto error;
2084+
2085+
return getSridDetails();
2086+
2087+
error:
2088+
// unexpected error
2089+
if ( errMsg != NULL )
2090+
{
2091+
QgsDebugMsg( QString( "SQL error: %1\n\n%2" ).arg( sql ).arg( errMsg ? QString::fromUtf8( errMsg ) : "unknown cause" ) );
2092+
sqlite3_free( errMsg );
2093+
}
2094+
return false;
2095+
}
2096+
18482097
bool QgsSpatiaLiteProvider::getSridDetails()
18492098
{
18502099
int ret;
@@ -1891,8 +2140,10 @@ bool QgsSpatiaLiteProvider::getTableSummary()
18912140
int columns;
18922141
char *errMsg = NULL;
18932142

1894-
QString sql = QString( "SELECT Min(MbrMinX(\"%1\")), Min(MbrMinY(\"%1\")), "
1895-
"Max(MbrMaxX(\"%1\")), Max(MbrMaxY(\"%1\")), Count(*) " "FROM \"%2\"" ).arg( mGeometryColumn ).arg( mTableName );
2143+
QString sql = QString( "SELECT Min(MbrMinX(%1)), Min(MbrMinY(%1)), "
2144+
"Max(MbrMaxX(%1)), Max(MbrMaxY(%1)), Count(*) " "FROM %2" )
2145+
.arg( quotedIdentifier( mGeometryColumn ) )
2146+
.arg( mQuery );
18962147

18972148
if ( !mSubsetString.isEmpty() )
18982149
{

‎src/providers/spatialite/qgsspatialiteprovider.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
257257
void repaintRequested();
258258

259259
private:
260+
260261
/** loads fields from input file to member attributeFields */
261262
void loadFields();
262263

@@ -265,6 +266,10 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
265266
* Flag indicating if the layer data source is a valid SpatiaLite layer
266267
*/
267268
bool valid;
269+
/**
270+
* Flag indicating if the layer data source is based on a query
271+
*/
272+
bool isQuery;
268273
/**
269274
* Flag indicating if the layer data source is based on a plain Table
270275
*/
@@ -289,6 +294,10 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
289294
* Name of the table with no schema
290295
*/
291296
QString mTableName;
297+
/**
298+
* Name of the table or subquery
299+
*/
300+
QString mQuery;
292301
/**
293302
* Name of the primary key column in the table
294303
*/
@@ -363,6 +372,7 @@ class QgsSpatiaLiteProvider: public QgsVectorDataProvider
363372
bool getTableGeometryDetails();
364373
bool getViewGeometryDetails();
365374
bool getVShapeGeometryDetails();
375+
bool getQueryGeometryDetails();
366376
bool getSridDetails();
367377
bool getTableSummary();
368378

0 commit comments

Comments
 (0)
Please sign in to comment.