Skip to content

Commit 37e7f3a

Browse files
author
jef
committed
improve database uri quoting (eg. for table names with quotes)
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13336 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent f25e6e3 commit 37e7f3a

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/core/qgsdatasourceuri.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,12 @@ void QgsDataSourceURI::clearSchema()
306306
mSchema = "";
307307
}
308308

309-
QString QgsDataSourceURI::escape( const QString &theVal ) const
309+
QString QgsDataSourceURI::escape( const QString &theVal, QChar delim = '\'' ) const
310310
{
311311
QString val = theVal;
312312

313313
val.replace( "\\", "\\\\" );
314-
val.replace( "\'", "\\'" );
314+
val.replace( delim, QString( "\\%1" ).arg( delim ) );
315315

316316
return val;
317317
}
@@ -349,7 +349,7 @@ QString QgsDataSourceURI::getValue( const QString &uri, int &i )
349349
i++;
350350
if ( i == uri.length() )
351351
continue;
352-
if ( uri[i] != '\'' && uri[i] != '\\' )
352+
if ( uri[i] != delim && uri[i] != '\\' )
353353
i--;
354354
}
355355
else if ( uri[i] == delim )
@@ -449,10 +449,13 @@ QString QgsDataSourceURI::uri() const
449449

450450
QString QgsDataSourceURI::quotedTablename() const
451451
{
452-
if ( mSchema != "" )
453-
return QString( "\"%1\".\"%2\"" ).arg( mSchema ).arg( mTable );
452+
if ( !mSchema.isEmpty() )
453+
return QString( "\"%1\".\"%2\"" )
454+
.arg( escape( mSchema, '"' ) )
455+
.arg( escape( mTable, '"' ) );
454456
else
455-
return QString( "\"%1\"" ).arg( mTable );
457+
return QString( "\"%1\"" )
458+
.arg( escape( mTable, '"' ) );
456459
}
457460

458461
void QgsDataSourceURI::setConnection( const QString &host,

src/core/qgsdatasourceuri.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class CORE_EXPORT QgsDataSourceURI
107107
private:
108108
void skipBlanks( const QString &uri, int &i );
109109
QString getValue( const QString &uri, int &i );
110-
QString escape( const QString &uri ) const;
110+
QString escape( const QString &uri, QChar delim ) const;
111111

112112
/* data */
113113

@@ -123,7 +123,7 @@ class CORE_EXPORT QgsDataSourceURI
123123
QString mTable;
124124
//! geometry column
125125
QString mGeometryColumn;
126-
//! SQL where clause used to limit features returned from the layer
126+
//! SQL query or where clause used to limit features returned from the layer
127127
QString mSql;
128128
//! username
129129
QString mUsername;

0 commit comments

Comments
 (0)