Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Revert "FIX virtual layers with CSV layers opened with "delimitedtext…
…" provider"

Reverts #37164, which has lead to
#38163

Fixes #38163
  • Loading branch information
nyalldawson committed Aug 14, 2020
1 parent 5114522 commit eeb2526
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 47 deletions.
10 changes: 1 addition & 9 deletions src/providers/delimitedtext/qgsdelimitedtextprovider.cpp
Expand Up @@ -62,15 +62,6 @@ QRegExp QgsDelimitedTextProvider::sCrdDmsRegexp( "^\\s*(?:([-+nsew])\\s*)?(\\d{1
QgsDelimitedTextProvider::QgsDelimitedTextProvider( const QString &uri, const ProviderOptions &options )
: QgsVectorDataProvider( uri, options )
{
// uri should be in the form of "file:///path/to/file.csv?query=params", if not, enforce it in that format
// first read the already encoded url to get the query string
QUrl url = QUrl::fromEncoded( uri.toLatin1() );
// temporarily store the query string
const QString tmpUrlQuery = url.query();
// make sure that the url is actually prefixed with "file://". However, this breaks the query part ("?" char gets encoded), so discard the query string
url = QUrl::fromLocalFile( url.path() );
// finally restore the query part
url.setQuery( tmpUrlQuery );

// Add supported types to enable creating expression fields in field calculator
setNativeTypes( QList< NativeType >()
Expand All @@ -87,6 +78,7 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( const QString &uri, const Pr

QgsDebugMsgLevel( "Delimited text file uri is " + uri, 2 );

const QUrl url = QUrl::fromEncoded( uri.toLatin1() );
mFile = qgis::make_unique< QgsDelimitedTextFile >();
mFile->setFromUrl( url );

Expand Down
19 changes: 5 additions & 14 deletions src/providers/virtual/qgsvirtuallayerprovider.cpp
Expand Up @@ -317,10 +317,7 @@ bool QgsVirtualLayerProvider::createIt()
provider.replace( QLatin1String( "'" ), QLatin1String( "''" ) );
QString source = mLayers.at( i ).source;
source.replace( QLatin1String( "'" ), QLatin1String( "''" ) );
// the encoding might be an empty string, which breaks the SQL query below
QString encoding = mLayers.at( i ).encoding.isEmpty()
? QStringLiteral( "System" )
: mLayers.at( i ).encoding;
QString encoding = mLayers.at( i ).encoding;
QString createStr = QStringLiteral( "DROP TABLE IF EXISTS \"%1\"; CREATE VIRTUAL TABLE \"%1\" USING QgsVLayer('%2','%4',%3)" )
.arg( vname,
provider,
Expand Down Expand Up @@ -558,17 +555,11 @@ QgsRectangle QgsVirtualLayerProvider::extent() const
void QgsVirtualLayerProvider::updateStatistics() const
{
bool hasGeometry = mDefinition.geometryWkbType() != QgsWkbTypes::NoGeometry;
QString subset = mSubset.isEmpty() ? QString() : QStringLiteral( " WHERE %1" ).arg( mSubset );

// `mTableName` might be a null string if the layer creation failed. Assert such situations at least during development.
Q_ASSERT( ! mTableName.isNull() );

QString subset = mSubset.isEmpty() ? QString() : " WHERE " + mSubset;
QString sql = QStringLiteral( "SELECT Count(*)%1 FROM %2%3" )
.arg(
hasGeometry ? QStringLiteral( ",Min(MbrMinX(%1)),Min(MbrMinY(%1)),Max(MbrMaxX(%1)),Max(MbrMaxY(%1))" ).arg( quotedColumn( mDefinition.geometryField() ) ) : QString(),
mTableName,
subset
);
.arg( hasGeometry ? QStringLiteral( ",Min(MbrMinX(%1)),Min(MbrMinY(%1)),Max(MbrMaxX(%1)),Max(MbrMaxY(%1))" ).arg( quotedColumn( mDefinition.geometryField() ) ) : QString(),
mTableName,
subset );
Sqlite::Query q( mSqlite.get(), sql );
if ( q.step() == SQLITE_ROW )
{
Expand Down
25 changes: 1 addition & 24 deletions tests/src/python/test_qgsdelimitedtextprovider.py
Expand Up @@ -63,7 +63,7 @@
class MyUrl:

def __init__(self, url):
self.url = QUrl(url)
self.url = url
self.query = QUrlQuery()

@classmethod
Expand Down Expand Up @@ -129,29 +129,6 @@ def messages(self):
return self.log


class TestQgsDelimitedTextProviderLoading(unittest.TestCase):

def test_open_filepath_with_file_prefix(self):
srcpath = os.path.join(TEST_DATA_DIR, 'provider')
basetestfile = os.path.join(srcpath, 'delimited_xy.csv')

url = MyUrl.fromLocalFile(basetestfile)
url.addQueryItem("type", "csv")

vl = QgsVectorLayer(url.toString(), 'test', 'delimitedtext')
assert vl.isValid(), "{} is invalid".format(basetestfile)

def test_treat_open_filepath_without_file_prefix(self):
srcpath = os.path.join(TEST_DATA_DIR, 'provider')
basetestfile = os.path.join(srcpath, 'delimited_xy.csv')

url = MyUrl(basetestfile)
url.addQueryItem("type", "csv")

vl = QgsVectorLayer(url.toString(), 'test', 'delimitedtext')
assert vl.isValid(), "{} is invalid".format(basetestfile)


class TestQgsDelimitedTextProviderXY(unittest.TestCase, ProviderTestCase):

@classmethod
Expand Down

0 comments on commit eeb2526

Please sign in to comment.