Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jef committed Apr 20, 2010
1 parent f25e6e3 commit 37e7f3a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
15 changes: 9 additions & 6 deletions src/core/qgsdatasourceuri.cpp
Expand Up @@ -306,12 +306,12 @@ void QgsDataSourceURI::clearSchema()
mSchema = "";
}

QString QgsDataSourceURI::escape( const QString &theVal ) const
QString QgsDataSourceURI::escape( const QString &theVal, QChar delim = '\'' ) const
{
QString val = theVal;

val.replace( "\\", "\\\\" );
val.replace( "\'", "\\'" );
val.replace( delim, QString( "\\%1" ).arg( delim ) );

return val;
}
Expand Down Expand Up @@ -349,7 +349,7 @@ QString QgsDataSourceURI::getValue( const QString &uri, int &i )
i++;
if ( i == uri.length() )
continue;
if ( uri[i] != '\'' && uri[i] != '\\' )
if ( uri[i] != delim && uri[i] != '\\' )
i--;
}
else if ( uri[i] == delim )
Expand Down Expand Up @@ -449,10 +449,13 @@ QString QgsDataSourceURI::uri() const

QString QgsDataSourceURI::quotedTablename() const
{
if ( mSchema != "" )
return QString( "\"%1\".\"%2\"" ).arg( mSchema ).arg( mTable );
if ( !mSchema.isEmpty() )
return QString( "\"%1\".\"%2\"" )
.arg( escape( mSchema, '"' ) )
.arg( escape( mTable, '"' ) );
else
return QString( "\"%1\"" ).arg( mTable );
return QString( "\"%1\"" )
.arg( escape( mTable, '"' ) );
}

void QgsDataSourceURI::setConnection( const QString &host,
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsdatasourceuri.h
Expand Up @@ -107,7 +107,7 @@ class CORE_EXPORT QgsDataSourceURI
private:
void skipBlanks( const QString &uri, int &i );
QString getValue( const QString &uri, int &i );
QString escape( const QString &uri ) const;
QString escape( const QString &uri, QChar delim ) const;

/* data */

Expand All @@ -123,7 +123,7 @@ class CORE_EXPORT QgsDataSourceURI
QString mTable;
//! geometry column
QString mGeometryColumn;
//! SQL where clause used to limit features returned from the layer
//! SQL query or where clause used to limit features returned from the layer
QString mSql;
//! username
QString mUsername;
Expand Down

0 comments on commit 37e7f3a

Please sign in to comment.