improve database uri quoting (eg. for table names with quotes)

git-svn-id: http://svn.osgeo.org/qgis/trunk@13336 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
jef 2010-04-20 14:54:50 +00:00
parent b2051a8a26
commit 9e93c6e9ae
2 changed files with 11 additions and 8 deletions

View File

@ -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;
}
@ -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 )
@ -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,

View File

@ -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 */
@ -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;